Commit 1ad059dc authored by Tamlyn Rhodes's avatar Tamlyn Rhodes

fix: add rudimentary tests to review backend

parent bd411966
Pipeline #4963 passed with stages
in 5 minutes and 8 seconds
......@@ -2,19 +2,25 @@
"name": "pubsweet-component-xpub-review-backend",
"version": "0.1.0",
"main": "src",
"scripts": {},
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "Collaborative Knowledge Foundation",
"license": "MIT",
"dependencies": {
"body-parser": "^1.17.2",
"config": "^1.26.1",
"moment": "^2.18.1",
"nodemailer": "^4.0.1"
},
"devDependencies": {
"body-parser": "^1.17.2",
"jest": "^22.4.2",
"superagent": "^3.8.2",
"supertest": "^3.0.0"
},
"peerDependencies": {
"@pubsweet/logger": ">=0.0.1",
"nodemailer": "^4.4.2",
"pubsweet-component-xpub-review": "^0.0.1",
"pubsweet-server": ">=1.0.0-alpha.1"
}
......
const { pick } = require('lodash')
const config = require('config')
const nodemailer = require('nodemailer')
const logger = require('@pubsweet/logger')
const User = require('pubsweet-server/src/models/User')
const Fragment = require('pubsweet-server/src/models/Fragment')
const Collection = require('pubsweet-server/src/models/Collection')
const authsome = require('pubsweet-server/src/helpers/authsome')
const AuthorizationError = require('pubsweet-server/src/errors/AuthorizationError')
const options = config.get('mailer.transport')
const transport = nodemailer.createTransport(options)
const transport = require('./transport')
module.exports = app => {
app.patch('/api/make-decision', async (req, res, next) => {
......
process.env.SUPPRESS_NO_CONFIG_WARNING = true
process.env.NODE_CONFIG = '{"mailer":{"from":"sender@example.com"}}'
const express = require('express')
const supertest = require('supertest')
const bodyParser = require('body-parser')
// mocks
jest.mock('./transport', () => ({ sendMail: jest.fn() }))
jest.mock('pubsweet-server/src/models/User', () => ({
find: jest.fn(() => ({ email: 'author@example.org' })),
}))
jest.mock('pubsweet-server/src/models/Fragment', () => ({
find: jest.fn(() => ({
version: 1,
owners: [{}],
updateProperties(update) {
Object.assign(this, update)
},
save: () => {},
})),
}))
jest.mock('pubsweet-server/src/models/Collection', () => ({
find: jest.fn(() => ({
updateProperties: () => {},
save: () => {},
})),
}))
jest.mock('pubsweet-server/src/helpers/authsome', () => ({
can: jest.fn(() => true),
}))
const authsome = require('pubsweet-server/src/helpers/authsome')
const transport = require('./transport')
const component = require('./reviewBackend')
function makeApp() {
const app = express()
app.use(bodyParser.json())
component(app)
return supertest(app)
}
describe('/api/make-decision route', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('sends email on acceptance', async () => {
const app = makeApp()
const response = await app.patch('/api/make-decision').send({
decision: { recommendation: 'accept', note: { content: 'blah blah' } },
versionId: 1,
projectId: 2,
})
expect(response.body.version).toBeDefined()
expect(response.body.project).toBeDefined()
expect(response.body.nextVersion).not.toBeDefined()
expect(transport.sendMail).toHaveBeenCalledWith(
expect.objectContaining({
from: 'sender@example.com',
to: ['author@example.org'],
subject: 'Decision made',
}),
)
})
it('rejects if not authorised', async () => {
authsome.can.mockReturnValue(false)
const app = makeApp()
const response = await app.patch('/api/make-decision').send({
decision: { recommendation: 'accept', note: { content: 'blah blah' } },
versionId: 1,
projectId: 2,
})
expect(response.status).toBe(403)
expect(transport.sendMail).not.toHaveBeenCalled()
})
})
const config = require('config')
const nodemailer = require('nodemailer')
// SMTP transport options: https://nodemailer.com/smtp/
const options = config.get('mailer.transport')
module.exports = nodemailer.createTransport(options)
This diff is collapsed.
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