PERF: Speed up tests (#15214)

This commit is contained in:
Daniel Waterworth 2021-12-07 12:45:58 -06:00 committed by GitHub
parent 95c75d3f4f
commit 8371c96b7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 59 deletions

View File

@ -4,6 +4,8 @@ require 'rails_helper'
require 'pretty_text' require 'pretty_text'
describe PrettyText do describe PrettyText do
fab!(:user) { Fabricate(:user) }
fab!(:post) { Fabricate(:post) }
before do before do
SiteSetting.enable_markdown_typographer = false SiteSetting.enable_markdown_typographer = false
@ -23,7 +25,6 @@ describe PrettyText do
describe "with avatar" do describe "with avatar" do
let(:default_avatar) { "//test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/{size}.png" } let(:default_avatar) { "//test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/{size}.png" }
fab!(:user) { Fabricate(:user) }
before do before do
User.stubs(:default_template).returns(default_avatar) User.stubs(:default_template).returns(default_avatar)
@ -349,8 +350,6 @@ describe PrettyText do
end end
describe "with letter avatar" do describe "with letter avatar" do
fab!(:user) { Fabricate(:user) }
context "subfolder" do context "subfolder" do
it "should have correct avatar url" do it "should have correct avatar url" do
set_subfolder "/forum" set_subfolder "/forum"
@ -455,8 +454,6 @@ describe PrettyText do
end end
it 'should not convert mentions to links' do it 'should not convert mentions to links' do
_user = Fabricate(:user)
expect(PrettyText.cook('hi @user')).to eq('<p>hi @user</p>') expect(PrettyText.cook('hi @user')).to eq('<p>hi @user</p>')
end end
end end
@ -1027,7 +1024,6 @@ describe PrettyText do
describe 'format_for_email' do describe 'format_for_email' do
let(:base_url) { "http://baseurl.net" } let(:base_url) { "http://baseurl.net" }
fab!(:post) { Fabricate(:post) }
before do before do
Discourse.stubs(:base_url).returns(base_url) Discourse.stubs(:base_url).returns(base_url)
@ -1648,7 +1644,7 @@ HTML
end end
it "can onebox local topics" do it "can onebox local topics" do
op = Fabricate(:post) op = post
reply = Fabricate(:post, topic_id: op.topic_id) reply = Fabricate(:post, topic_id: op.topic_id)
url = Discourse.base_url + reply.url url = Discourse.base_url + reply.url

View File

@ -4,6 +4,10 @@ require 'rails_helper'
require 'rotp' require 'rotp'
describe UsersController do describe UsersController do
fab!(:user1) { Fabricate(:user) }
fab!(:other_user) { Fabricate(:user) }
fab!(:admin) { Fabricate(:admin) }
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
describe "#full account registration flow" do describe "#full account registration flow" do
@ -139,7 +143,7 @@ describe UsersController do
end end
describe '#password_reset' do describe '#password_reset' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
let(:token) { SecureRandom.hex } let(:token) { SecureRandom.hex }
context "you can view it even if login is required" do context "you can view it even if login is required" do
@ -202,7 +206,7 @@ describe UsersController do
end end
context 'valid token' do context 'valid token' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
let!(:user_auth_token) { UserAuthToken.generate!(user_id: user.id) } let!(:user_auth_token) { UserAuthToken.generate!(user_id: user.id) }
let!(:email_token) { Fabricate(:email_token, user: user, scope: EmailToken.scopes[:password_reset]) } let!(:email_token) { Fabricate(:email_token, user: user, scope: EmailToken.scopes[:password_reset]) }
@ -485,7 +489,7 @@ describe UsersController do
end end
describe '#confirm_email_token' do describe '#confirm_email_token' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
let!(:email_token) { Fabricate(:email_token, user: user) } let!(:email_token) { Fabricate(:email_token, user: user) }
it "token doesn't match any records" do it "token doesn't match any records" do
@ -502,8 +506,7 @@ describe UsersController do
end end
describe '#admin_login' do describe '#admin_login' do
fab!(:admin) { Fabricate(:admin) } fab!(:user) { user1 }
fab!(:user) { Fabricate(:user) }
context 'enqueues mail' do context 'enqueues mail' do
it 'enqueues mail with admin email and sso enabled' do it 'enqueues mail with admin email and sso enabled' do
@ -638,7 +641,7 @@ describe UsersController do
end end
it "works with an admin api key" do it "works with an admin api key" do
api_key = Fabricate(:api_key, user: Fabricate(:admin)) api_key = Fabricate(:api_key, user: admin)
post "/u.json", params: post_user_params, headers: { HTTP_API_KEY: api_key.key } post "/u.json", params: post_user_params, headers: { HTTP_API_KEY: api_key.key }
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
@ -751,7 +754,7 @@ describe UsersController do
end end
context "with a regular api key" do context "with a regular api key" do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
fab!(:api_key, refind: false) { Fabricate(:api_key, user: user) } fab!(:api_key, refind: false) { Fabricate(:api_key, user: user) }
it "won't create the user as active with a regular key" do it "won't create the user as active with a regular key" do
@ -764,7 +767,6 @@ describe UsersController do
end end
context "with an admin api key" do context "with an admin api key" do
fab!(:admin) { Fabricate(:admin) }
fab!(:api_key, refind: false) { Fabricate(:api_key, user: admin) } fab!(:api_key, refind: false) { Fabricate(:api_key, user: admin) }
it "creates the user as active with a an admin key" do it "creates the user as active with a an admin key" do
@ -864,7 +866,7 @@ describe UsersController do
end end
context "with a regular api key" do context "with a regular api key" do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
fab!(:api_key, refind: false) { Fabricate(:api_key, user: user) } fab!(:api_key, refind: false) { Fabricate(:api_key, user: user) }
it "won't create the user as staged with a regular key" do it "won't create the user as staged with a regular key" do
@ -877,7 +879,7 @@ describe UsersController do
end end
context "with an admin api key" do context "with an admin api key" do
fab!(:user) { Fabricate(:admin) } fab!(:user) { admin }
fab!(:api_key, refind: false) { Fabricate(:api_key, user: user) } fab!(:api_key, refind: false) { Fabricate(:api_key, user: user) }
it "creates the user as staged with a regular key" do it "creates the user as staged with a regular key" do
@ -1514,7 +1516,7 @@ describe UsersController do
end end
it 'should create a staff action log when a staff member changes the username' do it 'should create a staff action log when a staff member changes the username' do
acting_user = Fabricate(:admin) acting_user = admin
sign_in(acting_user) sign_in(acting_user)
put "/u/#{user.username}/preferences/username.json", params: { new_username: new_username } put "/u/#{user.username}/preferences/username.json", params: { new_username: new_username }
@ -1534,7 +1536,7 @@ describe UsersController do
SiteSetting.discourse_connect_url = 'http://someurl.com' SiteSetting.discourse_connect_url = 'http://someurl.com'
SiteSetting.enable_discourse_connect = true SiteSetting.enable_discourse_connect = true
SiteSetting.auth_overrides_username = true SiteSetting.auth_overrides_username = true
acting_user = Fabricate(:admin) acting_user = admin
sign_in(acting_user) sign_in(acting_user)
put "/u/#{user.username}/preferences/username.json", params: { new_username: new_username } put "/u/#{user.username}/preferences/username.json", params: { new_username: new_username }
@ -1579,7 +1581,7 @@ describe UsersController do
end end
context 'username is unavailable' do context 'username is unavailable' do
let!(:user) { Fabricate(:user) } fab!(:user) { user1 }
before do before do
get "/u/check_username.json", params: { username: user.username } get "/u/check_username.json", params: { username: user.username }
end end
@ -1620,7 +1622,7 @@ describe UsersController do
describe 'different case of existing username' do describe 'different case of existing username' do
context "it's my username" do context "it's my username" do
let!(:user) { Fabricate(:user, username: 'hansolo') } fab!(:user) { Fabricate(:user, username: 'hansolo') }
before do before do
sign_in(user) sign_in(user)
@ -1630,7 +1632,7 @@ describe UsersController do
end end
context "it's someone else's username" do context "it's someone else's username" do
let!(:user) { Fabricate(:user, username: 'hansolo') } fab!(:user) { Fabricate(:user, username: 'hansolo') }
before do before do
sign_in(Fabricate(:user)) sign_in(Fabricate(:user))
@ -1640,9 +1642,9 @@ describe UsersController do
end end
context "an admin changing it for someone else" do context "an admin changing it for someone else" do
let!(:user) { Fabricate(:user, username: 'hansolo') } fab!(:user) { Fabricate(:user, username: 'hansolo') }
before do before do
sign_in(Fabricate(:admin)) sign_in(admin)
get "/u/check_username.json", params: { username: 'HanSolo', for_user_id: user.id } get "/u/check_username.json", params: { username: 'HanSolo', for_user_id: user.id }
end end
@ -2220,7 +2222,7 @@ describe UsersController do
end end
describe '#badge_title' do describe '#badge_title' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
fab!(:badge) { Fabricate(:badge) } fab!(:badge) { Fabricate(:badge) }
let(:user_badge) { BadgeGranter.grant(badge, user) } let(:user_badge) { BadgeGranter.grant(badge, user) }
@ -2589,7 +2591,12 @@ describe UsersController do
context 'while logged in' do context 'while logged in' do
let!(:user) { sign_in(Fabricate(:user)) } fab!(:user) { user1 }
before do
sign_in(user)
end
fab!(:avatar1) { Fabricate(:upload) } fab!(:avatar1) { Fabricate(:upload) }
fab!(:avatar2) { Fabricate(:upload) } fab!(:avatar2) { Fabricate(:upload) }
let(:url) { "https://www.discourse.org" } let(:url) { "https://www.discourse.org" }
@ -2671,7 +2678,7 @@ describe UsersController do
context 'while logged in' do context 'while logged in' do
fab!(:another_user) { Fabricate(:user) } fab!(:another_user) { Fabricate(:user) }
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
before do before do
sign_in(user) sign_in(user)
@ -2708,7 +2715,7 @@ describe UsersController do
end end
context 'while logged in' do context 'while logged in' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
fab!(:another_user) { Fabricate(:user) } fab!(:another_user) { Fabricate(:user) }
before do before do
sign_in(user) sign_in(user)
@ -2830,7 +2837,11 @@ describe UsersController do
end end
context "when the user is logged in" do context "when the user is logged in" do
let!(:user) { sign_in(Fabricate(:user)) } fab!(:user) { user1 }
before do
sign_in(user)
end
it "will not redirect to an invalid path" do it "will not redirect to an invalid path" do
get "/my/wat/..password.txt" get "/my/wat/..password.txt"
@ -2955,7 +2966,7 @@ describe UsersController do
context 'while logged in' do context 'while logged in' do
let(:sign_in_admin) { sign_in(Fabricate(:admin)) } let(:sign_in_admin) { sign_in(Fabricate(:admin)) }
let(:user) { Fabricate(:user) } fab!(:user) { user1 }
it "raises an error when you aren't allowed to check sso payload" do it "raises an error when you aren't allowed to check sso payload" do
sign_in(Fabricate(:user)) sign_in(Fabricate(:user))
@ -2976,7 +2987,7 @@ describe UsersController do
end end
describe '#update_primary_email' do describe '#update_primary_email' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
fab!(:user_email) { user.primary_email } fab!(:user_email) { user.primary_email }
fab!(:other_email) { Fabricate(:secondary_email, user: user) } fab!(:other_email) { Fabricate(:secondary_email, user: user) }
@ -3007,7 +3018,7 @@ describe UsersController do
end end
describe '#destroy_email' do describe '#destroy_email' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
fab!(:user_email) { user.primary_email } fab!(:user_email) { user.primary_email }
fab!(:other_email) { Fabricate(:secondary_email, user: user) } fab!(:other_email) { Fabricate(:secondary_email, user: user) }
@ -3074,7 +3085,7 @@ describe UsersController do
end end
describe '#is_local_username' do describe '#is_local_username' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
fab!(:group) { Fabricate(:group, name: "Discourse", mentionable_level: Group::ALIAS_LEVELS[:everyone]) } fab!(:group) { Fabricate(:group, name: "Discourse", mentionable_level: Group::ALIAS_LEVELS[:everyone]) }
let(:unmentionable) { let(:unmentionable) {
Fabricate(:group, name: "Unmentionable", mentionable_level: Group::ALIAS_LEVELS[:nobody]) Fabricate(:group, name: "Unmentionable", mentionable_level: Group::ALIAS_LEVELS[:nobody])
@ -3177,7 +3188,7 @@ describe UsersController do
end end
describe '#topic_tracking_state' do describe '#topic_tracking_state' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
context 'anon' do context 'anon' do
it "raises an error on anon for topic_tracking_state" do it "raises an error on anon for topic_tracking_state" do
@ -3215,7 +3226,7 @@ describe UsersController do
end end
context '`hide_profile_and_presence` user option is checked' do context '`hide_profile_and_presence` user option is checked' do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
before do before do
user.user_option.update_columns(hide_profile_and_presence: true) user.user_option.update_columns(hide_profile_and_presence: true)
@ -3284,6 +3295,8 @@ describe UsersController do
end end
describe '#confirm_admin' do describe '#confirm_admin' do
fab!(:user) { user1 }
it "fails without a valid token" do it "fails without a valid token" do
get "/u/confirm-admin/invalid-token.json" get "/u/confirm-admin/invalid-token.json"
expect(response).not_to be_successful expect(response).not_to be_successful
@ -3295,7 +3308,6 @@ describe UsersController do
end end
it "succeeds with a valid code as anonymous" do it "succeeds with a valid code as anonymous" do
user = Fabricate(:user)
ac = AdminConfirmation.new(user, Fabricate(:admin)) ac = AdminConfirmation.new(user, Fabricate(:admin))
ac.create_confirmation ac.create_confirmation
get "/u/confirm-admin/#{ac.token}.json" get "/u/confirm-admin/#{ac.token}.json"
@ -3306,8 +3318,7 @@ describe UsersController do
end end
it "succeeds with a valid code when logged in as that user" do it "succeeds with a valid code when logged in as that user" do
admin = sign_in(Fabricate(:admin)) sign_in(admin)
user = Fabricate(:user)
ac = AdminConfirmation.new(user, admin) ac = AdminConfirmation.new(user, admin)
ac.create_confirmation ac.create_confirmation
@ -3319,8 +3330,7 @@ describe UsersController do
end end
it "fails if you're logged in as a different account" do it "fails if you're logged in as a different account" do
sign_in(Fabricate(:admin)) sign_in(admin)
user = Fabricate(:user)
ac = AdminConfirmation.new(user, Fabricate(:admin)) ac = AdminConfirmation.new(user, Fabricate(:admin))
ac.create_confirmation ac.create_confirmation
@ -3333,8 +3343,7 @@ describe UsersController do
describe "post" do describe "post" do
it "gives the user admin access when POSTed" do it "gives the user admin access when POSTed" do
user = Fabricate(:user) ac = AdminConfirmation.new(user, admin)
ac = AdminConfirmation.new(user, Fabricate(:admin))
ac.create_confirmation ac.create_confirmation
post "/u/confirm-admin/#{ac.token}.json" post "/u/confirm-admin/#{ac.token}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
@ -3552,8 +3561,6 @@ describe UsersController do
end end
describe "user profile views" do describe "user profile views" do
fab!(:other_user) { Fabricate(:user) }
it "should track a user profile view for an anon user" do it "should track a user profile view for an anon user" do
get "/" get "/"
UserProfileView.expects(:add).with(other_user.user_profile.id, request.remote_ip, nil) UserProfileView.expects(:add).with(other_user.user_profile.id, request.remote_ip, nil)
@ -3572,7 +3579,7 @@ describe UsersController do
sign_in(user) sign_in(user)
end end
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
it 'returns success' do it 'returns success' do
get "/u/#{user.username}.json" get "/u/#{user.username}.json"
@ -3609,8 +3616,6 @@ describe UsersController do
end end
describe "user profile views" do describe "user profile views" do
fab!(:other_user) { Fabricate(:user) }
it "should track a user profile view for a signed in user" do it "should track a user profile view for a signed in user" do
UserProfileView.expects(:add).with(other_user.user_profile.id, request.remote_ip, user.id) UserProfileView.expects(:add).with(other_user.user_profile.id, request.remote_ip, user.id)
get "/u/#{other_user.username}.json" get "/u/#{other_user.username}.json"
@ -3674,11 +3679,9 @@ describe UsersController do
end end
describe "include_post_count_for" do describe "include_post_count_for" do
fab!(:admin) { Fabricate(:admin) }
fab!(:topic) { Fabricate(:topic) } fab!(:topic) { Fabricate(:topic) }
before do before_all do
Fabricate(:post, user: user, topic: topic) Fabricate(:post, user: user, topic: topic)
Fabricate(:post, user: admin, topic: topic) Fabricate(:post, user: admin, topic: topic)
Fabricate(:post, user: admin, topic: topic, post_type: Post.types[:whisper]) Fabricate(:post, user: admin, topic: topic, post_type: Post.types[:whisper])
@ -3747,7 +3750,7 @@ describe UsersController do
sign_in(user) sign_in(user)
end end
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
it 'works correctly' do it 'works correctly' do
get "/u/#{user.username}/card.json" get "/u/#{user.username}/card.json"
@ -3829,6 +3832,8 @@ describe UsersController do
end end
describe "#account_created" do describe "#account_created" do
fab!(:user) { user1 }
it "returns a message when no session is present" do it "returns a message when no session is present" do
get "/u/account-created" get "/u/account-created"
@ -3840,7 +3845,8 @@ describe UsersController do
end end
it "redirects when the user is logged in" do it "redirects when the user is logged in" do
sign_in(Fabricate(:user)) sign_in(user)
get "/u/account-created" get "/u/account-created"
expect(response).to redirect_to("/") expect(response).to redirect_to("/")
@ -3848,7 +3854,8 @@ describe UsersController do
context 'when cookies contains a destination URL' do context 'when cookies contains a destination URL' do
it 'should redirect to the URL' do it 'should redirect to the URL' do
sign_in(Fabricate(:user)) sign_in(user)
destination_url = 'http://thisisasite.com/somepath' destination_url = 'http://thisisasite.com/somepath'
cookies[:destination_url] = destination_url cookies[:destination_url] = destination_url
@ -4670,7 +4677,6 @@ describe UsersController do
end end
describe '#revoke_account' do describe '#revoke_account' do
fab!(:other_user) { Fabricate(:user) }
it 'errors for unauthorised users' do it 'errors for unauthorised users' do
post "/u/#{user.username}/preferences/revoke-account.json", params: { post "/u/#{user.username}/preferences/revoke-account.json", params: {
provider_name: 'facebook' provider_name: 'facebook'
@ -4909,7 +4915,6 @@ describe UsersController do
describe '#feature_topic' do describe '#feature_topic' do
fab!(:topic) { Fabricate(:topic) } fab!(:topic) { Fabricate(:topic) }
fab!(:other_topic) { Fabricate(:topic) } fab!(:other_topic) { Fabricate(:topic) }
fab!(:other_user) { Fabricate(:user) }
fab!(:private_message) { Fabricate(:private_message_topic, user: other_user) } fab!(:private_message) { Fabricate(:private_message_topic, user: other_user) }
fab!(:category) { Fabricate(:category_with_definition) } fab!(:category) { Fabricate(:category_with_definition) }
@ -4983,7 +4988,6 @@ describe UsersController do
describe '#clear_featured_topic' do describe '#clear_featured_topic' do
fab!(:topic) { Fabricate(:topic) } fab!(:topic) { Fabricate(:topic) }
fab!(:other_user) { Fabricate(:user) }
it 'requires the user to be logged in' do it 'requires the user to be logged in' do
put "/u/#{user.username}/clear-featured-topic.json" put "/u/#{user.username}/clear-featured-topic.json"
@ -5089,7 +5093,7 @@ describe UsersController do
end end
describe "#private_message_topic_tracking_state" do describe "#private_message_topic_tracking_state" do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
fab!(:user_2) { Fabricate(:user) } fab!(:user_2) { Fabricate(:user) }
fab!(:private_message) do fab!(:private_message) do
@ -5126,7 +5130,7 @@ describe UsersController do
end end
describe "#reset_recent_searches" do describe "#reset_recent_searches" do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
it 'does nothing for anon' do it 'does nothing for anon' do
delete "/u/recent-searches.json" delete "/u/recent-searches.json"
@ -5144,7 +5148,7 @@ describe UsersController do
end end
describe "#recent_searches" do describe "#recent_searches" do
fab!(:user) { Fabricate(:user) } fab!(:user) { user1 }
it 'does nothing for anon' do it 'does nothing for anon' do
get "/u/recent-searches.json" get "/u/recent-searches.json"