Skip to content

Commit

Permalink
DEV: User email test optimization (discourse#7405)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwaterworth authored and tgxworld committed Apr 23, 2019
1 parent 8c8d3be commit bf313c1
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions spec/fabricators/user_fabricator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Fabricator(:user_stat) do
end

Fabricator(:user_single_email, class_name: :user) do
Fabricator(:user, class_name: :user) do
name 'Bruce Wayne'
username { sequence(:username) { |i| "bruce#{i}" } }
email { sequence(:email) { |i| "bruce#{i}@wayne.com" } }
Expand All @@ -11,7 +11,7 @@
active true
end

Fabricator(:user, from: :user_single_email) do
Fabricator(:user_with_secondary_email, from: :user) do
after_create { |user| Fabricate(:secondary_email, user: user) }
end

Expand Down
2 changes: 1 addition & 1 deletion spec/models/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def real_staff
group = Group.refresh_automatic_group!(:staff)
expect(group.name).to eq('staff')

Fabricate(:user_single_email, username: I18n.t('groups.default_names.moderators').upcase)
Fabricate(:user, username: I18n.t('groups.default_names.moderators').upcase)
group = Group.refresh_automatic_group!(:moderators)
expect(group.name).to eq('moderators')
end
Expand Down
8 changes: 4 additions & 4 deletions spec/models/user_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
describe UserEmail do
context "validation" do
it "allows only one primary email" do
user = Fabricate(:user_single_email)
user = Fabricate(:user)
expect {
Fabricate(:secondary_email, user: user, primary: true)
}.to raise_error(ActiveRecord::RecordInvalid)
end

it "allows multiple secondary emails" do
user = Fabricate(:user_single_email)
user = Fabricate(:user)
Fabricate(:secondary_email, user: user, primary: false)
Fabricate(:secondary_email, user: user, primary: false)
expect(user.user_emails.count).to eq 3
Expand All @@ -20,14 +20,14 @@

context "indexes" do
it "allows only one primary email" do
user = Fabricate(:user_single_email)
user = Fabricate(:user)
expect {
Fabricate.build(:secondary_email, user: user, primary: true).save(validate: false)
}.to raise_error(ActiveRecord::RecordNotUnique)
end

it "allows multiple secondary emails" do
user = Fabricate(:user_single_email)
user = Fabricate(:user)
Fabricate.build(:secondary_email, user: user, primary: false).save(validate: false)
Fabricate.build(:secondary_email, user: user, primary: false).save(validate: false)
expect(user.user_emails.count).to eq 3
Expand Down
4 changes: 2 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def assert_good(username)

it 'email whitelist should be used when email is being changed' do
SiteSetting.email_domains_whitelist = 'vaynermedia.com'
u = Fabricate(:user_single_email, email: 'good@vaynermedia.com')
u = Fabricate(:user, email: 'good@vaynermedia.com')
u.email = 'nope@mailinator.com'
expect(u).not_to be_valid
end
Expand Down Expand Up @@ -1812,7 +1812,7 @@ def filter_by(method)
end

describe "#secondary_emails" do
let(:user) { Fabricate(:user_single_email) }
let(:user) { Fabricate(:user) }

it "only contains secondary emails" do
expect(user.user_emails.secondary).to eq([])
Expand Down
6 changes: 3 additions & 3 deletions spec/serializers/admin_user_list_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
end

context "emails" do
let(:admin) { Fabricate(:user_single_email, admin: true, email: "admin@email.com") }
let(:moderator) { Fabricate(:user_single_email, moderator: true, email: "moderator@email.com") }
let(:user) { Fabricate(:user_single_email, email: "user@email.com") }
let(:admin) { Fabricate(:user, admin: true, email: "admin@email.com") }
let(:moderator) { Fabricate(:user, moderator: true, email: "moderator@email.com") }
let(:user) { Fabricate(:user, email: "user@email.com") }

def serialize(user, viewed_by, opts = nil)
AdminUserListSerializer.new(
Expand Down
2 changes: 1 addition & 1 deletion spec/services/anonymous_shadow_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

before { SiteSetting.allow_anonymous_posting = true }

let(:user) { Fabricate(:user_single_email, trust_level: 3) }
let(:user) { Fabricate(:user, trust_level: 3) }

it "returns no shadow if trust level is not met" do
expect(AnonymousShadowCreator.get(Fabricate.build(:user, trust_level: 0))).to eq(nil)
Expand Down
2 changes: 1 addition & 1 deletion spec/services/user_anonymizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

describe "make_anonymous" do
let(:original_email) { "edward@example.net" }
let(:user) { Fabricate(:user_single_email, username: "edward", email: original_email) }
let(:user) { Fabricate(:user, username: "edward", email: original_email) }
let(:another_user) { Fabricate(:evil_trout) }
subject(:make_anonymous) { described_class.make_anonymous(user, admin) }

Expand Down
2 changes: 1 addition & 1 deletion spec/services/user_destroyer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
describe 'destroy' do
before do
@admin = Fabricate(:admin)
@user = Fabricate(:user)
@user = Fabricate(:user_with_secondary_email)
end

it 'raises an error when user is nil' do
Expand Down
6 changes: 3 additions & 3 deletions spec/services/user_merger_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'rails_helper'

describe UserMerger do
let!(:target_user) { Fabricate(:user_single_email, username: 'alice', email: 'alice@example.com') }
let!(:source_user) { Fabricate(:user_single_email, username: 'alice1', email: 'alice@work.com') }
let!(:target_user) { Fabricate(:user, username: 'alice', email: 'alice@example.com') }
let!(:source_user) { Fabricate(:user, username: 'alice1', email: 'alice@work.com') }
let(:walter) { Fabricate(:walter_white) }

def merge_users!(source = nil, target = nil)
Expand Down Expand Up @@ -838,7 +838,7 @@ def create_badge(badge, user, opts = {})
it "skips merging email adresses when a secondary email address exists" do
merge_users!(source_user, target_user)

alice2 = Fabricate(:user_single_email, username: 'alice2', email: 'alice@foo.com')
alice2 = Fabricate(:user, username: 'alice2', email: 'alice@foo.com')
merge_users!(alice2, target_user)

emails = UserEmail.where(user_id: target_user.id).pluck(:email, :primary)
Expand Down

0 comments on commit bf313c1

Please sign in to comment.