From 4d5769fbc6d350558acafd22fc597ef150174fa1 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Mon, 2 Sep 2019 16:03:15 +0200 Subject: [PATCH] Fix cypress tests and factories --- backend/src/seed/factories/index.js | 1 + backend/src/seed/factories/posts.js | 15 +++-- cypress/integration/common/steps.js | 58 ++++++++----------- .../moderation/ReportContent.feature | 7 ++- .../blocked-users/Blocking.feature | 4 +- docker-compose.override.yml | 1 + 6 files changed, 43 insertions(+), 43 deletions(-) diff --git a/backend/src/seed/factories/index.js b/backend/src/seed/factories/index.js index 4cc143e68..4c55f2dc7 100644 --- a/backend/src/seed/factories/index.js +++ b/backend/src/seed/factories/index.js @@ -79,6 +79,7 @@ export default function Factory(options = {}) { this.lastResponse = await factory({ args, neodeInstance, + factoryInstance: this, }) return this.lastResponse } else { diff --git a/backend/src/seed/factories/posts.js b/backend/src/seed/factories/posts.js index 8344e6c89..334a95489 100644 --- a/backend/src/seed/factories/posts.js +++ b/backend/src/seed/factories/posts.js @@ -4,10 +4,9 @@ import uuid from 'uuid/v4' export default function create() { return { - factory: async ({ args, neodeInstance }) => { + factory: async ({ args, neodeInstance, factoryInstance }) => { const defaults = { id: uuid(), - slug: '', title: faker.lorem.sentence(), content: [ faker.lorem.sentence(), @@ -21,11 +20,13 @@ export default function create() { deleted: false, categoryIds: [], } - defaults.slug = slugify(defaults.title, { lower: true }) args = { ...defaults, ...args, } + args.slug = args.slug || slugify(args.title, { lower: true }) + args.contentExcerpt = args.contentExcerpt || args.content + const { categoryIds } = args if (!categoryIds.length) throw new Error('CategoryIds are empty!') const categories = await Promise.all( @@ -33,8 +34,14 @@ export default function create() { return neodeInstance.find('Category', c) }), ) - const author = args.author || (await neodeInstance.create('User', args)) + + let { author, authorId } = args delete args.author + delete args.authorId + if (author && authorId) throw new Error('You provided both author and authorId') + if (authorId) author = await neodeInstance.find('User', authorId) + author = author || (await factoryInstance.create('User', args)) + const post = await neodeInstance.create('Post', args) await post.relateTo(author, 'author') await Promise.all(categories.map(c => c.relateTo(post, 'post'))) diff --git a/cypress/integration/common/steps.js b/cypress/integration/common/steps.js index 386035ea2..558feed69 100644 --- a/cypress/integration/common/steps.js +++ b/cypress/integration/common/steps.js @@ -1,6 +1,5 @@ import { Given, When, Then } from "cypress-cucumber-preprocessor/steps"; import helpers from "../../support/helpers"; -import slugify from "slug"; /* global cy */ @@ -11,6 +10,7 @@ let loginCredentials = { password: "1234" }; const narratorParams = { + id: 'id-of-peter-pan', name: "Peter Pan", slug: "peter-pan", avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/nerrsoft/128.jpg", @@ -158,40 +158,28 @@ When("I press {string}", label => { cy.contains(label).click(); }); +Given("we have this user in our database:", table => { + const [firstRow] = table.hashes() + cy.factory().create('User', firstRow) +}) + Given("we have the following posts in our database:", table => { - table.hashes().forEach(({ Author, ...postAttributes }, i) => { - Author = Author || `author-${i}`; - const userAttributes = { - name: Author, - email: `${slugify(Author, { lower: true })}@example.org`, - password: "1234" - }; - postAttributes.deleted = Boolean(postAttributes.deleted); - const disabled = Boolean(postAttributes.disabled); - postAttributes.categoryIds = [`cat${i}${new Date()}`]; - postAttributes; - cy.factory() - .create("User", userAttributes) - .authenticateAs(userAttributes) - .create("Category", { - id: `cat${i}${new Date()}`, - name: "Just For Fun", - slug: `just-for-fun-${i}`, - icon: "smile" - }) - .create("Post", postAttributes); - if (disabled) { - const moderatorParams = { - email: "moderator@example.org", - role: "moderator", - password: "1234" - }; - cy.factory() - .create("User", moderatorParams) - .authenticateAs(moderatorParams) - .mutate("mutation($id: ID!) { disable(id: $id) }", postAttributes); + cy.factory().create('Category', { + id: `cat-456`, + name: "Just For Fun", + slug: `just-for-fun`, + icon: "smile" + }) + + table.hashes().forEach(({ ...postAttributes }, i) => { + postAttributes = { + ...postAttributes, + deleted: Boolean(postAttributes.deleted), + disabled: Boolean(postAttributes.disabled), + categoryIds: ['cat-456'] } - }); + cy.factory().create("Post", postAttributes); + }) }); Then("I see a success message:", message => { @@ -210,11 +198,11 @@ When( ); Given("I previously created a post", () => { + lastPost.authorId = narratorParams.id lastPost.title = "previously created post"; lastPost.content = "with some content"; - lastPost.categoryIds = "cat0"; + lastPost.categoryIds = ["cat0"]; cy.factory() - .authenticateAs(loginCredentials) .create("Post", lastPost); }); diff --git a/cypress/integration/moderation/ReportContent.feature b/cypress/integration/moderation/ReportContent.feature index 62fb4f421..a5f7f2f50 100644 --- a/cypress/integration/moderation/ReportContent.feature +++ b/cypress/integration/moderation/ReportContent.feature @@ -8,9 +8,12 @@ Feature: Report and Moderate So I can look into it and decide what to do Background: + Given we have this user in our database: + | id | name | + | u67 | David Irving| Given we have the following posts in our database: - | Author | id | title | content | - | David Irving | p1 | The Truth about the Holocaust | It never existed! | + | authorId | id | title | content | + | u67 | p1 | The Truth about the Holocaust | It never existed! | Scenario Outline: Report a post from various pages diff --git a/cypress/integration/user_profile/blocked-users/Blocking.feature b/cypress/integration/user_profile/blocked-users/Blocking.feature index ed784b803..6ff81f4dc 100644 --- a/cypress/integration/user_profile/blocked-users/Blocking.feature +++ b/cypress/integration/user_profile/blocked-users/Blocking.feature @@ -27,8 +27,8 @@ Feature: Block a User Scenario: Posts of blocked users are filtered from search results Given we have the following posts in our database: - | Author | id | title | content | - | Some unblocked user | im-not-blocked | Post that should be seen | cause I'm not blocked | + | id | title | content | + | im-not-blocked | Post that should be seen | cause I'm not blocked | Given "Spammy Spammer" wrote a post "Spam Spam Spam" When I search for "Spam" Then I should see the following posts in the select dropdown: diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 41a88970f..32ed3ab92 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -54,6 +54,7 @@ services: - SMTP_HOST=mailserver - SMTP_PORT=25 - SMTP_IGNORE_TLS=true + - "DEBUG=${DEBUG}" neo4j: environment: - NEO4J_AUTH=none