diff --git a/src/constants.ts b/src/constants.ts index 4cb1d84706..d09d839acc 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -75,6 +75,12 @@ export const ORGANIZATION_IMAGE_NOT_FOUND_ERROR = { MESSAGE: "organizationImage.notFound", PARAM: "organizationImage", }; +export const PLUGIN_NOT_FOUND = { + DESC: "Plugin not found", + CODE: "plugin.notFound", + MESSAGE: "plugin.notFound", + PARAM: "plugin", +}; export const POST_NOT_FOUND_ERROR = { DESC: "Post not found", CODE: "post.notFound", diff --git a/src/resolvers/Mutation/updatePluginStatus.ts b/src/resolvers/Mutation/updatePluginStatus.ts index bd2b431008..b819062d30 100644 --- a/src/resolvers/Mutation/updatePluginStatus.ts +++ b/src/resolvers/Mutation/updatePluginStatus.ts @@ -1,6 +1,8 @@ import type { MutationResolvers } from "../../types/generatedGraphQLTypes"; import { Plugin } from "../../models"; import mongoose from "mongoose"; +import { errors, requestContext } from "../../libraries"; +import { PLUGIN_NOT_FOUND } from "../../constants"; /** * This function enables to update plugin install status. @@ -20,8 +22,11 @@ export const updatePluginStatus: MutationResolvers["updatePluginStatus"] = const plugin = await Plugin.findById(uid); if (!plugin) { - console.log("Document not found"); - return; + throw new errors.NotFoundError( + requestContext.translate(PLUGIN_NOT_FOUND.MESSAGE), + PLUGIN_NOT_FOUND.CODE, + PLUGIN_NOT_FOUND.PARAM + ); } let uninstalledOrgsList = plugin.uninstalledOrgs; @@ -51,7 +56,7 @@ export const updatePluginStatus: MutationResolvers["updatePluginStatus"] = // calls subscription context.pubsub.publish("TALAWA_PLUGIN_UPDATED", { - Plugin: res, + onPluginUpdate: res, }); return res; }; diff --git a/tests/resolvers/Mutation/updatePluginStatus.spec.ts b/tests/resolvers/Mutation/updatePluginStatus.spec.ts new file mode 100644 index 0000000000..a530a1690c --- /dev/null +++ b/tests/resolvers/Mutation/updatePluginStatus.spec.ts @@ -0,0 +1,133 @@ +import type mongoose from "mongoose"; +import { Types } from "mongoose"; +import { nanoid } from "nanoid"; +import { + beforeAll, + afterAll, + afterEach, + describe, + it, + expect, + vi, +} from "vitest"; + +import { PLUGIN_NOT_FOUND } from "../../../src/constants"; +import { pubsub } from "../../../src/index"; +import { createTestUserAndOrganization } from "../../helpers/userAndOrg"; +import type { TestOrganizationType } from "../../helpers/userAndOrg"; +import { connect, disconnect } from "../../helpers/db"; +import type { MutationUpdatePluginStatusArgs } from "../../../src/types/generatedGraphQLTypes"; +import { updatePluginStatus } from "../../../src/resolvers/Mutation/updatePluginStatus"; +import { Plugin } from "../../../src/models"; +import type { InterfacePlugin } from "../../../src/models"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testOrganization: TestOrganizationType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + const temp = await createTestUserAndOrganization(); + testOrganization = temp[1]; +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +afterEach(() => { + vi.doUnmock("../../../src/constants"); + vi.resetModules(); +}); + +afterEach(() => { + vi.doUnmock("../../../src/constants"); + vi.resetModules(); +}); + +describe("resolvers -> Mutation -> updatePluginStatus", () => { + it(`throws NotFoundError if no plugin exists with _id === args.id`, async () => { + const { requestContext } = await import("../../../src/libraries"); + + const spy = vi + .spyOn(requestContext, "translate") + .mockImplementation((message) => `Translated ${message}`); + + try { + const args: MutationUpdatePluginStatusArgs = { + id: Types.ObjectId().toString(), + orgId: Types.ObjectId().toString(), + }; + + const context = async () => { + pubsub; + }; + + const { updatePluginStatus: updatePluginStatusResolver } = await import( + "../../../src/resolvers/Mutation/updatePluginStatus" + ); + + await updatePluginStatusResolver?.({}, args, context); + } catch (error: any) { + expect(spy).toHaveBeenCalledWith(PLUGIN_NOT_FOUND.MESSAGE); + expect(error.message).toEqual(`Translated ${PLUGIN_NOT_FOUND.MESSAGE}`); + } + }); + + it(`updates the plugin with _id === args.id and returns the updated plugin when orgId does no exists in uninstalled orgs`, async () => { + const testPlugin: InterfacePlugin = await Plugin.create({ + pluginName: `${nanoid()}`, + pluginCreatedBy: `${nanoid()}`, + pluginDesc: `${nanoid()}`, + uninstalledOrgs: [], + }); + const args: MutationUpdatePluginStatusArgs = { + id: testPlugin._id.toString(), + orgId: testOrganization?._id.toString(), + }; + + const context = { + pubsub, + }; + + const updatePluginStatusPayload = await updatePluginStatus?.( + {}, + args, + context + ); + + const testUpdatePluginStatusPayload = await Plugin.findOne({ + _id: testPlugin._id, + }).lean(); + + expect(updatePluginStatusPayload).toEqual(testUpdatePluginStatusPayload); + }); + + it(`updates the plugin with _id === args.id and returns the updated plugin when orgId exists in uninstalled orgs`, async () => { + const testPlugin: InterfacePlugin = await Plugin.create({ + pluginName: `${nanoid()}`, + pluginCreatedBy: `${nanoid()}`, + pluginDesc: `${nanoid()}`, + uninstalledOrgs: [testOrganization?._id], + }); + const args: MutationUpdatePluginStatusArgs = { + id: testPlugin._id.toString(), + orgId: testOrganization?._id.toString(), + }; + + const context = { + pubsub, + }; + + const updatePluginStatusPayload = await updatePluginStatus?.( + {}, + args, + context + ); + + const testUpdatePluginStatusPayload = await Plugin.findOne({ + _id: testPlugin._id, + }).lean(); + + expect(updatePluginStatusPayload).toEqual(testUpdatePluginStatusPayload); + }); +});