diff --git a/packages/xpub-faraday-server/src/AuthorBackend.js b/packages/xpub-faraday-server/src/AuthorBackend.js index 6f784e4c27a5e533542c647121dd84152c6634b4..e71de40326aa0371cc64ce690a142c77f568e6e1 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.js @@ -1,14 +1,9 @@ const bodyParser = require('body-parser') const AuthorBackend = app => { - let authBearer = app.locals.passport.authenticate('bearer', { + const 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, diff --git a/packages/xpub-faraday-server/src/AuthorBackend.test.js b/packages/xpub-faraday-server/src/AuthorBackend.test.js index 4431bbf9a5bc7513ba8e3eb95de7496d5388c04f..e01f686aa5b2a4312afe8c89c83dab86e5883f3f 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.test.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.test.js @@ -7,14 +7,20 @@ const component = require('..') const express = require('express') const fixtures = require('./fixtures/fixtures') const passport = require('passport') -const AnonymousStrategy = require('passport-anonymous').Strategy +const BearerStrategy = require('passport-http-bearer').Strategy function makeApp(response) { const app = express() app.use(bodyParser.json()) // Passport strategies app.use(passport.initialize()) - passport.use('anonymous', new AnonymousStrategy()) + passport.use( + 'bearer', + new BearerStrategy((token, done) => + done(null, fixtures.user, { scope: 'all' }), + ), + ) + app.locals.passport = passport app.locals.models = { @@ -39,6 +45,7 @@ describe('Author Backend API', () => { error.status = 404 return makeApp(error) .post('/api/fragments/123/authors') + .set('Authorization', 'Bearer 123') .send(fixtures.author) .expect(404, '{"error":"Fragment not found"}') }) @@ -51,6 +58,7 @@ describe('Author Backend API', () => { error.details.push({ message: 'firstName is required' }) return makeApp(error) .post('/api/fragments/123/authors') + .set('Authorization', 'Bearer 123') .send(fixtures.invalidAuthor) .expect(404, '{"error":"firstName is required"}') }) @@ -58,12 +66,22 @@ describe('Author Backend API', () => { it('should return an error if an author already exists with the same email', () => makeApp(fixtures.fragment) .post('/api/fragments/123-valid-id/authors') + .set('Authorization', 'Bearer 123') .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') + .set('Authorization', 'Bearer 123') .send(fixtures.newSubmittingAuthor) .expect(400, '{"error":"There can only be one sumbitting author"}')) + + it('should return success', () => + makeApp(fixtures.fragment) + .post('/api/fragments/123-valid-id/authors') + .set('Authorization', 'Bearer 123') + .send(fixtures.newAuthor) + .expect(200, '') + .then(() => expect(fixtures.fragment.save).toHaveBeenCalled())) }) diff --git a/packages/xpub-faraday-server/src/fixtures/fixtures.js b/packages/xpub-faraday-server/src/fixtures/fixtures.js index d265352b1864c086b90baa457fb366fd7bcd5a0c..f570e534a22f13729c04e89d8fcc35846989c8ea 100644 --- a/packages/xpub-faraday-server/src/fixtures/fixtures.js +++ b/packages/xpub-faraday-server/src/fixtures/fixtures.js @@ -5,10 +5,21 @@ const author = { email: 'email@email.com', affiliation: 'University', country: '', - isCorresponding: true, + isCorresponding: false, isSubmitting: true, } +const newAuthor = { + firstName: 'Robert', + middleName: '', + lastName: 'Smith', + email: 'email_robert@email.com', + affiliation: 'University', + country: '', + isCorresponding: true, + isSubmitting: false, +} + const invalidAuthor = { firstName: '', middleName: '', @@ -41,9 +52,18 @@ const fragment = { save: jest.fn(), } +const user = { + type: 'user', + username: 'testuser', + email: 'test@example.com', + password: 'test', +} + module.exports = { author, invalidAuthor, fragment, newSubmittingAuthor, + newAuthor, + user, }