Skip to content

Commit

Permalink
fix tests for updateFundraisingCampaign
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Apr 14, 2024
1 parent 65069df commit 5655ebb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
8 changes: 3 additions & 5 deletions src/resolvers/Mutation/updateFundraisingCampaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,15 @@ export const updateFundraisingCampaign: MutationResolvers["updateFundraisingCamp
.select("organizationId")
.lean();

const currentOrgId = currentOrg?.organizationId?.toString() || "";
const currentOrgId = currentOrg?.organizationId?.toString();

const currentUserIsOrgAdmin = currentUserAppProfile.adminFor.some(
(organizationId) =>
new Types.ObjectId(organizationId?.toString()).equals(currentOrgId),
);

//Checks if the user is authorized to update the fundraising campaign
if (
!currentUserIsOrgAdmin ||
currentUserAppProfile.isSuperAdmin === false
) {
if (!(currentUserIsOrgAdmin || currentUserAppProfile.isSuperAdmin)) {
throw new errors.UnauthorizedError(
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
USER_NOT_AUTHORIZED_ERROR.CODE,
Expand All @@ -121,6 +118,7 @@ export const updateFundraisingCampaign: MutationResolvers["updateFundraisingCamp
const exisitingCampaign = await FundraisingCampaign.findOne({
name: args.data.name,
});

if (exisitingCampaign) {
throw new errors.ConflictError(
requestContext.translate(FUNDRAISING_CAMPAIGN_ALREADY_EXISTS.MESSAGE),
Expand Down
49 changes: 37 additions & 12 deletions tests/resolvers/Mutation/updateFundraisingCampaign.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Types } from "mongoose";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import {
END_DATE_VALIDATION_ERROR,
FUNDRAISING_CAMPAIGN_ALREADY_EXISTS,
FUNDRAISING_CAMPAIGN_NOT_FOUND_ERROR,
FUND_NOT_FOUND_ERROR,
START_DATE_VALIDATION_ERROR,
Expand All @@ -20,10 +21,12 @@ import { createTestFundraisingCampaign } from "../../helpers/FundraisingCampaign
import { connect, disconnect } from "../../helpers/db";
import type { TestUserType } from "../../helpers/user";
import { createTestUser } from "../../helpers/userAndOrg";

let testUser: TestUserType;
let testfund: TestFundType;
let testFundraisingCampaign: InterfaceFundraisingCampaign;
let MONGOOSE_INSTANCE: typeof mongoose;

beforeAll(async () => {
MONGOOSE_INSTANCE = await connect();
const { requestContext } = await import("../../../src/libraries");
Expand All @@ -36,9 +39,11 @@ beforeAll(async () => {
testfund = temp[2];
testFundraisingCampaign = await createTestFundraisingCampaign(testfund?._id);
});

afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

describe("resolvers->Mutation->updateFundrasingCampaign", () => {
it("throw error if no user exists with _id===context.userId", async () => {
try {
Expand All @@ -52,14 +57,17 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
fundingGoal: 1000,
},
};

const context = {
userId: new Types.ObjectId().toString(),
};

await updateFundraisingCampaign?.({}, args, context);
} catch (error: unknown) {
expect((error as Error).message).toEqual(USER_NOT_FOUND_ERROR.MESSAGE);
}
});

it("throw error if no campaign exists with _id===args.id", async () => {
try {
const args: MutationUpdateFundraisingCampaignArgs = {
Expand All @@ -72,9 +80,11 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
fundingGoal: 1000,
},
};

const context = {
userId: testUser?._id,
};

await updateFundraisingCampaign?.({}, args, context);
} catch (error: unknown) {
// console.log(error);
Expand All @@ -83,11 +93,13 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
);
}
});

it("throws error if no fund exists with _id===campaign.fundId", async () => {
try {
const campaign = await createTestFundraisingCampaign(
new Types.ObjectId().toString(),
);

const args: MutationUpdateFundraisingCampaignArgs = {
id: campaign?._id.toString() || "",
data: {
Expand All @@ -98,16 +110,19 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
fundingGoal: 1000,
},
};

const context = {
userId: testUser?._id,
};

await updateFundraisingCampaign?.({}, args, context);
} catch (error: unknown) {
// console.log(error);
expect((error as Error).message).toEqual(FUND_NOT_FOUND_ERROR.MESSAGE);
}
});
it("throw error if the user is not authorized to create the fundraisingCampaign", async () => {

it("throw error if the user is not authorized to update the fundraisingCampaign", async () => {
try {
const args: MutationUpdateFundraisingCampaignArgs = {
id: testFundraisingCampaign?._id.toString() || "",
Expand All @@ -119,18 +134,22 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
fundingGoal: 1000,
},
};

const randomUser = await createTestUser();

const context = {
userId: randomUser?._id,
};

await updateFundraisingCampaign?.({}, args, context);
} catch (error: unknown) {
// console.log((error as Error).message);
expect((error as Error).message).toEqual(
USER_NOT_AUTHORIZED_ERROR.MESSAGE,
);
}
});

it("throws error if startDate is invalid", async () => {
try {
const args: MutationUpdateFundraisingCampaignArgs = {
Expand All @@ -143,9 +162,11 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
fundingGoal: 1000,
},
};

const context = {
userId: testUser?._id,
};

await updateFundraisingCampaign?.({}, args, context);
} catch (error: unknown) {
// console.log(error);
Expand All @@ -154,6 +175,7 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
);
}
});

it("throws error if endDate is invalid", async () => {
try {
const args: MutationUpdateFundraisingCampaignArgs = {
Expand All @@ -167,9 +189,11 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
fundingGoal: 1000,
},
};

const context = {
userId: testUser?._id,
};

await updateFundraisingCampaign?.({}, args, context);
} catch (error: unknown) {
// console.log(error);
Expand All @@ -178,6 +202,7 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
);
}
});

it("throws error if the campaign already exists with the same name", async () => {
try {
const args: MutationUpdateFundraisingCampaignArgs = {
Expand All @@ -190,46 +215,45 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
fundingGoal: 1000,
},
};

const context = {
userId: testUser?._id,
};

await updateFundraisingCampaign?.({}, args, context);
} catch (error: unknown) {
// console.log(error);
// console.log(error);
expect((error as Error).message).toEqual(
USER_NOT_AUTHORIZED_ERROR.MESSAGE,
FUNDRAISING_CAMPAIGN_ALREADY_EXISTS.MESSAGE,
);
}
});

it("updates the fundraisingCampaign", async () => {
const args: MutationUpdateFundraisingCampaignArgs = {
id: testFundraisingCampaign?._id.toString() || "",
data: {
name: "testFundraisingCampaign",

startDate: new Date(new Date().toDateString()),
endDate: new Date(new Date().toDateString()),
currency: "USD",
fundingGoal: 1000,
},
};

const context = {
userId: testUser?._id,
};
try {
const result = await updateFundraisingCampaign?.({}, args, context);
const result = await updateFundraisingCampaign?.({}, args, context);

expect(result).toBeTruthy();
} catch (error) {
expect((error as Error).message).toEqual(
USER_NOT_AUTHORIZED_ERROR.MESSAGE,
);
}
expect(result?.name).toEqual("testFundraisingCampaign");
});

it("throws an error if the user does not have appUserProfile", async () => {
await AppUserProfile.deleteOne({
userId: testUser?._id,
});

const args: MutationUpdateFundraisingCampaignArgs = {
id: testFundraisingCampaign?._id.toString() || "",
data: {
Expand All @@ -241,6 +265,7 @@ describe("resolvers->Mutation->updateFundrasingCampaign", () => {
fundingGoal: 1000,
},
};

const context = {
userId: testUser?._id,
};
Expand Down

0 comments on commit 5655ebb

Please sign in to comment.