Skip to content

Commit

Permalink
Merge pull request #1763 from Giveth/fix/issue-743
Browse files Browse the repository at this point in the history
fix: add createDraftDonation Test Cases to prevent Users from Donatin…
  • Loading branch information
HrithikSampson authored Aug 16, 2024
2 parents 85557a3 + 0c5512f commit df9c0ad
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions src/resolvers/draftDonationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
DraftRecurringDonation,
} from '../entities/draftRecurringDonation';
import { ProjectAddress } from '../entities/projectAddress';
import { i18n, translationErrorMessagesKeys } from '../utils/errorMessages';

describe('createDraftDonation() test cases', createDraftDonationTestCases);
describe(
Expand Down Expand Up @@ -83,6 +84,46 @@ function createDraftDonationTestCases() {
toAddress: project.walletAddress,
};
});
it('should throw an error while creating draft donate to an invalid Project ID', async () => {
const saveDonationResponse = await axios.post(
graphqlUrl,
{
query: createDraftDonationMutation,
variables: { ...donationData, projectId: 1000000 },
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
assert.equal(
saveDonationResponse.data.errors[0].message,
i18n.__(translationErrorMessagesKeys.PROJECT_NOT_FOUND),
);
});
it('should throw an error while creating draft donating to his/her own project', async () => {
const copyProjectSecondUser = await saveProjectDirectlyToDb(
createProjectData(),
user,
);
const saveDonationResponse = await axios.post(
graphqlUrl,
{
query: createDraftDonationMutation,
variables: { ...donationData, projectId: copyProjectSecondUser.id },
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
assert.equal(
saveDonationResponse.data.errors[0].message,
"Donor can't create a draft to donate to his/her own project.",
);
});
it('create simple draft donation', async () => {
const saveDonationResponse = await axios.post(
graphqlUrl,
Expand Down
14 changes: 14 additions & 0 deletions src/resolvers/draftDonationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
findRecurringDonationByProjectIdAndUserIdAndCurrency,
} from '../repositories/recurringDonationRepository';
import { RecurringDonation } from '../entities/recurringDonation';
import { findProjectById } from '../repositories/projectRepository';

const draftDonationEnabled = process.env.ENABLE_DRAFT_DONATION === 'true';
const draftRecurringDonationEnabled =
Expand Down Expand Up @@ -84,6 +85,7 @@ export class DraftDonationResolver {
try {
const userId = ctx?.req?.user?.userId;
const donorUser = await findUserById(userId);
const project = await findProjectById(projectId);

if (!donorUser && !isQRDonation) {
throw new Error(i18n.__(translationErrorMessagesKeys.UN_AUTHORIZED));
Expand All @@ -95,6 +97,18 @@ export class DraftDonationResolver {
);
}

if (!project)
throw new Error(
i18n.__(translationErrorMessagesKeys.PROJECT_NOT_FOUND),
);

const ownProject = project.adminUserId === donorUser?.id;
if (ownProject) {
throw new Error(
"Donor can't create a draft to donate to his/her own project.",
);
}

const chainType = isQRDonation
? detectAddressChainType(toAddress)
: detectAddressChainType(donorUser?.walletAddress ?? '');
Expand Down

0 comments on commit df9c0ad

Please sign in to comment.