diff --git a/packages/xpub-faraday-server/src/AuthorBackend.js b/packages/xpub-faraday-server/src/AuthorBackend.js index 1cb6690c8c199aa26d274457c3f1a87a8c7dc52a..3c0fbcc1dab501f51fb9080d8612ada06746fcde 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.js @@ -72,7 +72,6 @@ const AuthorBackend = app => { } } } - console.log('FRAG', fragment) fragment = await fragment.save() await collection.save() res.status(200).json(fragment.authors[fragment.authors.length - 1]) diff --git a/packages/xpub-faraday-server/src/AuthorBackend.test.js b/packages/xpub-faraday-server/src/AuthorBackend.test.js index d5ed4bef945274cfd64c63efa3e970cbe4d8b8d9..697e0b8237acb26da5f79904d80b2bf6e01b543b 100644 --- a/packages/xpub-faraday-server/src/AuthorBackend.test.js +++ b/packages/xpub-faraday-server/src/AuthorBackend.test.js @@ -9,6 +9,8 @@ const fixtures = require('./fixtures/fixtures') const passport = require('passport') const BearerStrategy = require('passport-http-bearer').Strategy const cloneDeep = require('lodash/cloneDeep') +const UserMock = require('./mocks/User') +const Fragment = require('./mocks/Fragment') function makeApp(collection, fragment, standardUser, existingUser) { const app = express() @@ -21,7 +23,6 @@ function makeApp(collection, fragment, standardUser, existingUser) { done(null, fixtures.users.standardUser, { scope: 'all' }), ), ) - app.locals.passport = passport app.locals.models = { Fragment: { @@ -42,12 +43,6 @@ function makeApp(collection, fragment, standardUser, existingUser) { ), }, } - function UserMock(properties) { - this.type = 'user' - this.email = properties.email - this.username = properties.username - this.password = properties.password - } UserMock.find = jest.fn( () => @@ -62,11 +57,6 @@ function makeApp(collection, fragment, standardUser, existingUser) { : Promise.resolve(existingUser), ) - UserMock.prototype.save = jest.fn(() => { - this.id = '111222' - return Promise.resolve(this) - }) - app.locals.models.User = UserMock component.backend()(app) @@ -74,6 +64,18 @@ function makeApp(collection, fragment, standardUser, existingUser) { } const createAuthorUrl = '/api/collections/123/fragments/123/authors' +const getNewFragment = authors => { + const fragProps = { + type: 'fragment', + fragmentType: 'blogpost', + title: 'Just your regular blogpost', + source: '<blog></blog>', + presentation: '<p></p>', + authors, + owners: [], + } + return new Fragment(fragProps) +} describe('Author Backend API', () => { let testFixtures = {} beforeEach(() => (testFixtures = cloneDeep(fixtures))) @@ -113,97 +115,96 @@ describe('Author Backend API', () => { .expect(404, '{"error":"firstName is required"}') }) - it('should return an error if an author already exists with the same email', () => - makeApp( + it('should return an error if an author already exists with the same email', () => { + const fragment = getNewFragment([testFixtures.authors.standardAuthor]) + return makeApp(testFixtures.collections.standardCollection, fragment) + .post(createAuthorUrl) + .set('Authorization', 'Bearer 123') + .send(testFixtures.authors.standardAuthor) + .expect(400, '{"error":"Author with the same email already exists"}') + }) + + it('should return an error if there already is a submitting author', () => { + const fragment = getNewFragment([testFixtures.authors.standardAuthor]) + return makeApp(testFixtures.collections.standardCollection, fragment) + .post(createAuthorUrl) + .set('Authorization', 'Bearer 123') + .send(testFixtures.authors.newSubmittingAuthor) + .expect(400, '{"error":"There can only be one sumbitting author"}') + }) + + it('should return success when saving a new author', () => { + const fragment = getNewFragment([]) + + return makeApp( + fixtures.collections.standardCollection, + fragment, + testFixtures.users.standardUser, + ) + .post(createAuthorUrl) + .set('Authorization', 'Bearer 123') + .send(testFixtures.authors.newAuthor) + .expect(200) + .then(() => expect(fragment.save).toHaveBeenCalled()) + }) + + it('should return success when the admin adds a submitting author and the author already has a corresponding user account', () => { + const fragment = getNewFragment([]) + + return makeApp( testFixtures.collections.standardCollection, - testFixtures.fragments.standardFragment, + fragment, + testFixtures.users.admin, + testFixtures.users.existingUser, ) .post(createAuthorUrl) .set('Authorization', 'Bearer 123') .send(testFixtures.authors.standardAuthor) - .expect(400, '{"error":"Author with the same email already exists"}')) + .expect(200) + .then(() => { + expect(fragment.save).toHaveBeenCalled() + expect( + testFixtures.collections.standardCollection.save, + ).toHaveBeenCalled() + expect(fragment.owners.length).toBeGreaterThan(0) + expect( + testFixtures.collections.standardCollection.owners.length, + ).toBeGreaterThan(1) + expect(fragment.owners[0]).toBe('123987') + expect(testFixtures.collections.standardCollection.owners[1]).toBe( + '123987', + ) + }) + }) - it('should return an error if there already is a submitting author', () => - makeApp( + 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 fragment = getNewFragment([]) + return makeApp( testFixtures.collections.standardCollection, - testFixtures.fragments.standardFragment, + fragment, + testFixtures.users.admin, + error, ) .post(createAuthorUrl) .set('Authorization', 'Bearer 123') - .send(testFixtures.authors.newSubmittingAuthor) - .expect(400, '{"error":"There can only be one sumbitting author"}')) - - // it('should return success when saving a new author', () => - // makeApp( - // fixtures.collections.standardCollection, - // testFixtures.fragments.standardFragment, - // testFixtures.users.standardUser, - // ) - // .post(createAuthorUrl) - // .set('Authorization', 'Bearer 123') - // .send(testFixtures.authors.newAuthor) - // .expect(200, '') - // .then(() => - // expect(testFixtures.fragments.standardFragment.save).toHaveBeenCalled(), - // )) - - // it('should return success when the admin adds a submitting author and the author already has a corresponding user account', () => - // makeApp( - // testFixtures.collections.standardCollection, - // testFixtures.fragments.adminFragment, - // testFixtures.users.admin, - // testFixtures.users.existingUser, - // ) - // .post(createAuthorUrl) - // .set('Authorization', 'Bearer 123') - // .send(testFixtures.authors.standardAuthor) - // .expect(200, '') - // .then(() => { - // expect(testFixtures.fragments.adminFragment.save).toHaveBeenCalled() - // expect( - // testFixtures.collections.standardCollection.save, - // ).toHaveBeenCalled() - // expect( - // testFixtures.fragments.adminFragment.owners.length, - // ).toBeGreaterThan(0) - // expect( - // testFixtures.collections.standardCollection.owners.length, - // ).toBeGreaterThan(1) - // expect(testFixtures.fragments.adminFragment.owners[0]).toBe('123987') - // expect(testFixtures.collections.standardCollection.owners[1]).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 - // return makeApp( - // testFixtures.collections.standardCollection, - // testFixtures.fragments.adminFragment, - // testFixtures.users.admin, - // error, - // ) - // .post(createAuthorUrl) - // .set('Authorization', 'Bearer 123') - // .send(testFixtures.authors.standardAuthor) - // .expect(200, '') - // .then(() => { - // expect(testFixtures.fragments.adminFragment.save).toHaveBeenCalled() - // expect( - // testFixtures.collections.standardCollection.save, - // ).toHaveBeenCalled() - // expect( - // testFixtures.fragments.adminFragment.owners.length, - // ).toBeGreaterThan(0) - // expect( - // testFixtures.collections.standardCollection.owners.length, - // ).toBeGreaterThan(1) - // expect(testFixtures.fragments.adminFragment.owners[0]).toBe('111222') - // expect(testFixtures.collections.standardCollection.owners[1]).toBe( - // '111222', - // ) - // }) - // }) + .send(testFixtures.authors.standardAuthor) + .expect(200) + .then(() => { + expect(fragment.save).toHaveBeenCalled() + expect( + testFixtures.collections.standardCollection.save, + ).toHaveBeenCalled() + expect(fragment.owners.length).toBeGreaterThan(0) + expect( + testFixtures.collections.standardCollection.owners.length, + ).toBeGreaterThan(1) + expect(fragment.owners[0]).toBe('111222') + expect(testFixtures.collections.standardCollection.owners[1]).toBe( + '111222', + ) + }) + }) }) diff --git a/packages/xpub-faraday-server/src/fixtures/fragments.js b/packages/xpub-faraday-server/src/fixtures/fragments.js index 7bf3f31de4a4326bb5c0f11560c44045b0869419..ea40534ea148f7975a5c2fa4f93fbc66c85b6183 100644 --- a/packages/xpub-faraday-server/src/fixtures/fragments.js +++ b/packages/xpub-faraday-server/src/fixtures/fragments.js @@ -8,7 +8,7 @@ const fragments = { source: '<blog></blog>', presentation: '<p></p>', authors: [authors.standardAuthor], - save: jest.fn(), + save: () => fragments.standardFragment, }, adminFragment: { @@ -18,7 +18,7 @@ const fragments = { source: '<blog></blog>', presentation: '<p></p>', authors: [], - save: jest.fn(), + save: () => fragments.adminFragment, owners: [], }, } diff --git a/packages/xpub-faraday-server/src/mocks/Fragment.js b/packages/xpub-faraday-server/src/mocks/Fragment.js new file mode 100644 index 0000000000000000000000000000000000000000..cb4ceba637db457f6f8a6d201d8e3b18b0961cd1 --- /dev/null +++ b/packages/xpub-faraday-server/src/mocks/Fragment.js @@ -0,0 +1,12 @@ +function Fragment(properties) { + this.type = properties.type + this.fragmentType = properties.fragmentType + this.title = properties.title + this.source = properties.source + this.presentation = properties.presentation + this.authors = properties.authors + this.owners = properties.owners + this.save = jest.fn(() => Promise.resolve(this)) +} + +module.exports = Fragment diff --git a/packages/xpub-faraday-server/src/mocks/User.js b/packages/xpub-faraday-server/src/mocks/User.js new file mode 100644 index 0000000000000000000000000000000000000000..2ea447401e444a78401830c157d84aa84e6ba1c0 --- /dev/null +++ b/packages/xpub-faraday-server/src/mocks/User.js @@ -0,0 +1,13 @@ +function User(properties) { + this.type = 'user' + this.email = properties.email + this.username = properties.username + this.password = properties.password +} + +User.prototype.save = jest.fn(() => { + this.id = '111222' + return Promise.resolve(this) +}) + +module.exports = User