Skip to content
Snippets Groups Projects
Commit 39a27902 authored by Bogdan Cochior's avatar Bogdan Cochior
Browse files

Merge branch 'faraday-master' of gitlab.coko.foundation:xpub/xpub into faraday-master

parents b29029fd 3f00564d
No related branches found
No related tags found
No related merge requests found
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,
......
......@@ -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()))
})
......@@ -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,
}
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