diff --git a/backend/src/activitypub/routes/webfinger.spec.js b/backend/src/activitypub/routes/webfinger.spec.js index 06ca4577d2..1a81c219bd 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 fa426fb55f..f2ec318762 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 0000000000..b1ee08fe5f --- /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 3c981ae8a5..0000000000 --- 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 5dd89efbe7..0000000000 --- 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 0ecbb03ec9..0000000000 --- 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 a17c2511a0..0000000000 --- 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 8be427058f..0000000000 --- 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 983aaae263..0000000000 --- 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 29b6c30987..0000000000 --- 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 b6c7f14245..0000000000 --- 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 9e3024e35a..0000000000 --- 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 2baf058a05..0000000000 --- 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 cf7ca17299..0000000000 --- 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 2d6bd9f57a..0000000000 --- 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 91b9e9a8b0..0000000000 --- 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 d0bf2005df..1163f71a86 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 4967978a59..8560c97d41 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 0d07142896..fdc4d2a48d 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 8d92a5b5d3..2bca363643 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 7c62cfcc0d..5b24b3e741 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 c3c7384dd2..e13d66407f 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 713020d4f1..8d8adb56ba 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 4b9874d7b3..25524c9739 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 7bdde7014e..d112cde9ad 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 bcc3c542f1..80b9df8149 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 c382eb475c..2888651b5c 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 470eb7635a..7ef018739a 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 d7f5e31135..9bfdaf3a40 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 aba11f9bcd..6e2046132e 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 856f094d18..8ebce9811c 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 1ce95ddd4f..d2eb9b88ca 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 97fba47f4f..0d297a625c 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 b8fe25ec37..f6471035ab 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 36514bf4d6..5543d07702 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 9cd4c1279c..34f46c4fa0 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 ba5c2dc422..df15f4682e 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 dfe14f875a..9fa595db7f 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 05bb13eb44..dd8b71580b 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 e2b9dafe44..2ef1780efb 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 27bc84b10e..dd304077bf 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 3806319f39..7d5cab853d 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 85c0f091f0..824e03c2d3 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 130df08ced..68b26941d4 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 ff9d45b279..4e92688184 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 6b808c75fb..664d71d1bd 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 3867b35138..a0c3f30ae0 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 c9a2e213a6..6ab0a77cef 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 d5d4ee7681..7103ce524b 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 71f493834c..13e51007ee 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, })