Commit 867feb7e authored by Sebastian's avatar Sebastian
Browse files

success

parent 23d9fd55
Pipeline #5128 failed with stages
in 3 minutes and 51 seconds
......@@ -10,6 +10,11 @@ const HandleInvitation = app => {
authBearer,
require('./routes/postHandleInvitation')(app.locals.models),
)
app.get(
'/api/collections/:collectionId/users',
authBearer,
require('./routes/getCollectionUsers')(app.locals.models),
)
}
module.exports = HandleInvitation
const logger = require('@pubsweet/logger')
const config = require('config')
const get = require('lodash/get')
const configRoles = config.get('roles')
......@@ -134,6 +135,37 @@ const removeTeamMember = async (teamId, userId, TeamModel) => {
await TeamModel.updateProperties(team)
await team.save()
}
const getTeamMembersByCollection = async (collectionId, role, TeamModel) => {
const teams = await TeamModel.all()
const members = get(
teams.find(
team =>
team.group === role &&
team.object.type === 'collection' &&
team.object.id === collectionId,
),
'members',
)
return members
}
const getInviteData = (invitations, collectionId, role) => {
const matchingInvitation = invitations.find(
invite => invite.type === role && invite.collectionId === collectionId,
)
let status = 'pending'
if (matchingInvitation.isAccepted) {
status = 'accepted'
} else if (matchingInvitation.hasAnswer) {
status = 'refused'
}
const { timestamp } = matchingInvitation
return { timestamp, status }
}
module.exports = {
createNewTeam,
setupEiCTeams,
......@@ -141,4 +173,6 @@ module.exports = {
getMatchingTeams,
setupInvitation,
removeTeamMember,
getTeamMembersByCollection,
getInviteData,
}
const helpers = require('../helpers/helpers')
const teamHelper = require('../helpers/Team')
module.exports = models => async (req, res) => {
const { role } = req.query
if (!helpers.checkForUndefinedParams(role)) {
res.status(400).json({ error: 'Role is required' })
return
}
const { collectionId } = req.params
try {
await models.Collection.find(collectionId)
const members = await teamHelper.getTeamMembersByCollection(
collectionId,
role,
models.Team,
)
const membersData = members.map(async member => {
const user = await models.User.find(member)
const { timestamp, status } = teamHelper.getInviteData(
user.invitations,
collectionId,
role,
)
return {
name: `${user.firstName} ${user.lastName}`,
timestamp,
email: user.email,
status,
}
})
const resBody = await Promise.all(membersData)
res.status(200).json(resBody)
} catch (e) {
const notFoundError = await helpers.handleNotFoundError(e, 'collection')
return res.status(notFoundError.status).json({
error: notFoundError.message,
})
}
}
......@@ -2,7 +2,7 @@ const users = require('./users')
const collections = require('./collections')
const { standardCollection } = collections
const { editorInChief, handlingEditor, reviewer } = users
const { editorInChief, handlingEditor, reviewer, invitedHandlingEditor } = users
const teams = {
eicTeam: {
teamType: {
......@@ -30,7 +30,7 @@ const teams = {
type: 'collection',
id: standardCollection.id,
},
members: [handlingEditor.id],
members: [handlingEditor.id, invitedHandlingEditor.id],
save: jest.fn(() => teams.heTeam),
updateProperties: jest.fn(() => teams.heTeam),
id: 'he123',
......
......@@ -33,6 +33,8 @@ const users = {
password: 'test',
admin: false,
id: 'handling123',
firstName: 'Handling',
lastName: 'Editor',
invitations: [
{
type: 'handlingEditor',
......@@ -77,6 +79,28 @@ const users = {
isConfirmed: false,
teams: [],
},
invitedHandlingEditor: {
type: 'user',
username: 'handling',
email: 'handling@example.com',
password: 'test',
admin: false,
id: 'invitedHandling123',
firstName: 'Invited',
lastName: 'HE',
invitations: [
{
type: 'handlingEditor',
hasAnswer: true,
isAccepted: false,
collectionId: standardCollection.id,
timestamp: Date.now(),
teamId: 'he123',
},
],
save: jest.fn(() => users.handlingEditor),
editorInChief: false,
},
}
module.exports = users
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 user = fixtures.users.editorInChief
const query = {
role: 'handlingEditor',
}
const getCollectionUsersPath = '../routes/getCollectionUsers'
describe('Get collection users route handler', () => {
it('should return success when the role is correct, the collection exists and the request user is editorInChief ', async () => {
const req = httpMocks.createRequest()
req.query = query
req.params.collectionId = '2c4fb766-a798-4c32-b857-c5d21a2ab331'
const res = httpMocks.createResponse()
const models = Model.build()
await require(getCollectionUsersPath)(models)(req, res)
expect(res.statusCode).toBe(200)
const data = JSON.parse(res._getData())
expect(data).toHaveLength(2)
})
it('should return an error when the role parameter is missing', async () => {
delete query.role
const req = httpMocks.createRequest()
req.query = query
const res = httpMocks.createResponse()
const models = Model.build(user)
await require(getCollectionUsersPath)(models)(req, res)
expect(res.statusCode).toBe(400)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('Role is required')
query.email = 'handlingEditor'
})
})
......@@ -3,45 +3,25 @@ const fixtures = require('../fixtures/fixtures')
const UserMock = require('../mocks/User')
const TeamMock = require('../mocks/Team')
const build = (collection, findUser, emailUser, team) => {
const notFoundError = new Error()
notFoundError.name = 'NotFoundError'
notFoundError.status = 404
const build = () => {
const models = {
User: {},
Collection: {
find: jest.fn(
() =>
collection instanceof Error
? Promise.reject(collection)
: Promise.resolve(collection),
),
find: jest.fn(id => findMock(id, 'collections')),
},
Team: {},
}
UserMock.find = jest.fn(user => {
const foundUser = Object.values(fixtures.users).find(
fixUser => fixUser.id === user.id,
)
if (foundUser === undefined) {
return Promise.reject(findUser)
}
return Promise.resolve(findUser)
})
UserMock.findByEmail = jest.fn(
() =>
emailUser instanceof Error
? Promise.reject(emailUser)
: Promise.resolve(emailUser),
)
UserMock.find = jest.fn(id => findMock(id, 'users'))
TeamMock.find = jest.fn(
() =>
team instanceof Error ? Promise.reject(team) : Promise.resolve(team),
)
TeamMock.updateProperties = jest.fn(
() =>
team instanceof Error ? Promise.reject(team) : Promise.resolve(team),
UserMock.findByEmail = jest.fn(email => findByEmailMock(email))
TeamMock.find = jest.fn(id => findMock(id, 'teams'))
TeamMock.updateProperties = jest.fn(team =>
updatePropertiesMock(team, 'teams'),
)
TeamMock.all = jest.fn(() => Object.values(fixtures.teams))
......@@ -50,4 +30,30 @@ const build = (collection, findUser, emailUser, team) => {
return models
}
const findMock = (id, type) => {
const foundObj = Object.values(fixtures[type]).find(
fixtureObj => fixtureObj.id === id,
)
if (foundObj === undefined) return Promise.reject(notFoundError)
return Promise.resolve(foundObj)
}
const findByEmailMock = email => {
const foundUser = Object.values(fixtures.users).find(
fixtureUser => fixtureUser.email === email,
)
if (foundUser === undefined) return Promise.reject(notFoundError)
return Promise.resolve(foundUser)
}
const updatePropertiesMock = (obj, type) => {
const foundObj = Object.values(fixtures[type]).find(
fixtureObj => fixtureObj === obj,
)
if (foundObj === undefined) return Promise.reject(notFoundError)
return Promise.resolve(foundObj)
}
module.exports = { build }
Markdown is supported
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