Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/issue 743 #1756

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/resolvers/donationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,81 @@ function donationsTestCases() {
}

function createDonationTestCases() {
it('should not create a donation if user donates to his/her own project', async () => {
const firstUser = await User.findOne({
where: { id: SEED_DATA.FIRST_USER.id },
});
const project = await saveProjectDirectlyToDb(
createProjectData(),
firstUser!,
);
const accessToken = await generateTestAccessToken(firstUser!.id);
const saveDonationResponse = await axios.post(
graphqlUrl,
{
query: createDonationMutation,
variables: {
projectId: project.id,
transactionNetworkId: NETWORK_IDS.XDAI,
transactionId: generateRandomEvmTxHash(),
nonce: 1,
amount: 10,
token: 'GIV',
},
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
assert.equal(
saveDonationResponse.data.errors[0].message,
"Donor can't donate to his/her own project.",
);
});

it('should create a donation successfully if user creates a donation but is not the project creator', async () => {
const firstUser = await User.findOne({
where: { id: SEED_DATA.FIRST_USER.id },
});
const secondUser = await User.findOne({
where: { id: SEED_DATA.SECOND_USER.id },
});
const project = await saveProjectDirectlyToDb(
createProjectData(),
secondUser!,
);
const accessToken = await generateTestAccessToken(firstUser!.id);
const saveDonationResponse = await axios.post(
graphqlUrl,
{
query: createDonationMutation,
variables: {
projectId: project.id,
transactionNetworkId: NETWORK_IDS.XDAI,
transactionId: generateRandomEvmTxHash(),
nonce: 1,
amount: 10,
token: 'GIV',
},
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
assert.isOk(saveDonationResponse.data.data.createDonation);
const donation = await Donation.findOne({
where: {
id: saveDonationResponse.data.data.createDonation,
},
});
assert.isNotNull(donation);
assert.equal(donation?.projectId, project.id);
});

it('do not save referrer wallet if user refers himself', async () => {
const project = await saveProjectDirectlyToDb(createProjectData());
const referrerId = generateRandomString();
Expand Down
4 changes: 4 additions & 0 deletions src/resolvers/donationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,10 @@ export class DonationResolver {
),
);
}
const ownProject = project.adminUserId === donorUser.id;
if (ownProject) {
throw new Error("Donor can't donate to his/her own project.");
}
const tokenInDb = await Token.findOne({
where: {
networkId,
Expand Down
Loading