process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' process.env.SUPPRESS_NO_CONFIG_WARNING = true const httpMocks = require('node-mocks-http') const random = require('lodash/random') const fixtures = require('./../fixtures/fixtures') const Chance = require('chance') const Model = require('./../helpers/Model') const config = require('config') const configRoles = config.get('roles') const models = Model.build() jest.mock('pubsweet-component-mail-service', () => ({ setupInviteEmail: jest.fn(), setupAssignEmail: jest.fn(), setupDeclineEmail: jest.fn(), })) const chance = new Chance() const roles = configRoles.collection const body = { email: chance.email(), role: roles[random(0, roles.length - 1)], firstName: chance.first(), lastName: chance.last(), title: 'Mr', affiliation: chance.company(), } body.admin = body.role === 'admin' const notFoundError = new Error() notFoundError.name = 'NotFoundError' notFoundError.status = 404 const { admin, editorInChief, author, invitedHandlingEditor } = fixtures.users const { standardCollection } = fixtures.collections const { heTeam } = fixtures.teams const postPath = '../../routes/collectionsInvitations/post' describe('Post collections invitations route handler', () => { it('should return an error params are missing', async () => { delete body.email const req = httpMocks.createRequest({ body, }) req.user = admin.id const res = httpMocks.createResponse() await require(postPath)(models)(req, res) expect(res.statusCode).toBe(400) const data = JSON.parse(res._getData()) expect(data.error).toEqual('Email and role are required') body.email = chance.email() }) it('should return success when the editor in chief invites a handlingEditor with a collection', async () => { const body = { email: author.email, role: 'handlingEditor', } const req = httpMocks.createRequest({ body, }) req.user = editorInChief.id const initialSize = standardCollection.invitations.length req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() await require(postPath)(models)(req, res) expect(res.statusCode).toBe(200) const data = JSON.parse(res._getData()) expect(data.email).toEqual(body.email) expect(standardCollection.invitations.length).toBeGreaterThan(initialSize) expect(heTeam.members).toContain(author.id) expect(author.teams).toContain(heTeam.id) const matchingInvitation = standardCollection.invitations.find( inv => inv.userId === author.id && inv.role === body.role, ) expect(matchingInvitation).toBeDefined() }) it('should return an error when inviting his self', async () => { body.role = roles[random(0, roles.length - 1)] body.email = editorInChief.email const req = httpMocks.createRequest({ body, }) req.user = editorInChief.id const res = httpMocks.createResponse() await require(postPath)(models)(req, res) expect(res.statusCode).toBe(400) const data = JSON.parse(res._getData()) expect(data.error).toEqual('Cannot invite yourself') }) it('should return an error when the role is invalid', async () => { body.role = 'someRandomRole' const req = httpMocks.createRequest({ body, }) req.user = editorInChief.id const res = httpMocks.createResponse() await require(postPath)(models)(req, res) const data = JSON.parse(res._getData()) expect(data.error).toEqual(`Role ${body.role} is invalid`) }) it('should return success when the EiC resends an invitation to a handlingEditor with a collection', async () => { const body = { email: invitedHandlingEditor.email, role: 'handlingEditor', } const req = httpMocks.createRequest({ body, }) req.user = editorInChief.id req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() await require(postPath)(models)(req, res) expect(res.statusCode).toBe(200) const data = JSON.parse(res._getData()) expect(data.email).toEqual(body.email) }) })