From ee5a2349c04a7c1afb26731f6175ad92053bb28b Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 00:00:38 +0100 Subject: [PATCH 01/19] build(deps): Add rosie for factories Why have I not seen this before? --- backend/package.json | 1 + backend/yarn.lock | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/backend/package.json b/backend/package.json index e9e0f44a3..749972021 100644 --- a/backend/package.json +++ b/backend/package.json @@ -127,6 +127,7 @@ "jest": "~25.1.0", "nodemon": "~2.0.2", "prettier": "~1.19.1", + "rosie": "^2.0.1", "supertest": "~4.0.2" } } diff --git a/backend/yarn.lock b/backend/yarn.lock index 8f4cfc9a2..65570ab94 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -7754,6 +7754,11 @@ rimraf@^3.0.0: dependencies: glob "^7.1.3" +rosie@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/rosie/-/rosie-2.0.1.tgz#c250c4787ce450b72aa9eff26509f68589814fa2" + integrity sha1-wlDEeHzkULcqqe/yZQn2hYmBT6I= + router-ips@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/router-ips/-/router-ips-1.0.0.tgz#44e00858ebebc0133d58e40b2cd8a1fbb04203f5" @@ -7918,6 +7923,11 @@ serve-static@1.14.1: version "1.14.1" resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.14.1.tgz#666e636dc4f010f7ef29970a88a674320898b2f9" integrity sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg== + dependencies: + encodeurl "~1.0.2" + escape-html "~1.0.3" + parseurl "~1.3.3" + send "0.17.1" set-blocking@^2.0.0, set-blocking@~2.0.0: version "2.0.0" From 63939fa9e0c3138abbe7e3a048f9e146fb53b123 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 00:10:51 +0100 Subject: [PATCH 02/19] Refactor all dependencies without dependencies --- backend/src/factories/badges.js | 24 ++++++----- backend/src/factories/categories.js | 23 +++++----- backend/src/factories/donations.js | 23 +++++----- backend/src/factories/emailAddresses.js | 26 ++++++------ backend/src/factories/locations.js | 42 +++++++++++-------- backend/src/factories/reports.js | 13 +++++- backend/src/factories/socialMedia.js | 22 ++++++---- backend/src/factories/tags.js | 20 ++++++--- .../src/factories/unverifiedEmailAddresses.js | 13 +++++- 9 files changed, 128 insertions(+), 78 deletions(-) diff --git a/backend/src/factories/badges.js b/backend/src/factories/badges.js index 5f0482460..3c981ae8a 100644 --- a/backend/src/factories/badges.js +++ b/backend/src/factories/badges.js @@ -1,15 +1,19 @@ +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('badge') + .attr('type', 'crowdfunding') + .attr('status', 'permanent') + .after((buildObject, options) => { + return neode.create('Badge', buildObject) + }) + export default function create() { return { - factory: async ({ args, neodeInstance }) => { - const defaults = { - type: 'crowdfunding', - status: 'permanent', - } - args = { - ...defaults, - ...args, - } - return neodeInstance.create('Badge', args) + factory: ({ args, neodeInstance }) => { + return Factory.build('badge', args) }, } } diff --git a/backend/src/factories/categories.js b/backend/src/factories/categories.js index d3f5fed21..5dd89efbe 100644 --- a/backend/src/factories/categories.js +++ b/backend/src/factories/categories.js @@ -1,18 +1,21 @@ import uuid from 'uuid/v4' +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('category') + .attr('id', uuid) + .attr('icon', 'img/badges/fundraisingbox_de_airship.svg') + .attr('name', 'Some category name') + .after((buildObject, options) => { + return neode.create('Category', buildObject) + }) export default function create() { return { factory: async ({ args, neodeInstance }) => { - const defaults = { - id: uuid(), - icon: 'img/badges/fundraisingbox_de_airship.svg', - name: 'Some category name', - } - args = { - ...defaults, - ...args, - } - return neodeInstance.create('Category', args) + return Factory.build('category', args) }, } } diff --git a/backend/src/factories/donations.js b/backend/src/factories/donations.js index e22cdb6d7..a17c2511a 100644 --- a/backend/src/factories/donations.js +++ b/backend/src/factories/donations.js @@ -1,18 +1,21 @@ import uuid from 'uuid/v4' +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('donations') + .attr('id', uuid) + .attr('goal', 15000) + .attr('progress', 0) + .after((buildObject, options) => { + return neode.create('Donations', buildObject) + }) export default function create() { return { factory: async ({ args, neodeInstance }) => { - const defaults = { - id: uuid(), - goal: 15000, - progress: 0, - } - args = { - ...defaults, - ...args, - } - return neodeInstance.create('Donations', args) + return Factory.build('donations', args) }, } } diff --git a/backend/src/factories/emailAddresses.js b/backend/src/factories/emailAddresses.js index 41b1fe96c..8be427058 100644 --- a/backend/src/factories/emailAddresses.js +++ b/backend/src/factories/emailAddresses.js @@ -1,22 +1,24 @@ import faker from 'faker' +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' -export function defaults({ args }) { - const defaults = { - email: faker.internet.email(), - verifiedAt: new Date().toISOString(), - } - args = { - ...defaults, - ...args, - } - return args +export const defaults = { + email: faker.internet.email, + verifiedAt: () => new Date().toISOString(), } +const neode = getNeode() + +Factory.define('emailAddress') + .attr(defaults) + .after((buildObject, options) => { + return neode.create('EmailAddress', buildObject) + }) + export default function create() { return { factory: async ({ args, neodeInstance }) => { - args = defaults({ args }) - return neodeInstance.create('EmailAddress', args) + return Factory.build('emailAddress', args) }, } } diff --git a/backend/src/factories/locations.js b/backend/src/factories/locations.js index 99b666de8..29b6c3098 100644 --- a/backend/src/factories/locations.js +++ b/backend/src/factories/locations.js @@ -1,24 +1,30 @@ +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('location') + .attrs({ + name: 'Germany', + namePT: 'Alemanha', + nameDE: 'Deutschland', + nameES: 'Alemania', + nameNL: 'Duitsland', + namePL: 'Niemcy', + nameFR: 'Allemagne', + nameIT: 'Germania', + nameEN: 'Germany', + id: 'country.10743216036480410', + type: 'country', + }) + .after((buildObject, options) => { + return neode.create('Location', buildObject) + }) + export default function create() { return { factory: async ({ args, neodeInstance }) => { - const defaults = { - name: 'Germany', - namePT: 'Alemanha', - nameDE: 'Deutschland', - nameES: 'Alemania', - nameNL: 'Duitsland', - namePL: 'Niemcy', - nameFR: 'Allemagne', - nameIT: 'Germania', - nameEN: 'Germany', - id: 'country.10743216036480410', - type: 'country', - } - args = { - ...defaults, - ...args, - } - return neodeInstance.create('Location', args) + return Factory.build('location', args) }, } } diff --git a/backend/src/factories/reports.js b/backend/src/factories/reports.js index e2d5ec4dc..9e3024e35 100644 --- a/backend/src/factories/reports.js +++ b/backend/src/factories/reports.js @@ -1,7 +1,16 @@ +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('report').after((buildObject, options) => { + return neode.create('Report', buildObject) +}) + export default function create() { return { - factory: async ({ args, neodeInstance }) => { - return neodeInstance.create('Report', args) + factory: async ({ args }) => { + return Factory.build('report', args) }, } } diff --git a/backend/src/factories/socialMedia.js b/backend/src/factories/socialMedia.js index 49a237cef..164a49529 100644 --- a/backend/src/factories/socialMedia.js +++ b/backend/src/factories/socialMedia.js @@ -1,14 +1,20 @@ +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('socialMedia') + .attrs({ + url: 'https://mastodon.social/@Gargron', + }) + .after((buildObject, options) => { + return neode.create('SocialMedia', buildObject) + }) + export default function create() { return { factory: async ({ args, neodeInstance }) => { - const defaults = { - url: 'https://mastodon.social/@Gargron', - } - args = { - ...defaults, - ...args, - } - return neodeInstance.create('SocialMedia', args) + return Factory.build('socialMedia', args) }, } } diff --git a/backend/src/factories/tags.js b/backend/src/factories/tags.js index 9005d1406..cf7ca1729 100644 --- a/backend/src/factories/tags.js +++ b/backend/src/factories/tags.js @@ -1,12 +1,20 @@ +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('tag') + .attrs({ + name: '#human-connection', + }) + .after((buildObject, options) => { + return neode.create('Tag', buildObject) + }) + export default function create() { return { factory: async ({ args, neodeInstance }) => { - const defaults = { name: '#human-connection' } - args = { - ...defaults, - ...args, - } - return neodeInstance.create('Tag', args) + return Factory.build('tag', args) }, } } diff --git a/backend/src/factories/unverifiedEmailAddresses.js b/backend/src/factories/unverifiedEmailAddresses.js index 94e32af6e..2d6bd9f57 100644 --- a/backend/src/factories/unverifiedEmailAddresses.js +++ b/backend/src/factories/unverifiedEmailAddresses.js @@ -1,10 +1,19 @@ import { defaults } from './emailAddresses.js' +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('unverifiedEmailAddress') + .attr(defaults) + .after((buildObject, options) => { + return neode.create('UnverifiedEmailAddress', buildObject) + }) export default function create() { return { factory: async ({ args, neodeInstance }) => { - args = defaults({ args }) - return neodeInstance.create('UnverifiedEmailAddress', args) + return Factory.build('unverifiedEmailAddress', args) }, } } From 2a79c53765b73f9b91691eb75f55cf8c9e48306e Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 01:52:20 +0100 Subject: [PATCH 03/19] Refactor user factory --- backend/src/db/seed.js | 133 +++++++++++------- backend/src/factories/index.js | 3 +- backend/src/factories/socialMedia.js | 2 +- backend/src/factories/users.js | 80 ++++++----- backend/src/jwt/decode.spec.js | 21 +-- .../hashtags/hashtagsMiddleware.spec.js | 19 ++- .../notificationsMiddleware.spec.js | 114 +++++++++------ .../middleware/permissionsMiddleware.spec.js | 64 ++++++--- .../src/middleware/slugifyMiddleware.spec.js | 12 +- .../softDelete/softDeleteMiddleware.spec.js | 15 +- backend/src/schema/resolvers/emails.spec.js | 6 +- backend/src/schema/resolvers/follow.spec.js | 34 +++-- .../src/schema/resolvers/moderation.spec.js | 36 +++-- .../schema/resolvers/passwordReset.spec.js | 25 ++-- backend/src/schema/resolvers/posts.spec.js | 34 +++-- .../src/schema/resolvers/registration.spec.js | 23 +-- backend/src/schema/resolvers/reports.spec.js | 102 +++++++++----- backend/src/schema/resolvers/rewards.spec.js | 60 +++++--- backend/src/schema/resolvers/shout.spec.js | 34 +++-- .../src/schema/resolvers/socialMedia.spec.js | 40 +++--- .../schema/resolvers/user_management.spec.js | 34 +++-- backend/src/schema/resolvers/users.spec.js | 72 ++++++---- .../schema/resolvers/users/location.spec.js | 7 +- backend/test/features/support/steps.js | 3 +- 24 files changed, 611 insertions(+), 362 deletions(-) diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index ba7ace90b..75671d098 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -147,55 +147,90 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] louie, dagobert, ] = await Promise.all([ - factory.create('User', { - id: 'u1', - name: 'Peter Lustig', - slug: 'peter-lustig', - role: 'admin', - email: 'admin@example.org', - }), - factory.create('User', { - id: 'u2', - name: 'Bob der Baumeister', - slug: 'bob-der-baumeister', - role: 'moderator', - email: 'moderator@example.org', - }), - factory.create('User', { - id: 'u3', - name: 'Jenny Rostock', - slug: 'jenny-rostock', - role: 'user', - email: 'user@example.org', - }), - factory.create('User', { - id: 'u4', - name: 'Huey', - slug: 'huey', - role: 'user', - email: 'huey@example.org', - }), - factory.create('User', { - id: 'u5', - name: 'Dewey', - slug: 'dewey', - role: 'user', - email: 'dewey@example.org', - }), - factory.create('User', { - id: 'u6', - name: 'Louie', - slug: 'louie', - role: 'user', - email: 'louie@example.org', - }), - factory.create('User', { - id: 'u7', - name: 'Dagobert', - slug: 'dagobert', - role: 'user', - email: 'dagobert@example.org', - }), + factory.create( + 'User', + { + id: 'u1', + name: 'Peter Lustig', + slug: 'peter-lustig', + role: 'admin', + }, + { + email: 'admin@example.org', + }, + ), + factory.create( + 'User', + { + id: 'u2', + name: 'Bob der Baumeister', + slug: 'bob-der-baumeister', + role: 'moderator', + }, + { + email: 'moderator@example.org', + }, + ), + factory.create( + 'User', + { + id: 'u3', + name: 'Jenny Rostock', + slug: 'jenny-rostock', + role: 'user', + }, + { + email: 'user@example.org', + }, + ), + factory.create( + 'User', + { + id: 'u4', + name: 'Huey', + slug: 'huey', + role: 'user', + }, + { + email: 'huey@example.org', + }, + ), + factory.create( + 'User', + { + id: 'u5', + name: 'Dewey', + slug: 'dewey', + role: 'user', + }, + { + email: 'dewey@example.org', + }, + ), + factory.create( + 'User', + { + id: 'u6', + name: 'Louie', + slug: 'louie', + role: 'user', + }, + { + email: 'louie@example.org', + }, + ), + factory.create( + 'User', + { + id: 'u7', + name: 'Dagobert', + slug: 'dagobert', + role: 'user', + }, + { + email: 'dagobert@example.org', + }, + ), ]) await Promise.all([ diff --git a/backend/src/factories/index.js b/backend/src/factories/index.js index c3ab14f64..983aaae26 100644 --- a/backend/src/factories/index.js +++ b/backend/src/factories/index.js @@ -40,10 +40,11 @@ export default function Factory(options = {}) { factories, lastResponse: null, neodeInstance, - async create(node, args = {}) { + async create(node, args = {}, options = {}) { const { factory } = this.factories[node](args) this.lastResponse = await factory({ args, + options, neodeInstance, factoryInstance: this, }) diff --git a/backend/src/factories/socialMedia.js b/backend/src/factories/socialMedia.js index 164a49529..2baf058a0 100644 --- a/backend/src/factories/socialMedia.js +++ b/backend/src/factories/socialMedia.js @@ -13,7 +13,7 @@ Factory.define('socialMedia') export default function create() { return { - factory: async ({ args, neodeInstance }) => { + factory: async ({ args }) => { return Factory.build('socialMedia', args) }, } diff --git a/backend/src/factories/users.js b/backend/src/factories/users.js index 57f69b76b..9ed701441 100644 --- a/backend/src/factories/users.js +++ b/backend/src/factories/users.js @@ -1,44 +1,54 @@ import faker from 'faker' import uuid from 'uuid/v4' -import encryptPassword from '../helpers/encryptPassword' import slugify from 'slug' +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' +import { hashSync } from 'bcryptjs' + +const neode = getNeode() + +Factory.define('userWithoutEmailAddress') + .option('password', '1234') + .attrs({ + id: uuid, + name: faker.name.findName, + email: faker.internet.email, + password: '1234', + role: 'user', + avatar: faker.internet.avatar, + about: faker.lorem.paragraph, + termsAndConditionsAgreedVersion: '0.0.1', + termsAndConditionsAgreedAt: '2019-08-01T10:47:19.212Z', + allowEmbedIframes: false, + showShoutsPublicly: false, + locale: 'en', + }) + .attr('slug', ['slug', 'name'], (slug, name) => { + return slug || slugify(name, { lower: true }) + }) + .attr('encryptedPassword', ['password'], password => { + return hashSync(password, 10) + }) + .after(async (buildObject, options) => { + return neode.create('User', buildObject) + }) + +Factory.define('user') + .option('email', faker.internet.exampleEmail) + .extend('userWithoutEmailAddress') + .after(async (buildObject, options) => { + const [user, email] = await Promise.all([ + buildObject, + neode.create('EmailAddress', { email: options.email }), + ]) + await Promise.all([user.relateTo(email, 'primaryEmail'), email.relateTo(user, 'belongsTo')]) + return user + }) export default function create() { return { - factory: async ({ args, neodeInstance, factoryInstance }) => { - const defaults = { - id: uuid(), - name: faker.name.findName(), - email: faker.internet.email(), - password: '1234', - role: 'user', - avatar: faker.internet.avatar(), - about: faker.lorem.paragraph(), - termsAndConditionsAgreedVersion: '0.0.1', - termsAndConditionsAgreedAt: '2019-08-01T10:47:19.212Z', - allowEmbedIframes: false, - showShoutsPublicly: false, - locale: 'en', - } - defaults.slug = slugify(defaults.name, { lower: true }) - args = { - ...defaults, - ...args, - } - args = await encryptPassword(args) - const user = await neodeInstance.create('User', args) - - let email - if (typeof args.email === 'object') { - // probably a neode node - email = args.email - } else { - email = await factoryInstance.create('EmailAddress', { email: args.email }) - } - - await user.relateTo(email, 'primaryEmail') - await email.relateTo(user, 'belongsTo') - return user + factory: async ({ args, options }) => { + return Factory.build('user', args, options) }, } } diff --git a/backend/src/jwt/decode.spec.js b/backend/src/jwt/decode.spec.js index 71444a3e5..d0bf2005d 100644 --- a/backend/src/jwt/decode.spec.js +++ b/backend/src/jwt/decode.spec.js @@ -65,14 +65,19 @@ describe('decode', () => { describe('and corresponding user in the database', () => { let user beforeEach(async () => { - user = await factory.create('User', { - role: 'user', - name: 'Jenny Rostock', - avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/sasha_shestakov/128.jpg', - id: 'u3', - email: 'user@example.org', - slug: 'jenny-rostock', - }) + user = await factory.create( + 'User', + { + role: 'user', + name: 'Jenny Rostock', + avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/sasha_shestakov/128.jpg', + id: 'u3', + slug: 'jenny-rostock', + }, + { + email: 'user@example.org', + }, + ) }) it('returns user object except email', async () => { diff --git a/backend/src/middleware/hashtags/hashtagsMiddleware.spec.js b/backend/src/middleware/hashtags/hashtagsMiddleware.spec.js index 2247e692d..4967978a5 100644 --- a/backend/src/middleware/hashtags/hashtagsMiddleware.spec.js +++ b/backend/src/middleware/hashtags/hashtagsMiddleware.spec.js @@ -48,13 +48,18 @@ beforeAll(() => { }) beforeEach(async () => { - hashtagingUser = await neode.create('User', { - id: 'you', - name: 'Al Capone', - slug: 'al-capone', - email: 'test@example.org', - password: '1234', - }) + hashtagingUser = await neode.create( + 'User', + { + id: 'you', + name: 'Al Capone', + slug: 'al-capone', + }, + { + password: '1234', + email: 'test@example.org', + }, + ) await neode.create('Category', { id: 'cat9', name: 'Democracy & Politics', diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index 136388b88..0d0714289 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -53,13 +53,18 @@ beforeAll(async () => { }) beforeEach(async () => { - notifiedUser = await neode.create('User', { - id: 'you', - name: 'Al Capone', - slug: 'al-capone', - email: 'test@example.org', - password: '1234', - }) + notifiedUser = await neode.create( + 'User', + { + id: 'you', + name: 'Al Capone', + slug: 'al-capone', + }, + { + email: 'test@example.org', + password: '1234', + }, + ) await neode.create('Category', { id: 'cat9', name: 'Democracy & Politics', @@ -143,13 +148,18 @@ describe('notifications', () => { describe('commenter is not me', () => { beforeEach(async () => { commentContent = 'Commenters comment.' - commentAuthor = await neode.create('User', { - id: 'commentAuthor', - name: 'Mrs Comment', - slug: 'mrs-comment', - email: 'commentauthor@example.org', - password: '1234', - }) + commentAuthor = await neode.create( + 'User', + { + id: 'commentAuthor', + name: 'Mrs Comment', + slug: 'mrs-comment', + }, + { + email: 'commentauthor@example.org', + password: '1234', + }, + ) }) it('sends me a notification', async () => { @@ -224,13 +234,18 @@ describe('notifications', () => { }) beforeEach(async () => { - postAuthor = await neode.create('User', { - id: 'postAuthor', - name: 'Mrs Post', - slug: 'mrs-post', - email: 'post-author@example.org', - password: '1234', - }) + postAuthor = await neode.create( + 'User', + { + id: 'postAuthor', + name: 'Mrs Post', + slug: 'mrs-post', + }, + { + email: 'post-author@example.org', + password: '1234', + }, + ) }) describe('mentions me in a post', () => { @@ -428,23 +443,33 @@ describe('notifications', () => { beforeEach(async () => { commentContent = 'One mention about me with @al-capone.' - commentAuthor = await neode.create('User', { - id: 'commentAuthor', - name: 'Mrs Comment', - slug: 'mrs-comment', - email: 'comment-author@example.org', - password: '1234', - }) + commentAuthor = await neode.create( + 'User', + { + id: 'commentAuthor', + name: 'Mrs Comment', + slug: 'mrs-comment', + }, + { + email: 'comment-author@example.org', + password: '1234', + }, + ) }) it('sends only one notification with reason mentioned_in_comment', async () => { - postAuthor = await neode.create('User', { - id: 'MrPostAuthor', - name: 'Mr Author', - slug: 'mr-author', - email: 'post-author@example.org', - password: '1234', - }) + postAuthor = await neode.create( + 'User', + { + id: 'MrPostAuthor', + name: 'Mr Author', + slug: 'mr-author', + }, + { + email: 'post-author@example.org', + password: '1234', + }, + ) await createCommentOnPostAction() const expected = expect.objectContaining({ @@ -514,13 +539,18 @@ describe('notifications', () => { await postAuthor.relateTo(notifiedUser, 'blocked') commentContent = 'One mention about me with @al-capone.' - commentAuthor = await neode.create('User', { - id: 'commentAuthor', - name: 'Mrs Comment', - slug: 'mrs-comment', - email: 'comment-author@example.org', - password: '1234', - }) + commentAuthor = await neode.create( + 'User', + { + id: 'commentAuthor', + name: 'Mrs Comment', + slug: 'mrs-comment', + }, + { + email: 'comment-author@example.org', + password: '1234', + }, + ) }) it('sends no notification', async () => { diff --git a/backend/src/middleware/permissionsMiddleware.spec.js b/backend/src/middleware/permissionsMiddleware.spec.js index a4f13ea0c..7c62cfcc0 100644 --- a/backend/src/middleware/permissionsMiddleware.spec.js +++ b/backend/src/middleware/permissionsMiddleware.spec.js @@ -34,28 +34,48 @@ describe('authorization', () => { describe('given two existing users', () => { beforeEach(async () => { ;[owner, anotherRegularUser, administrator, moderator] = await Promise.all([ - factory.create('User', { - email: 'owner@example.org', - name: 'Owner', - password: 'iamtheowner', - }), - factory.create('User', { - email: 'another.regular.user@example.org', - name: 'Another Regular User', - password: 'else', - }), - factory.create('User', { - email: 'admin@example.org', - name: 'Admin', - password: 'admin', - role: 'admin', - }), - factory.create('User', { - email: 'moderator@example.org', - name: 'Moderator', - password: 'moderator', - role: 'moderator', - }), + factory.create( + 'User', + { + name: 'Owner', + }, + { + email: 'owner@example.org', + password: 'iamtheowner', + }, + ), + factory.create( + 'User', + { + name: 'Another Regular User', + }, + { + email: 'another.regular.user@example.org', + password: 'else', + }, + ), + factory.create( + 'User', + { + name: 'Admin', + role: 'admin', + }, + { + email: 'admin@example.org', + password: 'admin', + }, + ), + factory.create( + 'User', + { + name: 'Moderator', + role: 'moderator', + }, + { + email: 'moderator@example.org', + password: 'moderator', + }, + ), ]) variables = {} }) diff --git a/backend/src/middleware/slugifyMiddleware.spec.js b/backend/src/middleware/slugifyMiddleware.spec.js index cf9f0941c..5400d2c92 100644 --- a/backend/src/middleware/slugifyMiddleware.spec.js +++ b/backend/src/middleware/slugifyMiddleware.spec.js @@ -31,10 +31,14 @@ beforeEach(async () => { const admin = await factory.create('User', { role: 'admin', }) - await factory.create('User', { - email: 'someone@example.org', - password: '1234', - }) + await factory.create( + 'User', + {}, + { + email: 'someone@example.org', + password: '1234', + }, + ) await factory.create('Category', { id: 'cat9', name: 'Democracy & Politics', diff --git a/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js b/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js index 6e1735af2..334564582 100644 --- a/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js +++ b/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js @@ -19,11 +19,16 @@ beforeAll(async () => { // For performance reasons we do this only once const users = await Promise.all([ factory.create('User', { id: 'u1', role: 'user' }), - factory.create('User', { - id: 'm1', - role: 'moderator', - password: '1234', - }), + factory.create( + 'User', + { + id: 'm1', + role: 'moderator', + }, + { + password: '1234', + }, + ), factory.create('User', { id: 'u2', role: 'user', diff --git a/backend/src/schema/resolvers/emails.spec.js b/backend/src/schema/resolvers/emails.spec.js index 97a1f0c29..470eb7635 100644 --- a/backend/src/schema/resolvers/emails.spec.js +++ b/backend/src/schema/resolvers/emails.spec.js @@ -63,7 +63,7 @@ describe('AddEmailAddress', () => { describe('authenticated', () => { beforeEach(async () => { - user = await factory.create('User', { id: '567', email: 'user@example.org' }) + user = await factory.create('User', { id: '567' }, { email: 'user@example.org' }) authenticatedUser = await user.toJson() }) @@ -128,7 +128,7 @@ describe('AddEmailAddress', () => { describe('but if another user owns an `EmailAddress` already with that email', () => { it('throws UserInputError because of unique constraints', async () => { - await factory.create('User', { email: 'new-email@example.org' }) + await factory.create('User', {}, { email: 'new-email@example.org' }) await expect(mutate({ mutation, variables })).resolves.toMatchObject({ data: { AddEmailAddress: null }, errors: [{ message: 'A user account with this email already exists.' }], @@ -169,7 +169,7 @@ describe('VerifyEmailAddress', () => { describe('authenticated', () => { beforeEach(async () => { - user = await factory.create('User', { id: '567', email: 'user@example.org' }) + user = await factory.create('User', { id: '567' }, { email: 'user@example.org' }) authenticatedUser = await user.toJson() }) diff --git a/backend/src/schema/resolvers/follow.spec.js b/backend/src/schema/resolvers/follow.spec.js index ad836a461..d7f5e3113 100644 --- a/backend/src/schema/resolvers/follow.spec.js +++ b/backend/src/schema/resolvers/follow.spec.js @@ -73,20 +73,30 @@ beforeAll(async () => { beforeEach(async () => { user1 = await factory - .create('User', { - id: 'u1', - name: 'user1', - email: 'test@example.org', - password: '1234', - }) + .create( + 'User', + { + id: 'u1', + name: 'user1', + }, + { + email: 'test@example.org', + password: '1234', + }, + ) .then(user => user.toJson()) user2 = await factory - .create('User', { - id: 'u2', - name: 'user2', - email: 'test2@example.org', - password: '1234', - }) + .create( + 'User', + { + id: 'u2', + name: 'user2', + }, + { + email: 'test2@example.org', + password: '1234', + }, + ) .then(user => user.toJson()) authenticatedUser = user1 diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index cd502be75..856f094d1 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -80,19 +80,29 @@ describe('moderate resources', () => { closed: false, } authenticatedUser = null - moderator = await factory.create('User', { - id: 'moderator-id', - name: 'Moderator', - email: 'moderator@example.org', - password: '1234', - role: 'moderator', - }) - nonModerator = await factory.create('User', { - id: 'non-moderator', - name: 'Non Moderator', - email: 'non.moderator@example.org', - password: '1234', - }) + moderator = await factory.create( + 'User', + { + id: 'moderator-id', + name: 'Moderator', + role: 'moderator', + }, + { + email: 'moderator@example.org', + password: '1234', + }, + ) + nonModerator = await factory.create( + 'User', + { + id: 'non-moderator', + name: 'Non Moderator', + }, + { + email: 'non.moderator@example.org', + password: '1234', + }, + ) }) afterEach(async () => { diff --git a/backend/src/schema/resolvers/passwordReset.spec.js b/backend/src/schema/resolvers/passwordReset.spec.js index d7b3a0157..97fba47f4 100644 --- a/backend/src/schema/resolvers/passwordReset.spec.js +++ b/backend/src/schema/resolvers/passwordReset.spec.js @@ -45,9 +45,13 @@ afterEach(async () => { describe('passwordReset', () => { describe('given a user', () => { beforeEach(async () => { - await factory.create('User', { - email: 'user@example.org', - }) + await factory.create( + 'User', + {}, + { + email: 'user@example.org', + }, + ) }) describe('requestPasswordReset', () => { @@ -123,11 +127,16 @@ describe('resetPassword', () => { describe('given a user', () => { beforeEach(async () => { - await factory.create('User', { - email: 'user@example.org', - role: 'user', - password: '1234', - }) + await factory.create( + 'User', + { + role: 'user', + }, + { + email: 'user@example.org', + password: '1234', + }, + ) }) describe('invalid email', () => { diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 56a47afa7..d7195be97 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -56,12 +56,17 @@ beforeAll(async () => { beforeEach(async () => { variables = {} - user = await factory.create('User', { - id: 'current-user', - name: 'TestUser', - email: 'test@example.org', - password: '1234', - }) + user = await factory.create( + 'User', + { + id: 'current-user', + name: 'TestUser', + }, + { + email: 'test@example.org', + password: '1234', + }, + ) await Promise.all([ neode.create('Category', { id: 'cat9', @@ -96,12 +101,17 @@ describe('Post', () => { let followedUser, happyPost, cryPost beforeEach(async () => { ;[followedUser] = await Promise.all([ - factory.create('User', { - id: 'followed-by-me', - email: 'followed@example.org', - name: 'Followed User', - password: '1234', - }), + factory.create( + 'User', + { + id: 'followed-by-me', + name: 'Followed User', + }, + { + email: 'followed@example.org', + password: '1234', + }, + ), ]) ;[happyPost, cryPost] = await Promise.all([ factory.create('Post', { id: 'happy-post', categoryIds: ['cat4'] }), diff --git a/backend/src/schema/resolvers/registration.spec.js b/backend/src/schema/resolvers/registration.spec.js index 23b1f9d2a..36514bf4d 100644 --- a/backend/src/schema/resolvers/registration.spec.js +++ b/backend/src/schema/resolvers/registration.spec.js @@ -1,4 +1,5 @@ import Factory from '../../factories' +import { Factory as RosieFactory } from 'rosie' import { gql } from '../../helpers/jest' import { getDriver, getNeode } from '../../db/neo4j' import createServer from '../../server' @@ -58,11 +59,16 @@ describe('Signup', () => { describe('as admin', () => { beforeEach(async () => { - const admin = await factory.create('User', { - role: 'admin', - email: 'admin@example.org', - password: '1234', - }) + const admin = await factory.create( + 'User', + { + role: 'admin', + }, + { + email: 'admin@example.org', + password: '1234', + }, + ) authenticatedUser = await admin.toJson() }) @@ -90,9 +96,9 @@ describe('Signup', () => { }) describe('if the email already exists', () => { - let email + let emailAddress beforeEach(async () => { - email = await factory.create('EmailAddress', { + emailAddress = await factory.create('EmailAddress', { email: 'someuser@example.org', verifiedAt: null, }) @@ -100,7 +106,8 @@ describe('Signup', () => { describe('and the user has registered already', () => { beforeEach(async () => { - await factory.create('User', { email }) + const user = await RosieFactory.build('userWithoutEmailAddress') + await emailAddress.relateTo(user, 'belongsTo') }) it('throws UserInputError error because of unique constraint violation', async () => { diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 7f827b111..9992da200 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -84,24 +84,39 @@ describe('file a report on a resource', () => { describe('authenticated', () => { beforeEach(async () => { - currentUser = await factory.create('User', { - id: 'current-user-id', - role: 'user', - email: 'test@example.org', - password: '1234', - }) - otherReportingUser = await factory.create('User', { - id: 'other-reporting-user-id', - role: 'user', - email: 'reporting@example.org', - password: '1234', - }) - await factory.create('User', { - id: 'abusive-user-id', - role: 'user', - name: 'abusive-user', - email: 'abusive-user@example.org', - }) + currentUser = await factory.create( + 'User', + { + id: 'current-user-id', + role: 'user', + }, + { + email: 'test@example.org', + password: '1234', + }, + ) + otherReportingUser = await factory.create( + 'User', + { + id: 'other-reporting-user-id', + role: 'user', + }, + { + email: 'reporting@example.org', + password: '1234', + }, + ) + await factory.create( + 'User', + { + id: 'abusive-user-id', + role: 'user', + name: 'abusive-user', + }, + { + email: 'abusive-user@example.org', + }, + ) await instance.create('Category', { id: 'cat9', name: 'Democracy & Politics', @@ -515,24 +530,39 @@ describe('file a report on a resource', () => { beforeEach(async () => { authenticatedUser = null - moderator = await factory.create('User', { - id: 'moderator-1', - role: 'moderator', - email: 'moderator@example.org', - password: '1234', - }) - currentUser = await factory.create('User', { - id: 'current-user-id', - role: 'user', - email: 'current.user@example.org', - password: '1234', - }) - abusiveUser = await factory.create('User', { - id: 'abusive-user-1', - role: 'user', - name: 'abusive-user', - email: 'abusive-user@example.org', - }) + moderator = await factory.create( + 'User', + { + id: 'moderator-1', + role: 'moderator', + }, + { + email: 'moderator@example.org', + password: '1234', + }, + ) + currentUser = await factory.create( + 'User', + { + id: 'current-user-id', + role: 'user', + }, + { + email: 'current.user@example.org', + password: '1234', + }, + ) + abusiveUser = await factory.create( + 'User', + { + id: 'abusive-user-1', + role: 'user', + name: 'abusive-user', + }, + { + email: 'abusive-user@example.org', + }, + ) await instance.create('Category', { id: 'cat9', name: 'Democracy & Politics', diff --git a/backend/src/schema/resolvers/rewards.spec.js b/backend/src/schema/resolvers/rewards.spec.js index fe2807f25..ba5c2dc42 100644 --- a/backend/src/schema/resolvers/rewards.spec.js +++ b/backend/src/schema/resolvers/rewards.spec.js @@ -31,22 +31,37 @@ describe('rewards', () => { }) beforeEach(async () => { - regularUser = await factory.create('User', { - id: 'regular-user-id', - role: 'user', - email: 'user@example.org', - password: '1234', - }) - moderator = await factory.create('User', { - id: 'moderator-id', - role: 'moderator', - email: 'moderator@example.org', - }) - administrator = await factory.create('User', { - id: 'admin-id', - role: 'admin', - email: 'admin@example.org', - }) + regularUser = await factory.create( + 'User', + { + id: 'regular-user-id', + role: 'user', + }, + { + email: 'user@example.org', + password: '1234', + }, + ) + moderator = await factory.create( + 'User', + { + id: 'moderator-id', + role: 'moderator', + }, + { + email: 'moderator@example.org', + }, + ) + administrator = await factory.create( + 'User', + { + id: 'admin-id', + role: 'admin', + }, + { + email: 'admin@example.org', + }, + ) badge = await factory.create('Badge', { id: 'indiegogo_en_rhino', type: 'crowdfunding', @@ -172,10 +187,15 @@ describe('rewards', () => { }, errors: undefined, } - await factory.create('User', { - id: 'regular-user-2-id', - email: 'regular2@email.com', - }) + await factory.create( + 'User', + { + id: 'regular-user-2-id', + }, + { + email: 'regular2@email.com', + }, + ) await mutate({ mutation: rewardMutation, variables, diff --git a/backend/src/schema/resolvers/shout.spec.js b/backend/src/schema/resolvers/shout.spec.js index 104a28399..50ff23687 100644 --- a/backend/src/schema/resolvers/shout.spec.js +++ b/backend/src/schema/resolvers/shout.spec.js @@ -47,19 +47,29 @@ describe('shout and unshout posts', () => { query = createTestClient(server).query }) beforeEach(async () => { - currentUser = await factory.create('User', { - id: 'current-user-id', - name: 'Current User', - email: 'current.user@example.org', - password: '1234', - }) + currentUser = await factory.create( + 'User', + { + id: 'current-user-id', + name: 'Current User', + }, + { + email: 'current.user@example.org', + password: '1234', + }, + ) - postAuthor = await factory.create('User', { - id: 'id-of-another-user', - name: 'Another User', - email: 'another.user@example.org', - password: '1234', - }) + postAuthor = await factory.create( + 'User', + { + id: 'id-of-another-user', + name: 'Another User', + }, + { + email: 'another.user@example.org', + password: '1234', + }, + ) }) afterEach(async () => { await factory.cleanDatabase() diff --git a/backend/src/schema/resolvers/socialMedia.spec.js b/backend/src/schema/resolvers/socialMedia.spec.js index f292b58a0..05bb13eb4 100644 --- a/backend/src/schema/resolvers/socialMedia.spec.js +++ b/backend/src/schema/resolvers/socialMedia.spec.js @@ -2,40 +2,46 @@ import { createTestClient } from 'apollo-server-testing' import createServer from '../../server' import Factory from '../../factories' import { gql } from '../../helpers/jest' -import { getNeode, getDriver } from '../../db/neo4j' +import { getDriver } from '../../db/neo4j' const driver = getDriver() const factory = Factory() -const neode = getNeode() describe('SocialMedia', () => { let socialMediaAction, someUser, ownerNode, owner - const ownerParams = { - email: 'pippi@example.com', - password: '1234', - name: 'Pippi Langstrumpf', - } - - const userParams = { - email: 'kalle@example.com', - password: 'abcd', - name: 'Kalle Blomqvist', - } - const url = 'https://twitter.com/pippi-langstrumpf' const newUrl = 'https://twitter.com/bullerby' const setUpSocialMedia = async () => { - const socialMediaNode = await neode.create('SocialMedia', { url }) + const socialMediaNode = await factory.create('SocialMedia', { url }) await socialMediaNode.relateTo(ownerNode, 'ownedBy') return socialMediaNode.toJson() } beforeEach(async () => { - const someUserNode = await neode.create('User', userParams) + const someUserNode = await factory.create( + 'User', + { + name: 'Kalle Blomqvist', + }, + { + email: 'kalle@example.com', + password: 'abcd', + }, + ) + someUser = await someUserNode.toJson() - ownerNode = await neode.create('User', ownerParams) + ownerNode = await factory.create( + 'User', + { + name: 'Pippi Langstrumpf', + }, + { + email: 'pippi@example.com', + password: '1234', + }, + ) owner = await ownerNode.toJson() socialMediaAction = async (user, mutation, variables) => { diff --git a/backend/src/schema/resolvers/user_management.spec.js b/backend/src/schema/resolvers/user_management.spec.js index 5e7043e74..27bc84b10 100644 --- a/backend/src/schema/resolvers/user_management.spec.js +++ b/backend/src/schema/resolvers/user_management.spec.js @@ -127,15 +127,20 @@ describe('currentUser', () => { describe('authenticated', () => { describe('and corresponding user in the database', () => { beforeEach(async () => { - await factory.create('User', { - id: 'u3', - // the `id` is the only thing that has to match the decoded JWT bearer token - avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jimmuirhead/128.jpg', - email: 'test@example.org', - name: 'Matilde Hermiston', - slug: 'matilde-hermiston', - role: 'user', - }) + await factory.create( + 'User', + { + id: 'u3', + // the `id` is the only thing that has to match the decoded JWT bearer token + avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jimmuirhead/128.jpg', + name: 'Matilde Hermiston', + slug: 'matilde-hermiston', + role: 'user', + }, + { + email: 'test@example.org', + }, + ) const userBearerToken = encode({ id: 'u3' }) req = { headers: { authorization: `Bearer ${userBearerToken}` } } }) @@ -172,10 +177,13 @@ describe('login', () => { beforeEach(async () => { variables = { email: 'test@example.org', password: '1234' } - user = await factory.create('User', { - ...variables, - id: 'acb2d923-f3af-479e-9f00-61b12e864666', - }) + user = await factory.create( + 'User', + { + id: 'acb2d923-f3af-479e-9f00-61b12e864666', + }, + variables, + ) }) describe('ask for a `token`', () => { diff --git a/backend/src/schema/resolvers/users.spec.js b/backend/src/schema/resolvers/users.spec.js index cfd84fcf7..9ead48a2b 100644 --- a/backend/src/schema/resolvers/users.spec.js +++ b/backend/src/schema/resolvers/users.spec.js @@ -37,7 +37,7 @@ afterEach(async () => { describe('User', () => { describe('query by email address', () => { beforeEach(async () => { - await factory.create('User', { name: 'Johnny', email: 'any-email-address@example.org' }) + await factory.create('User', { name: 'Johnny' }, { email: 'any-email-address@example.org' }) }) const userQuery = gql` @@ -57,11 +57,16 @@ describe('User', () => { describe('as admin', () => { beforeEach(async () => { - const admin = await factory.create('User', { - role: 'admin', - email: 'admin@example.org', - password: '1234', - }) + const admin = await factory.create( + 'User', + { + role: 'admin', + }, + { + email: 'admin@example.org', + password: '1234', + }, + ) authenticatedUser = await admin.toJson() }) @@ -91,19 +96,9 @@ describe('User', () => { }) describe('UpdateUser', () => { - let userParams, variables + let variables beforeEach(async () => { - userParams = { - email: 'user@example.org', - password: '1234', - id: 'u47', - name: 'John Doe', - termsAndConditionsAgreedVersion: null, - termsAndConditionsAgreedAt: null, - allowEmbedIframes: false, - } - variables = { id: 'u47', name: 'John Doughnut', @@ -133,18 +128,33 @@ describe('UpdateUser', () => { ` beforeEach(async () => { - user = await factory.create('User', userParams) + user = await factory.create( + 'User', + { + id: 'u47', + name: 'John Doe', + termsAndConditionsAgreedVersion: null, + termsAndConditionsAgreedAt: null, + allowEmbedIframes: false, + }, + { + email: 'user@example.org', + }, + ) }) describe('as another user', () => { beforeEach(async () => { - const someoneElseParams = { - email: 'someone-else@example.org', - password: '1234', - name: 'James Doe', - } + const someoneElse = await factory.create( + 'User', + { + name: 'James Doe', + }, + { + email: 'someone-else@example.org', + }, + ) - const someoneElse = await factory.create('User', someoneElseParams) authenticatedUser = await someoneElse.toJson() }) @@ -272,11 +282,15 @@ describe('DeleteUser', () => { about: 'along with my about', id: 'u343', }) - await factory.create('User', { - email: 'friends-account@example.org', - password: '1234', - id: 'not-my-account', - }) + await factory.create( + 'User', + { + id: 'not-my-account', + }, + { + email: 'friends-account@example.org', + }, + ) }) describe('unauthenticated', () => { diff --git a/backend/src/schema/resolvers/users/location.spec.js b/backend/src/schema/resolvers/users/location.spec.js index f7315174c..85c0f091f 100644 --- a/backend/src/schema/resolvers/users/location.spec.js +++ b/backend/src/schema/resolvers/users/location.spec.js @@ -146,7 +146,7 @@ describe('userMiddleware', () => { }) describe('UpdateUser', () => { - let user, userParams + let user beforeEach(async () => { newlyCreatedNodesWithLocales = [ { @@ -182,10 +182,9 @@ describe('userMiddleware', () => { }, }, ] - userParams = { + user = await factory.create('User', { id: 'updating-user', - } - user = await factory.create('User', userParams) + }) authenticatedUser = await user.toJson() }) diff --git a/backend/test/features/support/steps.js b/backend/test/features/support/steps.js index 70802f4e2..ff9d45b27 100644 --- a/backend/test/features/support/steps.js +++ b/backend/test/features/support/steps.js @@ -13,8 +13,9 @@ function createUser (slug) { debug(`creating user ${slug}`) return factory.create('User', { name: slug, - email: 'example@test.org', + }, { password: '1234' + email: 'example@test.org', }) // await login({ email: 'example@test.org', password: '1234' }) } From 094fa196e6605ebce26f828554f88ac535a029cd Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 02:23:00 +0100 Subject: [PATCH 04/19] Fix factories for cypress tests --- backend/src/config/index.js | 5 ++++- backend/src/factories/users.js | 4 ++-- cypress/integration/common/report.js | 7 ++++--- cypress/integration/common/steps.js | 5 ----- cypress/support/factories.js | 12 ++---------- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/backend/src/config/index.js b/backend/src/config/index.js index 2f8d0ed22..058dc00ab 100644 --- a/backend/src/config/index.js +++ b/backend/src/config/index.js @@ -4,6 +4,9 @@ if (require.resolve) { dotenv.config({ path: require.resolve('../../.env') }) } +// eslint-disable-next-line no-undef +const env = typeof Cypress !== 'undefined' ? Cypress.env() : process.env + const { MAPBOX_TOKEN, JWT_SECRET, @@ -20,7 +23,7 @@ const { NEO4J_PASSWORD = 'neo4j', CLIENT_URI = 'http://localhost:3000', GRAPHQL_URI = 'http://localhost:4000', -} = process.env +} = env export const requiredConfigs = { MAPBOX_TOKEN, diff --git a/backend/src/factories/users.js b/backend/src/factories/users.js index 9ed701441..91b9e9a8b 100644 --- a/backend/src/factories/users.js +++ b/backend/src/factories/users.js @@ -34,8 +34,8 @@ Factory.define('userWithoutEmailAddress') }) Factory.define('user') - .option('email', faker.internet.exampleEmail) .extend('userWithoutEmailAddress') + .option('email', faker.internet.exampleEmail) .after(async (buildObject, options) => { const [user, email] = await Promise.all([ buildObject, @@ -47,7 +47,7 @@ Factory.define('user') export default function create() { return { - factory: async ({ args, options }) => { + factory: ({ args, options }) => { return Factory.build('user', args, options) }, } diff --git a/cypress/integration/common/report.js b/cypress/integration/common/report.js index 710928ff2..6b808c75f 100644 --- a/cypress/integration/common/report.js +++ b/cypress/integration/common/report.js @@ -31,10 +31,11 @@ Given("I see David Irving's post on the post page", page => { Given('I am logged in with a {string} role', role => { cy.factory().create('User', { - email: `${role}@example.org`, - password: '1234', termsAndConditionsAgreedVersion: VERSION, role + }, { + email: `${role}@example.org`, + password: '1234', }) cy.login({ email: `${role}@example.org`, @@ -127,7 +128,7 @@ Given('somebody reported the following posts:', table => { password: '1234' } cy.factory() - .create('User', submitter) + .create('User', {}, submitter) .authenticateAs(submitter) .mutate(gql`mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { fileReport(resourceId: $resourceId, reasonCategory: $reasonCategory, reasonDescription: $reasonDescription) { diff --git a/cypress/integration/common/steps.js b/cypress/integration/common/steps.js index 9a5c02d08..3867b3513 100644 --- a/cypress/integration/common/steps.js +++ b/cypress/integration/common/steps.js @@ -192,11 +192,6 @@ When("I press {string}", label => { cy.contains(label).click(); }); -Given("we have this user in our database:", table => { - const [firstRow] = table.hashes() - cy.factory().create('User', firstRow) -}) - Given("we have the following posts in our database:", table => { cy.factory().create('Category', { id: `cat-456`, diff --git a/cypress/support/factories.js b/cypress/support/factories.js index 1b76a1a01..d5d4ee768 100644 --- a/cypress/support/factories.js +++ b/cypress/support/factories.js @@ -40,17 +40,9 @@ Cypress.Commands.add('factory', () => { Cypress.Commands.add( 'create', { prevSubject: true }, - async (factory, node, properties) => { - await factory.create(node, properties) + async (factory, node, properties, options) => { + await factory.create(node, properties, options) return factory } ) -Cypress.Commands.add( - 'relate', - { prevSubject: true }, - async (factory, node, relationship, properties) => { - await factory.relate(node, relationship, properties) - return factory - } -) From 3d1a964120b922bcd29e42dd27db64a24f6bc9b6 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 10:03:13 +0100 Subject: [PATCH 05/19] Add missing dependency rosie for cypress tests --- package.json | 1 + yarn.lock | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index bd8f9312f..3e868b075 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "neo4j-driver": "^4.0.1", "neode": "^0.3.7", "npm-run-all": "^4.1.5", + "rosie": "^2.0.1", "slug": "^2.1.1", "standard-version": "^7.1.0" }, diff --git a/yarn.lock b/yarn.lock index e4aefda5b..95fcfd483 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3219,7 +3219,6 @@ extsprintf@^1.2.0: faker@Marak/faker.js#master: version "4.1.0" - uid "3b2fa4aebccee52ae1bafc15d575061fb30c3cf1" resolved "https://codeload.github.com/Marak/faker.js/tar.gz/3b2fa4aebccee52ae1bafc15d575061fb30c3cf1" fast-deep-equal@^2.0.1: @@ -5885,6 +5884,11 @@ ripemd160@^2.0.0, ripemd160@^2.0.1: hash-base "^3.0.0" inherits "^2.0.1" +rosie@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/rosie/-/rosie-2.0.1.tgz#c250c4787ce450b72aa9eff26509f68589814fa2" + integrity sha1-wlDEeHzkULcqqe/yZQn2hYmBT6I= + rsvp@^4.8.4: version "4.8.5" resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734" From 95bfb090fde6280ef26170de52a9e66ceba83ab9 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 13:42:29 +0100 Subject: [PATCH 06/19] Refactor factory for posts --- backend/src/db/seed.js | 69 ++++++---- backend/src/factories/posts.js | 130 +++++++++--------- .../src/middleware/slugifyMiddleware.spec.js | 1 + .../softDelete/softDeleteMiddleware.spec.js | 9 +- .../validation/validationMiddleware.spec.js | 2 + backend/src/models/Post.js | 1 - backend/src/schema/resolvers/comments.spec.js | 3 +- .../schema/resolvers/notifications.spec.js | 12 +- backend/src/schema/resolvers/posts.spec.js | 17 ++- backend/src/schema/resolvers/reports.spec.js | 26 ++-- backend/src/schema/resolvers/shout.spec.js | 3 + backend/src/schema/resolvers/users.spec.js | 3 +- 12 files changed, 156 insertions(+), 120 deletions(-) diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index 75671d098..82d6239c2 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -386,86 +386,98 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] const [p0, p1, p3, p4, p5, p6, p9, p10, p11, p13, p14, p15] = await Promise.all([ factory.create('Post', { - author: peterLustig, id: 'p0', language: sample(languages), image: faker.image.unsplash.food(300, 169), - categoryIds: ['cat16'], imageBlurred: true, imageAspectRatio: 300 / 169, + }, { + categoryIds: ['cat16'], + author: peterLustig, }), factory.create('Post', { - author: bobDerBaumeister, id: 'p1', language: sample(languages), image: faker.image.unsplash.technology(300, 1500), - categoryIds: ['cat1'], imageAspectRatio: 300 / 1500, + }, { + categoryIds: ['cat1'], + author: bobDerBaumeister, }), factory.create('Post', { - author: huey, id: 'p3', language: sample(languages), + }, { categoryIds: ['cat3'], + author: huey, }), factory.create('Post', { - author: dewey, id: 'p4', language: sample(languages), + }, { categoryIds: ['cat4'], + author: dewey, }), factory.create('Post', { - author: louie, id: 'p5', language: sample(languages), + }, { categoryIds: ['cat5'], + author: louie, }), factory.create('Post', { - authorId: 'u1', id: 'p6', language: sample(languages), image: faker.image.unsplash.buildings(300, 857), - categoryIds: ['cat6'], imageAspectRatio: 300 / 857, + }, { + categoryIds: ['cat6'], + author: peterLustig, }), factory.create('Post', { - author: huey, id: 'p9', language: sample(languages), + }, { categoryIds: ['cat9'], + author: huey, }), factory.create('Post', { - author: dewey, id: 'p10', - categoryIds: ['cat10'], imageBlurred: true, + }, { + author: dewey, + categoryIds: ['cat10'], }), factory.create('Post', { - author: louie, id: 'p11', language: sample(languages), image: faker.image.unsplash.people(300, 901), - categoryIds: ['cat11'], imageAspectRatio: 300 / 901, + }, { + categoryIds: ['cat11'], + author: louie, }), factory.create('Post', { - author: bobDerBaumeister, id: 'p13', language: sample(languages), + }, { + author: bobDerBaumeister, categoryIds: ['cat13'], }), factory.create('Post', { - author: jennyRostock, id: 'p14', language: sample(languages), image: faker.image.unsplash.objects(300, 200), - categoryIds: ['cat14'], imageAspectRatio: 300 / 450, + }, { + author: jennyRostock, + categoryIds: ['cat14'], }), factory.create('Post', { - author: huey, id: 'p15', language: sample(languages), + }, { + author: huey, categoryIds: ['cat15'], }), ]) @@ -828,8 +840,10 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(30).keys()].map(() => { return factory.create('Post', { - author: jennyRostock, image: faker.image.unsplash.objects(), + }, { + categoryIds: ['cat1'], + author: jennyRostock, }) }), ) @@ -864,8 +878,9 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(21).keys()].map(() => { return factory.create('Post', { - author: peterLustig, image: faker.image.unsplash.buildings(), + }, { + author: peterLustig, }) }), ) @@ -900,8 +915,9 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(11).keys()].map(() => { return factory.create('Post', { - author: dewey, image: faker.image.unsplash.food(), + }, { + author: dewey, }) }), ) @@ -936,8 +952,9 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(16).keys()].map(() => { return factory.create('Post', { - author: louie, image: faker.image.unsplash.technology(), + }, { + author: louie, }) }), ) @@ -945,8 +962,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(4).keys()].map(() => { return factory.create('Comment', { - author: louie, postId: 'p1', + author: louie, }) }), ) @@ -972,8 +989,9 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(45).keys()].map(() => { return factory.create('Post', { - author: bobDerBaumeister, image: faker.image.unsplash.people(), + }, { + author: bobDerBaumeister, }) }), ) @@ -1008,8 +1026,9 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(8).keys()].map(() => { return factory.create('Post', { - author: huey, image: faker.image.unsplash.nature(), + }, { + author: huey, }) }), ) diff --git a/backend/src/factories/posts.js b/backend/src/factories/posts.js index 3295665b7..2731cb60d 100644 --- a/backend/src/factories/posts.js +++ b/backend/src/factories/posts.js @@ -1,77 +1,71 @@ +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' import faker from 'faker' import slugify from 'slug' import uuid from 'uuid/v4' +const neode = getNeode() + +Factory.define('post') + .option('categoryIds', []) + .option('categories', ['categoryIds'], (categoryIds) => { + if (categoryIds.length) return Promise.all(categoryIds.map(id => neode.find('Category', id))) + // there must be at least one category + return Promise.all([Factory.build('category')]) + }) + .option('tagIds', []) + .option('tags', ['tagIds'], (tagIds) => { + return Promise.all(tagIds.map(id => neode.find('Tag', id))) + }) + .option('authorId', null) + .option('author', ['authorId'], (authorId) => { + if (authorId) return neode.find('User', authorId) + return Factory.build('user') + }) + .option('pinnedBy', null) + .attrs({ + id: uuid, + title: faker.lorem.sentence, + content: faker.lorem.paragraphs, + image: faker.image.unsplash.imageUrl, + visibility: 'public', + deleted: false, + imageBlurred: false, + imageAspectRatio: 1.333, + }) + .attr('pinned', ['pinned'], (pinned) => { + // Convert false to null + return pinned || null + }) + .attr('contentExcerpt', ['contentExcerpt', 'content'], (contentExcerpt, content) => { + return contentExcerpt || content + }) + .attr('slug', ['slug', 'title'], (slug, title) => { + return slug || slugify(title, { lower: true }) + }) + .after(async (buildObject, options) => { + const [post, author, categories, tags] = await Promise.all([ + neode.create('Post', buildObject), + options.author, + options.categories, + options.tags, + ]) + await Promise.all([ + post.relateTo(author, 'author'), + Promise.all(categories.map(c => c.relateTo(post, 'post'))), + Promise.all(tags.map(t => t.relateTo(post, 'post'))), + ]) + if (buildObject.pinned) { + const pinnedBy = await (options.pinnedBy || Factory.build('user', { role: 'admin' })) + await pinnedBy.relateTo(post, 'pinned') + } + return post + }) + export default function create() { return { - factory: async ({ args, neodeInstance, factoryInstance }) => { - const defaults = { - id: uuid(), - title: faker.lorem.sentence(), - content: [ - faker.lorem.sentence(), - faker.lorem.sentence(), - faker.lorem.sentence(), - faker.lorem.sentence(), - faker.lorem.sentence(), - ].join('. '), - image: faker.image.unsplash.imageUrl(), - visibility: 'public', - deleted: false, - categoryIds: [], - imageBlurred: false, - imageAspectRatio: 1.333, - pinned: null, - } - args = { - ...defaults, - ...args, - } - // Convert false to null - args.pinned = args.pinned || null - - args.slug = args.slug || slugify(args.title, { lower: true }) - args.contentExcerpt = args.contentExcerpt || args.content - - let { categories, categoryIds } = args - delete args.categories - delete args.categoryIds - if (categories && categoryIds) throw new Error('You provided both categories and categoryIds') - if (categoryIds) - categories = await Promise.all(categoryIds.map(id => neodeInstance.find('Category', id))) - categories = categories || (await Promise.all([factoryInstance.create('Category')])) - const { tagIds = [] } = args - delete args.tags - const tags = await Promise.all( - tagIds.map(t => { - return neodeInstance.find('Tag', t) - }), - ) - - let { author, authorId } = args - delete args.author - delete args.authorId - if (author && authorId) throw new Error('You provided both author and authorId') - if (authorId) author = await neodeInstance.find('User', authorId) - author = author || (await factoryInstance.create('User')) - const post = await neodeInstance.create('Post', args) - await post.relateTo(author, 'author') - - if (args.pinned) { - args.pinnedAt = args.pinnedAt || new Date().toISOString() - if (!args.pinnedBy) { - const admin = await factoryInstance.create('User', { - role: 'admin', - updatedAt: new Date().toISOString(), - }) - await admin.relateTo(post, 'pinned') - args.pinnedBy = admin - } - } - - await Promise.all(categories.map(c => c.relateTo(post, 'post'))) - await Promise.all(tags.map(t => t.relateTo(post, 'post'))) - return post + factory: async ({ args, options, neodeInstance, factoryInstance }) => { + return Factory.build('post', args, options) }, } } diff --git a/backend/src/middleware/slugifyMiddleware.spec.js b/backend/src/middleware/slugifyMiddleware.spec.js index 5400d2c92..8f05fd6cd 100644 --- a/backend/src/middleware/slugifyMiddleware.spec.js +++ b/backend/src/middleware/slugifyMiddleware.spec.js @@ -92,6 +92,7 @@ describe('slugifyMiddleware', () => { title: 'Pre-existing post', slug: 'pre-existing-post', content: 'as Someone else content', + }, { categoryIds, }) }) diff --git a/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js b/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js index 334564582..cd6e2f726 100644 --- a/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js +++ b/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js @@ -51,19 +51,21 @@ beforeAll(async () => { await Promise.all([ user.relateTo(troll, 'following'), factory.create('Post', { - author: user, id: 'p1', title: 'Deleted post', slug: 'deleted-post', deleted: true, + }, { + author: user, categoryIds, }), factory.create('Post', { - author: user, id: 'p3', title: 'Publicly visible post', slug: 'publicly-visible-post', deleted: false, + }, { + author: user, categoryIds, }), ]) @@ -77,12 +79,13 @@ beforeAll(async () => { }), factory.create('Post', { id: 'p2', - author: troll, title: 'Disabled post', content: 'This is an offensive post content', contentExcerpt: 'This is an offensive post content', image: '/some/offensive/image.jpg', deleted: false, + }, { + author: troll, categoryIds, }), factory.create('Comment', { diff --git a/backend/src/middleware/validation/validationMiddleware.spec.js b/backend/src/middleware/validation/validationMiddleware.spec.js index 38cd010b4..a419c2428 100644 --- a/backend/src/middleware/validation/validationMiddleware.spec.js +++ b/backend/src/middleware/validation/validationMiddleware.spec.js @@ -121,10 +121,12 @@ beforeEach(async () => { const posts = await Promise.all([ factory.create('Post', { id: 'offensive-post', + }, { authorId: 'moderating-user', }), factory.create('Post', { id: 'post-4-commenting', + }, { authorId: 'commenting-user', }), ]) diff --git a/backend/src/models/Post.js b/backend/src/models/Post.js index 2b553232e..cbc29d814 100644 --- a/backend/src/models/Post.js +++ b/backend/src/models/Post.js @@ -42,5 +42,4 @@ export default { imageBlurred: { type: 'boolean', default: false }, imageAspectRatio: { type: 'float', default: 1.0 }, pinned: { type: 'boolean', default: null, valid: [null, true] }, - pinnedAt: { type: 'string', isoDate: true }, } diff --git a/backend/src/schema/resolvers/comments.spec.js b/backend/src/schema/resolvers/comments.spec.js index 9877161db..355074804 100644 --- a/backend/src/schema/resolvers/comments.spec.js +++ b/backend/src/schema/resolvers/comments.spec.js @@ -52,6 +52,7 @@ const setupPostAndComment = async () => { await factory.create('Post', { id: 'p1', content: 'Post to be commented', + }, { categoryIds: ['cat9'], }) newlyCreatedComment = await factory.create('Comment', { @@ -88,7 +89,7 @@ describe('CreateComment', () => { describe('given a post', () => { beforeEach(async () => { - await factory.create('Post', { categoryIds: ['cat9'], id: 'p1' }) + await factory.create('Post', { id: 'p1' }, { categoryIds: ['cat9'] } ) variables = { ...variables, postId: 'p1', diff --git a/backend/src/schema/resolvers/notifications.spec.js b/backend/src/schema/resolvers/notifications.spec.js index a5c46e930..27b8489e2 100644 --- a/backend/src/schema/resolvers/notifications.spec.js +++ b/backend/src/schema/resolvers/notifications.spec.js @@ -45,18 +45,20 @@ describe('given some notifications', () => { factory.create('Category', { id: 'cat1' }), ]) const [post1, post2, post3] = await Promise.all([ - factory.create('Post', { author, id: 'p1', categoryIds, content: 'Not for you' }), + factory.create('Post', { id: 'p1', content: 'Not for you' }, { author, categoryIds }), factory.create('Post', { - author, id: 'p2', - categoryIds, content: 'Already seen post mention', + }, { + author, + categoryIds, }), factory.create('Post', { - author, id: 'p3', - categoryIds, content: 'You have been mentioned in a post', + }, { + author, + categoryIds, }), ]) const [comment1, comment2, comment3] = await Promise.all([ diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index d7195be97..83dc53eea 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -114,10 +114,11 @@ describe('Post', () => { ), ]) ;[happyPost, cryPost] = await Promise.all([ - factory.create('Post', { id: 'happy-post', categoryIds: ['cat4'] }), - factory.create('Post', { id: 'cry-post', categoryIds: ['cat15'] }), + factory.create('Post', { id: 'happy-post' }, { categoryIds: ['cat4'] }), + factory.create('Post', { id: 'cry-post'} , { categoryIds: ['cat15'] }), factory.create('Post', { id: 'post-by-followed-user', + }, { categoryIds: ['cat9'], author: followedUser, }), @@ -352,10 +353,11 @@ describe('UpdatePost', () => { beforeEach(async () => { author = await factory.create('User', { slug: 'the-author' }) newlyCreatedPost = await factory.create('Post', { - author, id: 'p9876', title: 'Old title', content: 'Old content', + }, { + author, categoryIds, }) @@ -541,6 +543,7 @@ describe('UpdatePost', () => { beforeEach(async () => { await factory.create('Post', { id: 'created-and-pinned-by-same-admin', + }, { author: admin, }) variables = { ...variables, id: 'created-and-pinned-by-same-admin' } @@ -606,6 +609,7 @@ describe('UpdatePost', () => { authenticatedUser = await otherAdmin.toJson() await factory.create('Post', { id: 'created-by-one-admin-pinned-by-different-one', + }, { author: otherAdmin, }) }) @@ -666,6 +670,7 @@ describe('UpdatePost', () => { beforeEach(async () => { await factory.create('Post', { id: 'only-pinned-post', + }, { author: admin, }) await mutate({ mutation: pinPostMutation, variables }) @@ -887,10 +892,11 @@ describe('DeletePost', () => { author = await factory.create('User') await factory.create('Post', { id: 'p4711', - author, title: 'I will be deleted', content: 'To be deleted', image: 'path/to/some/image', + }, { + author, categoryIds, }) variables = { ...variables, id: 'p4711' } @@ -999,8 +1005,9 @@ describe('emotions', () => { beforeEach(async () => { author = await neode.create('User', { id: 'u257' }) postToEmote = await factory.create('Post', { - author, id: 'p1376', + }, { + author, categoryIds, }) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 9992da200..cc5c6dd26 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -357,9 +357,10 @@ describe('file a report on a resource', () => { describe('reported resource is a post', () => { beforeEach(async () => { await factory.create('Post', { - author: currentUser, id: 'post-to-report-id', title: 'This is a post that is going to be reported', + }, { + author: currentUser, categoryIds, }) }) @@ -409,15 +410,15 @@ describe('file a report on a resource', () => { }) describe('reported resource is a comment', () => { - let createPostVariables beforeEach(async () => { - createPostVariables = { + await factory.create('Post', { id: 'p1', title: 'post to comment on', content: 'please comment on me', + }, { categoryIds, - } - await factory.create('Post', { ...createPostVariables, author: currentUser }) + author: currentUser + }) await factory.create('Comment', { author: currentUser, postId: 'p1', @@ -571,22 +572,25 @@ describe('file a report on a resource', () => { await Promise.all([ factory.create('Post', { - author: abusiveUser, id: 'abusive-post-1', - categoryIds, content: 'Interesting Knowledge', + }, { + categoryIds, + author: abusiveUser, }), factory.create('Post', { - author: moderator, id: 'post-2', - categoryIds, content: 'More things to do …', + }, { + author: moderator, + categoryIds, }), factory.create('Post', { - author: currentUser, id: 'post-3', - categoryIds, content: 'I am at school …', + }, { + categoryIds, + author: currentUser, }), ]) await Promise.all([ diff --git a/backend/src/schema/resolvers/shout.spec.js b/backend/src/schema/resolvers/shout.spec.js index 50ff23687..4fb467252 100644 --- a/backend/src/schema/resolvers/shout.spec.js +++ b/backend/src/schema/resolvers/shout.spec.js @@ -91,11 +91,13 @@ describe('shout and unshout posts', () => { await factory.create('Post', { name: 'Other user post', id: 'another-user-post-id', + }, { author: postAuthor, }) await factory.create('Post', { name: 'current user post', id: 'current-user-post-id', + }, { author: currentUser, }) variables = {} @@ -157,6 +159,7 @@ describe('shout and unshout posts', () => { await factory.create('Post', { name: 'Posted By Another User', id: 'posted-by-another-user', + }, { author: postAuthor, }) await mutate({ diff --git a/backend/src/schema/resolvers/users.spec.js b/backend/src/schema/resolvers/users.spec.js index 9ead48a2b..20508774e 100644 --- a/backend/src/schema/resolvers/users.spec.js +++ b/backend/src/schema/resolvers/users.spec.js @@ -329,9 +329,10 @@ describe('DeleteUser', () => { icon: 'university', }) await factory.create('Post', { - author: user, id: 'p139', content: 'Post by user u343', + }, { + author: user, categoryIds, }) await factory.create('Comment', { From 2fc71d75a5d5eab9c3467e94e00257ef6dd7d8a0 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 19:56:57 +0100 Subject: [PATCH 07/19] Refactor factory for comments --- backend/src/db/seed.js | 705 +++++++++++------- backend/src/factories/comments.js | 64 +- backend/src/factories/posts.js | 8 +- .../src/middleware/slugifyMiddleware.spec.js | 18 +- .../softDelete/softDeleteMiddleware.spec.js | 106 +-- .../validation/validationMiddleware.spec.js | 41 +- backend/src/schema/resolvers/comments.spec.js | 35 +- .../schema/resolvers/notifications.spec.js | 87 ++- backend/src/schema/resolvers/posts.spec.js | 133 ++-- backend/src/schema/resolvers/reports.spec.js | 124 +-- backend/src/schema/resolvers/shout.spec.js | 48 +- backend/src/schema/resolvers/users.spec.js | 48 +- 12 files changed, 890 insertions(+), 527 deletions(-) diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index 82d6239c2..fa426fb55 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -385,101 +385,149 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] ]) const [p0, p1, p3, p4, p5, p6, p9, p10, p11, p13, p14, p15] = await Promise.all([ - factory.create('Post', { - id: 'p0', - language: sample(languages), - image: faker.image.unsplash.food(300, 169), - imageBlurred: true, - imageAspectRatio: 300 / 169, - }, { - categoryIds: ['cat16'], - author: peterLustig, - }), - factory.create('Post', { - id: 'p1', - language: sample(languages), - image: faker.image.unsplash.technology(300, 1500), - imageAspectRatio: 300 / 1500, - }, { - categoryIds: ['cat1'], - author: bobDerBaumeister, - }), - factory.create('Post', { - id: 'p3', - language: sample(languages), - }, { - categoryIds: ['cat3'], - author: huey, - }), - factory.create('Post', { - id: 'p4', - language: sample(languages), - }, { - categoryIds: ['cat4'], - author: dewey, - }), - factory.create('Post', { - id: 'p5', - language: sample(languages), - }, { - categoryIds: ['cat5'], - author: louie, - }), - factory.create('Post', { - id: 'p6', - language: sample(languages), - image: faker.image.unsplash.buildings(300, 857), - imageAspectRatio: 300 / 857, - }, { - categoryIds: ['cat6'], - author: peterLustig, - }), - factory.create('Post', { - id: 'p9', - language: sample(languages), - }, { - categoryIds: ['cat9'], - author: huey, - }), - factory.create('Post', { - id: 'p10', - imageBlurred: true, - }, { - author: dewey, - categoryIds: ['cat10'], - }), - factory.create('Post', { - id: 'p11', - language: sample(languages), - image: faker.image.unsplash.people(300, 901), - imageAspectRatio: 300 / 901, - }, { - categoryIds: ['cat11'], - author: louie, - }), - factory.create('Post', { - id: 'p13', - language: sample(languages), - }, { - author: bobDerBaumeister, - categoryIds: ['cat13'], - }), - factory.create('Post', { - id: 'p14', - language: sample(languages), - image: faker.image.unsplash.objects(300, 200), - imageAspectRatio: 300 / 450, - }, { - author: jennyRostock, - categoryIds: ['cat14'], - }), - factory.create('Post', { - id: 'p15', - language: sample(languages), - }, { - author: huey, - categoryIds: ['cat15'], - }), + factory.create( + 'Post', + { + id: 'p0', + language: sample(languages), + image: faker.image.unsplash.food(300, 169), + imageBlurred: true, + imageAspectRatio: 300 / 169, + }, + { + categoryIds: ['cat16'], + author: peterLustig, + }, + ), + factory.create( + 'Post', + { + id: 'p1', + language: sample(languages), + image: faker.image.unsplash.technology(300, 1500), + imageAspectRatio: 300 / 1500, + }, + { + categoryIds: ['cat1'], + author: bobDerBaumeister, + }, + ), + factory.create( + 'Post', + { + id: 'p3', + language: sample(languages), + }, + { + categoryIds: ['cat3'], + author: huey, + }, + ), + factory.create( + 'Post', + { + id: 'p4', + language: sample(languages), + }, + { + categoryIds: ['cat4'], + author: dewey, + }, + ), + factory.create( + 'Post', + { + id: 'p5', + language: sample(languages), + }, + { + categoryIds: ['cat5'], + author: louie, + }, + ), + factory.create( + 'Post', + { + id: 'p6', + language: sample(languages), + image: faker.image.unsplash.buildings(300, 857), + imageAspectRatio: 300 / 857, + }, + { + categoryIds: ['cat6'], + author: peterLustig, + }, + ), + factory.create( + 'Post', + { + id: 'p9', + language: sample(languages), + }, + { + categoryIds: ['cat9'], + author: huey, + }, + ), + factory.create( + 'Post', + { + id: 'p10', + imageBlurred: true, + }, + { + author: dewey, + categoryIds: ['cat10'], + }, + ), + factory.create( + 'Post', + { + id: 'p11', + language: sample(languages), + image: faker.image.unsplash.people(300, 901), + imageAspectRatio: 300 / 901, + }, + { + categoryIds: ['cat11'], + author: louie, + }, + ), + factory.create( + 'Post', + { + id: 'p13', + language: sample(languages), + }, + { + author: bobDerBaumeister, + categoryIds: ['cat13'], + }, + ), + factory.create( + 'Post', + { + id: 'p14', + language: sample(languages), + image: faker.image.unsplash.objects(300, 200), + imageAspectRatio: 300 / 450, + }, + { + author: jennyRostock, + categoryIds: ['cat14'], + }, + ), + factory.create( + 'Post', + { + id: 'p15', + language: sample(languages), + }, + { + author: huey, + categoryIds: ['cat15'], + }, + ), ]) authenticatedUser = await louie.toJson() @@ -601,61 +649,116 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] authenticatedUser = null const comments = await Promise.all([ - factory.create('Comment', { - author: jennyRostock, - id: 'c1', - postId: 'p1', - }), - factory.create('Comment', { - author: huey, - id: 'c2', - postId: 'p1', - }), - factory.create('Comment', { - author: louie, - id: 'c3', - postId: 'p3', - }), - factory.create('Comment', { - author: jennyRostock, - id: 'c5', - postId: 'p3', - }), - factory.create('Comment', { - author: peterLustig, - id: 'c6', - postId: 'p4', - }), - factory.create('Comment', { - author: jennyRostock, - id: 'c7', - postId: 'p2', - }), - factory.create('Comment', { - author: huey, - id: 'c8', - postId: 'p15', - }), - factory.create('Comment', { - author: dewey, - id: 'c9', - postId: 'p15', - }), - factory.create('Comment', { - author: louie, - id: 'c10', - postId: 'p15', - }), - factory.create('Comment', { - author: jennyRostock, - id: 'c11', - postId: 'p15', - }), - factory.create('Comment', { - author: jennyRostock, - id: 'c12', - postId: 'p15', - }), + factory.create( + 'Comment', + { + id: 'c1', + }, + { + author: jennyRostock, + postId: 'p1', + }, + ), + factory.create( + 'Comment', + { + id: 'c2', + }, + { + author: huey, + postId: 'p1', + }, + ), + factory.create( + 'Comment', + { + id: 'c3', + }, + { + author: louie, + postId: 'p3', + }, + ), + factory.create( + 'Comment', + { + id: 'c5', + }, + { + author: jennyRostock, + postId: 'p3', + }, + ), + factory.create( + 'Comment', + { + id: 'c6', + }, + { + author: peterLustig, + postId: 'p4', + }, + ), + factory.create( + 'Comment', + { + id: 'c7', + }, + { + author: jennyRostock, + postId: 'p2', + }, + ), + factory.create( + 'Comment', + { + id: 'c8', + }, + { + author: huey, + postId: 'p15', + }, + ), + factory.create( + 'Comment', + { + id: 'c9', + }, + { + author: dewey, + postId: 'p15', + }, + ), + factory.create( + 'Comment', + { + id: 'c10', + }, + { + author: louie, + postId: 'p15', + }, + ), + factory.create( + 'Comment', + { + id: 'c11', + }, + { + author: jennyRostock, + postId: 'p15', + }, + ), + factory.create( + 'Comment', + { + id: 'c12', + }, + { + author: jennyRostock, + postId: 'p15', + }, + ), ]) const trollingComment = comments[0] @@ -839,224 +942,320 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(30).keys()].map(() => { - return factory.create('Post', { - image: faker.image.unsplash.objects(), - }, { - categoryIds: ['cat1'], - author: jennyRostock, - }) + return factory.create( + 'Post', + { + image: faker.image.unsplash.objects(), + }, + { + categoryIds: ['cat1'], + author: jennyRostock, + }, + ) }), ) await Promise.all( [...Array(6).keys()].map(() => { - return factory.create('Comment', { - author: jennyRostock, - postId: 'p2', - }) + return factory.create( + 'Comment', + {}, + { + author: jennyRostock, + postId: 'p2', + }, + ) }), ) await Promise.all( [...Array(4).keys()].map(() => { - return factory.create('Comment', { - author: jennyRostock, - postId: 'p15', - }) + return factory.create( + 'Comment', + {}, + { + author: jennyRostock, + postId: 'p15', + }, + ) }), ) await Promise.all( [...Array(2).keys()].map(() => { - return factory.create('Comment', { - author: jennyRostock, - postId: 'p4', - }) + return factory.create( + 'Comment', + {}, + { + author: jennyRostock, + postId: 'p4', + }, + ) }), ) await Promise.all( [...Array(21).keys()].map(() => { - return factory.create('Post', { - image: faker.image.unsplash.buildings(), - }, { - author: peterLustig, - }) + return factory.create( + 'Post', + { + image: faker.image.unsplash.buildings(), + }, + { + author: peterLustig, + }, + ) }), ) await Promise.all( [...Array(3).keys()].map(() => { - return factory.create('Comment', { - author: peterLustig, - postId: 'p4', - }) + return factory.create( + 'Comment', + {}, + { + author: peterLustig, + postId: 'p4', + }, + ) }), ) await Promise.all( [...Array(5).keys()].map(() => { - return factory.create('Comment', { - author: peterLustig, - postId: 'p14', - }) + return factory.create( + 'Comment', + {}, + { + author: peterLustig, + postId: 'p14', + }, + ) }), ) await Promise.all( [...Array(6).keys()].map(() => { - return factory.create('Comment', { - author: peterLustig, - postId: 'p0', - }) + return factory.create( + 'Comment', + {}, + { + author: peterLustig, + postId: 'p0', + }, + ) }), ) await Promise.all( [...Array(11).keys()].map(() => { - return factory.create('Post', { - image: faker.image.unsplash.food(), - }, { - author: dewey, - }) + return factory.create( + 'Post', + { + image: faker.image.unsplash.food(), + }, + { + author: dewey, + }, + ) }), ) await Promise.all( [...Array(7).keys()].map(() => { - return factory.create('Comment', { - author: dewey, - postId: 'p2', - }) + return factory.create( + 'Comment', + {}, + { + author: dewey, + postId: 'p2', + }, + ) }), ) await Promise.all( [...Array(5).keys()].map(() => { - return factory.create('Comment', { - author: dewey, - postId: 'p6', - }) + return factory.create( + 'Comment', + {}, + { + author: dewey, + postId: 'p6', + }, + ) }), ) await Promise.all( [...Array(2).keys()].map(() => { - return factory.create('Comment', { - author: dewey, - postId: 'p9', - }) + return factory.create( + 'Comment', + {}, + { + author: dewey, + postId: 'p9', + }, + ) }), ) await Promise.all( [...Array(16).keys()].map(() => { - return factory.create('Post', { - image: faker.image.unsplash.technology(), - }, { - author: louie, - }) + return factory.create( + 'Post', + { + image: faker.image.unsplash.technology(), + }, + { + author: louie, + }, + ) }), ) await Promise.all( [...Array(4).keys()].map(() => { - return factory.create('Comment', { - postId: 'p1', - author: louie, - }) + return factory.create( + 'Comment', + {}, + { + postId: 'p1', + author: louie, + }, + ) }), ) await Promise.all( [...Array(8).keys()].map(() => { - return factory.create('Comment', { - author: louie, - postId: 'p10', - }) + return factory.create( + 'Comment', + {}, + { + author: louie, + postId: 'p10', + }, + ) }), ) await Promise.all( [...Array(5).keys()].map(() => { - return factory.create('Comment', { - author: louie, - postId: 'p13', - }) + return factory.create( + 'Comment', + {}, + { + author: louie, + postId: 'p13', + }, + ) }), ) await Promise.all( [...Array(45).keys()].map(() => { - return factory.create('Post', { - image: faker.image.unsplash.people(), - }, { - author: bobDerBaumeister, - }) + return factory.create( + 'Post', + { + image: faker.image.unsplash.people(), + }, + { + author: bobDerBaumeister, + }, + ) }), ) await Promise.all( [...Array(2).keys()].map(() => { - return factory.create('Comment', { - author: bobDerBaumeister, - postId: 'p2', - }) + return factory.create( + 'Comment', + {}, + { + author: bobDerBaumeister, + postId: 'p2', + }, + ) }), ) await Promise.all( [...Array(3).keys()].map(() => { - return factory.create('Comment', { - author: bobDerBaumeister, - postId: 'p12', - }) + return factory.create( + 'Comment', + {}, + { + author: bobDerBaumeister, + postId: 'p12', + }, + ) }), ) await Promise.all( [...Array(7).keys()].map(() => { - return factory.create('Comment', { - author: bobDerBaumeister, - postId: 'p13', - }) + return factory.create( + 'Comment', + {}, + { + author: bobDerBaumeister, + postId: 'p13', + }, + ) }), ) await Promise.all( [...Array(8).keys()].map(() => { - return factory.create('Post', { - image: faker.image.unsplash.nature(), - }, { - author: huey, - }) + return factory.create( + 'Post', + { + image: faker.image.unsplash.nature(), + }, + { + author: huey, + }, + ) }), ) await Promise.all( [...Array(6).keys()].map(() => { - return factory.create('Comment', { - author: huey, - postId: 'p0', - }) + return factory.create( + 'Comment', + {}, + { + author: huey, + postId: 'p0', + }, + ) }), ) await Promise.all( [...Array(8).keys()].map(() => { - return factory.create('Comment', { - author: huey, - postId: 'p13', - }) + return factory.create( + 'Comment', + {}, + { + author: huey, + postId: 'p13', + }, + ) }), ) await Promise.all( [...Array(9).keys()].map(() => { - return factory.create('Comment', { - author: huey, - postId: 'p15', - }) + return factory.create( + 'Comment', + {}, + { + author: huey, + postId: 'p15', + }, + ) }), ) diff --git a/backend/src/factories/comments.js b/backend/src/factories/comments.js index de3390e1a..0ecbb03ec 100644 --- a/backend/src/factories/comments.js +++ b/backend/src/factories/comments.js @@ -1,38 +1,42 @@ import faker from 'faker' import uuid from 'uuid/v4' +import { Factory } from 'rosie' +import { getNeode } from '../db/neo4j' + +const neode = getNeode() + +Factory.define('comment') + .option('postId', null) + .option('post', ['postId'], postId => { + if (postId) return neode.find('Post', postId) + return Factory.build('post') + }) + .option('authorId', null) + .option('author', ['authorId'], authorId => { + if (authorId) return neode.find('User', authorId) + return Factory.build('user') + }) + .attrs({ + id: uuid, + content: faker.lorem.sentence, + }) + .attr('contentExcerpt', ['contentExcerpt', 'content'], (contentExcerpt, content) => { + return contentExcerpt || content + }) + .after(async (buildObject, options) => { + const [comment, author, post] = await Promise.all([ + neode.create('Comment', buildObject), + options.author, + options.post, + ]) + await Promise.all([comment.relateTo(author, 'author'), comment.relateTo(post, 'post')]) + return comment + }) export default function create() { return { - factory: async ({ args, neodeInstance, factoryInstance }) => { - const defaults = { - id: uuid(), - content: [faker.lorem.sentence(), faker.lorem.sentence()].join('. '), - } - args = { - ...defaults, - ...args, - } - args.contentExcerpt = args.contentExcerpt || args.content - - let { post, postId } = args - delete args.post - delete args.postId - if (post && postId) throw new Error('You provided both post and postId') - if (postId) post = await neodeInstance.find('Post', postId) - post = post || (await factoryInstance.create('Post')) - - let { author, authorId } = args - delete args.author - delete args.authorId - if (author && authorId) throw new Error('You provided both author and authorId') - if (authorId) author = await neodeInstance.find('User', authorId) - author = author || (await factoryInstance.create('User')) - - delete args.author - const comment = await neodeInstance.create('Comment', args) - await comment.relateTo(post, 'post') - await comment.relateTo(author, 'author') - return comment + factory: async ({ args, options, neodeInstance, factoryInstance }) => { + return Factory.build('comment', args, options) }, } } diff --git a/backend/src/factories/posts.js b/backend/src/factories/posts.js index 2731cb60d..b6c7f1424 100644 --- a/backend/src/factories/posts.js +++ b/backend/src/factories/posts.js @@ -8,17 +8,17 @@ const neode = getNeode() Factory.define('post') .option('categoryIds', []) - .option('categories', ['categoryIds'], (categoryIds) => { + .option('categories', ['categoryIds'], categoryIds => { if (categoryIds.length) return Promise.all(categoryIds.map(id => neode.find('Category', id))) // there must be at least one category return Promise.all([Factory.build('category')]) }) .option('tagIds', []) - .option('tags', ['tagIds'], (tagIds) => { + .option('tags', ['tagIds'], tagIds => { return Promise.all(tagIds.map(id => neode.find('Tag', id))) }) .option('authorId', null) - .option('author', ['authorId'], (authorId) => { + .option('author', ['authorId'], authorId => { if (authorId) return neode.find('User', authorId) return Factory.build('user') }) @@ -33,7 +33,7 @@ Factory.define('post') imageBlurred: false, imageAspectRatio: 1.333, }) - .attr('pinned', ['pinned'], (pinned) => { + .attr('pinned', ['pinned'], pinned => { // Convert false to null return pinned || null }) diff --git a/backend/src/middleware/slugifyMiddleware.spec.js b/backend/src/middleware/slugifyMiddleware.spec.js index 8f05fd6cd..c3c7384dd 100644 --- a/backend/src/middleware/slugifyMiddleware.spec.js +++ b/backend/src/middleware/slugifyMiddleware.spec.js @@ -88,13 +88,17 @@ describe('slugifyMiddleware', () => { describe('if slug exists', () => { beforeEach(async () => { - await factory.create('Post', { - title: 'Pre-existing post', - slug: 'pre-existing-post', - content: 'as Someone else content', - }, { - categoryIds, - }) + await factory.create( + 'Post', + { + title: 'Pre-existing post', + slug: 'pre-existing-post', + content: 'as Someone else content', + }, + { + categoryIds, + }, + ) }) it('chooses another slug', async () => { diff --git a/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js b/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js index cd6e2f726..713020d4f 100644 --- a/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js +++ b/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js @@ -50,51 +50,73 @@ beforeAll(async () => { await Promise.all([ user.relateTo(troll, 'following'), - factory.create('Post', { - id: 'p1', - title: 'Deleted post', - slug: 'deleted-post', - deleted: true, - }, { - author: user, - categoryIds, - }), - factory.create('Post', { - id: 'p3', - title: 'Publicly visible post', - slug: 'publicly-visible-post', - deleted: false, - }, { - author: user, - categoryIds, - }), + factory.create( + 'Post', + { + id: 'p1', + title: 'Deleted post', + slug: 'deleted-post', + deleted: true, + }, + { + author: user, + categoryIds, + }, + ), + factory.create( + 'Post', + { + id: 'p3', + title: 'Publicly visible post', + slug: 'publicly-visible-post', + deleted: false, + }, + { + author: user, + categoryIds, + }, + ), ]) const resources = await Promise.all([ - factory.create('Comment', { - author: user, - id: 'c2', - postId: 'p3', - content: 'Enabled comment on public post', - }), - factory.create('Post', { - id: 'p2', - title: 'Disabled post', - content: 'This is an offensive post content', - contentExcerpt: 'This is an offensive post content', - image: '/some/offensive/image.jpg', - deleted: false, - }, { - author: troll, - categoryIds, - }), - factory.create('Comment', { - id: 'c1', - author: troll, - postId: 'p3', - content: 'Disabled comment', - contentExcerpt: 'Disabled comment', - }), + factory.create( + 'Comment', + { + id: 'c2', + content: 'Enabled comment on public post', + }, + { + author: user, + postId: 'p3', + }, + ), + factory.create( + 'Post', + { + id: 'p2', + title: 'Disabled post', + content: 'This is an offensive post content', + contentExcerpt: 'This is an offensive post content', + image: '/some/offensive/image.jpg', + deleted: false, + }, + { + author: troll, + categoryIds, + }, + ), + factory.create( + 'Comment', + { + id: 'c1', + content: 'Disabled comment', + contentExcerpt: 'Disabled comment', + }, + { + author: troll, + postId: 'p3', + }, + ), ]) const { server } = createServer({ diff --git a/backend/src/middleware/validation/validationMiddleware.spec.js b/backend/src/middleware/validation/validationMiddleware.spec.js index a419c2428..4b9874d7b 100644 --- a/backend/src/middleware/validation/validationMiddleware.spec.js +++ b/backend/src/middleware/validation/validationMiddleware.spec.js @@ -119,16 +119,24 @@ beforeEach(async () => { moderatingUser = users[1] commentingUser = users[2] const posts = await Promise.all([ - factory.create('Post', { - id: 'offensive-post', - }, { - authorId: 'moderating-user', - }), - factory.create('Post', { - id: 'post-4-commenting', - }, { - authorId: 'commenting-user', - }), + factory.create( + 'Post', + { + id: 'offensive-post', + }, + { + authorId: 'moderating-user', + }, + ), + factory.create( + 'Post', + { + id: 'post-4-commenting', + }, + { + authorId: 'commenting-user', + }, + ), ]) offensivePost = posts[0] }) @@ -184,10 +192,15 @@ describe('validateCreateComment', () => { describe('validateUpdateComment', () => { let updateCommentVariables beforeEach(async () => { - await factory.create('Comment', { - id: 'comment-id', - authorId: 'commenting-user', - }) + await factory.create( + 'Comment', + { + id: 'comment-id', + }, + { + authorId: 'commenting-user', + }, + ) updateCommentVariables = { id: 'whatever', content: '', diff --git a/backend/src/schema/resolvers/comments.spec.js b/backend/src/schema/resolvers/comments.spec.js index 355074804..bcc3c542f 100644 --- a/backend/src/schema/resolvers/comments.spec.js +++ b/backend/src/schema/resolvers/comments.spec.js @@ -49,18 +49,27 @@ const createCommentMutation = gql` ` const setupPostAndComment = async () => { commentAuthor = await factory.create('User') - await factory.create('Post', { - id: 'p1', - content: 'Post to be commented', - }, { - categoryIds: ['cat9'], - }) - newlyCreatedComment = await factory.create('Comment', { - id: 'c456', - postId: 'p1', - author: commentAuthor, - content: 'Comment to be deleted', - }) + await factory.create( + 'Post', + { + id: 'p1', + content: 'Post to be commented', + }, + { + categoryIds: ['cat9'], + }, + ) + newlyCreatedComment = await factory.create( + 'Comment', + { + id: 'c456', + content: 'Comment to be deleted', + }, + { + postId: 'p1', + author: commentAuthor, + }, + ) variables = { ...variables, id: 'c456', @@ -89,7 +98,7 @@ describe('CreateComment', () => { describe('given a post', () => { beforeEach(async () => { - await factory.create('Post', { id: 'p1' }, { categoryIds: ['cat9'] } ) + await factory.create('Post', { id: 'p1' }, { categoryIds: ['cat9'] }) variables = { ...variables, postId: 'p1', diff --git a/backend/src/schema/resolvers/notifications.spec.js b/backend/src/schema/resolvers/notifications.spec.js index 27b8489e2..1ce95ddd4 100644 --- a/backend/src/schema/resolvers/notifications.spec.js +++ b/backend/src/schema/resolvers/notifications.spec.js @@ -46,40 +46,63 @@ describe('given some notifications', () => { ]) const [post1, post2, post3] = await Promise.all([ factory.create('Post', { id: 'p1', content: 'Not for you' }, { author, categoryIds }), - factory.create('Post', { - id: 'p2', - content: 'Already seen post mention', - }, { - author, - categoryIds, - }), - factory.create('Post', { - id: 'p3', - content: 'You have been mentioned in a post', - }, { - author, - categoryIds, - }), + factory.create( + 'Post', + { + id: 'p2', + content: 'Already seen post mention', + }, + { + author, + categoryIds, + }, + ), + factory.create( + 'Post', + { + id: 'p3', + content: 'You have been mentioned in a post', + }, + { + author, + categoryIds, + }, + ), ]) const [comment1, comment2, comment3] = await Promise.all([ - factory.create('Comment', { - author, - postId: 'p3', - id: 'c1', - content: 'You have seen this comment mentioning already', - }), - factory.create('Comment', { - author, - postId: 'p3', - id: 'c2', - content: 'You have been mentioned in a comment', - }), - factory.create('Comment', { - author, - postId: 'p3', - id: 'c3', - content: 'Somebody else was mentioned in a comment', - }), + factory.create( + 'Comment', + { + id: 'c1', + content: 'You have seen this comment mentioning already', + }, + { + author, + postId: 'p3', + }, + ), + factory.create( + 'Comment', + { + id: 'c2', + content: 'You have been mentioned in a comment', + }, + { + author, + postId: 'p3', + }, + ), + factory.create( + 'Comment', + { + id: 'c3', + content: 'Somebody else was mentioned in a comment', + }, + { + author, + postId: 'p3', + }, + ), ]) await Promise.all([ post1.relateTo(neighbor, 'notified', { diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 83dc53eea..b8fe25ec3 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -115,13 +115,17 @@ describe('Post', () => { ]) ;[happyPost, cryPost] = await Promise.all([ factory.create('Post', { id: 'happy-post' }, { categoryIds: ['cat4'] }), - factory.create('Post', { id: 'cry-post'} , { categoryIds: ['cat15'] }), - factory.create('Post', { - id: 'post-by-followed-user', - }, { - categoryIds: ['cat9'], - author: followedUser, - }), + factory.create('Post', { id: 'cry-post' }, { categoryIds: ['cat15'] }), + factory.create( + 'Post', + { + id: 'post-by-followed-user', + }, + { + categoryIds: ['cat9'], + author: followedUser, + }, + ), ]) }) @@ -352,14 +356,18 @@ describe('UpdatePost', () => { ` beforeEach(async () => { author = await factory.create('User', { slug: 'the-author' }) - newlyCreatedPost = await factory.create('Post', { - id: 'p9876', - title: 'Old title', - content: 'Old content', - }, { - author, - categoryIds, - }) + newlyCreatedPost = await factory.create( + 'Post', + { + id: 'p9876', + title: 'Old title', + content: 'Old content', + }, + { + author, + categoryIds, + }, + ) variables = { id: 'p9876', @@ -541,11 +549,15 @@ describe('UpdatePost', () => { describe('are allowed to pin posts', () => { beforeEach(async () => { - await factory.create('Post', { - id: 'created-and-pinned-by-same-admin', - }, { - author: admin, - }) + await factory.create( + 'Post', + { + id: 'created-and-pinned-by-same-admin', + }, + { + author: admin, + }, + ) variables = { ...variables, id: 'created-and-pinned-by-same-admin' } }) @@ -607,11 +619,15 @@ describe('UpdatePost', () => { name: 'otherAdmin', }) authenticatedUser = await otherAdmin.toJson() - await factory.create('Post', { - id: 'created-by-one-admin-pinned-by-different-one', - }, { - author: otherAdmin, - }) + await factory.create( + 'Post', + { + id: 'created-by-one-admin-pinned-by-different-one', + }, + { + author: otherAdmin, + }, + ) }) it('responds with the updated Post', async () => { @@ -668,11 +684,15 @@ describe('UpdatePost', () => { describe('pinned post already exists', () => { let pinnedPost beforeEach(async () => { - await factory.create('Post', { - id: 'only-pinned-post', - }, { - author: admin, - }) + await factory.create( + 'Post', + { + id: 'only-pinned-post', + }, + { + author: admin, + }, + ) await mutate({ mutation: pinPostMutation, variables }) }) @@ -890,15 +910,19 @@ describe('DeletePost', () => { beforeEach(async () => { author = await factory.create('User') - await factory.create('Post', { - id: 'p4711', - title: 'I will be deleted', - content: 'To be deleted', - image: 'path/to/some/image', - }, { - author, - categoryIds, - }) + await factory.create( + 'Post', + { + id: 'p4711', + title: 'I will be deleted', + content: 'To be deleted', + image: 'path/to/some/image', + }, + { + author, + categoryIds, + }, + ) variables = { ...variables, id: 'p4711' } }) @@ -945,11 +969,16 @@ describe('DeletePost', () => { describe('if there are comments on the post', () => { beforeEach(async () => { - await factory.create('Comment', { - postId: 'p4711', - content: 'to be deleted comment content', - contentExcerpt: 'to be deleted comment content', - }) + await factory.create( + 'Comment', + { + content: 'to be deleted comment content', + contentExcerpt: 'to be deleted comment content', + }, + { + postId: 'p4711', + }, + ) }) it('marks the comments as deleted', async () => { @@ -1004,12 +1033,16 @@ describe('emotions', () => { beforeEach(async () => { author = await neode.create('User', { id: 'u257' }) - postToEmote = await factory.create('Post', { - id: 'p1376', - }, { - author, - categoryIds, - }) + postToEmote = await factory.create( + 'Post', + { + id: 'p1376', + }, + { + author, + categoryIds, + }, + ) variables = { ...variables, diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index cc5c6dd26..9cd4c1279 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -356,13 +356,17 @@ describe('file a report on a resource', () => { describe('reported resource is a post', () => { beforeEach(async () => { - await factory.create('Post', { - id: 'post-to-report-id', - title: 'This is a post that is going to be reported', - }, { - author: currentUser, - categoryIds, - }) + await factory.create( + 'Post', + { + id: 'post-to-report-id', + title: 'This is a post that is going to be reported', + }, + { + author: currentUser, + categoryIds, + }, + ) }) it('returns type "Post"', async () => { @@ -411,20 +415,29 @@ describe('file a report on a resource', () => { describe('reported resource is a comment', () => { beforeEach(async () => { - await factory.create('Post', { - id: 'p1', - title: 'post to comment on', - content: 'please comment on me', - }, { - categoryIds, - author: currentUser - }) - await factory.create('Comment', { - author: currentUser, - postId: 'p1', - id: 'comment-to-report-id', - content: 'Post comment to be reported.', - }) + await factory.create( + 'Post', + { + id: 'p1', + title: 'post to comment on', + content: 'please comment on me', + }, + { + categoryIds, + author: currentUser, + }, + ) + await factory.create( + 'Comment', + { + id: 'comment-to-report-id', + content: 'Post comment to be reported.', + }, + { + author: currentUser, + postId: 'p1', + }, + ) }) it('returns type "Comment"', async () => { @@ -571,34 +584,51 @@ describe('file a report on a resource', () => { }) await Promise.all([ - factory.create('Post', { - id: 'abusive-post-1', - content: 'Interesting Knowledge', - }, { - categoryIds, - author: abusiveUser, - }), - factory.create('Post', { - id: 'post-2', - content: 'More things to do …', - }, { - author: moderator, - categoryIds, - }), - factory.create('Post', { - id: 'post-3', - content: 'I am at school …', - }, { - categoryIds, - author: currentUser, - }), + factory.create( + 'Post', + { + id: 'abusive-post-1', + content: 'Interesting Knowledge', + }, + { + categoryIds, + author: abusiveUser, + }, + ), + factory.create( + 'Post', + { + id: 'post-2', + content: 'More things to do …', + }, + { + author: moderator, + categoryIds, + }, + ), + factory.create( + 'Post', + { + id: 'post-3', + content: 'I am at school …', + }, + { + categoryIds, + author: currentUser, + }, + ), ]) await Promise.all([ - factory.create('Comment', { - author: currentUser, - id: 'abusive-comment-1', - postId: 'post-1', - }), + factory.create( + 'Comment', + { + id: 'abusive-comment-1', + }, + { + postId: 'post-2', + author: currentUser, + }, + ), ]) authenticatedUser = await currentUser.toJson() await Promise.all([ diff --git a/backend/src/schema/resolvers/shout.spec.js b/backend/src/schema/resolvers/shout.spec.js index 4fb467252..dfe14f875 100644 --- a/backend/src/schema/resolvers/shout.spec.js +++ b/backend/src/schema/resolvers/shout.spec.js @@ -88,18 +88,26 @@ describe('shout and unshout posts', () => { describe('authenticated', () => { beforeEach(async () => { authenticatedUser = await currentUser.toJson() - await factory.create('Post', { - name: 'Other user post', - id: 'another-user-post-id', - }, { - author: postAuthor, - }) - await factory.create('Post', { - name: 'current user post', - id: 'current-user-post-id', - }, { - author: currentUser, - }) + await factory.create( + 'Post', + { + name: 'Other user post', + id: 'another-user-post-id', + }, + { + author: postAuthor, + }, + ) + await factory.create( + 'Post', + { + name: 'current user post', + id: 'current-user-post-id', + }, + { + author: currentUser, + }, + ) variables = {} }) @@ -156,12 +164,16 @@ describe('shout and unshout posts', () => { describe('authenticated', () => { beforeEach(async () => { authenticatedUser = await currentUser.toJson() - await factory.create('Post', { - name: 'Posted By Another User', - id: 'posted-by-another-user', - }, { - author: postAuthor, - }) + await factory.create( + 'Post', + { + name: 'Posted By Another User', + id: 'posted-by-another-user', + }, + { + author: postAuthor, + }, + ) await mutate({ mutation: mutationShoutPost, variables: { id: 'posted-by-another-user' }, diff --git a/backend/src/schema/resolvers/users.spec.js b/backend/src/schema/resolvers/users.spec.js index 20508774e..3806319f3 100644 --- a/backend/src/schema/resolvers/users.spec.js +++ b/backend/src/schema/resolvers/users.spec.js @@ -328,23 +328,37 @@ describe('DeleteUser', () => { name: 'Democracy & Politics', icon: 'university', }) - await factory.create('Post', { - id: 'p139', - content: 'Post by user u343', - }, { - author: user, - categoryIds, - }) - await factory.create('Comment', { - author: user, - id: 'c155', - content: 'Comment by user u343', - }) - await factory.create('Comment', { - postId: 'p139', - id: 'c156', - content: "A comment by someone else on user u343's post", - }) + await factory.create( + 'Post', + { + id: 'p139', + content: 'Post by user u343', + }, + { + author: user, + categoryIds, + }, + ) + await factory.create( + 'Comment', + { + id: 'c155', + content: 'Comment by user u343', + }, + { + author: user, + }, + ) + await factory.create( + 'Comment', + { + id: 'c156', + content: "A comment by someone else on user u343's post", + }, + { + postId: 'p139', + }, + ) }) it("deletes my account, but doesn't delete posts or comments by default", async () => { From fc367297e3e054f09b7f8f31788ab68d87f6babf Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 20:32:20 +0100 Subject: [PATCH 08/19] Get rid of different factory files ..and fix some cypress tests --- .../src/activitypub/routes/webfinger.spec.js | 7 +- backend/src/db/seed.js | 291 +++++++++--------- backend/src/factories.js | 227 ++++++++++++++ backend/src/factories/badges.js | 19 -- backend/src/factories/categories.js | 21 -- backend/src/factories/comments.js | 42 --- backend/src/factories/donations.js | 21 -- backend/src/factories/emailAddresses.js | 24 -- backend/src/factories/index.js | 64 ---- backend/src/factories/locations.js | 30 -- backend/src/factories/posts.js | 71 ----- backend/src/factories/reports.js | 16 - backend/src/factories/socialMedia.js | 20 -- backend/src/factories/tags.js | 20 -- .../src/factories/unverifiedEmailAddresses.js | 19 -- backend/src/factories/users.js | 54 ---- backend/src/jwt/decode.spec.js | 9 +- .../hashtags/hashtagsMiddleware.spec.js | 5 +- .../notificationsMiddleware.spec.js | 7 +- .../src/middleware/orderByMiddleware.spec.js | 5 +- .../middleware/permissionsMiddleware.spec.js | 23 +- .../src/middleware/slugifyMiddleware.spec.js | 22 +- .../softDelete/softDeleteMiddleware.spec.js | 39 ++- .../validation/validationMiddleware.spec.js | 31 +- backend/src/models/User.spec.js | 5 +- backend/src/schema/resolvers/comments.spec.js | 23 +- .../src/schema/resolvers/donations.spec.js | 17 +- backend/src/schema/resolvers/emails.spec.js | 17 +- backend/src/schema/resolvers/follow.spec.js | 55 ++-- .../src/schema/resolvers/locations.spec.js | 10 +- .../src/schema/resolvers/moderation.spec.js | 43 ++- .../schema/resolvers/notifications.spec.js | 35 +-- .../schema/resolvers/passwordReset.spec.js | 13 +- backend/src/schema/resolvers/posts.spec.js | 63 ++-- .../src/schema/resolvers/registration.spec.js | 14 +- backend/src/schema/resolvers/reports.spec.js | 61 ++-- backend/src/schema/resolvers/rewards.spec.js | 25 +- backend/src/schema/resolvers/shout.spec.js | 25 +- .../src/schema/resolvers/socialMedia.spec.js | 15 +- .../src/schema/resolvers/statistics.spec.js | 17 +- .../schema/resolvers/user_management.spec.js | 21 +- backend/src/schema/resolvers/users.spec.js | 41 ++- .../schema/resolvers/users/location.spec.js | 7 +- .../schema/resolvers/users/mutedUsers.spec.js | 5 +- backend/test/features/support/steps.js | 3 +- cypress/integration/common/report.js | 9 +- cypress/integration/common/steps.js | 58 ++-- cypress/support/commands.js | 8 +- cypress/support/factories.js | 8 +- features/support/steps.js | 8 +- 50 files changed, 728 insertions(+), 965 deletions(-) create mode 100644 backend/src/factories.js delete mode 100644 backend/src/factories/badges.js delete mode 100644 backend/src/factories/categories.js delete mode 100644 backend/src/factories/comments.js delete mode 100644 backend/src/factories/donations.js delete mode 100644 backend/src/factories/emailAddresses.js delete mode 100644 backend/src/factories/index.js delete mode 100644 backend/src/factories/locations.js delete mode 100644 backend/src/factories/posts.js delete mode 100644 backend/src/factories/reports.js delete mode 100644 backend/src/factories/socialMedia.js delete mode 100644 backend/src/factories/tags.js delete mode 100644 backend/src/factories/unverifiedEmailAddresses.js delete mode 100644 backend/src/factories/users.js diff --git a/backend/src/activitypub/routes/webfinger.spec.js b/backend/src/activitypub/routes/webfinger.spec.js index 06ca4577d..1a81c219b 100644 --- a/backend/src/activitypub/routes/webfinger.spec.js +++ b/backend/src/activitypub/routes/webfinger.spec.js @@ -1,10 +1,9 @@ import { handler } from './webfinger' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { getDriver } from '../../db/neo4j' let resource, res, json, status, contentType -const factory = Factory() const driver = getDriver() const request = () => { @@ -28,7 +27,7 @@ const request = () => { } afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('webfinger', () => { @@ -90,7 +89,7 @@ describe('webfinger', () => { describe('given a user for acct', () => { beforeEach(async () => { - await factory.create('User', { slug: 'some-user' }) + await Factory.build('user', { slug: 'some-user' }) }) it('returns user object', async () => { diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index fa426fb55..f2ec31876 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -12,7 +12,6 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] ;(async function() { let authenticatedUser = null const driver = getDriver() - const factory = Factory() const neode = getNeode() try { @@ -28,7 +27,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] const { mutate } = createTestClient(server) const [Hamburg, Berlin, Germany, Paris, France] = await Promise.all([ - factory.create('Location', { + Factory.build('location', { id: 'region.5127278006398860', name: 'Hamburg', type: 'region', @@ -44,7 +43,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] namePL: 'Hamburg', nameRU: 'Гамбург', }), - factory.create('Location', { + Factory.build('location', { id: 'region.14880313158564380', type: 'region', name: 'Berlin', @@ -60,7 +59,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] namePL: 'Berlin', nameRU: 'Берлин', }), - factory.create('Location', { + Factory.build('location', { id: 'country.10743216036480410', name: 'Germany', type: 'country', @@ -74,7 +73,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] nameEN: 'Germany', nameRU: 'Германия', }), - factory.create('Location', { + Factory.build('location', { id: 'region.9397217726497330', name: 'Paris', type: 'region', @@ -90,7 +89,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] namePL: 'Paryż', nameRU: 'Париж', }), - factory.create('Location', { + Factory.build('location', { id: 'country.9759535382641660', name: 'France', type: 'country', @@ -112,27 +111,27 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] ]) const [racoon, rabbit, wolf, bear, turtle, rhino] = await Promise.all([ - factory.create('Badge', { + Factory.build('badge', { id: 'indiegogo_en_racoon', icon: '/img/badges/indiegogo_en_racoon.svg', }), - factory.create('Badge', { + Factory.build('badge', { id: 'indiegogo_en_rabbit', icon: '/img/badges/indiegogo_en_rabbit.svg', }), - factory.create('Badge', { + Factory.build('badge', { id: 'indiegogo_en_wolf', icon: '/img/badges/indiegogo_en_wolf.svg', }), - factory.create('Badge', { + Factory.build('badge', { id: 'indiegogo_en_bear', icon: '/img/badges/indiegogo_en_bear.svg', }), - factory.create('Badge', { + Factory.build('badge', { id: 'indiegogo_en_turtle', icon: '/img/badges/indiegogo_en_turtle.svg', }), - factory.create('Badge', { + Factory.build('badge', { id: 'indiegogo_en_rhino', icon: '/img/badges/indiegogo_en_rhino.svg', }), @@ -147,8 +146,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] louie, dagobert, ] = await Promise.all([ - factory.create( - 'User', + Factory.build( + 'user', { id: 'u1', name: 'Peter Lustig', @@ -159,8 +158,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] email: 'admin@example.org', }, ), - factory.create( - 'User', + Factory.build( + 'user', { id: 'u2', name: 'Bob der Baumeister', @@ -171,8 +170,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] email: 'moderator@example.org', }, ), - factory.create( - 'User', + Factory.build( + 'user', { id: 'u3', name: 'Jenny Rostock', @@ -183,8 +182,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] email: 'user@example.org', }, ), - factory.create( - 'User', + Factory.build( + 'user', { id: 'u4', name: 'Huey', @@ -195,8 +194,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] email: 'huey@example.org', }, ), - factory.create( - 'User', + Factory.build( + 'user', { id: 'u5', name: 'Dewey', @@ -207,8 +206,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] email: 'dewey@example.org', }, ), - factory.create( - 'User', + Factory.build( + 'user', { id: 'u6', name: 'Louie', @@ -219,8 +218,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] email: 'louie@example.org', }, ), - factory.create( - 'User', + Factory.build( + 'user', { id: 'u7', name: 'Dagobert', @@ -271,97 +270,97 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] ]) await Promise.all([ - factory.create('Category', { + Factory.build('category', { id: 'cat1', name: 'Just For Fun', slug: 'just-for-fun', icon: 'smile', }), - factory.create('Category', { + Factory.build('category', { id: 'cat2', name: 'Happiness & Values', slug: 'happiness-values', icon: 'heart-o', }), - factory.create('Category', { + Factory.build('category', { id: 'cat3', name: 'Health & Wellbeing', slug: 'health-wellbeing', icon: 'medkit', }), - factory.create('Category', { + Factory.build('category', { id: 'cat4', name: 'Environment & Nature', slug: 'environment-nature', icon: 'tree', }), - factory.create('Category', { + Factory.build('category', { id: 'cat5', name: 'Animal Protection', slug: 'animal-protection', icon: 'paw', }), - factory.create('Category', { + Factory.build('category', { id: 'cat6', name: 'Human Rights & Justice', slug: 'human-rights-justice', icon: 'balance-scale', }), - factory.create('Category', { + Factory.build('category', { id: 'cat7', name: 'Education & Sciences', slug: 'education-sciences', icon: 'graduation-cap', }), - factory.create('Category', { + Factory.build('category', { id: 'cat8', name: 'Cooperation & Development', slug: 'cooperation-development', icon: 'users', }), - factory.create('Category', { + Factory.build('category', { id: 'cat9', name: 'Democracy & Politics', slug: 'democracy-politics', icon: 'university', }), - factory.create('Category', { + Factory.build('category', { id: 'cat10', name: 'Economy & Finances', slug: 'economy-finances', icon: 'money', }), - factory.create('Category', { + Factory.build('category', { id: 'cat11', name: 'Energy & Technology', slug: 'energy-technology', icon: 'flash', }), - factory.create('Category', { + Factory.build('category', { id: 'cat12', name: 'IT, Internet & Data Privacy', slug: 'it-internet-data-privacy', icon: 'mouse-pointer', }), - factory.create('Category', { + Factory.build('category', { id: 'cat13', name: 'Art, Culture & Sport', slug: 'art-culture-sport', icon: 'paint-brush', }), - factory.create('Category', { + Factory.build('category', { id: 'cat14', name: 'Freedom of Speech', slug: 'freedom-of-speech', icon: 'bullhorn', }), - factory.create('Category', { + Factory.build('category', { id: 'cat15', name: 'Consumption & Sustainability', slug: 'consumption-sustainability', icon: 'shopping-cart', }), - factory.create('Category', { + Factory.build('category', { id: 'cat16', name: 'Global Peace & Nonviolence', slug: 'global-peace-nonviolence', @@ -370,23 +369,23 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] ]) const [environment, nature, democracy, freedom] = await Promise.all([ - factory.create('Tag', { + Factory.build('tag', { id: 'Environment', }), - factory.create('Tag', { + Factory.build('tag', { id: 'Nature', }), - factory.create('Tag', { + Factory.build('tag', { id: 'Democracy', }), - factory.create('Tag', { + Factory.build('tag', { id: 'Freedom', }), ]) const [p0, p1, p3, p4, p5, p6, p9, p10, p11, p13, p14, p15] = await Promise.all([ - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p0', language: sample(languages), @@ -399,8 +398,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] author: peterLustig, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p1', language: sample(languages), @@ -412,8 +411,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] author: bobDerBaumeister, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p3', language: sample(languages), @@ -423,8 +422,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] author: huey, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p4', language: sample(languages), @@ -434,8 +433,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] author: dewey, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p5', language: sample(languages), @@ -445,8 +444,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] author: louie, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p6', language: sample(languages), @@ -458,8 +457,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] author: peterLustig, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p9', language: sample(languages), @@ -469,8 +468,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] author: huey, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p10', imageBlurred: true, @@ -480,8 +479,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] categoryIds: ['cat10'], }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p11', language: sample(languages), @@ -493,8 +492,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] author: louie, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p13', language: sample(languages), @@ -504,8 +503,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] categoryIds: ['cat13'], }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p14', language: sample(languages), @@ -517,8 +516,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] categoryIds: ['cat14'], }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p15', language: sample(languages), @@ -649,8 +648,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] authenticatedUser = null const comments = await Promise.all([ - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c1', }, @@ -659,8 +658,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p1', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c2', }, @@ -669,8 +668,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p1', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c3', }, @@ -679,8 +678,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p3', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c5', }, @@ -689,8 +688,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p3', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c6', }, @@ -699,8 +698,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p4', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c7', }, @@ -709,8 +708,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p2', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c8', }, @@ -719,8 +718,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p15', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c9', }, @@ -729,8 +728,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p15', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c10', }, @@ -739,8 +738,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p15', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c11', }, @@ -749,8 +748,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] postId: 'p15', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c12', }, @@ -825,10 +824,10 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] ]) const reports = await Promise.all([ - factory.create('Report'), - factory.create('Report'), - factory.create('Report'), - factory.create('Report'), + Factory.build('report'), + Factory.build('report'), + Factory.build('report'), + Factory.build('report'), ]) const reportAgainstDagobert = reports[0] const reportAgainstTrollingPost = reports[1] @@ -936,14 +935,14 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(30).keys()].map(i => { - return factory.create('User') + return Factory.build('user') }), ) await Promise.all( [...Array(30).keys()].map(() => { - return factory.create( - 'Post', + return Factory.build( + 'post', { image: faker.image.unsplash.objects(), }, @@ -957,8 +956,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(6).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: jennyRostock, @@ -970,8 +969,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(4).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: jennyRostock, @@ -983,8 +982,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(2).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: jennyRostock, @@ -996,8 +995,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(21).keys()].map(() => { - return factory.create( - 'Post', + return Factory.build( + 'post', { image: faker.image.unsplash.buildings(), }, @@ -1010,8 +1009,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(3).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: peterLustig, @@ -1023,8 +1022,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(5).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: peterLustig, @@ -1036,8 +1035,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(6).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: peterLustig, @@ -1049,8 +1048,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(11).keys()].map(() => { - return factory.create( - 'Post', + return Factory.build( + 'post', { image: faker.image.unsplash.food(), }, @@ -1063,8 +1062,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(7).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: dewey, @@ -1076,8 +1075,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(5).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: dewey, @@ -1089,8 +1088,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(2).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: dewey, @@ -1102,8 +1101,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(16).keys()].map(() => { - return factory.create( - 'Post', + return Factory.build( + 'post', { image: faker.image.unsplash.technology(), }, @@ -1116,8 +1115,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(4).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { postId: 'p1', @@ -1129,8 +1128,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(8).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: louie, @@ -1142,8 +1141,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(5).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: louie, @@ -1155,8 +1154,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(45).keys()].map(() => { - return factory.create( - 'Post', + return Factory.build( + 'post', { image: faker.image.unsplash.people(), }, @@ -1169,8 +1168,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(2).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: bobDerBaumeister, @@ -1182,8 +1181,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(3).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: bobDerBaumeister, @@ -1195,8 +1194,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(7).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: bobDerBaumeister, @@ -1208,8 +1207,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(8).keys()].map(() => { - return factory.create( - 'Post', + return Factory.build( + 'post', { image: faker.image.unsplash.nature(), }, @@ -1222,8 +1221,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(6).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: huey, @@ -1235,8 +1234,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(8).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: huey, @@ -1248,8 +1247,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all( [...Array(9).keys()].map(() => { - return factory.create( - 'Comment', + return Factory.build( + 'comment', {}, { author: huey, @@ -1259,7 +1258,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] }), ) - await factory.create('Donations') + await Factory.build('donations') /* eslint-disable-next-line no-console */ console.log('Seeded Data...') process.exit(0) diff --git a/backend/src/factories.js b/backend/src/factories.js new file mode 100644 index 000000000..b1ee08fe5 --- /dev/null +++ b/backend/src/factories.js @@ -0,0 +1,227 @@ +import uuid from 'uuid/v4' +import faker from 'faker' +import slugify from 'slug' +import { hashSync } from 'bcryptjs' +import { Factory } from 'rosie' +import { getDriver, getNeode } from './db/neo4j' + +const neode = getNeode() + +export const cleanDatabase = async (options = {}) => { + const { driver = getDriver() } = options + const session = driver.session() + try { + await session.writeTransaction(transaction => { + return transaction.run( + ` + MATCH (everything) + DETACH DELETE everything + `, + ) + }) + } finally { + session.close() + } +} + +Factory.define('category') + .attr('id', uuid) + .attr('icon', 'img/badges/fundraisingbox_de_airship.svg') + .attr('name', 'Some category name') + .after((buildObject, options) => { + return neode.create('Category', buildObject) + }) + +Factory.define('badge') + .attr('type', 'crowdfunding') + .attr('status', 'permanent') + .after((buildObject, options) => { + return neode.create('Badge', buildObject) + }) + +Factory.define('userWithoutEmailAddress') + .option('password', '1234') + .attrs({ + id: uuid, + name: faker.name.findName, + email: faker.internet.email, + password: '1234', + role: 'user', + avatar: faker.internet.avatar, + about: faker.lorem.paragraph, + termsAndConditionsAgreedVersion: '0.0.1', + termsAndConditionsAgreedAt: '2019-08-01T10:47:19.212Z', + allowEmbedIframes: false, + showShoutsPublicly: false, + locale: 'en', + }) + .attr('slug', ['slug', 'name'], (slug, name) => { + return slug || slugify(name, { lower: true }) + }) + .attr('encryptedPassword', ['password'], password => { + return hashSync(password, 10) + }) + .after(async (buildObject, options) => { + return neode.create('User', buildObject) + }) + +Factory.define('user') + .extend('userWithoutEmailAddress') + .option('email', faker.internet.exampleEmail) + .after(async (buildObject, options) => { + const [user, email] = await Promise.all([ + buildObject, + neode.create('EmailAddress', { email: options.email }), + ]) + await Promise.all([user.relateTo(email, 'primaryEmail'), email.relateTo(user, 'belongsTo')]) + return user + }) + +Factory.define('post') + .option('categoryIds', []) + .option('categories', ['categoryIds'], categoryIds => { + if (categoryIds.length) return Promise.all(categoryIds.map(id => neode.find('Category', id))) + // there must be at least one category + return Promise.all([Factory.build('category')]) + }) + .option('tagIds', []) + .option('tags', ['tagIds'], tagIds => { + return Promise.all(tagIds.map(id => neode.find('Tag', id))) + }) + .option('authorId', null) + .option('author', ['authorId'], authorId => { + if (authorId) return neode.find('User', authorId) + return Factory.build('user') + }) + .option('pinnedBy', null) + .attrs({ + id: uuid, + title: faker.lorem.sentence, + content: faker.lorem.paragraphs, + image: faker.image.unsplash.imageUrl, + visibility: 'public', + deleted: false, + imageBlurred: false, + imageAspectRatio: 1.333, + }) + .attr('pinned', ['pinned'], pinned => { + // Convert false to null + return pinned || null + }) + .attr('contentExcerpt', ['contentExcerpt', 'content'], (contentExcerpt, content) => { + return contentExcerpt || content + }) + .attr('slug', ['slug', 'title'], (slug, title) => { + return slug || slugify(title, { lower: true }) + }) + .after(async (buildObject, options) => { + const [post, author, categories, tags] = await Promise.all([ + neode.create('Post', buildObject), + options.author, + options.categories, + options.tags, + ]) + await Promise.all([ + post.relateTo(author, 'author'), + Promise.all(categories.map(c => c.relateTo(post, 'post'))), + Promise.all(tags.map(t => t.relateTo(post, 'post'))), + ]) + if (buildObject.pinned) { + const pinnedBy = await (options.pinnedBy || Factory.build('user', { role: 'admin' })) + await pinnedBy.relateTo(post, 'pinned') + } + return post + }) + +Factory.define('comment') + .option('postId', null) + .option('post', ['postId'], postId => { + if (postId) return neode.find('Post', postId) + return Factory.build('post') + }) + .option('authorId', null) + .option('author', ['authorId'], authorId => { + if (authorId) return neode.find('User', authorId) + return Factory.build('user') + }) + .attrs({ + id: uuid, + content: faker.lorem.sentence, + }) + .attr('contentExcerpt', ['contentExcerpt', 'content'], (contentExcerpt, content) => { + return contentExcerpt || content + }) + .after(async (buildObject, options) => { + const [comment, author, post] = await Promise.all([ + neode.create('Comment', buildObject), + options.author, + options.post, + ]) + await Promise.all([comment.relateTo(author, 'author'), comment.relateTo(post, 'post')]) + return comment + }) + +Factory.define('donations') + .attr('id', uuid) + .attr('goal', 15000) + .attr('progress', 0) + .after((buildObject, options) => { + return neode.create('Donations', buildObject) + }) + +const emailDefaults = { + email: faker.internet.email, + verifiedAt: () => new Date().toISOString(), +} + +Factory.define('emailAddress') + .attr(emailDefaults) + .after((buildObject, options) => { + return neode.create('EmailAddress', buildObject) + }) + +Factory.define('unverifiedEmailAddress') + .attr(emailDefaults) + .after((buildObject, options) => { + return neode.create('UnverifiedEmailAddress', buildObject) + }) + +Factory.define('location') + .attrs({ + name: 'Germany', + namePT: 'Alemanha', + nameDE: 'Deutschland', + nameES: 'Alemania', + nameNL: 'Duitsland', + namePL: 'Niemcy', + nameFR: 'Allemagne', + nameIT: 'Germania', + nameEN: 'Germany', + id: 'country.10743216036480410', + type: 'country', + }) + .after((buildObject, options) => { + return neode.create('Location', buildObject) + }) + +Factory.define('report').after((buildObject, options) => { + return neode.create('Report', buildObject) +}) + +Factory.define('tag') + .attrs({ + name: '#human-connection', + }) + .after((buildObject, options) => { + return neode.create('Tag', buildObject) + }) + +Factory.define('socialMedia') + .attrs({ + url: 'https://mastodon.social/@Gargron', + }) + .after((buildObject, options) => { + return neode.create('SocialMedia', buildObject) + }) + +export default Factory diff --git a/backend/src/factories/badges.js b/backend/src/factories/badges.js deleted file mode 100644 index 3c981ae8a..000000000 --- a/backend/src/factories/badges.js +++ /dev/null @@ -1,19 +0,0 @@ -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('badge') - .attr('type', 'crowdfunding') - .attr('status', 'permanent') - .after((buildObject, options) => { - return neode.create('Badge', buildObject) - }) - -export default function create() { - return { - factory: ({ args, neodeInstance }) => { - return Factory.build('badge', args) - }, - } -} diff --git a/backend/src/factories/categories.js b/backend/src/factories/categories.js deleted file mode 100644 index 5dd89efbe..000000000 --- a/backend/src/factories/categories.js +++ /dev/null @@ -1,21 +0,0 @@ -import uuid from 'uuid/v4' -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('category') - .attr('id', uuid) - .attr('icon', 'img/badges/fundraisingbox_de_airship.svg') - .attr('name', 'Some category name') - .after((buildObject, options) => { - return neode.create('Category', buildObject) - }) - -export default function create() { - return { - factory: async ({ args, neodeInstance }) => { - return Factory.build('category', args) - }, - } -} diff --git a/backend/src/factories/comments.js b/backend/src/factories/comments.js deleted file mode 100644 index 0ecbb03ec..000000000 --- a/backend/src/factories/comments.js +++ /dev/null @@ -1,42 +0,0 @@ -import faker from 'faker' -import uuid from 'uuid/v4' -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('comment') - .option('postId', null) - .option('post', ['postId'], postId => { - if (postId) return neode.find('Post', postId) - return Factory.build('post') - }) - .option('authorId', null) - .option('author', ['authorId'], authorId => { - if (authorId) return neode.find('User', authorId) - return Factory.build('user') - }) - .attrs({ - id: uuid, - content: faker.lorem.sentence, - }) - .attr('contentExcerpt', ['contentExcerpt', 'content'], (contentExcerpt, content) => { - return contentExcerpt || content - }) - .after(async (buildObject, options) => { - const [comment, author, post] = await Promise.all([ - neode.create('Comment', buildObject), - options.author, - options.post, - ]) - await Promise.all([comment.relateTo(author, 'author'), comment.relateTo(post, 'post')]) - return comment - }) - -export default function create() { - return { - factory: async ({ args, options, neodeInstance, factoryInstance }) => { - return Factory.build('comment', args, options) - }, - } -} diff --git a/backend/src/factories/donations.js b/backend/src/factories/donations.js deleted file mode 100644 index a17c2511a..000000000 --- a/backend/src/factories/donations.js +++ /dev/null @@ -1,21 +0,0 @@ -import uuid from 'uuid/v4' -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('donations') - .attr('id', uuid) - .attr('goal', 15000) - .attr('progress', 0) - .after((buildObject, options) => { - return neode.create('Donations', buildObject) - }) - -export default function create() { - return { - factory: async ({ args, neodeInstance }) => { - return Factory.build('donations', args) - }, - } -} diff --git a/backend/src/factories/emailAddresses.js b/backend/src/factories/emailAddresses.js deleted file mode 100644 index 8be427058..000000000 --- a/backend/src/factories/emailAddresses.js +++ /dev/null @@ -1,24 +0,0 @@ -import faker from 'faker' -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -export const defaults = { - email: faker.internet.email, - verifiedAt: () => new Date().toISOString(), -} - -const neode = getNeode() - -Factory.define('emailAddress') - .attr(defaults) - .after((buildObject, options) => { - return neode.create('EmailAddress', buildObject) - }) - -export default function create() { - return { - factory: async ({ args, neodeInstance }) => { - return Factory.build('emailAddress', args) - }, - } -} diff --git a/backend/src/factories/index.js b/backend/src/factories/index.js deleted file mode 100644 index 983aaae26..000000000 --- a/backend/src/factories/index.js +++ /dev/null @@ -1,64 +0,0 @@ -import { getDriver, getNeode } from '../db/neo4j' - -const factories = { - Badge: require('./badges.js').default, - User: require('./users.js').default, - Post: require('./posts.js').default, - Comment: require('./comments.js').default, - Category: require('./categories.js').default, - Tag: require('./tags.js').default, - SocialMedia: require('./socialMedia.js').default, - Location: require('./locations.js').default, - EmailAddress: require('./emailAddresses.js').default, - UnverifiedEmailAddress: require('./unverifiedEmailAddresses.js').default, - Donations: require('./donations.js').default, - Report: require('./reports.js').default, -} - -export const cleanDatabase = async (options = {}) => { - const { driver = getDriver() } = options - const session = driver.session() - try { - await session.writeTransaction(transaction => { - return transaction.run( - ` - MATCH (everything) - DETACH DELETE everything - `, - ) - }) - } finally { - session.close() - } -} - -export default function Factory(options = {}) { - const { neo4jDriver = getDriver(), neodeInstance = getNeode() } = options - - const result = { - neo4jDriver, - factories, - lastResponse: null, - neodeInstance, - async create(node, args = {}, options = {}) { - const { factory } = this.factories[node](args) - this.lastResponse = await factory({ - args, - options, - neodeInstance, - factoryInstance: this, - }) - return this.lastResponse - }, - - async cleanDatabase() { - this.lastResponse = await cleanDatabase({ - driver: this.neo4jDriver, - }) - return this - }, - } - result.create.bind(result) - result.cleanDatabase.bind(result) - return result -} diff --git a/backend/src/factories/locations.js b/backend/src/factories/locations.js deleted file mode 100644 index 29b6c3098..000000000 --- a/backend/src/factories/locations.js +++ /dev/null @@ -1,30 +0,0 @@ -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('location') - .attrs({ - name: 'Germany', - namePT: 'Alemanha', - nameDE: 'Deutschland', - nameES: 'Alemania', - nameNL: 'Duitsland', - namePL: 'Niemcy', - nameFR: 'Allemagne', - nameIT: 'Germania', - nameEN: 'Germany', - id: 'country.10743216036480410', - type: 'country', - }) - .after((buildObject, options) => { - return neode.create('Location', buildObject) - }) - -export default function create() { - return { - factory: async ({ args, neodeInstance }) => { - return Factory.build('location', args) - }, - } -} diff --git a/backend/src/factories/posts.js b/backend/src/factories/posts.js deleted file mode 100644 index b6c7f1424..000000000 --- a/backend/src/factories/posts.js +++ /dev/null @@ -1,71 +0,0 @@ -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' -import faker from 'faker' -import slugify from 'slug' -import uuid from 'uuid/v4' - -const neode = getNeode() - -Factory.define('post') - .option('categoryIds', []) - .option('categories', ['categoryIds'], categoryIds => { - if (categoryIds.length) return Promise.all(categoryIds.map(id => neode.find('Category', id))) - // there must be at least one category - return Promise.all([Factory.build('category')]) - }) - .option('tagIds', []) - .option('tags', ['tagIds'], tagIds => { - return Promise.all(tagIds.map(id => neode.find('Tag', id))) - }) - .option('authorId', null) - .option('author', ['authorId'], authorId => { - if (authorId) return neode.find('User', authorId) - return Factory.build('user') - }) - .option('pinnedBy', null) - .attrs({ - id: uuid, - title: faker.lorem.sentence, - content: faker.lorem.paragraphs, - image: faker.image.unsplash.imageUrl, - visibility: 'public', - deleted: false, - imageBlurred: false, - imageAspectRatio: 1.333, - }) - .attr('pinned', ['pinned'], pinned => { - // Convert false to null - return pinned || null - }) - .attr('contentExcerpt', ['contentExcerpt', 'content'], (contentExcerpt, content) => { - return contentExcerpt || content - }) - .attr('slug', ['slug', 'title'], (slug, title) => { - return slug || slugify(title, { lower: true }) - }) - .after(async (buildObject, options) => { - const [post, author, categories, tags] = await Promise.all([ - neode.create('Post', buildObject), - options.author, - options.categories, - options.tags, - ]) - await Promise.all([ - post.relateTo(author, 'author'), - Promise.all(categories.map(c => c.relateTo(post, 'post'))), - Promise.all(tags.map(t => t.relateTo(post, 'post'))), - ]) - if (buildObject.pinned) { - const pinnedBy = await (options.pinnedBy || Factory.build('user', { role: 'admin' })) - await pinnedBy.relateTo(post, 'pinned') - } - return post - }) - -export default function create() { - return { - factory: async ({ args, options, neodeInstance, factoryInstance }) => { - return Factory.build('post', args, options) - }, - } -} diff --git a/backend/src/factories/reports.js b/backend/src/factories/reports.js deleted file mode 100644 index 9e3024e35..000000000 --- a/backend/src/factories/reports.js +++ /dev/null @@ -1,16 +0,0 @@ -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('report').after((buildObject, options) => { - return neode.create('Report', buildObject) -}) - -export default function create() { - return { - factory: async ({ args }) => { - return Factory.build('report', args) - }, - } -} diff --git a/backend/src/factories/socialMedia.js b/backend/src/factories/socialMedia.js deleted file mode 100644 index 2baf058a0..000000000 --- a/backend/src/factories/socialMedia.js +++ /dev/null @@ -1,20 +0,0 @@ -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('socialMedia') - .attrs({ - url: 'https://mastodon.social/@Gargron', - }) - .after((buildObject, options) => { - return neode.create('SocialMedia', buildObject) - }) - -export default function create() { - return { - factory: async ({ args }) => { - return Factory.build('socialMedia', args) - }, - } -} diff --git a/backend/src/factories/tags.js b/backend/src/factories/tags.js deleted file mode 100644 index cf7ca1729..000000000 --- a/backend/src/factories/tags.js +++ /dev/null @@ -1,20 +0,0 @@ -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('tag') - .attrs({ - name: '#human-connection', - }) - .after((buildObject, options) => { - return neode.create('Tag', buildObject) - }) - -export default function create() { - return { - factory: async ({ args, neodeInstance }) => { - return Factory.build('tag', args) - }, - } -} diff --git a/backend/src/factories/unverifiedEmailAddresses.js b/backend/src/factories/unverifiedEmailAddresses.js deleted file mode 100644 index 2d6bd9f57..000000000 --- a/backend/src/factories/unverifiedEmailAddresses.js +++ /dev/null @@ -1,19 +0,0 @@ -import { defaults } from './emailAddresses.js' -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' - -const neode = getNeode() - -Factory.define('unverifiedEmailAddress') - .attr(defaults) - .after((buildObject, options) => { - return neode.create('UnverifiedEmailAddress', buildObject) - }) - -export default function create() { - return { - factory: async ({ args, neodeInstance }) => { - return Factory.build('unverifiedEmailAddress', args) - }, - } -} diff --git a/backend/src/factories/users.js b/backend/src/factories/users.js deleted file mode 100644 index 91b9e9a8b..000000000 --- a/backend/src/factories/users.js +++ /dev/null @@ -1,54 +0,0 @@ -import faker from 'faker' -import uuid from 'uuid/v4' -import slugify from 'slug' -import { Factory } from 'rosie' -import { getNeode } from '../db/neo4j' -import { hashSync } from 'bcryptjs' - -const neode = getNeode() - -Factory.define('userWithoutEmailAddress') - .option('password', '1234') - .attrs({ - id: uuid, - name: faker.name.findName, - email: faker.internet.email, - password: '1234', - role: 'user', - avatar: faker.internet.avatar, - about: faker.lorem.paragraph, - termsAndConditionsAgreedVersion: '0.0.1', - termsAndConditionsAgreedAt: '2019-08-01T10:47:19.212Z', - allowEmbedIframes: false, - showShoutsPublicly: false, - locale: 'en', - }) - .attr('slug', ['slug', 'name'], (slug, name) => { - return slug || slugify(name, { lower: true }) - }) - .attr('encryptedPassword', ['password'], password => { - return hashSync(password, 10) - }) - .after(async (buildObject, options) => { - return neode.create('User', buildObject) - }) - -Factory.define('user') - .extend('userWithoutEmailAddress') - .option('email', faker.internet.exampleEmail) - .after(async (buildObject, options) => { - const [user, email] = await Promise.all([ - buildObject, - neode.create('EmailAddress', { email: options.email }), - ]) - await Promise.all([user.relateTo(email, 'primaryEmail'), email.relateTo(user, 'belongsTo')]) - return user - }) - -export default function create() { - return { - factory: ({ args, options }) => { - return Factory.build('user', args, options) - }, - } -} diff --git a/backend/src/jwt/decode.spec.js b/backend/src/jwt/decode.spec.js index d0bf2005d..1163f71a8 100644 --- a/backend/src/jwt/decode.spec.js +++ b/backend/src/jwt/decode.spec.js @@ -1,8 +1,7 @@ -import Factory from '../factories/index' +import Factory, { cleanDatabase } from '../factories' import { getDriver, getNeode } from '../db/neo4j' import decode from './decode' -const factory = Factory() const driver = getDriver() const neode = getNeode() @@ -26,7 +25,7 @@ export const validAuthorizationHeader = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoidXNlciIsImxvY2F0aW9uTmFtZSI6bnVsbCwibmFtZSI6Ikplbm55IFJvc3RvY2siLCJhYm91dCI6bnVsbCwiYXZhdGFyIjoiaHR0cHM6Ly9zMy5hbWF6b25hd3MuY29tL3VpZmFjZXMvZmFjZXMvdHdpdHRlci9zYXNoYV9zaGVzdGFrb3YvMTI4LmpwZyIsImlkIjoidTMiLCJlbWFpbCI6InVzZXJAZXhhbXBsZS5vcmciLCJzbHVnIjoiamVubnktcm9zdG9jayIsImlhdCI6MTU1MDg0NjY4MCwiZXhwIjoxNjM3MjQ2NjgwLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjMwMDAiLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjQwMDAiLCJzdWIiOiJ1MyJ9.eZ_mVKas4Wzoc_JrQTEWXyRn7eY64cdIg4vqQ-F_7Jc' afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('decode', () => { @@ -65,8 +64,8 @@ describe('decode', () => { describe('and corresponding user in the database', () => { let user beforeEach(async () => { - user = await factory.create( - 'User', + user = await Factory.build( + 'user', { role: 'user', name: 'Jenny Rostock', diff --git a/backend/src/middleware/hashtags/hashtagsMiddleware.spec.js b/backend/src/middleware/hashtags/hashtagsMiddleware.spec.js index 4967978a5..8560c97d4 100644 --- a/backend/src/middleware/hashtags/hashtagsMiddleware.spec.js +++ b/backend/src/middleware/hashtags/hashtagsMiddleware.spec.js @@ -1,5 +1,5 @@ import { gql } from '../../helpers/jest' -import Factory from '../../factories' +import { cleanDatabase } from '../../factories' import { createTestClient } from 'apollo-server-testing' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' @@ -9,7 +9,6 @@ let query let mutate let hashtagingUser let authenticatedUser -const factory = Factory() const driver = getDriver() const neode = getNeode() const categoryIds = ['cat9'] @@ -68,7 +67,7 @@ beforeEach(async () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('hashtags', () => { diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index 0d0714289..fdc4d2a48 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -1,11 +1,10 @@ import { gql } from '../../helpers/jest' -import Factory from '../../factories' +import { cleanDatabase } from '../../factories' import { createTestClient } from 'apollo-server-testing' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' let server, query, mutate, notifiedUser, authenticatedUser -const factory = Factory() const driver = getDriver() const neode = getNeode() const categoryIds = ['cat9'] @@ -36,7 +35,7 @@ const createCommentMutation = gql` ` beforeAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() const createServerResult = createServer({ context: () => { return { @@ -73,7 +72,7 @@ beforeEach(async () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('notifications', () => { diff --git a/backend/src/middleware/orderByMiddleware.spec.js b/backend/src/middleware/orderByMiddleware.spec.js index 8d92a5b5d..2bca36364 100644 --- a/backend/src/middleware/orderByMiddleware.spec.js +++ b/backend/src/middleware/orderByMiddleware.spec.js @@ -1,10 +1,9 @@ import { gql } from '../helpers/jest' -import Factory from '../factories' +import { cleanDatabase } from '../factories' import { getNeode, getDriver } from '../db/neo4j' import { createTestClient } from 'apollo-server-testing' import createServer from '../server' -const factory = Factory() const neode = getNeode() const driver = getDriver() @@ -27,7 +26,7 @@ beforeEach(async () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('Query', () => { diff --git a/backend/src/middleware/permissionsMiddleware.spec.js b/backend/src/middleware/permissionsMiddleware.spec.js index 7c62cfcc0..5b24b3e74 100644 --- a/backend/src/middleware/permissionsMiddleware.spec.js +++ b/backend/src/middleware/permissionsMiddleware.spec.js @@ -1,10 +1,9 @@ import { createTestClient } from 'apollo-server-testing' import createServer from '../server' -import Factory from '../factories' +import Factory, { cleanDatabase } from '../factories' import { gql } from '../helpers/jest' import { getDriver, getNeode } from '../db/neo4j' -const factory = Factory() const instance = getNeode() const driver = getDriver() @@ -20,7 +19,7 @@ const userQuery = gql` describe('authorization', () => { beforeAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() const { server } = createServer({ context: () => ({ driver, @@ -34,8 +33,8 @@ describe('authorization', () => { describe('given two existing users', () => { beforeEach(async () => { ;[owner, anotherRegularUser, administrator, moderator] = await Promise.all([ - factory.create( - 'User', + Factory.build( + 'user', { name: 'Owner', }, @@ -44,8 +43,8 @@ describe('authorization', () => { password: 'iamtheowner', }, ), - factory.create( - 'User', + Factory.build( + 'user', { name: 'Another Regular User', }, @@ -54,8 +53,8 @@ describe('authorization', () => { password: 'else', }, ), - factory.create( - 'User', + Factory.build( + 'user', { name: 'Admin', role: 'admin', @@ -65,8 +64,8 @@ describe('authorization', () => { password: 'admin', }, ), - factory.create( - 'User', + Factory.build( + 'user', { name: 'Moderator', role: 'moderator', @@ -81,7 +80,7 @@ describe('authorization', () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('access email address', () => { diff --git a/backend/src/middleware/slugifyMiddleware.spec.js b/backend/src/middleware/slugifyMiddleware.spec.js index c3c7384dd..e13d66407 100644 --- a/backend/src/middleware/slugifyMiddleware.spec.js +++ b/backend/src/middleware/slugifyMiddleware.spec.js @@ -1,11 +1,9 @@ -import Factory from '../factories' +import Factory, { cleanDatabase } from '../factories' import { gql } from '../helpers/jest' import { getNeode, getDriver } from '../db/neo4j' import createServer from '../server' import { createTestClient } from 'apollo-server-testing' -const factory = Factory() - let mutate let authenticatedUser let variables @@ -28,18 +26,18 @@ beforeAll(() => { beforeEach(async () => { variables = {} - const admin = await factory.create('User', { + const admin = await Factory.build('user', { role: 'admin', }) - await factory.create( - 'User', + await Factory.build( + 'user', {}, { email: 'someone@example.org', password: '1234', }, ) - await factory.create('Category', { + await Factory.build('category', { id: 'cat9', name: 'Democracy & Politics', icon: 'university', @@ -48,7 +46,7 @@ beforeEach(async () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('slugifyMiddleware', () => { @@ -88,8 +86,8 @@ describe('slugifyMiddleware', () => { describe('if slug exists', () => { beforeEach(async () => { - await factory.create( - 'Post', + await Factory.build( + 'post', { title: 'Pre-existing post', slug: 'pre-existing-post', @@ -199,7 +197,7 @@ describe('slugifyMiddleware', () => { describe('given a user has signed up with their email address', () => { beforeEach(async () => { - await factory.create('EmailAddress', { + await Factory.build('emailAddress', { email: '123@example.org', nonce: '123456', verifiedAt: null, @@ -223,7 +221,7 @@ describe('slugifyMiddleware', () => { describe('if slug exists', () => { beforeEach(async () => { - await factory.create('User', { + await Factory.build('user', { name: 'I am a user', slug: 'i-am-a-user', }) diff --git a/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js b/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js index 713020d4f..8d8adb56b 100644 --- a/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js +++ b/backend/src/middleware/softDelete/softDeleteMiddleware.spec.js @@ -1,10 +1,9 @@ -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' import { createTestClient } from 'apollo-server-testing' -const factory = Factory() const neode = getNeode() const driver = getDriver() @@ -18,9 +17,9 @@ const action = () => { beforeAll(async () => { // For performance reasons we do this only once const users = await Promise.all([ - factory.create('User', { id: 'u1', role: 'user' }), - factory.create( - 'User', + Factory.build('user', { id: 'u1', role: 'user' }), + Factory.build( + 'user', { id: 'm1', role: 'moderator', @@ -29,7 +28,7 @@ beforeAll(async () => { password: '1234', }, ), - factory.create('User', { + Factory.build('user', { id: 'u2', role: 'user', name: 'Offensive Name', @@ -50,8 +49,8 @@ beforeAll(async () => { await Promise.all([ user.relateTo(troll, 'following'), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p1', title: 'Deleted post', @@ -63,8 +62,8 @@ beforeAll(async () => { categoryIds, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p3', title: 'Publicly visible post', @@ -79,8 +78,8 @@ beforeAll(async () => { ]) const resources = await Promise.all([ - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c2', content: 'Enabled comment on public post', @@ -90,8 +89,8 @@ beforeAll(async () => { postId: 'p3', }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p2', title: 'Disabled post', @@ -105,8 +104,8 @@ beforeAll(async () => { categoryIds, }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c1', content: 'Disabled comment', @@ -135,9 +134,9 @@ beforeAll(async () => { const trollingComment = resources[2] const reports = await Promise.all([ - factory.create('Report'), - factory.create('Report'), - factory.create('Report'), + Factory.build('report'), + Factory.build('report'), + Factory.build('report'), ]) const reportAgainstTroll = reports[0] const reportAgainstTrollingPost = reports[1] @@ -184,7 +183,7 @@ beforeAll(async () => { }) afterAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('softDeleteMiddleware', () => { diff --git a/backend/src/middleware/validation/validationMiddleware.spec.js b/backend/src/middleware/validation/validationMiddleware.spec.js index 4b9874d7b..25524c973 100644 --- a/backend/src/middleware/validation/validationMiddleware.spec.js +++ b/backend/src/middleware/validation/validationMiddleware.spec.js @@ -1,10 +1,9 @@ import { gql } from '../../helpers/jest' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { getNeode, getDriver } from '../../db/neo4j' import { createTestClient } from 'apollo-server-testing' import createServer from '../../server' -const factory = Factory() const neode = getNeode() const driver = getDriver() let authenticatedUser, @@ -94,14 +93,14 @@ beforeAll(() => { beforeEach(async () => { users = await Promise.all([ - factory.create('User', { + Factory.build('user', { id: 'reporting-user', }), - factory.create('User', { + Factory.build('user', { id: 'moderating-user', role: 'moderator', }), - factory.create('User', { + Factory.build('user', { id: 'commenting-user', }), ]) @@ -119,8 +118,8 @@ beforeEach(async () => { moderatingUser = users[1] commentingUser = users[2] const posts = await Promise.all([ - factory.create( - 'Post', + Factory.build( + 'post', { id: 'offensive-post', }, @@ -128,8 +127,8 @@ beforeEach(async () => { authorId: 'moderating-user', }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'post-4-commenting', }, @@ -142,7 +141,7 @@ beforeEach(async () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('validateCreateComment', () => { @@ -192,8 +191,8 @@ describe('validateCreateComment', () => { describe('validateUpdateComment', () => { let updateCommentVariables beforeEach(async () => { - await factory.create( - 'Comment', + await Factory.build( + 'comment', { id: 'comment-id', }, @@ -343,7 +342,7 @@ describe('validateReport', () => { describe('validateReview', () => { beforeEach(async () => { - const reportAgainstModerator = await factory.create('Report') + const reportAgainstModerator = await Factory.build('report') await Promise.all([ reportAgainstModerator.relateTo(reportingUser, 'filed', { ...reportVariables, @@ -385,7 +384,7 @@ describe('validateReview', () => { }) it('throws an error if a moderator tries to review their own resource(Post|Comment)', async () => { - const reportAgainstOffensivePost = await factory.create('Report') + const reportAgainstOffensivePost = await Factory.build('report') await Promise.all([ reportAgainstOffensivePost.relateTo(reportingUser, 'filed', { ...reportVariables, @@ -404,7 +403,7 @@ describe('validateReview', () => { describe('moderate a resource that is not a (Comment|Post|User) ', () => { beforeEach(async () => { - await Promise.all([factory.create('Tag', { id: 'tag-id' })]) + await Promise.all([Factory.build('tag', { id: 'tag-id' })]) }) it('returns null', async () => { @@ -434,7 +433,7 @@ describe('validateReview', () => { id: 'updating-user', name: 'John Doughnut', } - updatingUser = await factory.create('User', userParams) + updatingUser = await Factory.build('user', userParams) authenticatedUser = await updatingUser.toJson() }) diff --git a/backend/src/models/User.spec.js b/backend/src/models/User.spec.js index 7bdde7014..d112cde9a 100644 --- a/backend/src/models/User.spec.js +++ b/backend/src/models/User.spec.js @@ -1,11 +1,10 @@ -import Factory from '../factories' +import { cleanDatabase } from '../factories' import { getNeode } from '../db/neo4j' -const factory = Factory() const neode = getNeode() afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('role', () => { diff --git a/backend/src/schema/resolvers/comments.spec.js b/backend/src/schema/resolvers/comments.spec.js index bcc3c542f..80b9df814 100644 --- a/backend/src/schema/resolvers/comments.spec.js +++ b/backend/src/schema/resolvers/comments.spec.js @@ -1,4 +1,4 @@ -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { createTestClient } from 'apollo-server-testing' import createServer from '../../server' @@ -6,12 +6,11 @@ import { getNeode, getDriver } from '../../db/neo4j' const driver = getDriver() const neode = getNeode() -const factory = Factory() let variables, mutate, authenticatedUser, commentAuthor, newlyCreatedComment beforeAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() const { server } = createServer({ context: () => { return { @@ -33,7 +32,7 @@ beforeEach(async () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) const createCommentMutation = gql` @@ -48,9 +47,9 @@ const createCommentMutation = gql` } ` const setupPostAndComment = async () => { - commentAuthor = await factory.create('User') - await factory.create( - 'Post', + commentAuthor = await Factory.build('user') + await Factory.build( + 'post', { id: 'p1', content: 'Post to be commented', @@ -59,8 +58,8 @@ const setupPostAndComment = async () => { categoryIds: ['cat9'], }, ) - newlyCreatedComment = await factory.create( - 'Comment', + newlyCreatedComment = await Factory.build( + 'comment', { id: 'c456', content: 'Comment to be deleted', @@ -98,7 +97,7 @@ describe('CreateComment', () => { describe('given a post', () => { beforeEach(async () => { - await factory.create('Post', { id: 'p1' }, { categoryIds: ['cat9'] }) + await Factory.build('post', { id: 'p1' }, { categoryIds: ['cat9'] }) variables = { ...variables, postId: 'p1', @@ -151,7 +150,7 @@ describe('UpdateComment', () => { describe('authenticated but not the author', () => { beforeEach(async () => { - const randomGuy = await factory.create('User') + const randomGuy = await Factory.build('user') authenticatedUser = await randomGuy.toJson() }) @@ -243,7 +242,7 @@ describe('DeleteComment', () => { describe('authenticated but not the author', () => { beforeEach(async () => { - const randomGuy = await factory.create('User') + const randomGuy = await Factory.build('user') authenticatedUser = await randomGuy.toJson() }) diff --git a/backend/src/schema/resolvers/donations.spec.js b/backend/src/schema/resolvers/donations.spec.js index c382eb475..2888651b5 100644 --- a/backend/src/schema/resolvers/donations.spec.js +++ b/backend/src/schema/resolvers/donations.spec.js @@ -1,11 +1,10 @@ import { createTestClient } from 'apollo-server-testing' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' let mutate, query, authenticatedUser, variables -const factory = Factory() const instance = getNeode() const driver = getDriver() @@ -33,7 +32,7 @@ const donationsQuery = gql` describe('donations', () => { let currentUser, newlyCreatedDonations beforeAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() authenticatedUser = undefined const { server } = createServer({ context: () => { @@ -50,11 +49,11 @@ describe('donations', () => { beforeEach(async () => { variables = {} - newlyCreatedDonations = await factory.create('Donations') + newlyCreatedDonations = await Factory.build('donations') }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('query for donations', () => { @@ -68,7 +67,7 @@ describe('donations', () => { describe('authenticated', () => { beforeEach(async () => { - currentUser = await factory.create('User', { + currentUser = await Factory.build('user', { id: 'normal-user', role: 'user', }) @@ -102,7 +101,7 @@ describe('donations', () => { describe('authenticated', () => { describe('as a normal user', () => { beforeEach(async () => { - currentUser = await factory.create('User', { + currentUser = await Factory.build('user', { id: 'normal-user', role: 'user', }) @@ -121,7 +120,7 @@ describe('donations', () => { describe('as a moderator', () => { beforeEach(async () => { - currentUser = await factory.create('User', { + currentUser = await Factory.build('user', { id: 'moderator', role: 'moderator', }) @@ -140,7 +139,7 @@ describe('donations', () => { describe('as an admin', () => { beforeEach(async () => { - currentUser = await factory.create('User', { + currentUser = await Factory.build('user', { id: 'admin', role: 'admin', }) diff --git a/backend/src/schema/resolvers/emails.spec.js b/backend/src/schema/resolvers/emails.spec.js index 470eb7635..7ef018739 100644 --- a/backend/src/schema/resolvers/emails.spec.js +++ b/backend/src/schema/resolvers/emails.spec.js @@ -1,10 +1,9 @@ -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getDriver, getNeode } from '../../db/neo4j' import createServer from '../../server' import { createTestClient } from 'apollo-server-testing' -const factory = Factory() const neode = getNeode() let mutate @@ -31,7 +30,7 @@ beforeAll(() => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('AddEmailAddress', () => { @@ -63,7 +62,7 @@ describe('AddEmailAddress', () => { describe('authenticated', () => { beforeEach(async () => { - user = await factory.create('User', { id: '567' }, { email: 'user@example.org' }) + user = await Factory.build('user', { id: '567' }, { email: 'user@example.org' }) authenticatedUser = await user.toJson() }) @@ -110,7 +109,7 @@ describe('AddEmailAddress', () => { describe('if another `UnverifiedEmailAddress` node already exists with that email', () => { it('throws no unique constraint violation error', async () => { - await factory.create('UnverifiedEmailAddress', { + await Factory.build('unverifiedEmailAddress', { createdAt: '2019-09-24T14:00:01.565Z', email: 'new-email@example.org', }) @@ -128,7 +127,7 @@ describe('AddEmailAddress', () => { describe('but if another user owns an `EmailAddress` already with that email', () => { it('throws UserInputError because of unique constraints', async () => { - await factory.create('User', {}, { email: 'new-email@example.org' }) + await Factory.build('user', {}, { email: 'new-email@example.org' }) await expect(mutate({ mutation, variables })).resolves.toMatchObject({ data: { AddEmailAddress: null }, errors: [{ message: 'A user account with this email already exists.' }], @@ -169,7 +168,7 @@ describe('VerifyEmailAddress', () => { describe('authenticated', () => { beforeEach(async () => { - user = await factory.create('User', { id: '567' }, { email: 'user@example.org' }) + user = await Factory.build('user', { id: '567' }, { email: 'user@example.org' }) authenticatedUser = await user.toJson() }) @@ -185,7 +184,7 @@ describe('VerifyEmailAddress', () => { describe('given a `UnverifiedEmailAddress`', () => { let emailAddress beforeEach(async () => { - emailAddress = await factory.create('UnverifiedEmailAddress', { + emailAddress = await Factory.build('unverifiedEmailAddress', { nonce: 'abcdef', verifiedAt: null, createdAt: new Date().toISOString(), @@ -281,7 +280,7 @@ describe('VerifyEmailAddress', () => { describe('Edge case: In the meantime someone created an `EmailAddress` node with the given email', () => { beforeEach(async () => { - await factory.create('EmailAddress', { email: 'to-be-verified@example.org' }) + await Factory.build('emailAddress', { email: 'to-be-verified@example.org' }) }) it('throws UserInputError because of unique constraints', async () => { diff --git a/backend/src/schema/resolvers/follow.spec.js b/backend/src/schema/resolvers/follow.spec.js index d7f5e3113..9bfdaf3a4 100644 --- a/backend/src/schema/resolvers/follow.spec.js +++ b/backend/src/schema/resolvers/follow.spec.js @@ -1,10 +1,9 @@ import { createTestClient } from 'apollo-server-testing' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { getDriver, getNeode } from '../../db/neo4j' import createServer from '../../server' import { gql } from '../../helpers/jest' -const factory = Factory() const driver = getDriver() const neode = getNeode() @@ -54,7 +53,7 @@ const userQuery = gql` ` beforeAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() const { server } = createServer({ context: () => ({ driver, @@ -72,39 +71,35 @@ beforeAll(async () => { }) beforeEach(async () => { - user1 = await factory - .create( - 'User', - { - id: 'u1', - name: 'user1', - }, - { - email: 'test@example.org', - password: '1234', - }, - ) - .then(user => user.toJson()) - user2 = await factory - .create( - 'User', - { - id: 'u2', - name: 'user2', - }, - { - email: 'test2@example.org', - password: '1234', - }, - ) - .then(user => user.toJson()) + user1 = await Factory.build( + 'user', + { + id: 'u1', + name: 'user1', + }, + { + email: 'test@example.org', + password: '1234', + }, + ).then(user => user.toJson()) + user2 = await Factory.build( + 'user', + { + id: 'u2', + name: 'user2', + }, + { + email: 'test2@example.org', + password: '1234', + }, + ).then(user => user.toJson()) authenticatedUser = user1 variables = { id: user2.id } }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('follow', () => { diff --git a/backend/src/schema/resolvers/locations.spec.js b/backend/src/schema/resolvers/locations.spec.js index aba11f9bc..6e2046132 100644 --- a/backend/src/schema/resolvers/locations.spec.js +++ b/backend/src/schema/resolvers/locations.spec.js @@ -1,11 +1,9 @@ -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' import { createTestClient } from 'apollo-server-testing' -const factory = Factory() - let mutate, authenticatedUser const driver = getDriver() @@ -25,7 +23,7 @@ beforeAll(() => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('resolvers', () => { @@ -49,7 +47,7 @@ describe('resolvers', () => { id: 'u47', name: 'John Doughnut', } - const Paris = await factory.create('Location', { + const Paris = await Factory.build('location', { id: 'region.9397217726497330', name: 'Paris', type: 'region', @@ -58,7 +56,7 @@ describe('resolvers', () => { nameEN: 'Paris', }) - const user = await factory.create('User', { + const user = await Factory.build('user', { id: 'u47', name: 'John Doe', }) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 856f094d1..8ebce9811 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -1,10 +1,9 @@ import { createTestClient } from 'apollo-server-testing' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' -const factory = Factory() const neode = getNeode() const driver = getDriver() @@ -54,7 +53,7 @@ const reviewMutation = gql` describe('moderate resources', () => { beforeAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() authenticatedUser = undefined const { server } = createServer({ context: () => { @@ -80,8 +79,8 @@ describe('moderate resources', () => { closed: false, } authenticatedUser = null - moderator = await factory.create( - 'User', + moderator = await Factory.build( + 'user', { id: 'moderator-id', name: 'Moderator', @@ -92,8 +91,8 @@ describe('moderate resources', () => { password: '1234', }, ) - nonModerator = await factory.create( - 'User', + nonModerator = await Factory.build( + 'user', { id: 'non-moderator', name: 'Non Moderator', @@ -106,7 +105,7 @@ describe('moderate resources', () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('review to close report, leaving resource enabled', () => { @@ -137,10 +136,10 @@ describe('moderate resources', () => { describe('moderator', () => { beforeEach(async () => { authenticatedUser = await moderator.toJson() - const questionablePost = await factory.create('Post', { + const questionablePost = await Factory.build('post', { id: 'should-i-be-disabled', }) - const reportAgainstQuestionablePost = await factory.create('Report') + const reportAgainstQuestionablePost = await Factory.build('report') await Promise.all([ reportAgainstQuestionablePost.relateTo(nonModerator, 'filed', { resourceId: 'should-i-be-disabled', @@ -239,10 +238,10 @@ describe('moderate resources', () => { describe('moderate a comment', () => { beforeEach(async () => { - const trollingComment = await factory.create('Comment', { + const trollingComment = await Factory.build('comment', { id: 'comment-id', }) - const reportAgainstTrollingComment = await factory.create('Report') + const reportAgainstTrollingComment = await Factory.build('report') await Promise.all([ reportAgainstTrollingComment.relateTo(nonModerator, 'filed', { resourceId: 'comment-id', @@ -317,10 +316,10 @@ describe('moderate resources', () => { describe('moderate a post', () => { beforeEach(async () => { - const trollingPost = await factory.create('Post', { + const trollingPost = await Factory.build('post', { id: 'post-id', }) - const reportAgainstTrollingPost = await factory.create('Report') + const reportAgainstTrollingPost = await Factory.build('report') await Promise.all([ reportAgainstTrollingPost.relateTo(nonModerator, 'filed', { resourceId: 'post-id', @@ -397,10 +396,10 @@ describe('moderate resources', () => { describe('moderate a user', () => { beforeEach(async () => { - const troll = await factory.create('User', { + const troll = await Factory.build('user', { id: 'user-id', }) - const reportAgainstTroll = await factory.create('Report') + const reportAgainstTroll = await Factory.build('report') await Promise.all([ reportAgainstTroll.relateTo(nonModerator, 'filed', { resourceId: 'user-id', @@ -514,10 +513,10 @@ describe('moderate resources', () => { describe('moderate a comment', () => { beforeEach(async () => { - const trollingComment = await factory.create('Comment', { + const trollingComment = await Factory.build('comment', { id: 'comment-id', }) - const reportAgainstTrollingComment = await factory.create('Report') + const reportAgainstTrollingComment = await Factory.build('report') await Promise.all([ reportAgainstTrollingComment.relateTo(nonModerator, 'filed', { resourceId: 'comment-id', @@ -578,10 +577,10 @@ describe('moderate resources', () => { describe('moderate a post', () => { beforeEach(async () => { - const trollingPost = await factory.create('Post', { + const trollingPost = await Factory.build('post', { id: 'post-id', }) - const reportAgainstTrollingPost = await factory.create('Report') + const reportAgainstTrollingPost = await Factory.build('report') await Promise.all([ reportAgainstTrollingPost.relateTo(nonModerator, 'filed', { resourceId: 'post-id', @@ -643,10 +642,10 @@ describe('moderate resources', () => { describe('moderate a user', () => { beforeEach(async () => { - const troll = await factory.create('User', { + const troll = await Factory.build('user', { id: 'user-id', }) - const reportAgainstTroll = await factory.create('Report') + const reportAgainstTroll = await Factory.build('report') await Promise.all([ reportAgainstTroll.relateTo(nonModerator, 'filed', { resourceId: 'user-id', diff --git a/backend/src/schema/resolvers/notifications.spec.js b/backend/src/schema/resolvers/notifications.spec.js index 1ce95ddd4..d2eb9b88c 100644 --- a/backend/src/schema/resolvers/notifications.spec.js +++ b/backend/src/schema/resolvers/notifications.spec.js @@ -1,10 +1,9 @@ -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getDriver } from '../../db/neo4j' import { createTestClient } from 'apollo-server-testing' import createServer from '../.././server' -const factory = Factory() const driver = getDriver() let authenticatedUser let user @@ -32,22 +31,22 @@ beforeEach(async () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('given some notifications', () => { beforeEach(async () => { const categoryIds = ['cat1'] - author = await factory.create('User', { id: 'author' }) - user = await factory.create('User', { id: 'you' }) + author = await Factory.build('user', { id: 'author' }) + user = await Factory.build('user', { id: 'you' }) const [neighbor] = await Promise.all([ - factory.create('User', { id: 'neighbor' }), - factory.create('Category', { id: 'cat1' }), + Factory.build('user', { id: 'neighbor' }), + Factory.build('category', { id: 'cat1' }), ]) const [post1, post2, post3] = await Promise.all([ - factory.create('Post', { id: 'p1', content: 'Not for you' }, { author, categoryIds }), - factory.create( - 'Post', + Factory.build('post', { id: 'p1', content: 'Not for you' }, { author, categoryIds }), + Factory.build( + 'post', { id: 'p2', content: 'Already seen post mention', @@ -57,8 +56,8 @@ describe('given some notifications', () => { categoryIds, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'p3', content: 'You have been mentioned in a post', @@ -70,8 +69,8 @@ describe('given some notifications', () => { ), ]) const [comment1, comment2, comment3] = await Promise.all([ - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c1', content: 'You have seen this comment mentioning already', @@ -81,8 +80,8 @@ describe('given some notifications', () => { postId: 'p3', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c2', content: 'You have been mentioned in a comment', @@ -92,8 +91,8 @@ describe('given some notifications', () => { postId: 'p3', }, ), - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'c3', content: 'Somebody else was mentioned in a comment', diff --git a/backend/src/schema/resolvers/passwordReset.spec.js b/backend/src/schema/resolvers/passwordReset.spec.js index 97fba47f4..0d297a625 100644 --- a/backend/src/schema/resolvers/passwordReset.spec.js +++ b/backend/src/schema/resolvers/passwordReset.spec.js @@ -1,4 +1,4 @@ -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createPasswordReset from './helpers/createPasswordReset' @@ -7,7 +7,6 @@ import { createTestClient } from 'apollo-server-testing' const neode = getNeode() const driver = getDriver() -const factory = Factory() let mutate let authenticatedUser @@ -39,14 +38,14 @@ beforeAll(() => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('passwordReset', () => { describe('given a user', () => { beforeEach(async () => { - await factory.create( - 'User', + await Factory.build( + 'user', {}, { email: 'user@example.org', @@ -127,8 +126,8 @@ describe('resetPassword', () => { describe('given a user', () => { beforeEach(async () => { - await factory.create( - 'User', + await Factory.build( + 'user', { role: 'user', }, diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index b8fe25ec3..f6471035a 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -1,11 +1,10 @@ import { createTestClient } from 'apollo-server-testing' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' const driver = getDriver() -const factory = Factory() const neode = getNeode() let query @@ -40,7 +39,7 @@ const createPostMutation = gql` ` beforeAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() const { server } = createServer({ context: () => { return { @@ -56,8 +55,8 @@ beforeAll(async () => { beforeEach(async () => { variables = {} - user = await factory.create( - 'User', + user = await Factory.build( + 'user', { id: 'current-user', name: 'TestUser', @@ -93,7 +92,7 @@ beforeEach(async () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('Post', () => { @@ -101,8 +100,8 @@ describe('Post', () => { let followedUser, happyPost, cryPost beforeEach(async () => { ;[followedUser] = await Promise.all([ - factory.create( - 'User', + Factory.build( + 'user', { id: 'followed-by-me', name: 'Followed User', @@ -114,10 +113,10 @@ describe('Post', () => { ), ]) ;[happyPost, cryPost] = await Promise.all([ - factory.create('Post', { id: 'happy-post' }, { categoryIds: ['cat4'] }), - factory.create('Post', { id: 'cry-post' }, { categoryIds: ['cat15'] }), - factory.create( - 'Post', + Factory.build('post', { id: 'happy-post' }, { categoryIds: ['cat4'] }), + Factory.build('post', { id: 'cry-post' }, { categoryIds: ['cat15'] }), + Factory.build( + 'post', { id: 'post-by-followed-user', }, @@ -355,9 +354,9 @@ describe('UpdatePost', () => { } ` beforeEach(async () => { - author = await factory.create('User', { slug: 'the-author' }) - newlyCreatedPost = await factory.create( - 'Post', + author = await Factory.build('user', { slug: 'the-author' }) + newlyCreatedPost = await Factory.build( + 'post', { id: 'p9876', title: 'Old title', @@ -549,8 +548,8 @@ describe('UpdatePost', () => { describe('are allowed to pin posts', () => { beforeEach(async () => { - await factory.create( - 'Post', + await Factory.build( + 'post', { id: 'created-and-pinned-by-same-admin', }, @@ -614,13 +613,13 @@ describe('UpdatePost', () => { describe('post created by another admin', () => { let otherAdmin beforeEach(async () => { - otherAdmin = await factory.create('User', { + otherAdmin = await Factory.build('user', { role: 'admin', name: 'otherAdmin', }) authenticatedUser = await otherAdmin.toJson() - await factory.create( - 'Post', + await Factory.build( + 'post', { id: 'created-by-one-admin-pinned-by-different-one', }, @@ -684,8 +683,8 @@ describe('UpdatePost', () => { describe('pinned post already exists', () => { let pinnedPost beforeEach(async () => { - await factory.create( - 'Post', + await Factory.build( + 'post', { id: 'only-pinned-post', }, @@ -718,12 +717,12 @@ describe('UpdatePost', () => { describe('PostOrdering', () => { beforeEach(async () => { - await factory.create('Post', { + await Factory.build('post', { id: 'im-a-pinned-post', createdAt: '2019-11-22T17:26:29.070Z', pinned: true, }) - await factory.create('Post', { + await Factory.build('post', { id: 'i-was-created-before-pinned-post', // fairly old, so this should be 3rd createdAt: '2019-10-22T17:26:29.070Z', @@ -842,7 +841,7 @@ describe('UpdatePost', () => { describe('admin can unpin posts', () => { let admin, pinnedPost beforeEach(async () => { - pinnedPost = await factory.create('Post', { id: 'post-to-be-unpinned' }) + pinnedPost = await Factory.build('post', { id: 'post-to-be-unpinned' }) admin = await user.update({ role: 'admin', name: 'Admin', @@ -909,9 +908,9 @@ describe('DeletePost', () => { ` beforeEach(async () => { - author = await factory.create('User') - await factory.create( - 'Post', + author = await Factory.build('user') + await Factory.build( + 'post', { id: 'p4711', title: 'I will be deleted', @@ -969,8 +968,8 @@ describe('DeletePost', () => { describe('if there are comments on the post', () => { beforeEach(async () => { - await factory.create( - 'Comment', + await Factory.build( + 'comment', { content: 'to be deleted comment content', contentExcerpt: 'to be deleted comment content', @@ -1033,8 +1032,8 @@ describe('emotions', () => { beforeEach(async () => { author = await neode.create('User', { id: 'u257' }) - postToEmote = await factory.create( - 'Post', + postToEmote = await Factory.build( + 'post', { id: 'p1376', }, diff --git a/backend/src/schema/resolvers/registration.spec.js b/backend/src/schema/resolvers/registration.spec.js index 36514bf4d..5543d0770 100644 --- a/backend/src/schema/resolvers/registration.spec.js +++ b/backend/src/schema/resolvers/registration.spec.js @@ -1,11 +1,9 @@ -import Factory from '../../factories' -import { Factory as RosieFactory } from 'rosie' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getDriver, getNeode } from '../../db/neo4j' import createServer from '../../server' import { createTestClient } from 'apollo-server-testing' -const factory = Factory() const neode = getNeode() let mutate @@ -31,7 +29,7 @@ beforeAll(() => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('Signup', () => { @@ -59,8 +57,8 @@ describe('Signup', () => { describe('as admin', () => { beforeEach(async () => { - const admin = await factory.create( - 'User', + const admin = await Factory.build( + 'user', { role: 'admin', }, @@ -98,7 +96,7 @@ describe('Signup', () => { describe('if the email already exists', () => { let emailAddress beforeEach(async () => { - emailAddress = await factory.create('EmailAddress', { + emailAddress = await Factory.build('emailAddress', { email: 'someuser@example.org', verifiedAt: null, }) @@ -106,7 +104,7 @@ describe('Signup', () => { describe('and the user has registered already', () => { beforeEach(async () => { - const user = await RosieFactory.build('userWithoutEmailAddress') + const user = await Factory.build('userWithoutEmailAddress') await emailAddress.relateTo(user, 'belongsTo') }) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 9cd4c1279..34f46c4fa 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -1,10 +1,9 @@ import { createTestClient } from 'apollo-server-testing' import createServer from '../.././server' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getDriver, getNeode } from '../../db/neo4j' -const factory = Factory() const instance = getNeode() const driver = getDriver() @@ -53,7 +52,7 @@ describe('file a report on a resource', () => { } beforeAll(async () => { - await factory.cleanDatabase() + await cleanDatabase() const { server } = createServer({ context: () => { return { @@ -68,7 +67,7 @@ describe('file a report on a resource', () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('report a resource', () => { @@ -84,8 +83,8 @@ describe('file a report on a resource', () => { describe('authenticated', () => { beforeEach(async () => { - currentUser = await factory.create( - 'User', + currentUser = await Factory.build( + 'user', { id: 'current-user-id', role: 'user', @@ -95,8 +94,8 @@ describe('file a report on a resource', () => { password: '1234', }, ) - otherReportingUser = await factory.create( - 'User', + otherReportingUser = await Factory.build( + 'user', { id: 'other-reporting-user-id', role: 'user', @@ -106,8 +105,8 @@ describe('file a report on a resource', () => { password: '1234', }, ) - await factory.create( - 'User', + await Factory.build( + 'user', { id: 'abusive-user-id', role: 'user', @@ -356,8 +355,8 @@ describe('file a report on a resource', () => { describe('reported resource is a post', () => { beforeEach(async () => { - await factory.create( - 'Post', + await Factory.build( + 'post', { id: 'post-to-report-id', title: 'This is a post that is going to be reported', @@ -415,8 +414,8 @@ describe('file a report on a resource', () => { describe('reported resource is a comment', () => { beforeEach(async () => { - await factory.create( - 'Post', + await Factory.build( + 'post', { id: 'p1', title: 'post to comment on', @@ -427,8 +426,8 @@ describe('file a report on a resource', () => { author: currentUser, }, ) - await factory.create( - 'Comment', + await Factory.build( + 'comment', { id: 'comment-to-report-id', content: 'Post comment to be reported.', @@ -486,7 +485,7 @@ describe('file a report on a resource', () => { describe('reported resource is a tag', () => { beforeEach(async () => { - await factory.create('Tag', { + await Factory.build('tag', { id: 'tag-to-report-id', }) }) @@ -544,8 +543,8 @@ describe('file a report on a resource', () => { beforeEach(async () => { authenticatedUser = null - moderator = await factory.create( - 'User', + moderator = await Factory.build( + 'user', { id: 'moderator-1', role: 'moderator', @@ -555,8 +554,8 @@ describe('file a report on a resource', () => { password: '1234', }, ) - currentUser = await factory.create( - 'User', + currentUser = await Factory.build( + 'user', { id: 'current-user-id', role: 'user', @@ -566,8 +565,8 @@ describe('file a report on a resource', () => { password: '1234', }, ) - abusiveUser = await factory.create( - 'User', + abusiveUser = await Factory.build( + 'user', { id: 'abusive-user-1', role: 'user', @@ -584,8 +583,8 @@ describe('file a report on a resource', () => { }) await Promise.all([ - factory.create( - 'Post', + Factory.build( + 'post', { id: 'abusive-post-1', content: 'Interesting Knowledge', @@ -595,8 +594,8 @@ describe('file a report on a resource', () => { author: abusiveUser, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'post-2', content: 'More things to do …', @@ -606,8 +605,8 @@ describe('file a report on a resource', () => { categoryIds, }, ), - factory.create( - 'Post', + Factory.build( + 'post', { id: 'post-3', content: 'I am at school …', @@ -619,8 +618,8 @@ describe('file a report on a resource', () => { ), ]) await Promise.all([ - factory.create( - 'Comment', + Factory.build( + 'comment', { id: 'abusive-comment-1', }, diff --git a/backend/src/schema/resolvers/rewards.spec.js b/backend/src/schema/resolvers/rewards.spec.js index ba5c2dc42..df15f4682 100644 --- a/backend/src/schema/resolvers/rewards.spec.js +++ b/backend/src/schema/resolvers/rewards.spec.js @@ -1,10 +1,9 @@ import { createTestClient } from 'apollo-server-testing' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' -const factory = Factory() const driver = getDriver() const instance = getNeode() @@ -31,8 +30,8 @@ describe('rewards', () => { }) beforeEach(async () => { - regularUser = await factory.create( - 'User', + regularUser = await Factory.build( + 'user', { id: 'regular-user-id', role: 'user', @@ -42,8 +41,8 @@ describe('rewards', () => { password: '1234', }, ) - moderator = await factory.create( - 'User', + moderator = await Factory.build( + 'user', { id: 'moderator-id', role: 'moderator', @@ -52,8 +51,8 @@ describe('rewards', () => { email: 'moderator@example.org', }, ) - administrator = await factory.create( - 'User', + administrator = await Factory.build( + 'user', { id: 'admin-id', role: 'admin', @@ -62,7 +61,7 @@ describe('rewards', () => { email: 'admin@example.org', }, ) - badge = await factory.create('Badge', { + badge = await Factory.build('badge', { id: 'indiegogo_en_rhino', type: 'crowdfunding', status: 'permanent', @@ -71,7 +70,7 @@ describe('rewards', () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('reward', () => { @@ -145,7 +144,7 @@ describe('rewards', () => { }) it('rewards a second different badge to same user', async () => { - await factory.create('Badge', { + await Factory.build('badge', { id: 'indiegogo_en_racoon', icon: '/img/badges/indiegogo_en_racoon.svg', }) @@ -187,8 +186,8 @@ describe('rewards', () => { }, errors: undefined, } - await factory.create( - 'User', + await Factory.build( + 'user', { id: 'regular-user-2-id', }, diff --git a/backend/src/schema/resolvers/shout.spec.js b/backend/src/schema/resolvers/shout.spec.js index dfe14f875..9fa595db7 100644 --- a/backend/src/schema/resolvers/shout.spec.js +++ b/backend/src/schema/resolvers/shout.spec.js @@ -1,11 +1,10 @@ import { createTestClient } from 'apollo-server-testing' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' let mutate, query, authenticatedUser, variables -const factory = Factory() const instance = getNeode() const driver = getDriver() @@ -47,8 +46,8 @@ describe('shout and unshout posts', () => { query = createTestClient(server).query }) beforeEach(async () => { - currentUser = await factory.create( - 'User', + currentUser = await Factory.build( + 'user', { id: 'current-user-id', name: 'Current User', @@ -59,8 +58,8 @@ describe('shout and unshout posts', () => { }, ) - postAuthor = await factory.create( - 'User', + postAuthor = await Factory.build( + 'user', { id: 'id-of-another-user', name: 'Another User', @@ -72,7 +71,7 @@ describe('shout and unshout posts', () => { ) }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('shout', () => { @@ -88,8 +87,8 @@ describe('shout and unshout posts', () => { describe('authenticated', () => { beforeEach(async () => { authenticatedUser = await currentUser.toJson() - await factory.create( - 'Post', + await Factory.build( + 'post', { name: 'Other user post', id: 'another-user-post-id', @@ -98,8 +97,8 @@ describe('shout and unshout posts', () => { author: postAuthor, }, ) - await factory.create( - 'Post', + await Factory.build( + 'post', { name: 'current user post', id: 'current-user-post-id', @@ -164,8 +163,8 @@ describe('shout and unshout posts', () => { describe('authenticated', () => { beforeEach(async () => { authenticatedUser = await currentUser.toJson() - await factory.create( - 'Post', + await Factory.build( + 'post', { name: 'Posted By Another User', id: 'posted-by-another-user', diff --git a/backend/src/schema/resolvers/socialMedia.spec.js b/backend/src/schema/resolvers/socialMedia.spec.js index 05bb13eb4..dd8b71580 100644 --- a/backend/src/schema/resolvers/socialMedia.spec.js +++ b/backend/src/schema/resolvers/socialMedia.spec.js @@ -1,11 +1,10 @@ import { createTestClient } from 'apollo-server-testing' import createServer from '../../server' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getDriver } from '../../db/neo4j' const driver = getDriver() -const factory = Factory() describe('SocialMedia', () => { let socialMediaAction, someUser, ownerNode, owner @@ -14,14 +13,14 @@ describe('SocialMedia', () => { const newUrl = 'https://twitter.com/bullerby' const setUpSocialMedia = async () => { - const socialMediaNode = await factory.create('SocialMedia', { url }) + const socialMediaNode = await Factory.build('socialMedia', { url }) await socialMediaNode.relateTo(ownerNode, 'ownedBy') return socialMediaNode.toJson() } beforeEach(async () => { - const someUserNode = await factory.create( - 'User', + const someUserNode = await Factory.build( + 'user', { name: 'Kalle Blomqvist', }, @@ -32,8 +31,8 @@ describe('SocialMedia', () => { ) someUser = await someUserNode.toJson() - ownerNode = await factory.create( - 'User', + ownerNode = await Factory.build( + 'user', { name: 'Pippi Langstrumpf', }, @@ -63,7 +62,7 @@ describe('SocialMedia', () => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('create social media', () => { diff --git a/backend/src/schema/resolvers/statistics.spec.js b/backend/src/schema/resolvers/statistics.spec.js index e2b9dafe4..2ef1780ef 100644 --- a/backend/src/schema/resolvers/statistics.spec.js +++ b/backend/src/schema/resolvers/statistics.spec.js @@ -1,11 +1,10 @@ import { createTestClient } from 'apollo-server-testing' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' let query, authenticatedUser -const factory = Factory() const instance = getNeode() const driver = getDriver() @@ -37,7 +36,7 @@ beforeAll(() => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('statistics', () => { @@ -45,7 +44,7 @@ describe('statistics', () => { beforeEach(async () => { await Promise.all( [...Array(6).keys()].map(() => { - return factory.create('User') + return Factory.build('user') }), ) }) @@ -62,7 +61,7 @@ describe('statistics', () => { beforeEach(async () => { await Promise.all( [...Array(3).keys()].map(() => { - return factory.create('Post') + return Factory.build('post') }), ) }) @@ -79,7 +78,7 @@ describe('statistics', () => { beforeEach(async () => { await Promise.all( [...Array(2).keys()].map(() => { - return factory.create('Comment') + return Factory.build('comment') }), ) }) @@ -97,7 +96,7 @@ describe('statistics', () => { beforeEach(async () => { users = await Promise.all( [...Array(2).keys()].map(() => { - return factory.create('User') + return Factory.build('user') }), ) await users[0].relateTo(users[1], 'following') @@ -116,12 +115,12 @@ describe('statistics', () => { beforeEach(async () => { users = await Promise.all( [...Array(2).keys()].map(() => { - return factory.create('User') + return Factory.build('user') }), ) posts = await Promise.all( [...Array(3).keys()].map(() => { - return factory.create('Post') + return Factory.build('post') }), ) await Promise.all([ diff --git a/backend/src/schema/resolvers/user_management.spec.js b/backend/src/schema/resolvers/user_management.spec.js index 27bc84b10..dd304077b 100644 --- a/backend/src/schema/resolvers/user_management.spec.js +++ b/backend/src/schema/resolvers/user_management.spec.js @@ -1,20 +1,19 @@ import jwt from 'jsonwebtoken' import CONFIG from './../../config' -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { createTestClient } from 'apollo-server-testing' import createServer, { context } from '../../server' import encode from '../../jwt/encode' import { getNeode } from '../../db/neo4j' -const factory = Factory() const neode = getNeode() let query, mutate, variables, req, user const disable = async id => { - const moderator = await factory.create('User', { id: 'u2', role: 'moderator' }) + const moderator = await Factory.build('user', { id: 'u2', role: 'moderator' }) const user = await neode.find('User', id) - const reportAgainstUser = await factory.create('Report') + const reportAgainstUser = await Factory.build('report') await Promise.all([ reportAgainstUser.relateTo(moderator, 'filed', { resourceId: id, @@ -48,7 +47,7 @@ beforeAll(() => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('isLoggedIn', () => { @@ -69,7 +68,7 @@ describe('isLoggedIn', () => { describe('authenticated', () => { beforeEach(async () => { - user = await factory.create('User', { id: 'u3' }) + user = await Factory.build('user', { id: 'u3' }) const userBearerToken = encode({ id: 'u3' }) req = { headers: { authorization: `Bearer ${userBearerToken}` } } }) @@ -127,8 +126,8 @@ describe('currentUser', () => { describe('authenticated', () => { describe('and corresponding user in the database', () => { beforeEach(async () => { - await factory.create( - 'User', + await Factory.build( + 'user', { id: 'u3', // the `id` is the only thing that has to match the decoded JWT bearer token @@ -177,8 +176,8 @@ describe('login', () => { beforeEach(async () => { variables = { email: 'test@example.org', password: '1234' } - user = await factory.create( - 'User', + user = await Factory.build( + 'user', { id: 'acb2d923-f3af-479e-9f00-61b12e864666', }, @@ -303,7 +302,7 @@ describe('change password', () => { describe('authenticated', () => { beforeEach(async () => { - await factory.create('User', { id: 'u3' }) + await Factory.build('user', { id: 'u3' }) const userBearerToken = encode({ id: 'u3' }) req = { headers: { authorization: `Bearer ${userBearerToken}` } } }) diff --git a/backend/src/schema/resolvers/users.spec.js b/backend/src/schema/resolvers/users.spec.js index 3806319f3..7d5cab853 100644 --- a/backend/src/schema/resolvers/users.spec.js +++ b/backend/src/schema/resolvers/users.spec.js @@ -1,10 +1,9 @@ -import Factory from '../../factories' +import Factory, { cleanDatabase } from '../../factories' import { gql } from '../../helpers/jest' import { getNeode, getDriver } from '../../db/neo4j' import createServer from '../../server' import { createTestClient } from 'apollo-server-testing' -const factory = Factory() const categoryIds = ['cat9'] let user @@ -31,13 +30,13 @@ beforeAll(() => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('User', () => { describe('query by email address', () => { beforeEach(async () => { - await factory.create('User', { name: 'Johnny' }, { email: 'any-email-address@example.org' }) + await Factory.build('user', { name: 'Johnny' }, { email: 'any-email-address@example.org' }) }) const userQuery = gql` @@ -57,8 +56,8 @@ describe('User', () => { describe('as admin', () => { beforeEach(async () => { - const admin = await factory.create( - 'User', + const admin = await Factory.build( + 'user', { role: 'admin', }, @@ -128,8 +127,8 @@ describe('UpdateUser', () => { ` beforeEach(async () => { - user = await factory.create( - 'User', + user = await Factory.build( + 'user', { id: 'u47', name: 'John Doe', @@ -145,8 +144,8 @@ describe('UpdateUser', () => { describe('as another user', () => { beforeEach(async () => { - const someoneElse = await factory.create( - 'User', + const someoneElse = await Factory.build( + 'user', { name: 'James Doe', }, @@ -277,13 +276,13 @@ describe('DeleteUser', () => { beforeEach(async () => { variables = { id: ' u343', resource: [] } - user = await factory.create('User', { + user = await Factory.build('user', { name: 'My name should be deleted', about: 'along with my about', id: 'u343', }) - await factory.create( - 'User', + await Factory.build( + 'user', { id: 'not-my-account', }, @@ -323,13 +322,13 @@ describe('DeleteUser', () => { describe('given posts and comments', () => { beforeEach(async () => { - await factory.create('Category', { + await Factory.build('category', { id: 'cat9', name: 'Democracy & Politics', icon: 'university', }) - await factory.create( - 'Post', + await Factory.build( + 'post', { id: 'p139', content: 'Post by user u343', @@ -339,8 +338,8 @@ describe('DeleteUser', () => { categoryIds, }, ) - await factory.create( - 'Comment', + await Factory.build( + 'comment', { id: 'c155', content: 'Comment by user u343', @@ -349,8 +348,8 @@ describe('DeleteUser', () => { author: user, }, ) - await factory.create( - 'Comment', + await Factory.build( + 'comment', { id: 'c156', content: "A comment by someone else on user u343's post", @@ -556,7 +555,7 @@ describe('DeleteUser', () => { describe('connected `SocialMedia` nodes', () => { beforeEach(async () => { - const socialMedia = await factory.create('SocialMedia') + const socialMedia = await Factory.build('socialMedia') await socialMedia.relateTo(user, 'ownedBy') }) diff --git a/backend/src/schema/resolvers/users/location.spec.js b/backend/src/schema/resolvers/users/location.spec.js index 85c0f091f..824e03c2d 100644 --- a/backend/src/schema/resolvers/users/location.spec.js +++ b/backend/src/schema/resolvers/users/location.spec.js @@ -1,10 +1,9 @@ import { gql } from '../../../helpers/jest' -import Factory from '../../../factories' +import Factory, { cleanDatabase } from '../../../factories' import { getNeode, getDriver } from '../../../db/neo4j' import { createTestClient } from 'apollo-server-testing' import createServer from '../../../server' -const factory = Factory() const neode = getNeode() const driver = getDriver() let authenticatedUser, mutate, variables @@ -107,7 +106,7 @@ beforeEach(() => { }) afterEach(() => { - factory.cleanDatabase() + cleanDatabase() }) describe('userMiddleware', () => { @@ -182,7 +181,7 @@ describe('userMiddleware', () => { }, }, ] - user = await factory.create('User', { + user = await Factory.build('user', { id: 'updating-user', }) authenticatedUser = await user.toJson() diff --git a/backend/src/schema/resolvers/users/mutedUsers.spec.js b/backend/src/schema/resolvers/users/mutedUsers.spec.js index 130df08ce..68b26941d 100644 --- a/backend/src/schema/resolvers/users/mutedUsers.spec.js +++ b/backend/src/schema/resolvers/users/mutedUsers.spec.js @@ -1,11 +1,10 @@ import { createTestClient } from 'apollo-server-testing' import createServer from '../../../server' -import Factory from '../../../factories' +import { cleanDatabase } from '../../../factories' import { gql } from '../../../helpers/jest' import { getNeode, getDriver } from '../../../db/neo4j' const driver = getDriver() -const factory = Factory() const neode = getNeode() let currentUser @@ -30,7 +29,7 @@ beforeEach(() => { }) afterEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) describe('mutedUsers', () => { diff --git a/backend/test/features/support/steps.js b/backend/test/features/support/steps.js index ff9d45b27..4e9268818 100644 --- a/backend/test/features/support/steps.js +++ b/backend/test/features/support/steps.js @@ -6,12 +6,11 @@ import { GraphQLClient } from 'graphql-request' import Factory from '../../../src/factories' const debug = require('debug')('ea:test:steps') -const factory = Factory() const client = new GraphQLClient(host) function createUser (slug) { debug(`creating user ${slug}`) - return factory.create('User', { + return Factory.build('user', { name: slug, }, { password: '1234' diff --git a/cypress/integration/common/report.js b/cypress/integration/common/report.js index 6b808c75f..664d71d1b 100644 --- a/cypress/integration/common/report.js +++ b/cypress/integration/common/report.js @@ -30,7 +30,7 @@ Given("I see David Irving's post on the post page", page => { }) Given('I am logged in with a {string} role', role => { - cy.factory().create('User', { + cy.factory().build('user', { termsAndConditionsAgreedVersion: VERSION, role }, { @@ -128,7 +128,7 @@ Given('somebody reported the following posts:', table => { password: '1234' } cy.factory() - .create('User', {}, submitter) + .build('user', {}, submitter) .authenticateAs(submitter) .mutate(gql`mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { fileReport(resourceId: $resourceId, reasonCategory: $reasonCategory, reasonDescription: $reasonDescription) { @@ -167,8 +167,9 @@ Then('I can visit the post page', () => { When("they have a post someone has reported", () => { cy.factory() - .create("Post", { - authorId: 'annnoying-user', + .build("post", { title, + }, { + authorId: 'annnoying-user', }); }) diff --git a/cypress/integration/common/steps.js b/cypress/integration/common/steps.js index 3867b3513..a0c3f30ae 100644 --- a/cypress/integration/common/steps.js +++ b/cypress/integration/common/steps.js @@ -25,14 +25,12 @@ const narratorParams = { name: "Peter Pan", slug: "peter-pan", avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/nerrsoft/128.jpg", - ...loginCredentials, ...termsAndConditionsAgreedVersion, }; const annoyingParams = { email: "spammy-spammer@example.org", password: "1234", - ...termsAndConditionsAgreedVersion }; Given("I am logged in", () => { @@ -48,44 +46,44 @@ Given("we have a selection of categories", () => { }); Given("we have a selection of tags and categories as well as posts", () => { - cy.createCategories("cat12") + cy.createCategories("cat12", "cat121", "cat122") .factory() - .create("Tag", { + .build("tag", { id: "Ecology" }) - .create("Tag", { + .build("tag", { id: "Nature" }) - .create("Tag", { + .build("tag", { id: "Democracy" }); cy.factory() - .create("User", { + .build("user", { id: 'a1' }) - .create("Post", { + .build("post", {}, { authorId: 'a1', tagIds: ["Ecology", "Nature", "Democracy"], categoryIds: ["cat12"] }) - .create("Post", { + .build("post", {}, { authorId: 'a1', tagIds: ["Nature", "Democracy"], categoryIds: ["cat121"] }); cy.factory() - .create("User", { + .build("user", { id: 'a2' }) - .create("Post", { + .build("post", {}, { authorId: 'a2', tagIds: ['Nature', 'Democracy'], categoryIds: ["cat12"] }); cy.factory() - .create("Post", { + .build("post", {}, { authorId: narratorParams.id, tagIds: ['Democracy'], categoryIds: ["cat122"] @@ -94,7 +92,7 @@ Given("we have a selection of tags and categories as well as posts", () => { Given("we have the following user accounts:", table => { table.hashes().forEach(params => { - cy.factory().create("User", { + cy.factory().build("user", { ...params, ...termsAndConditionsAgreedVersion }); @@ -102,15 +100,14 @@ Given("we have the following user accounts:", table => { }); Given("I have a user account", () => { - cy.factory().create("User", narratorParams); + cy.factory().build("user", narratorParams); }); Given("my user account has the role {string}", role => { - cy.factory().create("User", { + cy.factory().build("user", { role, - ...loginCredentials, ...termsAndConditionsAgreedVersion, - }); + }, loginCredentials); }); When("I log out", cy.logout); @@ -193,7 +190,7 @@ When("I press {string}", label => { }); Given("we have the following posts in our database:", table => { - cy.factory().create('Category', { + cy.factory().build('category', { id: `cat-456`, name: "Just For Fun", slug: `just-for-fun`, @@ -208,9 +205,8 @@ Given("we have the following posts in our database:", table => { deleted: Boolean(postAttributes.deleted), disabled: Boolean(postAttributes.disabled), pinned: Boolean(postAttributes.pinned), - categoryIds: ['cat-456'] } - cy.factory().create("Post", postAttributes); + cy.factory().build("post", postAttributes, { categoryIds: ['cat-456'] }); }) }); @@ -234,7 +230,7 @@ Given("I previously created a post", () => { lastPost.title = "previously created post"; lastPost.content = "with some content"; cy.factory() - .create("Post", lastPost); + .build("post", lastPost); }); When("I choose {string} as the title of the post", title => { @@ -302,10 +298,9 @@ Then( Given("my user account has the following login credentials:", table => { loginCredentials = table.hashes()[0]; cy.debug(); - cy.factory().create("User", { + cy.factory().build("user", { ...termsAndConditionsAgreedVersion, - ...loginCredentials - }); + }, loginCredentials); }); When("I fill the password form with:", table => { @@ -412,12 +407,11 @@ Then("there are no notifications in the top menu", () => { }); Given("there is an annoying user called {string}", name => { - cy.factory().create("User", { - ...annoyingParams, + cy.factory().build("user", { id: "annoying-user", name, ...termsAndConditionsAgreedVersion, - }); + }, annoyingParams); }); Given("there is an annoying user who has muted me", () => { @@ -484,9 +478,10 @@ Given("I follow the user {string}", name => { Given('"Spammy Spammer" wrote a post {string}', title => { cy.createCategories("cat21") .factory() - .create("Post", { - authorId: 'annoying-user', + .build("post", { title, + }, { + authorId: 'annoying-user', categoryIds: ["cat21"] }); }); @@ -507,9 +502,10 @@ Then("nobody is following the user profile anymore", () => { Given("I wrote a post {string}", title => { cy.createCategories(`cat213`, title) .factory() - .create("Post", { - authorId: narratorParams.id, + .build("post", { title, + }, { + authorId: narratorParams.id, categoryIds: ["cat213"] }); }); diff --git a/cypress/support/commands.js b/cypress/support/commands.js index c9a2e213a..6ab0a77ce 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -76,19 +76,19 @@ Cypress.Commands.add("openPage", page => { }); Cypress.Commands.add("createCategories", (id, slug) => { - cy.neode() - .create("Category", { + cy.factory() + .build("category", { id: `${id}`, name: "Just For Fun", slug: `${slug}`, icon: "smile" }) - .create("Category", { + .build("category", { id: `${id}1`, name: "Happiness & Values", icon: "heart-o" }) - .create("Category", { + .build("category", { id: `${id}2`, name: "Health & Wellbeing", icon: "medkit" diff --git a/cypress/support/factories.js b/cypress/support/factories.js index d5d4ee768..7103ce524 100644 --- a/cypress/support/factories.js +++ b/cypress/support/factories.js @@ -1,4 +1,4 @@ -import Factory from '../../backend/src/factories' +import Factory, { cleanDatabase } from '../../backend/src/factories' import { getDriver, getNeode } from '../../backend/src/db/neo4j' const neo4jConfigs = { @@ -12,7 +12,7 @@ const factoryOptions = { neo4jDriver, neodeInstance } const factory = Factory(factoryOptions) beforeEach(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) Cypress.Commands.add('neode', () => { @@ -38,10 +38,10 @@ Cypress.Commands.add('factory', () => { }) Cypress.Commands.add( - 'create', + 'build', { prevSubject: true }, async (factory, node, properties, options) => { - await factory.create(node, properties, options) + await Factory.build(node, properties, options) return factory } ) diff --git a/features/support/steps.js b/features/support/steps.js index 71f493834..13e51007e 100644 --- a/features/support/steps.js +++ b/features/support/steps.js @@ -1,15 +1,13 @@ // features/support/steps.js import { Given, When, Then, After, AfterAll } from 'cucumber' -import Factory from '../../backend/src/factories' +import Factory, { cleanDatabase } from '../../backend/src/factories' import dotenv from 'dotenv' import expect from 'expect' const debug = require('debug')('ea:test:steps') -const factory = Factory() - After(async () => { - await factory.cleanDatabase() + await cleanDatabase() }) Given('our CLIENT_URI is {string}', function (string) { @@ -21,7 +19,7 @@ Given('our CLIENT_URI is {string}', function (string) { Given('we have the following users in our database:', function (dataTable) { return Promise.all(dataTable.hashes().map(({ slug, name }) => { - return factory.create('User', { + return Factory.build('user', { name, slug, }) From f9c42ab2a552d26371dd6d4608d4ceb4586a9f48 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 28 Jan 2020 23:30:09 +0100 Subject: [PATCH 09/19] refactor(seeds): Use buildList to setup lists --- backend/src/db/seed.js | 406 +++++++++++------------------------------ 1 file changed, 106 insertions(+), 300 deletions(-) diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index f2ec31876..e3fbe7fc1 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -933,334 +933,140 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] trollingComment.update({ disabled: true, updatedAt: new Date().toISOString(), closed: true }), ]) - await Promise.all( - [...Array(30).keys()].map(i => { - return Factory.build('user') - }), - ) + await Factory.buildList('user', 30) - await Promise.all( - [...Array(30).keys()].map(() => { - return Factory.build( - 'post', - { - image: faker.image.unsplash.objects(), - }, - { - categoryIds: ['cat1'], - author: jennyRostock, - }, - ) - }), - ) + await Factory.buildList( 'post', 30, { + image: faker.image.unsplash.objects(), + }, { + categoryIds: ['cat1'], + author: jennyRostock, + }) - await Promise.all( - [...Array(6).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: jennyRostock, - postId: 'p2', - }, - ) - }), - ) + await Factory.buildList('comment', 6, {}, { + author: jennyRostock, + postId: 'p2', + }) - await Promise.all( - [...Array(4).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: jennyRostock, - postId: 'p15', - }, - ) - }), - ) + await Factory.buildList('comment', 4, {}, { + author: jennyRostock, + postId: 'p15', + }) - await Promise.all( - [...Array(2).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: jennyRostock, - postId: 'p4', - }, - ) - }), - ) + await Factory.buildList( 'comment', 2, {}, { + author: jennyRostock, + postId: 'p4', + }) - await Promise.all( - [...Array(21).keys()].map(() => { - return Factory.build( - 'post', - { - image: faker.image.unsplash.buildings(), - }, - { - author: peterLustig, - }, - ) - }), - ) + await Factory.buildList('post', 21, { + image: faker.image.unsplash.buildings(), + }, { + author: peterLustig, + }) - await Promise.all( - [...Array(3).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: peterLustig, - postId: 'p4', - }, - ) - }), - ) + await Factory.buildList('comment', 3, {}, { + author: peterLustig, + postId: 'p4', + }) - await Promise.all( - [...Array(5).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: peterLustig, - postId: 'p14', - }, - ) - }), - ) + await Factory.buildList('comment', 5, {}, { + author: peterLustig, + postId: 'p14', + }) - await Promise.all( - [...Array(6).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: peterLustig, - postId: 'p0', - }, - ) - }), - ) + await Factory.buildList('comment', 6, {}, { + author: peterLustig, + postId: 'p0', + }) - await Promise.all( - [...Array(11).keys()].map(() => { - return Factory.build( - 'post', - { - image: faker.image.unsplash.food(), - }, - { - author: dewey, - }, - ) - }), - ) + await Factory.buildList( 'post', 11, { + image: faker.image.unsplash.food(), + }, { + author: dewey, + }) - await Promise.all( - [...Array(7).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: dewey, - postId: 'p2', - }, - ) - }), - ) + await Factory.buildList( 'comment', 7, {}, { + author: dewey, + postId: 'p2', + }) - await Promise.all( - [...Array(5).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: dewey, - postId: 'p6', - }, - ) - }), - ) + await Factory.buildList( 'comment', 5, {}, { + author: dewey, + postId: 'p6', + }) - await Promise.all( - [...Array(2).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: dewey, - postId: 'p9', - }, - ) - }), - ) + await Factory.buildList( 'comment', 2, {}, { + author: dewey, + postId: 'p9', + }) - await Promise.all( - [...Array(16).keys()].map(() => { - return Factory.build( - 'post', - { - image: faker.image.unsplash.technology(), - }, - { - author: louie, - }, - ) - }), - ) + await Factory.buildList('post', 16, { + image: faker.image.unsplash.technology(), + }, { + author: louie, + }) - await Promise.all( - [...Array(4).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - postId: 'p1', - author: louie, - }, - ) - }), - ) + await Factory.buildList('comment', 4, {}, { + postId: 'p1', + author: louie, + }) - await Promise.all( - [...Array(8).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: louie, - postId: 'p10', - }, - ) - }), - ) + await Factory.buildList('comment', 8, {}, { + author: louie, + postId: 'p10', + }) - await Promise.all( - [...Array(5).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: louie, - postId: 'p13', - }, - ) - }), - ) + await Factory.buildList('comment', 5, {}, { + author: louie, + postId: 'p13', + }) - await Promise.all( - [...Array(45).keys()].map(() => { - return Factory.build( - 'post', - { - image: faker.image.unsplash.people(), - }, - { - author: bobDerBaumeister, - }, - ) - }), - ) + await Factory.buildList('post', 45, { + image: faker.image.unsplash.people(), + }, { + author: bobDerBaumeister, + }) - await Promise.all( - [...Array(2).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: bobDerBaumeister, - postId: 'p2', - }, - ) - }), - ) + await Factory.buildList('comment', 2, {}, { + author: bobDerBaumeister, + postId: 'p2', + }) - await Promise.all( - [...Array(3).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: bobDerBaumeister, - postId: 'p12', - }, - ) - }), - ) + await Factory.buildList( 'comment', 3, {}, { + author: bobDerBaumeister, + postId: 'p12', + }) - await Promise.all( - [...Array(7).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: bobDerBaumeister, - postId: 'p13', - }, - ) - }), - ) + await Factory.buildList( 'comment', 7, {}, { + author: bobDerBaumeister, + postId: 'p13', + }) - await Promise.all( - [...Array(8).keys()].map(() => { - return Factory.build( - 'post', - { - image: faker.image.unsplash.nature(), - }, - { - author: huey, - }, - ) - }), - ) + await Factory.buildList( 'post', 8, { + image: faker.image.unsplash.nature(), + }, { + author: huey, + }) - await Promise.all( - [...Array(6).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: huey, - postId: 'p0', - }, - ) - }), - ) + await Factory.buildList('comment', 6, {}, { + author: huey, + postId: 'p0', + }) - await Promise.all( - [...Array(8).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: huey, - postId: 'p13', - }, - ) - }), - ) + await Factory.buildList( 'comment', 8, {}, { + author: huey, + postId: 'p13', + }) - await Promise.all( - [...Array(9).keys()].map(() => { - return Factory.build( - 'comment', - {}, - { - author: huey, - postId: 'p15', - }, - ) - }), - ) + await Factory.buildList('comment', 9, {}, { + author: huey, + postId: 'p15', + }) await Factory.build('donations') /* eslint-disable-next-line no-console */ console.log('Seeded Data...') + await driver.close() + await neode.close() process.exit(0) } catch (err) { /* eslint-disable-next-line no-console */ From 910f12208714f3c2e2ce17ce4b7aa40212873e7d Mon Sep 17 00:00:00 2001 From: roschaefer Date: Wed, 29 Jan 2020 00:51:04 +0100 Subject: [PATCH 10/19] Fix lint and cypress --- backend/src/db/seed.js | 326 +++++++++++++++++++--------- cypress/integration/common/steps.js | 66 ++---- cypress/support/commands.js | 21 -- cypress/support/factories.js | 4 +- 4 files changed, 249 insertions(+), 168 deletions(-) diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index e3fbe7fc1..b5493d81d 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -935,132 +935,252 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Factory.buildList('user', 30) - await Factory.buildList( 'post', 30, { - image: faker.image.unsplash.objects(), - }, { - categoryIds: ['cat1'], - author: jennyRostock, - }) + await Factory.buildList( + 'post', + 30, + { + image: faker.image.unsplash.objects(), + }, + { + categoryIds: ['cat1'], + author: jennyRostock, + }, + ) - await Factory.buildList('comment', 6, {}, { - author: jennyRostock, - postId: 'p2', - }) + await Factory.buildList( + 'comment', + 6, + {}, + { + author: jennyRostock, + postId: 'p2', + }, + ) - await Factory.buildList('comment', 4, {}, { - author: jennyRostock, - postId: 'p15', - }) + await Factory.buildList( + 'comment', + 4, + {}, + { + author: jennyRostock, + postId: 'p15', + }, + ) - await Factory.buildList( 'comment', 2, {}, { - author: jennyRostock, - postId: 'p4', - }) + await Factory.buildList( + 'comment', + 2, + {}, + { + author: jennyRostock, + postId: 'p4', + }, + ) - await Factory.buildList('post', 21, { - image: faker.image.unsplash.buildings(), - }, { - author: peterLustig, - }) + await Factory.buildList( + 'post', + 21, + { + image: faker.image.unsplash.buildings(), + }, + { + author: peterLustig, + }, + ) - await Factory.buildList('comment', 3, {}, { - author: peterLustig, - postId: 'p4', - }) + await Factory.buildList( + 'comment', + 3, + {}, + { + author: peterLustig, + postId: 'p4', + }, + ) - await Factory.buildList('comment', 5, {}, { - author: peterLustig, - postId: 'p14', - }) + await Factory.buildList( + 'comment', + 5, + {}, + { + author: peterLustig, + postId: 'p14', + }, + ) - await Factory.buildList('comment', 6, {}, { - author: peterLustig, - postId: 'p0', - }) + await Factory.buildList( + 'comment', + 6, + {}, + { + author: peterLustig, + postId: 'p0', + }, + ) - await Factory.buildList( 'post', 11, { - image: faker.image.unsplash.food(), - }, { - author: dewey, - }) + await Factory.buildList( + 'post', + 11, + { + image: faker.image.unsplash.food(), + }, + { + author: dewey, + }, + ) - await Factory.buildList( 'comment', 7, {}, { - author: dewey, - postId: 'p2', - }) + await Factory.buildList( + 'comment', + 7, + {}, + { + author: dewey, + postId: 'p2', + }, + ) - await Factory.buildList( 'comment', 5, {}, { - author: dewey, - postId: 'p6', - }) + await Factory.buildList( + 'comment', + 5, + {}, + { + author: dewey, + postId: 'p6', + }, + ) - await Factory.buildList( 'comment', 2, {}, { - author: dewey, - postId: 'p9', - }) + await Factory.buildList( + 'comment', + 2, + {}, + { + author: dewey, + postId: 'p9', + }, + ) - await Factory.buildList('post', 16, { - image: faker.image.unsplash.technology(), - }, { - author: louie, - }) + await Factory.buildList( + 'post', + 16, + { + image: faker.image.unsplash.technology(), + }, + { + author: louie, + }, + ) - await Factory.buildList('comment', 4, {}, { - postId: 'p1', - author: louie, - }) + await Factory.buildList( + 'comment', + 4, + {}, + { + postId: 'p1', + author: louie, + }, + ) - await Factory.buildList('comment', 8, {}, { - author: louie, - postId: 'p10', - }) + await Factory.buildList( + 'comment', + 8, + {}, + { + author: louie, + postId: 'p10', + }, + ) - await Factory.buildList('comment', 5, {}, { - author: louie, - postId: 'p13', - }) + await Factory.buildList( + 'comment', + 5, + {}, + { + author: louie, + postId: 'p13', + }, + ) - await Factory.buildList('post', 45, { - image: faker.image.unsplash.people(), - }, { - author: bobDerBaumeister, - }) + await Factory.buildList( + 'post', + 45, + { + image: faker.image.unsplash.people(), + }, + { + author: bobDerBaumeister, + }, + ) - await Factory.buildList('comment', 2, {}, { - author: bobDerBaumeister, - postId: 'p2', - }) + await Factory.buildList( + 'comment', + 2, + {}, + { + author: bobDerBaumeister, + postId: 'p2', + }, + ) - await Factory.buildList( 'comment', 3, {}, { - author: bobDerBaumeister, - postId: 'p12', - }) + await Factory.buildList( + 'comment', + 3, + {}, + { + author: bobDerBaumeister, + postId: 'p12', + }, + ) - await Factory.buildList( 'comment', 7, {}, { - author: bobDerBaumeister, - postId: 'p13', - }) + await Factory.buildList( + 'comment', + 7, + {}, + { + author: bobDerBaumeister, + postId: 'p13', + }, + ) - await Factory.buildList( 'post', 8, { - image: faker.image.unsplash.nature(), - }, { - author: huey, - }) + await Factory.buildList( + 'post', + 8, + { + image: faker.image.unsplash.nature(), + }, + { + author: huey, + }, + ) - await Factory.buildList('comment', 6, {}, { - author: huey, - postId: 'p0', - }) + await Factory.buildList( + 'comment', + 6, + {}, + { + author: huey, + postId: 'p0', + }, + ) - await Factory.buildList( 'comment', 8, {}, { - author: huey, - postId: 'p13', - }) + await Factory.buildList( + 'comment', + 8, + {}, + { + author: huey, + postId: 'p13', + }, + ) - await Factory.buildList('comment', 9, {}, { - author: huey, - postId: 'p15', - }) + await Factory.buildList( + 'comment', + 9, + {}, + { + author: huey, + postId: 'p15', + }, + ) await Factory.build('donations') /* eslint-disable-next-line no-console */ diff --git a/cypress/integration/common/steps.js b/cypress/integration/common/steps.js index a0c3f30ae..a22ee8e77 100644 --- a/cypress/integration/common/steps.js +++ b/cypress/integration/common/steps.js @@ -42,26 +42,18 @@ Given("I am logged in as the muted user", () => { }); Given("we have a selection of categories", () => { - cy.createCategories("cat0", "just-for-fun"); + cy.factory().build('category', { id: "cat0", slug: "just-for-fun" }); }); Given("we have a selection of tags and categories as well as posts", () => { - cy.createCategories("cat12", "cat121", "cat122") - .factory() - .build("tag", { - id: "Ecology" - }) - .build("tag", { - id: "Nature" - }) - .build("tag", { - id: "Democracy" - }); - cy.factory() - .build("user", { - id: 'a1' - }) + .build('category', { id: 'cat12', name: "Just For Fun", icon: "smile", }) + .build('category', { id: 'cat121', name: "Happiness & Values", icon: "heart-o"}) + .build('category', { id: 'cat122', name: "Health & Wellbeing", icon: "medkit"}) + .build("tag", { id: "Ecology" }) + .build("tag", { id: "Nature" }) + .build("tag", { id: "Democracy" }) + .build("user", { id: 'a1' }) .build("post", {}, { authorId: 'a1', tagIds: ["Ecology", "Nature", "Democracy"], @@ -71,20 +63,14 @@ Given("we have a selection of tags and categories as well as posts", () => { authorId: 'a1', tagIds: ["Nature", "Democracy"], categoryIds: ["cat121"] - }); - - cy.factory() - .build("user", { - id: 'a2' }) + .build("user", { id: 'a2' }) .build("post", {}, { authorId: 'a2', tagIds: ['Nature', 'Democracy'], categoryIds: ["cat12"] - }); - cy.factory() + }) .build("post", {}, { - authorId: narratorParams.id, tagIds: ['Democracy'], categoryIds: ["cat122"] }) @@ -95,12 +81,12 @@ Given("we have the following user accounts:", table => { cy.factory().build("user", { ...params, ...termsAndConditionsAgreedVersion - }); + }, params); }); }); Given("I have a user account", () => { - cy.factory().build("user", narratorParams); + cy.factory().build("user", narratorParams, loginCredentials); }); Given("my user account has the role {string}", role => { @@ -197,16 +183,16 @@ Given("we have the following posts in our database:", table => { icon: "smile" }) - table.hashes().forEach(({ - ...postAttributes - }, i) => { - postAttributes = { - ...postAttributes, - deleted: Boolean(postAttributes.deleted), - disabled: Boolean(postAttributes.disabled), - pinned: Boolean(postAttributes.pinned), - } - cy.factory().build("post", postAttributes, { categoryIds: ['cat-456'] }); + table.hashes().forEach((attributesOrOptions, i) => { + cy.factory().build("post", { + ...attributesOrOptions, + deleted: Boolean(attributesOrOptions.deleted), + disabled: Boolean(attributesOrOptions.disabled), + pinned: Boolean(attributesOrOptions.pinned), + }, { + ...attributesOrOptions, + categoryIds: ['cat-456'] + }); }) }); @@ -476,13 +462,11 @@ Given("I follow the user {string}", name => { }); Given('"Spammy Spammer" wrote a post {string}', title => { - cy.createCategories("cat21") - .factory() + cy.factory() .build("post", { title, }, { authorId: 'annoying-user', - categoryIds: ["cat21"] }); }); @@ -500,13 +484,11 @@ Then("nobody is following the user profile anymore", () => { }); Given("I wrote a post {string}", title => { - cy.createCategories(`cat213`, title) - .factory() + cy.factory() .build("post", { title, }, { authorId: narratorParams.id, - categoryIds: ["cat213"] }); }); diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 6ab0a77ce..8e586fde9 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -75,27 +75,6 @@ Cypress.Commands.add("openPage", page => { cy.visit(`/${page}`); }); -Cypress.Commands.add("createCategories", (id, slug) => { - cy.factory() - .build("category", { - id: `${id}`, - name: "Just For Fun", - slug: `${slug}`, - icon: "smile" - }) - .build("category", { - id: `${id}1`, - name: "Happiness & Values", - icon: "heart-o" - }) - .build("category", { - id: `${id}2`, - name: "Health & Wellbeing", - icon: "medkit" - }); -}); - - Cypress.Commands.add( 'authenticateAs', async ({email, password}) => { diff --git a/cypress/support/factories.js b/cypress/support/factories.js index 7103ce524..951833de1 100644 --- a/cypress/support/factories.js +++ b/cypress/support/factories.js @@ -40,8 +40,8 @@ Cypress.Commands.add('factory', () => { Cypress.Commands.add( 'build', { prevSubject: true }, - async (factory, node, properties, options) => { - await Factory.build(node, properties, options) + async (factory, name, atrributes, options) => { + await Factory.build(name, atrributes, options) return factory } ) From 46edc3fdd5b83c2f00506f595b1254d7597767e0 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Fri, 7 Feb 2020 14:24:54 +0100 Subject: [PATCH 11/19] Replace buildList with array of Promises reason: Rosie.js does not support promises. So for the time being, we shall not use more than one `after` callback and we shall not use buildList as it does not return a `Promise.all([])`. We have to ask the maintainers of Rosie.js if they accept the breaking change of returning a Promise always. --- backend/src/db/seed.js | 473 ++++++++++++++++++++++++----------------- 1 file changed, 273 insertions(+), 200 deletions(-) diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index 04f7f7bdb..32a20145b 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -933,253 +933,326 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] trollingComment.update({ disabled: true, updatedAt: new Date().toISOString(), closed: true }), ]) - await Factory.buildList('user', 30) + await Promise.all([...Array(30).keys()].map(() => Factory.build('user'))) - await Factory.buildList( - 'post', - 30, - { - image: faker.image.unsplash.objects(), - }, - { - categoryIds: ['cat1'], - author: jennyRostock, - }, + await Promise.all( + [...Array(30).keys()].map(() => + Factory.build( + 'post', + { + image: faker.image.unsplash.objects(), + }, + { + categoryIds: ['cat1'], + author: jennyRostock, + }, + ), + ), ) - await Factory.buildList( - 'comment', - 6, - {}, - { - author: jennyRostock, - postId: 'p2', - }, + await Promise.all( + [...Array(6).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: jennyRostock, + postId: 'p2', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 4, - {}, - { - author: jennyRostock, - postId: 'p15', - }, + await Promise.all( + [...Array(4).keys()].map(() => + Factory.build( + 'comment', + 4, + {}, + { + author: jennyRostock, + postId: 'p15', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 2, - {}, - { - author: jennyRostock, - postId: 'p4', - }, + await Promise.all( + [...Array(2).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: jennyRostock, + postId: 'p4', + }, + ), + ), ) - await Factory.buildList( - 'post', - 21, - { - image: faker.image.unsplash.buildings(), - }, - { - author: peterLustig, - }, + await Promise.all( + [...Array(21).keys()].map(() => + Factory.build( + 'post', + { + image: faker.image.unsplash.buildings(), + }, + { + author: peterLustig, + }, + ), + ), ) - await Factory.buildList( - 'comment', - 3, - {}, - { - author: peterLustig, - postId: 'p4', - }, + await Promise.all( + [...Array(3).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: peterLustig, + postId: 'p4', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 5, - {}, - { - author: peterLustig, - postId: 'p14', - }, + await Promise.all( + [...Array(3).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: peterLustig, + postId: 'p14', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 6, - {}, - { - author: peterLustig, - postId: 'p0', - }, + await Promise.all( + [...Array(6).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: peterLustig, + postId: 'p0', + }, + ), + ), ) - await Factory.buildList( - 'post', - 11, - { - image: faker.image.unsplash.food(), - }, - { - author: dewey, - }, + await Promise.all( + [...Array(11).keys()].map(() => + Factory.build( + 'post', + { + image: faker.image.unsplash.food(), + }, + { + author: dewey, + }, + ), + ), ) - await Factory.buildList( - 'comment', - 7, - {}, - { - author: dewey, - postId: 'p2', - }, + await Promise.all( + [...Array(7).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: dewey, + postId: 'p2', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 5, - {}, - { - author: dewey, - postId: 'p6', - }, + await Promise.all( + [...Array(5).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: dewey, + postId: 'p6', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 2, - {}, - { - author: dewey, - postId: 'p9', - }, + await Promise.all( + [...Array(2).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: dewey, + postId: 'p9', + }, + ), + ), ) - await Factory.buildList( - 'post', - 16, - { - image: faker.image.unsplash.technology(), - }, - { - author: louie, - }, + await Promise.all( + [...Array(16).keys()].map(() => + Factory.build( + 'post', + { + image: faker.image.unsplash.technology(), + }, + { + author: louie, + }, + ), + ), ) - await Factory.buildList( - 'comment', - 4, - {}, - { - postId: 'p1', - author: louie, - }, + await Promise.all( + [...Array(4).keys()].map(() => + Factory.build( + 'comment', + {}, + { + postId: 'p1', + author: louie, + }, + ), + ), ) - await Factory.buildList( - 'comment', - 8, - {}, - { - author: louie, - postId: 'p10', - }, + await Promise.all( + [...Array(8).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: louie, + postId: 'p10', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 5, - {}, - { - author: louie, - postId: 'p13', - }, + await Promise.all( + [...Array(5).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: louie, + postId: 'p13', + }, + ), + ), ) - await Factory.buildList( - 'post', - 45, - { - image: faker.image.unsplash.people(), - }, - { - author: bobDerBaumeister, - }, + await Promise.all( + [...Array(45).keys()].map(() => + Factory.build( + 'post', + { + image: faker.image.unsplash.people(), + }, + { + author: bobDerBaumeister, + }, + ), + ), ) - await Factory.buildList( - 'comment', - 2, - {}, - { - author: bobDerBaumeister, - postId: 'p2', - }, + await Promise.all( + [...Array(2).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: bobDerBaumeister, + postId: 'p2', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 3, - {}, - { - author: bobDerBaumeister, - postId: 'p12', - }, + await Promise.all( + [...Array(3).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: bobDerBaumeister, + postId: 'p12', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 7, - {}, - { - author: bobDerBaumeister, - postId: 'p13', - }, + await Promise.all( + [...Array(7).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: bobDerBaumeister, + postId: 'p13', + }, + ), + ), ) - await Factory.buildList( - 'post', - 8, - { - image: faker.image.unsplash.nature(), - }, - { - author: huey, - }, + await Promise.all( + [...Array(8).keys()].map(() => + Factory.build( + 'post', + { + image: faker.image.unsplash.nature(), + }, + { + author: huey, + }, + ), + ), ) - await Factory.buildList( - 'comment', - 6, - {}, - { - author: huey, - postId: 'p0', - }, + await Promise.all( + [...Array(6).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: huey, + postId: 'p0', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 8, - {}, - { - author: huey, - postId: 'p13', - }, + await Promise.all( + [...Array(8).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: huey, + postId: 'p13', + }, + ), + ), ) - await Factory.buildList( - 'comment', - 9, - {}, - { - author: huey, - postId: 'p15', - }, + await Promise.all( + [...Array(8).keys()].map(() => + Factory.build( + 'comment', + {}, + { + author: huey, + postId: 'p15', + }, + ), + ), ) await Factory.build('donations') From a77f8d92bc6b590beb77fe081fbe69c9b75a90a6 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Sat, 8 Feb 2020 17:55:07 +0100 Subject: [PATCH 12/19] Speed up factories a little --- backend/src/db/seed.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index 32a20145b..f966f8b07 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -475,8 +475,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] imageBlurred: true, }, { - author: dewey, categoryIds: ['cat10'], + author: dewey, }, ), Factory.build( @@ -499,8 +499,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] language: sample(languages), }, { - author: bobDerBaumeister, categoryIds: ['cat13'], + author: bobDerBaumeister, }, ), Factory.build( @@ -512,8 +512,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] imageAspectRatio: 300 / 450, }, { - author: jennyRostock, categoryIds: ['cat14'], + author: jennyRostock, }, ), Factory.build( @@ -523,8 +523,8 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] language: sample(languages), }, { - author: huey, categoryIds: ['cat15'], + author: huey, }, ), ]) @@ -998,6 +998,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] image: faker.image.unsplash.buildings(), }, { + categoryIds: ['cat1'], author: peterLustig, }, ), @@ -1051,6 +1052,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] image: faker.image.unsplash.food(), }, { + categoryIds: ['cat1'], author: dewey, }, ), @@ -1104,6 +1106,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] image: faker.image.unsplash.technology(), }, { + categoryIds: ['cat1'], author: louie, }, ), @@ -1157,6 +1160,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] image: faker.image.unsplash.people(), }, { + categoryIds: ['cat1'], author: bobDerBaumeister, }, ), @@ -1210,6 +1214,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] image: faker.image.unsplash.nature(), }, { + categoryIds: ['cat1'], author: huey, }, ), From 38088235789b6aa4caed80d8268895958fae5d07 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2020 04:16:48 +0000 Subject: [PATCH 13/19] build(deps-dev): bump codecov from 3.6.4 to 3.6.5 Bumps [codecov](https://github.com/codecov/codecov-node) from 3.6.4 to 3.6.5. - [Release notes](https://github.com/codecov/codecov-node/releases) - [Commits](https://github.com/codecov/codecov-node/commits) Signed-off-by: dependabot-preview[bot] --- package.json | 2 +- yarn.lock | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index caed8a5b2..1234d09be 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@babel/register": "^7.8.3", "auto-changelog": "^1.16.2", "bcryptjs": "^2.4.3", - "codecov": "^3.6.4", + "codecov": "^3.6.5", "cross-env": "^6.0.3", "cucumber": "^6.0.5", "cypress": "^3.8.3", diff --git a/yarn.lock b/yarn.lock index f6fae5cc5..cf51f0b46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2020,17 +2020,16 @@ code-point-at@^1.0.0: resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" integrity sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c= -codecov@^3.6.4: - version "3.6.4" - resolved "https://registry.yarnpkg.com/codecov/-/codecov-3.6.4.tgz#c993a58704ad5f15dcdd6aa3c3af22070c69eb18" - integrity sha512-R9zEcTR5/IDLepdGUHAkRMZOIKhBE1NRGS1N2H96zhBOskhdW9Q22GtYYyecbZL1aheU00D2F21nNBGRISui0A== +codecov@^3.6.5: + version "3.6.5" + resolved "https://registry.yarnpkg.com/codecov/-/codecov-3.6.5.tgz#d73ce62e8a021f5249f54b073e6f2d6a513f172a" + integrity sha512-v48WuDMUug6JXwmmfsMzhCHRnhUf8O3duqXvltaYJKrO1OekZWpB/eH6iIoaxMl8Qli0+u3OxptdsBOYiD7VAQ== dependencies: argv "0.0.2" ignore-walk "3.0.3" js-yaml "3.13.1" teeny-request "6.0.1" urlgrey "0.4.4" - validator "12.2.0" coffeeify@3.0.1: version "3.0.1" @@ -6868,11 +6867,6 @@ validate-npm-package-license@^3.0.1: spdx-correct "^3.0.0" spdx-expression-parse "^3.0.0" -validator@12.2.0: - version "12.2.0" - resolved "https://registry.yarnpkg.com/validator/-/validator-12.2.0.tgz#660d47e96267033fd070096c3b1a6f2db4380a0a" - integrity sha512-jJfE/DW6tIK1Ek8nCfNFqt8Wb3nzMoAbocBF6/Icgg1ZFSBpObdnwVY2jQj6qUqzhx5jc71fpvBWyLGO7Xl+nQ== - verror@1.10.0, verror@^1.9.0: version "1.10.0" resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400" From 64d90e5311139e8fdcb1d954b35004073ba8dc0b Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2020 04:17:22 +0000 Subject: [PATCH 14/19] build(deps): bump graphql-shield from 7.0.10 to 7.0.11 in /backend Bumps [graphql-shield](https://github.com/maticzav/graphql-shield) from 7.0.10 to 7.0.11. - [Release notes](https://github.com/maticzav/graphql-shield/releases) - [Commits](https://github.com/maticzav/graphql-shield/compare/v7.0.10...v7.0.11) Signed-off-by: dependabot-preview[bot] --- backend/package.json | 2 +- backend/yarn.lock | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/package.json b/backend/package.json index ae4ae0804..50c5e1011 100644 --- a/backend/package.json +++ b/backend/package.json @@ -60,7 +60,7 @@ "graphql-iso-date": "~3.6.1", "graphql-middleware": "~4.0.2", "graphql-middleware-sentry": "^3.2.1", - "graphql-shield": "~7.0.10", + "graphql-shield": "~7.0.11", "graphql-tag": "~2.10.3", "helmet": "~3.21.2", "jsonwebtoken": "~8.5.1", diff --git a/backend/yarn.lock b/backend/yarn.lock index 819b215c5..4df5537a1 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -1943,7 +1943,7 @@ apollo-server-caching@^0.5.1: dependencies: lru-cache "^5.0.0" -apollo-server-core@^2.10.0, apollo-server-core@^2.9.16: +apollo-server-core@^2.10.0: version "2.10.0" resolved "https://registry.yarnpkg.com/apollo-server-core/-/apollo-server-core-2.10.0.tgz#b8d51bdffe6529f0e3ca670ee8f1238765cfade4" integrity sha512-x/UK6XvU307W8D/pzTclU04JIjRarcbg5mFPe0nVmO4OTc26uQgKi1WlZkcewXsAUnn+nDwKVn2c2G3dHEgXzQ== @@ -4476,10 +4476,10 @@ graphql-middleware@~4.0.2: dependencies: graphql-tools "^4.0.5" -graphql-shield@~7.0.10: - version "7.0.10" - resolved "https://registry.yarnpkg.com/graphql-shield/-/graphql-shield-7.0.10.tgz#c25517a07e97bfd74089fde66ea2d2044addf743" - integrity sha512-m1HPQ3DpzuW8b7derWcsjjYtiBk+Hjgb1eL/0YK/Y2A0EV7vDQzJwRzlIwn9My7Xv+F4DJwm25yydnnUNpZt8g== +graphql-shield@~7.0.11: + version "7.0.11" + resolved "https://registry.yarnpkg.com/graphql-shield/-/graphql-shield-7.0.11.tgz#78d49f346326be71090d35d8f5843da9ee8136e2" + integrity sha512-iWn/aiom2c8NuOj60euWTmsKKUjX1DB4ynBcDitQOLXG3WrWgss2Iolzr553qooJvkR5czeAFgPlZgI+nUgwsQ== dependencies: "@types/yup" "0.26.30" object-hash "^2.0.0" From e8410a62dce3457584e163cddca9f009309a7aae Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2020 04:31:28 +0000 Subject: [PATCH 15/19] build(deps-dev): bump @vue/cli-shared-utils in /webapp Bumps [@vue/cli-shared-utils](https://github.com/vuejs/vue-cli/tree/HEAD/packages/@vue/cli-shared-utils) from 4.1.2 to 4.2.2. - [Release notes](https://github.com/vuejs/vue-cli/releases) - [Changelog](https://github.com/vuejs/vue-cli/blob/dev/CHANGELOG.md) - [Commits](https://github.com/vuejs/vue-cli/commits/v4.2.2/packages/@vue/cli-shared-utils) Signed-off-by: dependabot-preview[bot] --- webapp/package.json | 2 +- webapp/yarn.lock | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/webapp/package.json b/webapp/package.json index 620a53165..0d36bef1d 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -103,7 +103,7 @@ "@storybook/addon-actions": "^5.3.12", "@storybook/addon-notes": "^5.3.12", "@storybook/vue": "~5.3.12", - "@vue/cli-shared-utils": "~4.1.2", + "@vue/cli-shared-utils": "~4.2.2", "@vue/eslint-config-prettier": "~6.0.0", "@vue/server-test-utils": "~1.0.0-beta.31", "@vue/test-utils": "~1.0.0-beta.31", diff --git a/webapp/yarn.lock b/webapp/yarn.lock index 63f1b4bd8..d5d6cf14b 100644 --- a/webapp/yarn.lock +++ b/webapp/yarn.lock @@ -2821,6 +2821,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.6.tgz#9cbfcb62c50947217f4d88d4d274cc40c22625a9" integrity sha512-Fvm24+u85lGmV4hT5G++aht2C5I4Z4dYlWZIh62FAfFO/TfzXtPpoLI6I7AuBWkIFqZCnhFOoTT7RjjaIL5Fjg== +"@types/normalize-package-data@^2.4.0": + version "2.4.0" + resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" + integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA== + "@types/npmlog@^4.1.2": version "4.1.2" resolved "https://registry.yarnpkg.com/@types/npmlog/-/npmlog-4.1.2.tgz#d070fe6a6b78755d1092a3dc492d34c3d8f871c4" @@ -3025,10 +3030,10 @@ "@vue/babel-plugin-transform-vue-jsx" "^1.1.2" camelcase "^5.0.0" -"@vue/cli-shared-utils@~4.1.2": - version "4.1.2" - resolved "https://registry.yarnpkg.com/@vue/cli-shared-utils/-/cli-shared-utils-4.1.2.tgz#d33984c8790ad869ef77f5229abd3e8e584fe58b" - integrity sha512-uQAVqxCWdL5ipZ0TPu6SVsdokQp4yHt8SzzpUGhq8TkW4vwalGddJAAJrqZHMl91ZTIJ4p4ixofmCaaJo5rSRA== +"@vue/cli-shared-utils@~4.2.2": + version "4.2.2" + resolved "https://registry.yarnpkg.com/@vue/cli-shared-utils/-/cli-shared-utils-4.2.2.tgz#953fec34115cb12d0820012a9d7400f8c27d6660" + integrity sha512-EK5wcxgjadqUpSzfh6Bnxd46Zx+SAaHusygqV11UZKHr4EObc/SjCpq7c7drmFkBjRqmVvrHs4jRnJJo5VgCgQ== dependencies: "@hapi/joi" "^15.0.1" chalk "^2.4.2" @@ -3038,6 +3043,7 @@ node-ipc "^9.1.1" open "^6.3.0" ora "^3.4.0" + read-pkg "^5.1.1" request "^2.87.0" request-promise-native "^1.0.8" semver "^6.1.0" @@ -11582,7 +11588,7 @@ nopt@~1.0.10: dependencies: abbrev "1" -normalize-package-data@^2.3.2, normalize-package-data@^2.3.4: +normalize-package-data@^2.3.2, normalize-package-data@^2.3.4, normalize-package-data@^2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8" integrity sha512-/5CMN3T0R4XTj4DcGaexo+roZSdSFW/0AOOTROrjxzCG1wrWXEsGbRKevjlIL+ZDE4sZlJr5ED4YW0yqmkK+eA== @@ -13811,6 +13817,16 @@ read-pkg@^2.0.0: normalize-package-data "^2.3.2" path-type "^2.0.0" +read-pkg@^5.1.1: + version "5.2.0" + resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-5.2.0.tgz#7bf295438ca5a33e56cd30e053b34ee7250c93cc" + integrity sha512-Ug69mNOpfvKDAc2Q8DRpMjjzdtrnv9HcSMX+4VsZxD1aZ6ZzrIE7rlzXBtWTyhULSMKg076AW6WR5iZpD0JiOg== + dependencies: + "@types/normalize-package-data" "^2.4.0" + normalize-package-data "^2.5.0" + parse-json "^5.0.0" + type-fest "^0.6.0" + "readable-stream@1 || 2", readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.3.3, readable-stream@^2.3.6, readable-stream@~2.3.6: version "2.3.6" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" @@ -15952,6 +15968,11 @@ type-fest@^0.5.2: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.5.2.tgz#d6ef42a0356c6cd45f49485c3b6281fc148e48a2" integrity sha512-DWkS49EQKVX//Tbupb9TFa19c7+MK1XmzkrZUR8TAktmE/DizXoaoJV6TZ/tSIPXipqNiRI6CyAe7x69Jb6RSw== +type-fest@^0.6.0: + version "0.6.0" + resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.6.0.tgz#8d2a2370d3df886eb5c90ada1c5bf6188acf838b" + integrity sha512-q+MB8nYR1KDLrgr4G5yemftpMC7/QLqVndBmEEdqzmNj5dcFOO4Oo8qlwZE3ULT3+Zim1F8Kq4cBnikNhlCMlg== + type-fest@^0.8.0: version "0.8.0" resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.0.tgz#ee92ee2ec95479869dec66d17d9698666b90f29d" From 3b29d480e3915ad9ff30945ff8598d74588ed20c Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Mon, 10 Feb 2020 12:34:40 +0100 Subject: [PATCH 16/19] Display unblock feature only for blocking user - add hasBlocked query to check if userA initiated the blocking and, if true, only them to unblock userB. - add cypress tests to make sure we don't break this in the future. --- backend/src/schema/types/type/User.gql | 7 ++++- cypress/integration/common/steps.js | 30 +++++++++++++++++++ .../user_profile/BlockUser.feature | 11 +++++++ webapp/components/ContentMenu/ContentMenu.vue | 2 +- webapp/pages/profile/_id/_slug.vue | 4 +-- 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/backend/src/schema/types/type/User.gql b/backend/src/schema/types/type/User.gql index 71cc1edb0..ec9bfeba7 100644 --- a/backend/src/schema/types/type/User.gql +++ b/backend/src/schema/types/type/User.gql @@ -68,7 +68,12 @@ type User { RETURN COUNT(u) >= 1 """ ) - + hasBlocked: Boolean! @cypher( + statement: """ + MATCH (this)<-[:BLOCKED]-(user:User {id: $cypherParams.currentUserId}) + RETURN COUNT(user) >= 1 + """ + ) blocked: Boolean! @cypher( statement: """ MATCH (this)-[:BLOCKED]-(user:User {id: $cypherParams.currentUserId}) diff --git a/cypress/integration/common/steps.js b/cypress/integration/common/steps.js index 36dbb50d4..753160b84 100644 --- a/cypress/integration/common/steps.js +++ b/cypress/integration/common/steps.js @@ -558,6 +558,20 @@ When("I block the user {string}", name => { }); }); +When("a user has blocked me", () => { + cy.neode() + .first("User", { + name: narratorParams.name + }) + .then(blockedUser => { + cy.neode() + .first("User", { + name: 'Harassing User' + }) + .relateTo(blockedUser, "blocked"); + }); +}); + When("I log in with:", table => { const [firstRow] = table.hashes(); const { @@ -583,4 +597,20 @@ Then("they should not see the comment from", () => { Then("they should see a text explaining commenting is not possible", () => { cy.get('.ds-placeholder').should('contain', "Commenting is not possible at this time on this post.") +}) + +Then("I should see no users in my blocked users list", () => { + cy.get('.ds-placeholder') + .should('contain', "So far, you have not blocked anybody.") +}) + +Then("I should not see {string} from the content menu in the user info box", link => { + cy.get(".user-content-menu .base-button").click() + cy.get(".popover .ds-menu-item-link") + .should('not.contain', link) +}) + +Then('I should not see {string} button', button => { + cy.get('.ds-card-content') + .should('not.contain', '.action-buttons') }) \ No newline at end of file diff --git a/cypress/integration/user_profile/BlockUser.feature b/cypress/integration/user_profile/BlockUser.feature index 43efe7807..50b90ee62 100644 --- a/cypress/integration/user_profile/BlockUser.feature +++ b/cypress/integration/user_profile/BlockUser.feature @@ -44,3 +44,14 @@ Feature: Block a User Then I should see the following posts in the select dropdown: | title | | previously created post | + + Scenario: Blocked users cannot see they are blocked in their list + Given a user has blocked me + And I navigate to my "Blocked users" settings page + Then I should see no users in my blocked users list + + Scenario: Blocked users should not see link or button to unblock, only blocking users + Given a user has blocked me + When I visit the profile page of the annoying user + And I should not see "Unblock user" from the content menu in the user info box + And I should not see "Unblock user" button \ No newline at end of file diff --git a/webapp/components/ContentMenu/ContentMenu.vue b/webapp/components/ContentMenu/ContentMenu.vue index ee2bd4a62..2be51c6ac 100644 --- a/webapp/components/ContentMenu/ContentMenu.vue +++ b/webapp/components/ContentMenu/ContentMenu.vue @@ -172,7 +172,7 @@ export default { icon: 'microphone-slash', }) } - if (this.resource.blocked) { + if (this.resource.hasBlocked) { routes.push({ label: this.$t(`settings.blocked-users.unblock`), callback: () => { diff --git a/webapp/pages/profile/_id/_slug.vue b/webapp/pages/profile/_id/_slug.vue index 80471fff4..4d9653dcb 100644 --- a/webapp/pages/profile/_id/_slug.vue +++ b/webapp/pages/profile/_id/_slug.vue @@ -67,14 +67,14 @@
- + {{ $t('settings.blocked-users.unblock') }} {{ $t('settings.muted-users.unmute') }} Date: Mon, 10 Feb 2020 13:13:51 +0100 Subject: [PATCH 17/19] Remove unused email property from user factory --- backend/src/db/factories.js | 1 - backend/yarn.lock | 7 +------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/backend/src/db/factories.js b/backend/src/db/factories.js index aa6fcec22..7a8979b3c 100644 --- a/backend/src/db/factories.js +++ b/backend/src/db/factories.js @@ -44,7 +44,6 @@ Factory.define('userWithoutEmailAddress') .attrs({ id: uuid, name: faker.name.findName, - email: faker.internet.email, password: '1234', role: 'user', avatar: faker.internet.avatar, diff --git a/backend/yarn.lock b/backend/yarn.lock index 7811c2b81..b092c3864 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -1943,7 +1943,7 @@ apollo-server-caching@^0.5.1: dependencies: lru-cache "^5.0.0" -apollo-server-core@^2.10.0, apollo-server-core@^2.9.16: +apollo-server-core@^2.10.0: version "2.10.0" resolved "https://registry.yarnpkg.com/apollo-server-core/-/apollo-server-core-2.10.0.tgz#b8d51bdffe6529f0e3ca670ee8f1238765cfade4" integrity sha512-x/UK6XvU307W8D/pzTclU04JIjRarcbg5mFPe0nVmO4OTc26uQgKi1WlZkcewXsAUnn+nDwKVn2c2G3dHEgXzQ== @@ -7923,11 +7923,6 @@ serve-static@1.14.1: version "1.14.1" resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.14.1.tgz#666e636dc4f010f7ef29970a88a674320898b2f9" integrity sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg== - dependencies: - encodeurl "~1.0.2" - escape-html "~1.0.3" - parseurl "~1.3.3" - send "0.17.1" set-blocking@^2.0.0, set-blocking@~2.0.0: version "2.0.0" From bcf9680ad993f0566a57d400cd76cda531cceb38 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Mon, 10 Feb 2020 14:33:49 +0100 Subject: [PATCH 18/19] Add type resolver for hasBlocked --- backend/src/schema/resolvers/users.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/src/schema/resolvers/users.js b/backend/src/schema/resolvers/users.js index d1d9111b6..55e4165b7 100644 --- a/backend/src/schema/resolvers/users.js +++ b/backend/src/schema/resolvers/users.js @@ -252,9 +252,11 @@ export default { followedByCurrentUser: 'MATCH (this)<-[:FOLLOWS]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', blocked: - 'MATCH (this)<-[:BLOCKED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', + 'MATCH (this)-[:BLOCKED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', isMuted: 'MATCH (this)<-[:MUTED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', + hasBlocked: + 'MATCH (this)<-[:BLOCKED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', }, count: { contributionsCount: From 5347f734af524c18565f0cf0e8497d074d77c40c Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Mon, 10 Feb 2020 16:25:00 +0100 Subject: [PATCH 19/19] Fix cypress tests/frontend implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Co-authored-by: Wolfgang Huß --- backend/src/schema/resolvers/users.js | 2 +- backend/src/schema/types/type/User.gql | 2 +- cypress/integration/common/steps.js | 13 +++++++++---- cypress/integration/user_profile/BlockUser.feature | 5 ++++- webapp/components/ContentMenu/ContentMenu.vue | 2 +- webapp/graphql/User.js | 1 + webapp/pages/post/_id/_slug/index.vue | 2 +- webapp/pages/profile/_id/_slug.vue | 4 ++-- 8 files changed, 20 insertions(+), 11 deletions(-) diff --git a/backend/src/schema/resolvers/users.js b/backend/src/schema/resolvers/users.js index 55e4165b7..cbdc683e8 100644 --- a/backend/src/schema/resolvers/users.js +++ b/backend/src/schema/resolvers/users.js @@ -255,7 +255,7 @@ export default { 'MATCH (this)-[:BLOCKED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', isMuted: 'MATCH (this)<-[:MUTED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', - hasBlocked: + isBlocked: 'MATCH (this)<-[:BLOCKED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', }, count: { diff --git a/backend/src/schema/types/type/User.gql b/backend/src/schema/types/type/User.gql index ec9bfeba7..baefc9d29 100644 --- a/backend/src/schema/types/type/User.gql +++ b/backend/src/schema/types/type/User.gql @@ -68,7 +68,7 @@ type User { RETURN COUNT(u) >= 1 """ ) - hasBlocked: Boolean! @cypher( + isBlocked: Boolean! @cypher( statement: """ MATCH (this)<-[:BLOCKED]-(user:User {id: $cypherParams.currentUserId}) RETURN COUNT(user) >= 1 diff --git a/cypress/integration/common/steps.js b/cypress/integration/common/steps.js index 52e8d7bba..c51290537 100644 --- a/cypress/integration/common/steps.js +++ b/cypress/integration/common/steps.js @@ -591,13 +591,18 @@ Then("I should see no users in my blocked users list", () => { .should('contain', "So far, you have not blocked anybody.") }) -Then("I should not see {string} from the content menu in the user info box", link => { +Then("I {string} see {string} from the content menu in the user info box", (condition, link) => { cy.get(".user-content-menu .base-button").click() cy.get(".popover .ds-menu-item-link") - .should('not.contain', link) + .should(condition === 'should' ? 'contain' : 'not.contain', link) }) Then('I should not see {string} button', button => { - cy.get('.ds-card-content') - .should('not.contain', '.action-buttons') + cy.get('.ds-card-content .action-buttons') + .should('have.length', 1) +}) + +Then('I should see the {string} button', button => { + cy.get('.ds-card-content .action-buttons .base-button') + .should('contain', button) }) diff --git a/cypress/integration/user_profile/BlockUser.feature b/cypress/integration/user_profile/BlockUser.feature index bf56e2b02..256d79dfb 100644 --- a/cypress/integration/user_profile/BlockUser.feature +++ b/cypress/integration/user_profile/BlockUser.feature @@ -11,6 +11,7 @@ Feature: Block a User Scenario: Block a user Given I am on the profile page of the annoying user When I click on "Block user" from the content menu in the user info box + And I "should" see "Unblock user" from the content menu in the user info box And I navigate to my "Blocked users" settings page Then I can see the following table: | Avatar | Name | @@ -28,6 +29,7 @@ Feature: Block a User When I visit the profile page of the annoying user And I click on "Block user" from the content menu in the user info box And I get removed from his follower collection + And I "should" see "Unblock user" from the content menu in the user info box Scenario: Posts of blocked users are not filtered from search results Given "Harassing User" wrote a post "You can still see my posts" @@ -53,5 +55,6 @@ Feature: Block a User Scenario: Blocked users should not see link or button to unblock, only blocking users Given a user has blocked me When I visit the profile page of the annoying user - And I should not see "Unblock user" from the content menu in the user info box + And I "should not" see "Unblock user" from the content menu in the user info box + And I should see the "Follow" button And I should not see "Unblock user" button \ No newline at end of file diff --git a/webapp/components/ContentMenu/ContentMenu.vue b/webapp/components/ContentMenu/ContentMenu.vue index 2be51c6ac..cee8a6f5c 100644 --- a/webapp/components/ContentMenu/ContentMenu.vue +++ b/webapp/components/ContentMenu/ContentMenu.vue @@ -172,7 +172,7 @@ export default { icon: 'microphone-slash', }) } - if (this.resource.hasBlocked) { + if (this.resource.isBlocked) { routes.push({ label: this.$t(`settings.blocked-users.unblock`), callback: () => { diff --git a/webapp/graphql/User.js b/webapp/graphql/User.js index a73941794..c36383a95 100644 --- a/webapp/graphql/User.js +++ b/webapp/graphql/User.js @@ -24,6 +24,7 @@ export default i18n => { createdAt followedByCurrentUser isMuted + isBlocked blocked following(first: 7) { ...user diff --git a/webapp/pages/post/_id/_slug/index.vue b/webapp/pages/post/_id/_slug/index.vue index 1d107941a..8115294c5 100644 --- a/webapp/pages/post/_id/_slug/index.vue +++ b/webapp/pages/post/_id/_slug/index.vue @@ -97,7 +97,7 @@ :post="post" @createComment="createComment" /> - + {{ $t('settings.blocked-users.explanation.commenting-disabled') }}
{{ $t('settings.blocked-users.explanation.commenting-explanation') }} diff --git a/webapp/pages/profile/_id/_slug.vue b/webapp/pages/profile/_id/_slug.vue index 4d9653dcb..372b73b40 100644 --- a/webapp/pages/profile/_id/_slug.vue +++ b/webapp/pages/profile/_id/_slug.vue @@ -67,14 +67,14 @@
- + {{ $t('settings.blocked-users.unblock') }} {{ $t('settings.muted-users.unmute') }}