From 46a028ee3fcbd4af0c2c74a8778363d5a913279e Mon Sep 17 00:00:00 2001 From: Sebastian <sebastian.mihalache@thinslices.com> Date: Wed, 24 Jan 2018 16:51:28 +0200 Subject: [PATCH] added submitting authors as owners --- packages/xpub-faraday-server/package.json | 3 +- .../xpub-faraday-server/src/AuthorBackend.js | 32 +++++++ .../src/AuthorBackend.test.js | 92 +++++++++++++++---- .../src/fixtures/fixtures.js | 34 +++++++ packages/xpub-faraday/config/validations.js | 1 + yarn.lock | 4 + 6 files changed, 149 insertions(+), 17 deletions(-) diff --git a/packages/xpub-faraday-server/package.json b/packages/xpub-faraday-server/package.json index ee016b7c3..be17dc271 100644 --- a/packages/xpub-faraday-server/package.json +++ b/packages/xpub-faraday-server/package.json @@ -17,7 +17,8 @@ "body-parser": "^1.17.2", "config": "^1.26.1", "moment": "^2.18.1", - "nodemailer": "^4.0.1" + "nodemailer": "^4.0.1", + "uuid": "^3.2.1" }, "peerDependencies": { "@pubsweet/logger": "^0.0.1", diff --git a/packages/xpub-faraday-server/src/AuthorBackend.js b/packages/xpub-faraday-server/src/AuthorBackend.js index e71de4032..7197a8e7c 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.js @@ -1,4 +1,5 @@ const bodyParser = require('body-parser') +const uuid = require('uuid') const AuthorBackend = app => { const authBearer = app.locals.passport.authenticate('bearer', { @@ -39,7 +40,38 @@ const AuthorBackend = app => { return } } + req.body.id = uuid.v4() fragment.authors.push(req.body) + const reqUser = await app.locals.models.User.find(req.user) + if (reqUser.admin === true && req.body.isSubmitting === true) { + try { + // check if author has corresponding user + const user = await app.locals.models.User.findByEmail( + req.body.email, + ) + fragment.owners.push(user.id) + } catch (e) { + if (e.name === 'NotFoundError') { + // create a new User account + const userBody = { + username: `${req.body.firstName}${ + req.body.lastName + }${Math.floor(Math.random() * 100)}`, + email: req.body.email, + password: uuid.v4(), + } + console.log(new app.locals.models.User()) + let newUser = new app.locals.models.User(userBody) + console.log('user nou', newUser) + newUser = await newUser.save() + fragment.owners.push(newUser.id) + } else { + console.log('error naspa') + res.status(e.status).json(e) + return + } + } + } fragment = await fragment.save() res.status(200).json(fragment) } catch (e) { diff --git a/packages/xpub-faraday-server/src/AuthorBackend.test.js b/packages/xpub-faraday-server/src/AuthorBackend.test.js index e01f686aa..5f9975d19 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.test.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.test.js @@ -8,8 +8,9 @@ const express = require('express') const fixtures = require('./fixtures/fixtures') const passport = require('passport') const BearerStrategy = require('passport-http-bearer').Strategy +const cloneDeep = require('lodash/cloneDeep') -function makeApp(response) { +function makeApp(fragment, user, existingUser, newUser) { const app = express() app.use(bodyParser.json()) // Passport strategies @@ -20,16 +21,26 @@ function makeApp(response) { done(null, fixtures.user, { scope: 'all' }), ), ) - app.locals.passport = passport - app.locals.models = { Fragment: { find: jest.fn( () => - response instanceof Error - ? Promise.reject(response) - : Promise.resolve(response), + fragment instanceof Error + ? Promise.reject(fragment) + : Promise.resolve(fragment), + ), + }, + User: { + find: jest.fn( + () => + user instanceof Error ? Promise.reject(user) : Promise.resolve(user), + ), + findByEmail: jest.fn( + () => + existingUser instanceof Error + ? Promise.reject(existingUser) + : Promise.resolve(existingUser), ), }, } @@ -39,6 +50,9 @@ function makeApp(response) { } describe('Author Backend API', () => { + let testFixtures = {} + beforeEach(() => (testFixtures = cloneDeep(fixtures))) + it('should return an error if fragment is not found', () => { const error = new Error() error.name = 'NotFoundError' @@ -46,7 +60,7 @@ describe('Author Backend API', () => { return makeApp(error) .post('/api/fragments/123/authors') .set('Authorization', 'Bearer 123') - .send(fixtures.author) + .send(testFixtures.author) .expect(404, '{"error":"Fragment not found"}') }) @@ -59,29 +73,75 @@ describe('Author Backend API', () => { return makeApp(error) .post('/api/fragments/123/authors') .set('Authorization', 'Bearer 123') - .send(fixtures.invalidAuthor) + .send(testFixtures.invalidAuthor) .expect(404, '{"error":"firstName is required"}') }) it('should return an error if an author already exists with the same email', () => - makeApp(fixtures.fragment) + makeApp(testFixtures.fragment) .post('/api/fragments/123-valid-id/authors') .set('Authorization', 'Bearer 123') - .send(fixtures.author) + .send(testFixtures.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) + makeApp(testFixtures.fragment) .post('/api/fragments/123-valid-id/authors') .set('Authorization', 'Bearer 123') - .send(fixtures.newSubmittingAuthor) + .send(testFixtures.newSubmittingAuthor) .expect(400, '{"error":"There can only be one sumbitting author"}')) - it('should return success', () => - makeApp(fixtures.fragment) + it('should return success when saving a new author', () => + makeApp(testFixtures.fragment, testFixtures.user) .post('/api/fragments/123-valid-id/authors') .set('Authorization', 'Bearer 123') - .send(fixtures.newAuthor) + .send(testFixtures.newAuthor) .expect(200, '') - .then(() => expect(fixtures.fragment.save).toHaveBeenCalled())) + .then(() => expect(testFixtures.fragment.save).toHaveBeenCalled())) + + it('should return success when the admin adds a submitting author and the author already has a corresponding user account', () => + makeApp( + testFixtures.adminFragment, + testFixtures.admin, + testFixtures.existingUser, + ) + .post('/api/fragments/123-valid-id/authors') + .set('Authorization', 'Bearer 123') + .send(testFixtures.author) + .expect(200, '') + .then(() => { + expect(testFixtures.adminFragment.save).toHaveBeenCalled() + expect(testFixtures.adminFragment.owners.length).toBeGreaterThan(0) + expect(testFixtures.adminFragment.owners[0]).toBe('123987') + })) + + // it('should return success when the admin adds a submitting author and creates a corresponding user account', () => { + // const error = new Error() + // error.name = 'NotFoundError' + // error.status = 404 + // const newUser = { + // username: `${testFixtures.author.firstName}${ + // testFixtures.author.lastName + // }${Math.floor(Math.random() * 100)}`, + // email: testFixtures.author.email, + // password: 'test', + // id: '888999', + // } + // // console.log(testFixtures.adminFragment) + // return makeApp( + // testFixtures.adminFragment, + // testFixtures.admin, + // error, + // newUser, + // ) + // .post('/api/fragments/123-valid-id/authors') + // .set('Authorization', 'Bearer 123') + // .send(testFixtures.author) + // .expect(200, '') + // .then(() => { + // expect(testFixtures.adminFragment.save).toHaveBeenCalled() + // expect(testFixtures.adminFragment.owners.length).toBeGreaterThan(0) + // expect(testFixtures.adminFragment.owners[0]).toBe('888999') + // }) + // }) }) diff --git a/packages/xpub-faraday-server/src/fixtures/fixtures.js b/packages/xpub-faraday-server/src/fixtures/fixtures.js index f570e534a..80759aaa4 100644 --- a/packages/xpub-faraday-server/src/fixtures/fixtures.js +++ b/packages/xpub-faraday-server/src/fixtures/fixtures.js @@ -7,6 +7,7 @@ const author = { country: '', isCorresponding: false, isSubmitting: true, + id: '123', } const newAuthor = { @@ -18,6 +19,7 @@ const newAuthor = { country: '', isCorresponding: true, isSubmitting: false, + id: '456', } const invalidAuthor = { @@ -29,6 +31,7 @@ const invalidAuthor = { country: '', isCorresponding: false, isSubmitting: false, + id: '768', } const newSubmittingAuthor = { @@ -40,6 +43,7 @@ const newSubmittingAuthor = { country: '', isCorresponding: false, isSubmitting: true, + id: '879', } const fragment = { @@ -52,6 +56,17 @@ const fragment = { save: jest.fn(), } +const adminFragment = { + type: 'fragment', + fragmentType: 'blogpost', + title: 'Just your admin blogpost', + source: '<blog></blog>', + presentation: '<p></p>', + authors: [], + save: jest.fn(), + owners: [], +} + const user = { type: 'user', username: 'testuser', @@ -59,6 +74,22 @@ const user = { password: 'test', } +const admin = { + type: 'user', + username: 'admin', + email: 'admin@example.com', + password: 'test', + admin: true, +} + +const existingUser = { + type: 'user', + username: 'authoruser', + email: 'email@email.com', + password: 'test', + id: '123987', +} + module.exports = { author, invalidAuthor, @@ -66,4 +97,7 @@ module.exports = { newSubmittingAuthor, newAuthor, user, + admin, + adminFragment, + existingUser, } diff --git a/packages/xpub-faraday/config/validations.js b/packages/xpub-faraday/config/validations.js index 8e82f6e46..198a4d45f 100644 --- a/packages/xpub-faraday/config/validations.js +++ b/packages/xpub-faraday/config/validations.js @@ -74,6 +74,7 @@ module.exports = { country: Joi.string().allow(''), isSubmitting: Joi.boolean(), isCorresponding: Joi.boolean(), + id: Joi.string().uuid(), }), ), }, diff --git a/yarn.lock b/yarn.lock index c49ef8c6b..978b7a328 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10997,6 +10997,10 @@ uuid@^3.0.0, uuid@^3.0.1, uuid@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.1.0.tgz#3dd3d3e790abc24d7b0d3a034ffababe28ebbc04" +uuid@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14" + validate-npm-package-license@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.1.tgz#2804babe712ad3379459acfbe24746ab2c303fbc" -- GitLab