Skip to content
Snippets Groups Projects
decline.test.js 4.44 KiB
Newer Older
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
process.env.SUPPRESS_NO_CONFIG_WARNING = true

const httpMocks = require('node-mocks-http')
const fixtures = require('./../fixtures/fixtures')
const Model = require('./../helpers/Model')
const cloneDeep = require('lodash/cloneDeep')

jest.mock('pubsweet-component-mail-service', () => ({
  setupReviewerDeclinedEmail: jest.fn(),
}))

const reqBody = {
  invitationToken: 'inv-token-123',
}
const patchPath = '../../routes/collectionsInvitations/decline'
describe('Patch collections invitations route handler', () => {
  let testFixtures = {}
  let body = {}
  let models
  beforeEach(() => {
    testFixtures = cloneDeep(fixtures)
    body = cloneDeep(reqBody)
    models = Model.build(testFixtures)
  })
  it('should return success when the reviewer declines work on a collection', async () => {
    const { reviewer } = testFixtures.users
    const { collection } = testFixtures.collections
    body.isAccepted = false
    const req = httpMocks.createRequest({
      body,
    })
    req.user = reviewer.id
    req.params.collectionId = collection.id
    const inv = collection.invitations.find(
      inv => inv.role === 'reviewer' && inv.hasAnswer === false,
    )
    req.params.invitationId = inv.id
    const res = httpMocks.createResponse()
    await require(patchPath)(models)(req, res)

    expect(res.statusCode).toBe(200)
  })
  it('should return an error params are missing', async () => {
    const { handlingEditor } = testFixtures.users
    const { collection } = testFixtures.collections
    delete body.isAccepted
    const req = httpMocks.createRequest({
      body,
    })
    req.user = handlingEditor.id
    req.params.collectionId = collection.id
    req.params.invitationId = collection.invitations[0].id
    const res = httpMocks.createResponse()
    await require(patchPath)(models)(req, res)

    expect(res.statusCode).toBe(400)
    const data = JSON.parse(res._getData())
    expect(data.error).toEqual('Missing parameters')
  })
  it('should return an error if the collection does not exists', async () => {
    const { handlingEditor } = testFixtures.users
    const req = httpMocks.createRequest({
      body,
    })
    req.user = handlingEditor.id
    req.params.collectionId = 'invalid-id'
    const res = httpMocks.createResponse()
    await require(patchPath)(models)(req, res)

    expect(res.statusCode).toBe(404)
    const data = JSON.parse(res._getData())
    expect(data.error).toEqual('collection not found')
  })
  it('should return an error when the invitation does not exist', async () => {
    const { user } = testFixtures.users
    const { collection } = testFixtures.collections
    const req = httpMocks.createRequest({
      body,
    })
    req.user = user.id
    req.params.collectionId = collection.id
    req.params.invitationId = 'invalid-id'
    const res = httpMocks.createResponse()
    await require(patchPath)(models)(req, res)

    expect(res.statusCode).toBe(404)
    const data = JSON.parse(res._getData())
    expect(data.error).toEqual('Invitation invalid-id not found')
  })
  it("should return an error when a user tries to patch another user's invitation", async () => {
    const { reviewer } = testFixtures.users
    const { collection } = testFixtures.collections
    const req = httpMocks.createRequest({
      body,
    })
    req.user = reviewer.id
    req.params.collectionId = collection.id
    const inv = collection.invitations.find(
      inv => inv.role === 'handlingEditor' && inv.hasAnswer === false,
    )
    req.params.invitationId = inv.id
    const res = httpMocks.createResponse()
    await require(patchPath)(models)(req, res)
    expect(res.statusCode).toBe(403)
    const data = JSON.parse(res._getData())
    expect(data.error).toEqual(
      `User ${reviewer.email} is not allowed to modify invitation ${inv.id}`,
    )
  })
  it('should return an error when the invitation is already answered', async () => {
    const { handlingEditor } = testFixtures.users
    const { collection } = testFixtures.collections
    const req = httpMocks.createRequest({
      body,
    })
    req.user = handlingEditor.id
    req.params.collectionId = collection.id
    const inv = collection.invitations.find(inv => inv.hasAnswer)
    req.params.invitationId = inv.id
    const res = httpMocks.createResponse()
    await require(patchPath)(models)(req, res)
    expect(res.statusCode).toBe(400)
    const data = JSON.parse(res._getData())
    expect(data.error).toEqual(`${inv.id} has already been answered`)
  })
})