From b43c7467f43afb89a2170f49043e1a637d642672 Mon Sep 17 00:00:00 2001 From: Sebastian <sebastian.mihalache@thinslices.com> Date: Thu, 25 Jan 2018 11:06:41 +0200 Subject: [PATCH] added test for creating a new submitting user --- .../xpub-faraday-server/src/AuthorBackend.js | 5 +- .../src/AuthorBackend.test.js | 73 +++++++++++-------- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/packages/xpub-faraday-server/src/AuthorBackend.js b/packages/xpub-faraday-server/src/AuthorBackend.js index 7197a8e7c..f6b6d8908 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.js @@ -56,17 +56,14 @@ const AuthorBackend = app => { const userBody = { username: `${req.body.firstName}${ req.body.lastName - }${Math.floor(Math.random() * 100)}`, + }${Math.floor(Math.random() * 1000)}`, 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 } diff --git a/packages/xpub-faraday-server/src/AuthorBackend.test.js b/packages/xpub-faraday-server/src/AuthorBackend.test.js index 5f9975d19..d381356d4 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.test.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.test.js @@ -10,7 +10,7 @@ const passport = require('passport') const BearerStrategy = require('passport-http-bearer').Strategy const cloneDeep = require('lodash/cloneDeep') -function makeApp(fragment, user, existingUser, newUser) { +function makeApp(fragment, user, existingUser) { const app = express() app.use(bodyParser.json()) // Passport strategies @@ -21,6 +21,7 @@ function makeApp(fragment, user, existingUser, newUser) { done(null, fixtures.user, { scope: 'all' }), ), ) + app.locals.passport = passport app.locals.models = { Fragment: { @@ -44,6 +45,30 @@ function makeApp(fragment, user, existingUser, newUser) { ), }, } + function UserMock(properties) { + this.type = 'user' + this.email = properties.email + this.username = properties.username + this.password = properties.password + } + + UserMock.find = jest.fn( + () => + user instanceof Error ? Promise.reject(user) : Promise.resolve(user), + ) + UserMock.findByEmail = jest.fn( + () => + existingUser instanceof Error + ? Promise.reject(existingUser) + : Promise.resolve(existingUser), + ) + + UserMock.prototype.save = jest.fn(() => { + this.id = '111222' + return Promise.resolve(this) + }) + + app.locals.models.User = UserMock component.backend()(app) return supertest(app) @@ -115,33 +140,21 @@ describe('Author Backend API', () => { 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') - // }) - // }) + 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 + + // console.log(testFixtures.adminFragment) + return makeApp(testFixtures.adminFragment, testFixtures.admin, error) + .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('111222') + }) + }) }) -- GitLab