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

test(adminform): integration tests for GET /admin/forms/:formId/collaborators #1777

Merged
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
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'

import { createAuthedSession } from 'tests/integration/helpers/express-auth'
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)
Expand Down Expand Up @@ -265,4 +269,139 @@ describe('admin-form.settings.routes', () => {
})
})
})

describe('GET /admin/forms/:formId/collaborators', () => {
const MOCK_COLLABORATORS = [
{
email: `[email protected]`,
write: false,
},
]
it('should return the list of collaborators on a valid request', async () => {
// Arrange
const { form, user } = await dbHandler.insertEmailForm({
formOptions: {
permissionList: MOCK_COLLABORATORS,
},
})
const session = await createAuthedSession(user.email, request)

// Act
const response = await session.get(
`/admin/forms/${form._id}/collaborators`,
)

// Assert
expect(response.status).toEqual(200)
expect(response.body).toMatchObject(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not toEqual?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh because of _id

jsonParseStringify(MOCK_COLLABORATORS),
)
})

it('should return 403 when the current user does not have read permissions for the specified form', async () => {
// Arrange
const { form } = await dbHandler.insertEmailForm({
formOptions: {
permissionList: MOCK_COLLABORATORS,
},
})
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 read operation on Form ${form._id} with title: ${form.title}.`,
})

// Act
const response = await session.get(
`/admin/forms/${form._id}/collaborators`,
)

// Assert
expect(response.status).toEqual(403)
expect(response.body).toMatchObject(jsonParseStringify(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.get(
`/admin/forms/${new ObjectId().toHexString()}/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.get(
`/admin/forms/${form._id}/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.get(
`/admin/forms/${form._id}/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.get(
`/admin/forms/${form._id}/collaborators`,
)

// Assert
expect(response.status).toEqual(500)
expect(response.body).toEqual(expectedResponse)
})
})
})