Skip to content
Snippets Groups Projects
Commit 8e665cd3 authored by Bogdan Cochior's avatar Bogdan Cochior
Browse files

Merge branch 'faraday-master' of gitlab.coko.foundation:xpub/xpub into faraday-master

parents c6b6c8bd 51b7103f
No related branches found
No related tags found
No related merge requests found
...@@ -11,10 +11,12 @@ ...@@ -11,10 +11,12 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://gitlab.coko.foundation/xpub/xpub" "url": "https://gitlab.coko.foundation/xpub/xpub",
"path": "component-invite"
}, },
"dependencies": { "dependencies": {
"body-parser": "^1.17.2" "body-parser": "^1.17.2",
"chance": "^1.0.13"
}, },
"peerDependencies": { "peerDependencies": {
"@pubsweet/logger": "^0.0.1", "@pubsweet/logger": "^0.0.1",
...@@ -30,7 +32,12 @@ ...@@ -30,7 +32,12 @@
"moduleNameMapper": { "moduleNameMapper": {
"\\.s?css$": "identity-obj-proxy" "\\.s?css$": "identity-obj-proxy"
}, },
"transformIgnorePatterns": ["/node_modules/(?!@?pubsweet)"], "transformIgnorePatterns": [
"testPathIgnorePatterns": ["/node_modules", "config/"] "/node_modules/(?!@?pubsweet)"
],
"testPathIgnorePatterns": [
"/node_modules",
"config/"
]
} }
} }
...@@ -14,7 +14,7 @@ module.exports = models => async (req, res) => { ...@@ -14,7 +14,7 @@ module.exports = models => async (req, res) => {
logger.error('some parameters are missing') logger.error('some parameters are missing')
return return
} }
// console.log(req.body)
const collectionId = get(req, 'params.collectionId') const collectionId = get(req, 'params.collectionId')
const reqUser = await models.User.find(req.user) const reqUser = await models.User.find(req.user)
let collection let collection
......
...@@ -5,6 +5,12 @@ function User(properties) { ...@@ -5,6 +5,12 @@ function User(properties) {
this.email = properties.email this.email = properties.email
this.username = properties.username this.username = properties.username
this.password = properties.password this.password = properties.password
this.roles = properties.roles
this.title = properties.title
this.affiliation = properties.affiliation
this.firstName = properties.firstName
this.lastName = properties.lastName
this.admin = properties.admin
} }
User.prototype.save = jest.fn(function saveUser() { User.prototype.save = jest.fn(function saveUser() {
......
...@@ -5,8 +5,12 @@ const httpMocks = require('node-mocks-http') ...@@ -5,8 +5,12 @@ const httpMocks = require('node-mocks-http')
const random = require('lodash/random') const random = require('lodash/random')
const fixtures = require('./fixtures/fixtures') const fixtures = require('./fixtures/fixtures')
const UserMock = require('./mocks/User') const UserMock = require('./mocks/User')
const Chance = require('chance')
const roles = ['editorInChief', 'author', 'admin'] jest.mock('pubsweet-component-mail-service', () => ({ setupEmail: jest.fn() }))
const chance = new Chance()
const globalRoles = ['editorInChief', 'author', 'admin']
const manuscriptRoles = ['handlingEditor', 'reviewer']
const buildModels = (collection, findUser, emailUser) => { const buildModels = (collection, findUser, emailUser) => {
const models = { const models = {
User: {}, User: {},
...@@ -37,27 +41,83 @@ const buildModels = (collection, findUser, emailUser) => { ...@@ -37,27 +41,83 @@ const buildModels = (collection, findUser, emailUser) => {
return models return models
} }
const body = {
email: chance.email(),
role: globalRoles[random(0, globalRoles.length - 1)],
firstName: chance.first(),
lastName: chance.last(),
title: 'professor',
affiliation: 'MIT',
}
body.admin = body.role === 'admin'
const getNotFoundError = () => {
const error = new Error()
error.name = 'NotFoundError'
error.status = 404
return error
}
describe('Post invite route handler', () => { describe('Post invite route handler', () => {
it('should return success when the admin invites an Editor in Chief, Author or Admin', async () => { it('should return success when the admin invites an Editor in Chief, Author or Admin', async () => {
const body = {
email: 'sebi.mihalache@gmail.com',
role: roles[random(0, roles.length - 1)],
firstName: 'john',
lastName: 'smith',
title: 'professor',
affiliation: 'MIT',
}
const req = httpMocks.createRequest({ const req = httpMocks.createRequest({
body, body,
}) })
req.user = fixtures.users.admin req.user = fixtures.users.admin
const res = httpMocks.createResponse() const res = httpMocks.createResponse()
const error = new Error() const error = getNotFoundError()
error.name = 'NotFoundError'
error.status = 404
const models = buildModels(error, fixtures.users.admin, error) const models = buildModels(error, fixtures.users.admin, error)
await require('../routes/post')(models)(req, res) await require('../routes/post')(models)(req, res)
expect(res.statusCode).toBe(200) expect(res.statusCode).toBe(200)
const data = JSON.parse(res._getData()) const data = JSON.parse(res._getData())
expect(data.roles[0]).toEqual(body.role)
expect(data.firstName).toEqual(body.firstName)
expect(data.email).toEqual(body.email)
expect(data.admin).toEqual(body.admin)
})
it('should return an error when the admin invites an user on a collection', async () => {
const req = httpMocks.createRequest({
body,
})
req.user = fixtures.users.admin
req.params.collectionId = '123'
const res = httpMocks.createResponse()
const error = getNotFoundError()
const models = buildModels(error, fixtures.users.admin)
await require('../routes/post')(models)(req, res)
expect(res.statusCode).toBe(403)
const data = JSON.parse(res._getData())
expect(data.error).toEqual(
`admin cannot invite an ${body.role} to a collection`,
)
})
it('should return an error when the admin invites a manuscript role', async () => {
body.role = manuscriptRoles[random(0, manuscriptRoles.length - 1)]
const req = httpMocks.createRequest({
body,
})
req.user = fixtures.users.admin
const res = httpMocks.createResponse()
const error = getNotFoundError()
const models = buildModels(error, fixtures.users.admin)
await require('../routes/post')(models)(req, res)
expect(res.statusCode).toBe(403)
const data = JSON.parse(res._getData())
expect(data.error).toEqual(`admin cannot invite a ${body.role}`)
})
it('should return an error params are missing', async () => {
delete body.email
const req = httpMocks.createRequest({
body,
})
req.user = fixtures.users.admin
const res = httpMocks.createResponse()
const error = getNotFoundError()
const models = buildModels(error, fixtures.users.admin)
await require('../routes/post')(models)(req, res)
expect(res.statusCode).toBe(400)
const data = JSON.parse(res._getData())
expect(data.error).toEqual(`Email and role are required`)
}) })
}) })
...@@ -1959,6 +1959,10 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0, chalk@^2.3.1: ...@@ -1959,6 +1959,10 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0, chalk@^2.3.1:
escape-string-regexp "^1.0.5" escape-string-regexp "^1.0.5"
supports-color "^5.2.0" supports-color "^5.2.0"
chance@^1.0.13:
version "1.0.13"
resolved "https://registry.yarnpkg.com/chance/-/chance-1.0.13.tgz#666bec2db42b3084456a3e4f4c28a82db5ccb7e6"
change-emitter@^0.1.2: change-emitter@^0.1.2:
version "0.1.6" version "0.1.6"
resolved "https://registry.yarnpkg.com/change-emitter/-/change-emitter-0.1.6.tgz#e8b2fe3d7f1ab7d69a32199aff91ea6931409515" resolved "https://registry.yarnpkg.com/change-emitter/-/change-emitter-0.1.6.tgz#e8b2fe3d7f1ab7d69a32199aff91ea6931409515"
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment