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 cloneDeep = require('lodash/cloneDeep') const configRoles = config.get('roles') jest.mock('pubsweet-component-mail-service', () => ({ setupInviteEmail: jest.fn(), setupAssignEmail: jest.fn(), setupDeclineEmail: jest.fn(), setupReviewerInvitationEmail: jest.fn(), })) const chance = new Chance() const roles = configRoles.collection const reqBody = { email: chance.email(), role: roles[random(0, roles.length - 1)], firstName: chance.first(), lastName: chance.last(), title: 'Mr', affiliation: chance.company(), admin: false, } const postPath = '../../routes/collectionsInvitations/post' describe('Post collections invitations route handler', () => { let testFixtures = {} let body = {} let models beforeEach(() => { testFixtures = cloneDeep(fixtures) body = cloneDeep(reqBody) models = Model.build(testFixtures) }) it('should return an error params are missing', async () => { const { admin } = testFixtures.users 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') }) it('should return success when the editor in chief invites a handlingEditor with a collection', async () => { const { user, editorInChief } = testFixtures.users const { collection } = testFixtures.collections body = { email: user.email, role: 'handlingEditor', } const req = httpMocks.createRequest({ body, }) req.user = editorInChief.id req.params.collectionId = collection.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) }) it('should return success when the a reviewer is invited', async () => { const { user, editorInChief } = testFixtures.users const { collection } = testFixtures.collections body = { email: user.email, role: 'reviewer', } const req = httpMocks.createRequest({ body, }) req.user = editorInChief.id req.params.collectionId = collection.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) }) it('should return an error when inviting his self', async () => { const { editorInChief } = testFixtures.users 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 () => { const { editorInChief } = testFixtures.users 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 { handlingEditor, editorInChief } = testFixtures.users const { collection } = testFixtures.collections body = { email: handlingEditor.email, role: 'handlingEditor', } const req = httpMocks.createRequest({ body, }) req.user = editorInChief.id req.params.collectionId = collection.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) }) })