From e5be418076da66b9d0a3297c68ae62ef09d5af12 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Mon, 2 Sep 2019 18:14:37 +0200 Subject: [PATCH] Refactor seeds - no need for a factories server! Fix #1427 --- backend/package.json | 5 +- backend/src/models/User.js | 6 + backend/src/seed/seed-db.js | 752 +++++++++++++++--------------------- 3 files changed, 326 insertions(+), 437 deletions(-) diff --git a/backend/package.json b/backend/package.json index 00f7be4bb..8d43b6d67 100644 --- a/backend/package.json +++ b/backend/package.json @@ -19,9 +19,8 @@ "test:jest": "run-p --race test:before:* \"test:jest:cmd {@}\" --", "test:cucumber": " cross-env CLIENT_URI=http://localhost:4123 run-p --race test:before:* 'test:cucumber:cmd {@}' --", "test:jest:debug": "run-p --race test:before:* 'test:jest:cmd:debug {@}' --", - "db:script:seed": "wait-on tcp:4001 && babel-node src/seed/seed-db.js", - "db:reset": "cross-env babel-node src/seed/reset-db.js", - "db:seed": "cross-env GRAPHQL_URI=http://localhost:4001 GRAPHQL_PORT=4001 DISABLED_MIDDLEWARES=permissions run-p --race dev db:script:seed" + "db:reset": "babel-node src/seed/reset-db.js", + "db:seed": "babel-node src/seed/seed-db.js" }, "author": "Human Connection gGmbH", "license": "MIT", diff --git a/backend/src/models/User.js b/backend/src/models/User.js index fa578f8ad..b10144315 100644 --- a/backend/src/models/User.js +++ b/backend/src/models/User.js @@ -83,4 +83,10 @@ module.exports = { target: 'Notification', direction: 'in', }, + shouted: { + type: 'relationship', + relationship: 'SHOUTED', + target: 'Post', + direction: 'out', + }, } diff --git a/backend/src/seed/seed-db.js b/backend/src/seed/seed-db.js index acd50cae3..153b65805 100644 --- a/backend/src/seed/seed-db.js +++ b/backend/src/seed/seed-db.js @@ -1,9 +1,29 @@ import faker from 'faker' +import { createTestClient } from 'apollo-server-testing' +import createServer from '../server' import Factory from './factories' +import { neode as getNeode, getDriver } from '../bootstrap/neo4j' +import { gql } from '../jest/helpers' /* eslint-disable no-multi-spaces */ ;(async function() { + let authenticatedUser = null + const driver = getDriver() + const factory = Factory() + const neode = getNeode() + try { + const { server } = createServer({ + context: () => { + return { + driver, + neode, + user: authenticatedUser, + } + }, + }) + const { mutate } = createTestClient(server) + const f = Factory() const [racoon, rabbit, wolf, bear, turtle, rhino] = await Promise.all([ f.create('Badge', { @@ -36,9 +56,9 @@ import Factory from './factories' peterLustig, bobDerBaumeister, jennyRostock, - tick, // eslint-disable-line no-unused-vars - trick, // eslint-disable-line no-unused-vars - track, // eslint-disable-line no-unused-vars + huey, + dewey, + louie, dagobert, ] = await Promise.all([ f.create('User', { @@ -64,22 +84,22 @@ import Factory from './factories' }), f.create('User', { id: 'u4', - name: 'Huey (Tick)', - slug: 'huey-tick', + name: 'Huey', + slug: 'huey', role: 'user', email: 'huey@example.org', }), f.create('User', { id: 'u5', - name: 'Dewey (Trick)', - slug: 'dewey-trick', + name: 'Dewey', + slug: 'dewey', role: 'user', email: 'dewey@example.org', }), f.create('User', { id: 'u6', - name: 'Louie (Track)', - slug: 'louie-track', + name: 'Louie', + slug: 'louie', role: 'user', email: 'louie@example.org', }), @@ -92,33 +112,6 @@ import Factory from './factories' }), ]) - const [asAdmin, asModerator, asUser, asTick, asTrick, asTrack] = await Promise.all([ - Factory().authenticateAs({ - email: 'admin@example.org', - password: '1234', - }), - Factory().authenticateAs({ - email: 'moderator@example.org', - password: '1234', - }), - Factory().authenticateAs({ - email: 'user@example.org', - password: '1234', - }), - Factory().authenticateAs({ - email: 'huey@example.org', - password: '1234', - }), - Factory().authenticateAs({ - email: 'dewey@example.org', - password: '1234', - }), - Factory().authenticateAs({ - email: 'louie@example.org', - password: '1234', - }), - ]) - await Promise.all([ peterLustig.relateTo(racoon, 'rewarded'), peterLustig.relateTo(rhino, 'rewarded'), @@ -133,16 +126,16 @@ import Factory from './factories' bobDerBaumeister.relateTo(jennyRostock, 'friends'), peterLustig.relateTo(jennyRostock, 'following'), - peterLustig.relateTo(tick, 'following'), - bobDerBaumeister.relateTo(tick, 'following'), - jennyRostock.relateTo(tick, 'following'), - tick.relateTo(track, 'following'), - trick.relateTo(tick, 'following'), - track.relateTo(jennyRostock, 'following'), + peterLustig.relateTo(huey, 'following'), + bobDerBaumeister.relateTo(huey, 'following'), + jennyRostock.relateTo(huey, 'following'), + huey.relateTo(dewey, 'following'), + dewey.relateTo(huey, 'following'), + louie.relateTo(jennyRostock, 'following'), - dagobert.relateTo(tick, 'blocked'), - dagobert.relateTo(trick, 'blocked'), - dagobert.relateTo(track, 'blocked'), + dagobert.relateTo(huey, 'blocked'), + dagobert.relateTo(dewey, 'blocked'), + dagobert.relateTo(louie, 'blocked'), ]) await Promise.all([ @@ -244,25 +237,90 @@ import Factory from './factories' }), ]) - await Promise.all([ + const [environment, nature, democracy, freedom] = await Promise.all([ f.create('Tag', { - id: 'Umwelt', - name: 'Umwelt', + id: 'Environment', }), f.create('Tag', { - id: 'Naturschutz', - name: 'Naturschutz', + id: 'Nature', }), f.create('Tag', { - id: 'Demokratie', - name: 'Demokratie', + id: 'Democracy', }), f.create('Tag', { - id: 'Freiheit', - name: 'Freiheit', + id: 'Freedom', }), ]) + const [p0, p1, p3, p4, p5, p6, p9, p10, p11, p13, p14, p15] = await Promise.all([ + factory.create('Post', { + author: peterLustig, + id: 'p0', + image: faker.image.unsplash.food(), + categoryIds: ['cat16'], + }), + factory.create('Post', { + author: bobDerBaumeister, + id: 'p1', + image: faker.image.unsplash.technology(), + categoryIds: ['cat1'], + }), + factory.create('Post', { + author: huey, + id: 'p3', + categoryIds: ['cat3'], + }), + factory.create('Post', { + author: dewey, + id: 'p4', + categoryIds: ['cat4'], + }), + factory.create('Post', { + author: louie, + id: 'p5', + categoryIds: ['cat5'], + }), + factory.create('Post', { + authorId: 'u1', + id: 'p6', + image: faker.image.unsplash.buildings(), + categoryIds: ['cat6'], + }), + factory.create('Post', { + author: huey, + id: 'p9', + categoryIds: ['cat9'], + }), + factory.create('Post', { + author: dewey, + id: 'p10', + categoryIds: ['cat10'], + }), + factory.create('Post', { + author: louie, + id: 'p11', + image: faker.image.unsplash.people(), + categoryIds: ['cat11'], + }), + factory.create('Post', { + author: bobDerBaumeister, + id: 'p13', + categoryIds: ['cat13'], + }), + factory.create('Post', { + author: jennyRostock, + id: 'p14', + image: faker.image.unsplash.objects(), + categoryIds: ['cat14'], + }), + factory.create('Post', { + author: huey, + id: 'p15', + categoryIds: ['cat15'], + }), + ]) + + authenticatedUser = await louie.toJson() const mention1 = 'Hey @jenny-rostock, what\'s up?' const mention2 = @@ -271,445 +329,271 @@ import Factory from './factories' 'See #NaturphilosophieYoga can really help you!' const hashtagAndMention1 = 'The new physics of #QuantenFlussTheorie can explain #QuantumGravity! @peter-lustig got that already. ;-)' + const createPostMutation = gql` + mutation($id: ID, $title: String!, $content: String!, $categoryIds: [ID]) { + CreatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) { + id + } + } + ` await Promise.all([ - asAdmin.create('Post', { - id: 'p0', - image: faker.image.unsplash.food(), - categoryIds: ['cat16'], + mutate({ + mutation: createPostMutation, + variables: { + id: 'p2', + title: `Nature Philosophy Yoga`, + content: hashtag1, + categoryIds: ['cat2'], + }, }), - asModerator.create('Post', { - id: 'p1', - image: faker.image.unsplash.technology(), - categoryIds: ['cat1'], + mutate({ + mutation: createPostMutation, + variables: { + id: 'p7', + title: 'This is post #7', + content: `${mention1} ${faker.lorem.paragraph()}`, + categoryIds: ['cat7'], + }, }), - asUser.create('Post', { - id: 'p2', - title: `Nature Philosophy Yoga`, - content: `${hashtag1}`, - categoryIds: ['cat2'], + mutate({ + mutation: createPostMutation, + variables: { + id: 'p8', + image: faker.image.unsplash.nature(), + title: `Quantum Flow Theory explains Quantum Gravity`, + content: hashtagAndMention1, + categoryIds: ['cat8'], + }, }), - asTick.create('Post', { - id: 'p3', - categoryIds: ['cat3'], - }), - asTrick.create('Post', { - id: 'p4', - categoryIds: ['cat4'], - }), - asTrack.create('Post', { - id: 'p5', - categoryIds: ['cat5'], - }), - asAdmin.create('Post', { - id: 'p6', - image: faker.image.unsplash.buildings(), - categoryIds: ['cat6'], - }), - asModerator.create('Post', { - id: 'p7', - content: `${mention1} ${faker.lorem.paragraph()}`, - categoryIds: ['cat7'], - }), - asUser.create('Post', { - id: 'p8', - image: faker.image.unsplash.nature(), - title: `Quantum Flow Theory explains Quantum Gravity`, - content: `${hashtagAndMention1}`, - categoryIds: ['cat8'], - }), - asTick.create('Post', { - id: 'p9', - categoryIds: ['cat9'], - }), - asTrick.create('Post', { - id: 'p10', - categoryIds: ['cat10'], - }), - asTrack.create('Post', { - id: 'p11', - image: faker.image.unsplash.people(), - categoryIds: ['cat11'], - }), - asAdmin.create('Post', { - id: 'p12', - content: `${mention2} ${faker.lorem.paragraph()}`, - categoryIds: ['cat12'], - }), - asModerator.create('Post', { - id: 'p13', - categoryIds: ['cat13'], - }), - asUser.create('Post', { - id: 'p14', - image: faker.image.unsplash.objects(), - categoryIds: ['cat14'], - }), - asTick.create('Post', { - id: 'p15', - categoryIds: ['cat15'], - }), - ]) - - await Promise.all([ - f.relate('Post', 'Tags', { - from: 'p0', - to: 'Freiheit', - }), - f.relate('Post', 'Tags', { - from: 'p1', - to: 'Umwelt', - }), - f.relate('Post', 'Tags', { - from: 'p2', - to: 'Naturschutz', - }), - f.relate('Post', 'Tags', { - from: 'p3', - to: 'Demokratie', - }), - f.relate('Post', 'Tags', { - from: 'p4', - to: 'Freiheit', - }), - f.relate('Post', 'Tags', { - from: 'p5', - to: 'Umwelt', - }), - f.relate('Post', 'Tags', { - from: 'p6', - to: 'Naturschutz', - }), - f.relate('Post', 'Tags', { - from: 'p7', - to: 'Demokratie', - }), - f.relate('Post', 'Tags', { - from: 'p8', - to: 'Freiheit', - }), - f.relate('Post', 'Tags', { - from: 'p9', - to: 'Umwelt', - }), - f.relate('Post', 'Tags', { - from: 'p10', - to: 'Naturschutz', - }), - f.relate('Post', 'Tags', { - from: 'p11', - to: 'Demokratie', - }), - f.relate('Post', 'Tags', { - from: 'p12', - to: 'Freiheit', - }), - f.relate('Post', 'Tags', { - from: 'p13', - to: 'Umwelt', - }), - f.relate('Post', 'Tags', { - from: 'p14', - to: 'Naturschutz', - }), - f.relate('Post', 'Tags', { - from: 'p15', - to: 'Demokratie', - }), - f.emote({ - from: 'u1', - to: 'p15', - data: 'surprised', - }), - f.emote({ - from: 'u2', - to: 'p15', - data: 'surprised', - }), - f.emote({ - from: 'u3', - to: 'p15', - data: 'surprised', - }), - f.emote({ - from: 'u4', - to: 'p15', - data: 'surprised', - }), - f.emote({ - from: 'u5', - to: 'p15', - data: 'surprised', - }), - f.emote({ - from: 'u6', - to: 'p15', - data: 'surprised', - }), - f.emote({ - from: 'u7', - to: 'p15', - data: 'surprised', - }), - f.emote({ - from: 'u2', - to: 'p14', - data: 'cry', - }), - f.emote({ - from: 'u3', - to: 'p13', - data: 'angry', - }), - f.emote({ - from: 'u4', - to: 'p12', - data: 'funny', - }), - f.emote({ - from: 'u5', - to: 'p11', - data: 'surprised', - }), - f.emote({ - from: 'u6', - to: 'p10', - data: 'cry', - }), - f.emote({ - from: 'u5', - to: 'p9', - data: 'happy', - }), - f.emote({ - from: 'u4', - to: 'p8', - data: 'angry', - }), - f.emote({ - from: 'u3', - to: 'p7', - data: 'funny', - }), - f.emote({ - from: 'u2', - to: 'p6', - data: 'surprised', - }), - f.emote({ - from: 'u1', - to: 'p5', - data: 'cry', - }), - f.emote({ - from: 'u2', - to: 'p4', - data: 'happy', - }), - f.emote({ - from: 'u3', - to: 'p3', - data: 'angry', - }), - f.emote({ - from: 'u4', - to: 'p2', - data: 'funny', - }), - f.emote({ - from: 'u5', - to: 'p1', - data: 'surprised', - }), - f.emote({ - from: 'u6', - to: 'p0', - data: 'cry', - }), - ]) - - await Promise.all([ - asAdmin.shout({ - id: 'p2', - type: 'Post', - }), - asAdmin.shout({ - id: 'p6', - type: 'Post', - }), - asModerator.shout({ - id: 'p0', - type: 'Post', - }), - asModerator.shout({ - id: 'p6', - type: 'Post', - }), - asUser.shout({ - id: 'p6', - type: 'Post', - }), - asUser.shout({ - id: 'p7', - type: 'Post', - }), - asTick.shout({ - id: 'p8', - type: 'Post', - }), - asTick.shout({ - id: 'p9', - type: 'Post', - }), - asTrack.shout({ - id: 'p10', - type: 'Post', - }), - ]) - await Promise.all([ - asAdmin.shout({ - id: 'p2', - type: 'Post', - }), - asAdmin.shout({ - id: 'p6', - type: 'Post', - }), - asModerator.shout({ - id: 'p0', - type: 'Post', - }), - asModerator.shout({ - id: 'p6', - type: 'Post', - }), - asUser.shout({ - id: 'p6', - type: 'Post', - }), - asUser.shout({ - id: 'p7', - type: 'Post', - }), - asTick.shout({ - id: 'p8', - type: 'Post', - }), - asTick.shout({ - id: 'p9', - type: 'Post', - }), - asTrack.shout({ - id: 'p10', - type: 'Post', + mutate({ + mutation: createPostMutation, + variables: { + id: 'p12', + title: 'This is post #12', + content: `${mention2} ${faker.lorem.paragraph()}`, + categoryIds: ['cat12'], + }, }), ]) + const [p2, p7, p8, p12] = await Promise.all( + ['p2', 'p7', 'p8', 'p12'].map(id => neode.find('Post', id)), + ) + authenticatedUser = null + authenticatedUser = await dewey.toJson() const mentionInComment1 = 'I heard @jenny-rostock, practice it since 3 years now.' const mentionInComment2 = 'Did @peter-lustig told you?' + const createCommentMutation = gql` + mutation($id: ID, $postId: ID!, $content: String!) { + CreateComment(id: $id, postId: $postId, content: $content) { + id + } + } + ` + await Promise.all([ + mutate({ + mutation: createCommentMutation, + variables: { + id: 'c4', + postId: 'p2', + content: mentionInComment1, + }, + }), + mutate({ + mutation: createCommentMutation, + variables: { + id: 'c4-1', + postId: 'p2', + content: mentionInComment2, + }, + }), + mutate({ + mutation: createCommentMutation, + variables: { + postId: 'p14', + content: faker.lorem.paragraph(), + }, + }), // should send a notification + ]) + authenticatedUser = null await Promise.all([ - asUser.create('Comment', { + factory.create('Comment', { + author: jennyRostock, id: 'c1', postId: 'p1', }), - asTick.create('Comment', { + factory.create('Comment', { + author: huey, id: 'c2', postId: 'p1', }), - asTrack.create('Comment', { + factory.create('Comment', { + author: louie, id: 'c3', postId: 'p3', }), - asTrick.create('Comment', { - id: 'c4', - postId: 'p2', - content: `${mentionInComment1}`, - }), - asUser.create('Comment', { - id: 'c4-1', - postId: 'p2', - content: `${mentionInComment2}`, - }), - asModerator.create('Comment', { + factory.create('Comment', { + author: bobDerBaumeister, id: 'c5', postId: 'p3', }), - asAdmin.create('Comment', { + factory.create('Comment', { + author: peterLustig, id: 'c6', postId: 'p4', }), - asUser.create('Comment', { + factory.create('Comment', { + author: jennyRostock, id: 'c7', postId: 'p2', }), - asTick.create('Comment', { + factory.create('Comment', { + author: huey, id: 'c8', postId: 'p15', }), - asTrick.create('Comment', { + factory.create('Comment', { + author: dewey, id: 'c9', postId: 'p15', }), - asTrack.create('Comment', { + factory.create('Comment', { + author: louie, id: 'c10', postId: 'p15', }), - asUser.create('Comment', { + factory.create('Comment', { + author: jennyRostock, id: 'c11', postId: 'p15', }), - asUser.create('Comment', { + factory.create('Comment', { + author: jennyRostock, id: 'c12', postId: 'p15', }), ]) - const disableMutation = 'mutation($id: ID!) { disable(id: $id) }' await Promise.all([ - asModerator.mutate(disableMutation, { - id: 'p11', - }), - asModerator.mutate(disableMutation, { - id: 'c5', - }), + democracy.relateTo(p3, 'post'), + democracy.relateTo(p11, 'post'), + democracy.relateTo(p15, 'post'), + democracy.relateTo(p7, 'post'), + environment.relateTo(p1, 'post'), + environment.relateTo(p5, 'post'), + environment.relateTo(p9, 'post'), + environment.relateTo(p13, 'post'), + freedom.relateTo(p0, 'post'), + freedom.relateTo(p4, 'post'), + freedom.relateTo(p8, 'post'), + freedom.relateTo(p12, 'post'), + nature.relateTo(p2, 'post'), + nature.relateTo(p6, 'post'), + nature.relateTo(p10, 'post'), + nature.relateTo(p14, 'post'), + peterLustig.relateTo(p15, 'emoted', { emotion: 'surprised' }), + bobDerBaumeister.relateTo(p15, 'emoted', { emotion: 'surprised' }), + jennyRostock.relateTo(p15, 'emoted', { emotion: 'surprised' }), + huey.relateTo(p15, 'emoted', { emotion: 'surprised' }), + dewey.relateTo(p15, 'emoted', { emotion: 'surprised' }), + louie.relateTo(p15, 'emoted', { emotion: 'surprised' }), + dagobert.relateTo(p15, 'emoted', { emotion: 'surprised' }), + bobDerBaumeister.relateTo(p14, 'emoted', { emotion: 'cry' }), + jennyRostock.relateTo(p13, 'emoted', { emotion: 'angry' }), + huey.relateTo(p12, 'emoted', { emotion: 'funny' }), + dewey.relateTo(p11, 'emoted', { emotion: 'surprised' }), + louie.relateTo(p10, 'emoted', { emotion: 'cry' }), + dewey.relateTo(p9, 'emoted', { emotion: 'happy' }), + huey.relateTo(p8, 'emoted', { emotion: 'angry' }), + jennyRostock.relateTo(p7, 'emoted', { emotion: 'funny' }), + bobDerBaumeister.relateTo(p6, 'emoted', { emotion: 'surprised' }), + peterLustig.relateTo(p5, 'emoted', { emotion: 'cry' }), + bobDerBaumeister.relateTo(p4, 'emoted', { emotion: 'happy' }), + jennyRostock.relateTo(p3, 'emoted', { emotion: 'angry' }), + huey.relateTo(p2, 'emoted', { emotion: 'funny' }), + dewey.relateTo(p1, 'emoted', { emotion: 'surprised' }), + louie.relateTo(p0, 'emoted', { emotion: 'cry' }), ]) await Promise.all([ - asTick.create('Report', { - description: "I don't like this comment", - id: 'c1', - }), - asTrick.create('Report', { - description: "I don't like this post", - id: 'p1', - }), - asTrack.create('Report', { - description: "I don't like this user", - id: 'u1', - }), + peterLustig.relateTo(p1, 'shouted'), + peterLustig.relateTo(p6, 'shouted'), + bobDerBaumeister.relateTo(p0, 'shouted'), + bobDerBaumeister.relateTo(p6, 'shouted'), + jennyRostock.relateTo(p6, 'shouted'), + jennyRostock.relateTo(p7, 'shouted'), + huey.relateTo(p8, 'shouted'), + huey.relateTo(p9, 'shouted'), + dewey.relateTo(p10, 'shouted'), + peterLustig.relateTo(p2, 'shouted'), + peterLustig.relateTo(p6, 'shouted'), + bobDerBaumeister.relateTo(p0, 'shouted'), + bobDerBaumeister.relateTo(p6, 'shouted'), + jennyRostock.relateTo(p6, 'shouted'), + jennyRostock.relateTo(p7, 'shouted'), + huey.relateTo(p8, 'shouted'), + huey.relateTo(p9, 'shouted'), + louie.relateTo(p10, 'shouted'), ]) + const disableMutation = gql` + mutation($id: ID!) { + disable(id: $id) + } + ` + authenticatedUser = await bobDerBaumeister.toJson() await Promise.all([ - f.relate('Organization', 'CreatedBy', { - from: 'u1', - to: 'o1', + mutate({ + mutation: disableMutation, + variables: { + id: 'p11', + }, }), - f.relate('Organization', 'CreatedBy', { - from: 'u1', - to: 'o2', - }), - f.relate('Organization', 'OwnedBy', { - from: 'u2', - to: 'o2', - }), - f.relate('Organization', 'OwnedBy', { - from: 'u2', - to: 'o3', + mutate({ + mutation: disableMutation, + variables: { + id: 'c5', + }, }), ]) + authenticatedUser = null + + const reportMutation = gql` + mutation($id: ID!, $description: String!) { + report(description: $description, id: $id) { + id + } + } + ` + authenticatedUser = await huey.toJson() + await Promise.all([ + mutate({ + mutation: reportMutation, + variables: { + description: "I don't like this comment", + id: 'c1', + }, + }), + mutate({ + mutation: reportMutation, + variables: { + description: "I don't like this post", + id: 'p1', + }, + }), + mutate({ + mutation: reportMutation, + variables: { + description: "I don't like this user", + id: 'u1', + }, + }), + ]) + authenticatedUser = null await Promise.all( [...Array(30).keys()].map(i => {