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 6046310406d7b567df21139092052776fe6d7d2d..1e9467ee14a2c8e812f23f1582bcf3d0423a15e8 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.test.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.test.js @@ -7,14 +7,23 @@ const component = require('..') const express = require('express') const fixtures = require('./fixtures/fixtures') const passport = require('passport') -const AnonymousStrategy = require('passport-anonymous').Strategy +// const AnonymousStrategy = require('passport-anonymous').Strategy +const jwt = require('jsonwebtoken') +const BearerStrategy = require('passport-http-bearer').Strategy +const config = require('config') 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 +48,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 +61,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,18 +69,21 @@ 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 8f9abc7794a7c6199d0b45dbb17b62920bada375..f570e534a22f13729c04e89d8fcc35846989c8ea 100644 --- a/packages/xpub-faraday-server/src/fixtures/fixtures.js +++ b/packages/xpub-faraday-server/src/fixtures/fixtures.js @@ -52,10 +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, }