Make e2e seed safe to run twice (#15049)

* Make e2e seed safe to run twice

We have a number of checks in the seed file that prevent creating
objects if they're found in the database, but nothing is guarding
against this breaking.

Change a few create! to create_with().find_or_create_by() and update
guard clauses where we create if doesn't exist to match the objects
created. These had drifted, preventing subsequent runs of the seed in
a "dirty" database. In CI we're typically creating a new database,
loading schema, and only running this once, so the guards are never
checked and the objects are always created, but locally that might not
always be the case.

* Update multiple guards

We're creating test users against @forem.local domain, let's check
that there's not already a forem.local user with the same email
account.

Additionally, something was causing the "find article by title" to
miss identifying the commented article, but it was not create()'d due
to a slug collision, and the comment could not be created. Update the
call to use `create!` instead (since we rely on it existing in the
comment create) and guard against the slug instead of the title.
This commit is contained in:
Daniel Uber 2021-10-12 10:43:08 -05:00 committed by GitHub
parent c012149703
commit 5c69cf5880
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,8 +18,8 @@ Settings::SMTP.password = "password"
##############################################################################
# Some of our Cypress tests assume specific DEV profile fields to exist
ProfileField.create!(label: "Work", display_area: :header)
ProfileField.create!(label: "Education", display_area: :header)
ProfileField.create_with(display_area: :header).find_or_create_by(label: "Work")
ProfileField.create_with(display_area: :header).find_or_create_by(label: "Education")
Profile.refresh_attributes!
##############################################################################
@ -133,7 +133,7 @@ end
##############################################################################
seeder.create_if_doesnt_exist(User, "email", "article-editor-v1-user@forem.com") do
seeder.create_if_doesnt_exist(User, "email", "article-editor-v1-user@forem.local") do
user = User.create!(
name: "Article Editor v1 User",
email: "article-editor-v1-user@forem.local",
@ -160,7 +160,7 @@ end
##############################################################################
seeder.create_if_doesnt_exist(User, "email", "article-editor-v2-user@forem.com") do
seeder.create_if_doesnt_exist(User, "email", "article-editor-v2-user@forem.local") do
user = User.create!(
name: "Article Editor v2 User",
email: "article-editor-v2-user@forem.local",
@ -237,7 +237,7 @@ chat_user_2 = seeder.create_if_doesnt_exist(User, "email", "chat-user-2@forem.lo
end
##############################################################################
seeder.create_if_doesnt_exist(User, "email", "notifications-user@forem.com") do
seeder.create_if_doesnt_exist(User, "email", "notifications-user@forem.local") do
user = User.create!(
name: "Notifications User",
email: "notifications-user@forem.local",
@ -265,7 +265,7 @@ end
##############################################################################
seeder.create_if_doesnt_exist(User, "email", "liquid-tags-user@forem.com") do
seeder.create_if_doesnt_exist(User, "email", "liquid-tags-user@forem.local") do
liquid_tags_user = User.create!(
name: "Liquid tags User",
email: "liquid-tags-user@forem.local",
@ -344,7 +344,7 @@ end
##############################################################################
seeder.create_if_doesnt_exist(Article, "title", "Test article") do
seeder.create_if_doesnt_exist(Article, "slug", "test-article-slug") do
markdown = <<~MARKDOWN
---
title: Test article
@ -355,7 +355,7 @@ seeder.create_if_doesnt_exist(Article, "title", "Test article") do
#{Faker::Markdown.random}
#{Faker::Hipster.paragraph(sentence_count: 2)}
MARKDOWN
article = Article.create(
article = Article.create!(
body_markdown: markdown,
featured: true,
show_comments: true,
@ -397,7 +397,7 @@ end
##############################################################################
seeder.create_if_doesnt_exist(User, "email", "series-user@forem.com") do
seeder.create_if_doesnt_exist(User, "email", "series-user@forem.local") do
series_user = User.create!(
name: "Series User",
email: "series-user@forem.local",
@ -552,7 +552,7 @@ end
##############################################################################
seeder.create_if_doesnt_exist(Podcast, "title", "Test podcast") do
seeder.create_if_doesnt_exist(Podcast, "title", "Developer on Fire") do
podcast_attributes = {
title: "Developer on Fire",
description: "",