diff --git a/packages/xpub-faraday-server/src/AuthorBackend.js b/packages/xpub-faraday-server/src/AuthorBackend.js index a7ec49f6ef68ba7031dc1f27c0f701ca1878358c..6f784e4c27a5e533542c647121dd84152c6634b4 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.js @@ -1,19 +1,20 @@ const bodyParser = require('body-parser') const AuthorBackend = app => { - const authBearer = app.locals.passport.authenticate('bearer', { + let authBearer = app.locals.passport.authenticate('bearer', { session: false, }) + + if (process.env.NODE_ENV === 'test') { + authBearer = app.locals.passport.authenticate('anonymous') + } + app.post( '/api/fragments/:fragmentId/authors', authBearer, bodyParser.json(), async (req, res, next) => { try { - if (!req.params.fragmentId) { - res.status(400).json({ error: 'Fragment ID is required' }) - return - } let fragment = await app.locals.models.Fragment.find( req.params.fragmentId, ) @@ -24,19 +25,22 @@ const AuthorBackend = app => { ) if (emailAuthors.length > 0) { - res.status(400).json({ error: 'Author already exists' }) + res + .status(400) + .json({ error: 'Author with the same email already exists' }) return } - const nameAuthors = fragment.authors.filter( + const submittingAuthors = fragment.authors.filter( author => - author.firstName === req.body.firstName && - author.middleName === req.body.middleName && - author.lastName === req.body.lastName, + author.isSubmitting === true && + author.isSubmitting === req.body.isSubmitting, ) - if (nameAuthors.length > 0) { - res.status(400).json({ error: 'Author already exists' }) + if (submittingAuthors.length > 0) { + res + .status(400) + .json({ error: 'There can only be one sumbitting author' }) return } } diff --git a/packages/xpub-faraday-server/src/AuthorBackend.test.js b/packages/xpub-faraday-server/src/AuthorBackend.test.js index 22536c86a7e9e0a41d88acc544291ced7d1a2c0c..4431bbf9a5bc7513ba8e3eb95de7496d5388c04f 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.test.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.test.js @@ -2,30 +2,21 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' process.env.SUPPRESS_NO_CONFIG_WARNING = true const bodyParser = require('body-parser') -const express = require('express') const supertest = require('supertest') const component = require('..') - -const author = { - first_name: 'marcel', - middle_name: 'sss', - last_name: 'iures', - email: 'email@ciment2.com', - affiliation: 'UTI', - country: '', - is_corresponding: true, - is_submitting: true, - save: jest.fn(), -} +const express = require('express') +const fixtures = require('./fixtures/fixtures') +const passport = require('passport') +const AnonymousStrategy = require('passport-anonymous').Strategy function makeApp(response) { const app = express() app.use(bodyParser.json()) - app.locals = { - passport: { - authenticate: jest.fn(() => () => Promise.resolve(true)), - }, - } + // Passport strategies + app.use(passport.initialize()) + passport.use('anonymous', new AnonymousStrategy()) + app.locals.passport = passport + app.locals.models = { Fragment: { find: jest.fn( @@ -36,16 +27,43 @@ function makeApp(response) { ), }, } + component.backend()(app) return supertest(app) } describe('Author Backend API', () => { - it('should return error if fragment is not found', () => - makeApp(new Error('Not Found')) - .post('/api/fragments/cf7b9ea6-47ac-4188-b0ef-f89cc17364fe/authors') - .set('Content-Type', 'application/json') - // .set('Authentication', `Bearer ${token}`) - .send(author) - .expect(404, '"error": "Fragment not found"')) + it('should return an error if fragment is not found', () => { + const error = new Error() + error.name = 'NotFoundError' + error.status = 404 + return makeApp(error) + .post('/api/fragments/123/authors') + .send(fixtures.author) + .expect(404, '{"error":"Fragment not found"}') + }) + + it('should return an error if an author field is invalid', () => { + const error = new Error() + error.name = 'ValidationError' + error.status = 404 + error.details = [] + error.details.push({ message: 'firstName is required' }) + return makeApp(error) + .post('/api/fragments/123/authors') + .send(fixtures.invalidAuthor) + .expect(404, '{"error":"firstName is required"}') + }) + + it('should return an error if an author already exists with the same email', () => + makeApp(fixtures.fragment) + .post('/api/fragments/123-valid-id/authors') + .send(fixtures.author) + .expect(400, '{"error":"Author with the same email already exists"}')) + + it('should return an error if there already is a submitting author', () => + makeApp(fixtures.fragment) + .post('/api/fragments/123-valid-id/authors') + .send(fixtures.newSubmittingAuthor) + .expect(400, '{"error":"There can only be one sumbitting author"}')) }) diff --git a/packages/xpub-faraday-server/src/fixtures/fixtures.js b/packages/xpub-faraday-server/src/fixtures/fixtures.js new file mode 100644 index 0000000000000000000000000000000000000000..d265352b1864c086b90baa457fb366fd7bcd5a0c --- /dev/null +++ b/packages/xpub-faraday-server/src/fixtures/fixtures.js @@ -0,0 +1,49 @@ +const author = { + firstName: 'Andrew', + middleName: '', + lastName: 'Smith', + email: 'email@email.com', + affiliation: 'University', + country: '', + isCorresponding: true, + isSubmitting: true, +} + +const invalidAuthor = { + firstName: '', + middleName: '', + lastName: 'Jones', + email: 'email2@email.com', + affiliation: 'University', + country: '', + isCorresponding: false, + isSubmitting: false, +} + +const newSubmittingAuthor = { + firstName: 'Andrew', + middleName: '', + lastName: 'Smith', + email: 'email3@email.com', + affiliation: 'University', + country: '', + isCorresponding: false, + isSubmitting: true, +} + +const fragment = { + type: 'fragment', + fragmentType: 'blogpost', + title: 'Just your regular blogpost', + source: '<blog></blog>', + presentation: '<p></p>', + authors: [author], + save: jest.fn(), +} + +module.exports = { + author, + invalidAuthor, + fragment, + newSubmittingAuthor, +} diff --git a/packages/xpub-faraday/app/routes.js b/packages/xpub-faraday/app/routes.js index 4a15dc071cb4d5c16b955d73a5bdb609f88ca814..525a7266e9c5701231d10709272c330c07ef3fed 100644 --- a/packages/xpub-faraday/app/routes.js +++ b/packages/xpub-faraday/app/routes.js @@ -7,6 +7,7 @@ import App from 'pubsweet-component-xpub-app/src/components' import { PrivateRoute, + SignupPage, LoginPage, LogoutPage, } from 'pubsweet-component-xpub-authentication/src/components' @@ -20,6 +21,7 @@ import { WizardPage } from 'pubsweet-component-wizard/src/components' const Routes = () => ( <App> <Route component={LoginPage} exact path="/login" /> + <Route component={SignupPage} exact path="/signup" /> <PrivateRoute component={DashboardPage} exact path="/" /> <PrivateRoute component={LogoutPage} exact path="/logout" /> <PrivateRoute