From 52e03b91ad3095709b864607447f602a9288b18c Mon Sep 17 00:00:00 2001 From: seaerchin Date: Wed, 28 Apr 2021 17:27:25 +0800 Subject: [PATCH] test(admin-forms/settings): integration tests for updating of collaborators --- .../admin-forms.settings.routes.spec.ts | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/src/app/routes/api/v3/admin/forms/__tests__/admin-forms.settings.routes.spec.ts b/src/app/routes/api/v3/admin/forms/__tests__/admin-forms.settings.routes.spec.ts index c6efd1968c..023c074599 100644 --- a/src/app/routes/api/v3/admin/forms/__tests__/admin-forms.settings.routes.spec.ts +++ b/src/app/routes/api/v3/admin/forms/__tests__/admin-forms.settings.routes.spec.ts @@ -1,8 +1,10 @@ import { ObjectId } from 'bson-ext' import mongoose from 'mongoose' +import { errAsync } from 'neverthrow' import supertest, { Session } from 'supertest-session' import getUserModel from 'src/app/models/user.server.model' +import { DatabaseError } from 'src/app/modules/core/core.errors' import { Status } from 'src/types' import { SettingsUpdateDto } from 'src/types/api' @@ -11,6 +13,8 @@ import { setupApp } from 'tests/integration/helpers/express-setup' import { buildCelebrateError } from 'tests/unit/backend/helpers/celebrate' import dbHandler from 'tests/unit/backend/helpers/jest-db' +import { jsonParseStringify } from '../../../../../../../../tests/unit/backend/helpers/serialize-data' +import * as UserService from '../../../../../../modules/user/user.service' import { AdminFormsRouter } from '../admin-forms.routes' const UserModel = getUserModel(mongoose) @@ -265,4 +269,133 @@ describe('admin-form.settings.routes', () => { }) }) }) + + describe('PUT /admin/forms/:formId/collaborators', () => { + const MOCK_COLLABORATORS = [ + { + email: `fakeuser@test.gov.sg`, + write: false, + }, + ] + it('should return 200 when the collaborators are updated successfully', async () => { + // Arrange + const { form, user } = await dbHandler.insertEmailForm() + const session = await createAuthedSession(user.email, request) + const expectedResponse = jsonParseStringify({ + permissionList: MOCK_COLLABORATORS, + }) + + // Act + const response = await session + .put(`/admin/forms/${form._id}/collaborators`) + .send({ permissionList: MOCK_COLLABORATORS }) + + // Assert + expect(response.status).toEqual(200) + // NOTE: This is not strict equality because mongoose attaches an extra _id parameters, which is not caught by the type system + expect(response.body).toMatchObject(expectedResponse) + }) + + it('should return 403 when the current session user does not have sufficient permissions to update the collaborators', async () => { + // Arrange + const { form } = await dbHandler.insertEmailForm() + const fakeUser = await dbHandler.insertUser({ + mailName: 'fakeUser', + agencyId: new ObjectId(), + }) + const session = await createAuthedSession(fakeUser.email, request) + const expectedResponse = jsonParseStringify({ + message: `User ${fakeUser.email} not authorized to perform write operation on Form ${form._id} with title: ${form.title}.`, + }) + + // Act + const response = await session + .put(`/admin/forms/${form._id}/collaborators`) + .send({ permissionList: MOCK_COLLABORATORS }) + + // Assert + expect(response.status).toEqual(403) + expect(response.body).toEqual(expectedResponse) + }) + + it('should return 404 when the form could not be found', async () => { + // Arrange + const { user } = await dbHandler.insertEmailForm() + const session = await createAuthedSession(user.email, request) + const expectedResponse = jsonParseStringify({ + message: 'Form not found', + }) + + // Act + const response = await session + .put(`/admin/forms/${new ObjectId().toHexString()}/collaborators`) + .send({ permissionList: MOCK_COLLABORATORS }) + + // Assert + expect(response.status).toEqual(404) + expect(response.body).toEqual(expectedResponse) + }) + + it('should return 410 when the form has been archived', async () => { + // Arrange + const { form, user } = await dbHandler.insertEmailForm({ + formOptions: { + status: Status.Archived, + }, + }) + const session = await createAuthedSession(user.email, request) + const expectedResponse = jsonParseStringify({ + message: 'Form has been archived', + }) + + // Act + const response = await session + .put(`/admin/forms/${form._id}/collaborators`) + .send({ permissionList: MOCK_COLLABORATORS }) + + // Assert + expect(response.status).toEqual(410) + expect(response.body).toEqual(expectedResponse) + }) + + it('should return 422 when the current session user cannot be retrieved', async () => { + // Arrange + const { form, user } = await dbHandler.insertEmailForm() + const session = await createAuthedSession(user.email, request) + const expectedResponse = jsonParseStringify({ + message: 'User not found', + }) + await dbHandler.clearCollection(UserModel.collection.name) + + // Act + const response = await session + .put(`/admin/forms/${form._id}/collaborators`) + .send({ permissionList: MOCK_COLLABORATORS }) + + // Assert + expect(response.status).toEqual(422) + expect(response.body).toEqual(expectedResponse) + }) + + it('should return 500 when a database error occurs', async () => { + // Arrange + const { form, user } = await dbHandler.insertEmailForm() + const session = await createAuthedSession(user.email, request) + const expectedResponse = jsonParseStringify({ + message: 'Something went wrong. Please try again.', + }) + jest + .spyOn(UserService, 'getPopulatedUserById') + .mockReturnValueOnce(errAsync(new DatabaseError())) + + // Act + const response = await session + .put(`/admin/forms/${form._id}/collaborators`) + .send({ permissionList: MOCK_COLLABORATORS }) + + // Assert + expect(response.status).toEqual(500) + expect(response.body).toEqual(expectedResponse) + }) + }) })