A platform for community discussion. Free, open, simple.
DEV: Enable RSpec/InstanceVariable rule for models
Loïc Guitaut140775d50598
diff --git a/.rubocop.yml b/.rubocop.ymlindex bc8c3e592bf..60b8f3612da 100644--- a/.rubocop.yml+++ b/.rubocop.yml@@ -35,3 +35,8 @@ Discourse/Plugins/NoMonkeyPatching:Lint/Debugger:Exclude:- script/**/*++RSpec/InstanceVariable:+ Enabled: true+ Include:+ - spec/models/**/*diff --git a/spec/models/category_group_spec.rb b/spec/models/category_group_spec.rbindex aec4466085b..0e2d9fca34e 100644--- a/spec/models/category_group_spec.rb+++ b/spec/models/category_group_spec.rb@@ -3,14 +3,12 @@RSpec.describe CategoryGroup dodescribe "#permission_types" docontext "when verifying enum sequence" do- before { @permission_types = CategoryGroup.permission_types }-it "'full' should be at 1st position" do- expect(@permission_types[:full]).to eq(1)+ expect(described_class.permission_types[:full]).to eq(1)endit "'readonly' should be at 3rd position" do- expect(@permission_types[:readonly]).to eq(3)+ expect(described_class.permission_types[:readonly]).to eq(3)endendenddiff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rbindex 6d6f9eb1d2c..84d95fa4ec0 100644--- a/spec/models/category_spec.rb+++ b/spec/models/category_spec.rb@@ -291,54 +291,40 @@ RSpec.describe Category doenddescribe "non-english characters" do+ let(:category) { Fabricate(:category_with_definition, name: "测试") }+context "when using ascii slug generator" do- before do- SiteSetting.slug_generation_method = "ascii"- @category = Fabricate(:category_with_definition, name: "测试")- end- after { @category.destroy }+ before { SiteSetting.slug_generation_method = "ascii" }it "creates a blank slug" do- expect(@category.slug).to be_blank- expect(@category.slug_for_url).to eq("#{@category.id}-category")+ expect(category.slug).to be_blank+ expect(category.slug_for_url).to eq("#{category.id}-category")endendcontext "when using none slug generator" do- before do- SiteSetting.slug_generation_method = "none"- @category = Fabricate(:category_with_definition, name: "测试")- end- after do- SiteSetting.slug_generation_method = "ascii"- @category.destroy- end+ before { SiteSetting.slug_generation_method = "none" }+ after { SiteSetting.slug_generation_method = "ascii" }it "creates a blank slug" do- expect(@category.slug).to be_blank- expect(@category.slug_for_url).to eq("#{@category.id}-category")+ expect(category.slug).to be_blank+ expect(category.slug_for_url).to eq("#{category.id}-category")endendcontext "when using encoded slug generator" do- before do- SiteSetting.slug_generation_method = "encoded"- @category = Fabricate(:category_with_definition, name: "测试")- end- after do- SiteSetting.slug_generation_method = "ascii"- @category.destroy- end+ before { SiteSetting.slug_generation_method = "encoded" }+ after { SiteSetting.slug_generation_method = "ascii" }it "creates a slug" do- expect(@category.slug).to eq("%E6%B5%8B%E8%AF%95")- expect(@category.slug_for_url).to eq("%E6%B5%8B%E8%AF%95")+ expect(category.slug).to eq("%E6%B5%8B%E8%AF%95")+ expect(category.slug_for_url).to eq("%E6%B5%8B%E8%AF%95")endit "keeps the encoded slug after saving" do- @category.save- expect(@category.slug).to eq("%E6%B5%8B%E8%AF%95")- expect(@category.slug_for_url).to eq("%E6%B5%8B%E8%AF%95")+ category.save+ expect(category.slug).to eq("%E6%B5%8B%E8%AF%95")+ expect(category.slug_for_url).to eq("%E6%B5%8B%E8%AF%95")endendend@@ -353,25 +339,25 @@ RSpec.describe Category doenddescribe "custom slug can be provided" do- it "can be sanitized" do- @c = Fabricate(:category_with_definition, name: "Fun Cats", slug: "fun-cats")- @cat = Fabricate(:category_with_definition, name: "love cats", slug: "love-cats")+ let(:category_1) { Fabricate(:category_with_definition, name: "Fun Cats", slug: "fun-cats") }+ let!(:category_2) { Fabricate(:category_with_definition, name: "love cats", slug: "love-cats") }- @c.slug = " invalid slug"- @c.save- expect(@c.slug).to eq("invalid-slug")+ it "can be sanitized" do+ category_1.slug = " invalid slug"+ category_1.save+ expect(category_1.slug).to eq("invalid-slug")c = Fabricate.build(:category, name: "More Fun Cats", slug: "love-cats")expect(c).not_to be_validexpect(c.errors[:slug]).to be_present- @cat.slug = "#{@c.id}-category"- expect(@cat).not_to be_valid- expect(@cat.errors[:slug]).to be_present+ category_2.slug = "#{category_1.id}-category"+ expect(category_2).not_to be_valid+ expect(category_2.errors[:slug]).to be_present- @cat.slug = "#{@cat.id}-category"- expect(@cat).to be_valid- expect(@cat.errors[:slug]).not_to be_present+ category_2.slug = "#{category_2.id}-category"+ expect(category_2).to be_valid+ expect(category_2.errors[:slug]).not_to be_presentendcontext "if SiteSettings.slug_generation_method = ascii" do@@ -396,40 +382,38 @@ RSpec.describe Category doenddescribe "after create" do- before do- @category = Fabricate(:category_with_definition, name: "Amazing Category")- @topic = @category.topic- end+ fab!(:category) { Fabricate(:category_with_definition, name: "Amazing Category") }+ let(:topic) { category.topic }it "is created correctly" do- expect(@category.slug).to eq("amazing-category")- expect(@category.slug_for_url).to eq(@category.slug)+ expect(category.slug).to eq("amazing-category")+ expect(category.slug_for_url).to eq(category.slug)- expect(@category.description).to be_blank+ expect(category.description).to be_blank- expect(Topic.where(category_id: @category).count).to eq(1)+ expect(Topic.where(category_id: category).count).to eq(1)- expect(@topic).to be_present+ expect(topic).to be_present- expect(@topic.category).to eq(@category)+ expect(topic.category).to eq(category)- expect(@topic).to be_visible+ expect(topic).to be_visible- expect(@topic.pinned_at).to be_present+ expect(topic.pinned_at).to be_present- expect(Guardian.new(@category.user).can_delete?(@topic)).to be false+ expect(Guardian.new(category.user).can_delete?(topic)).to be false- expect(@topic.posts.count).to eq(1)+ expect(topic.posts.count).to eq(1)- expect(@category.topic_url).to be_present+ expect(category.topic_url).to be_present- expect(@category.posts_week).to eq(0)- expect(@category.posts_month).to eq(0)- expect(@category.posts_year).to eq(0)+ expect(category.posts_week).to eq(0)+ expect(category.posts_month).to eq(0)+ expect(category.posts_year).to eq(0)- expect(@category.topics_week).to eq(0)- expect(@category.topics_month).to eq(0)- expect(@category.topics_year).to eq(0)+ expect(category.topics_week).to eq(0)+ expect(category.topics_month).to eq(0)+ expect(category.topics_year).to eq(0)endit "cooks the definition" do@@ -444,31 +428,31 @@ RSpec.describe Category doendit "renames the definition when renamed" do- @category.update(name: "Troutfishing")- @topic.reload- expect(@topic.title).to match(/Troutfishing/)- expect(@topic.fancy_title).to match(/Troutfishing/)+ category.update(name: "Troutfishing")+ topic.reload+ expect(topic.title).to match(/Troutfishing/)+ expect(topic.fancy_title).to match(/Troutfishing/)endit "doesn't raise an error if there is no definition topic to rename (uncategorized)" do- expect { @category.update(name: "Troutfishing", topic_id: nil) }.to_not raise_error+ expect { category.update(name: "Troutfishing", topic_id: nil) }.to_not raise_errorendit "creates permalink when category slug is changed" do- @category.update(slug: "new-category")+ category.update(slug: "new-category")expect(Permalink.count).to eq(1)endit "reuses existing permalink when category slug is changed" do- permalink = Permalink.create!(url: "c/#{@category.slug}/#{@category.id}", category_id: 42)+ permalink = Permalink.create!(url: "c/#{category.slug}/#{category.id}", category_id: 42)- expect { @category.update(slug: "new-slug") }.to_not change { Permalink.count }- expect(permalink.reload.category_id).to eq(@category.id)+ expect { category.update(slug: "new-slug") }.to_not change { Permalink.count }+ expect(permalink.reload.category_id).to eq(category.id)endit "creates permalink when sub category slug is changed" dosub_category =- Fabricate(:category_with_definition, slug: "sub-category", parent_category_id: @category.id)+ Fabricate(:category_with_definition, slug: "sub-category", parent_category_id: category.id)sub_category.update(slug: "new-sub-category")expect(Permalink.count).to eq(1)end@@ -492,8 +476,8 @@ RSpec.describe Category doit "correctly creates permalink when category slug is changed in subfolder install" doset_subfolder "/forum"- old_url = @category.url- @category.update(slug: "new-category")+ old_url = category.url+ category.update(slug: "new-category")permalink = Permalink.lastexpect(permalink.url).to eq(old_url[1..-1])end@@ -524,17 +508,19 @@ RSpec.describe Category doenddescribe "trying to change the category topic's category" do+ let(:new_category) do+ Fabricate(:category_with_definition, name: "2nd Category", user: category.user)+ endbefore do- @new_cat = Fabricate(:category_with_definition, name: "2nd Category", user: @category.user)- @topic.change_category_to_id(@new_cat.id)- @topic.reload- @category.reload+ topic.change_category_to_id(new_category.id)+ topic.reload+ category.reloadendit "does not cause changes" do- expect(@category.topic_count).to eq(0)- expect(@topic.category).to eq(@category)- expect(@category.topic).to eq(@topic)+ expect(category.topic_count).to eq(0)+ expect(topic.category).to eq(category)+ expect(category.topic).to eq(topic)endendend@@ -561,31 +547,30 @@ RSpec.describe Category doenddescribe "destroy" do- before do- @category = Fabricate(:category_with_definition)- @category_id = @category.id- @topic_id = @category.topic_id- SiteSetting.shared_drafts_category = @category.id.to_s- end+ let(:category) { Fabricate(:category_with_definition) }+ let(:category_id) { category.id }+ let(:topic_id) { category.topic_id }++ before { SiteSetting.shared_drafts_category = category.id.to_s }it "is deleted correctly" do- @category.destroy- expect(Category.exists?(id: @category_id)).to be false- expect(Topic.with_deleted.where.not(deleted_at: nil).exists?(id: @topic_id)).to be true+ category.destroy+ expect(Category.exists?(id: category_id)).to be false+ expect(Topic.with_deleted.where.not(deleted_at: nil).exists?(id: topic_id)).to be trueexpect(SiteSetting.shared_drafts_category).to be_blankendit "deletes related embeddable host" do- embeddable_host = Fabricate(:embeddable_host, category: @category)- @category.destroy!+ embeddable_host = Fabricate(:embeddable_host, category:)+ category.destroy!expect { embeddable_host.reload }.to raise_error(ActiveRecord::RecordNotFound)endit "triggers a extensibility event" do- event = DiscourseEvent.track(:category_destroyed) { @category.destroy }+ event = DiscourseEvent.track(:category_destroyed) { category.destroy }expect(event[:event_name]).to eq(:category_destroyed)- expect(event[:params].first).to eq(@category)+ expect(event[:params].first).to eq(category)endend@@ -616,101 +601,101 @@ RSpec.describe Category doenddescribe "update_stats" do- before do- @category =- Fabricate(:category_with_definition, user: Fabricate(:user, refresh_auto_groups: true))+ let(:category) do+ Fabricate(:category_with_definition, user: Fabricate(:user, refresh_auto_groups: true))endcontext "with regular topics" dobefore do- create_post(user: @category.user, category: @category.id)+ create_post(user: category.user, category:)Category.update_stats- @category.reload+ category.reloadendit "updates topic stats" do- expect(@category.topics_week).to eq(1)- expect(@category.topics_month).to eq(1)- expect(@category.topics_year).to eq(1)- expect(@category.topic_count).to eq(1)- expect(@category.post_count).to eq(1)- expect(@category.posts_year).to eq(1)- expect(@category.posts_month).to eq(1)- expect(@category.posts_week).to eq(1)+ expect(category.topics_week).to eq(1)+ expect(category.topics_month).to eq(1)+ expect(category.topics_year).to eq(1)+ expect(category.topic_count).to eq(1)+ expect(category.post_count).to eq(1)+ expect(category.posts_year).to eq(1)+ expect(category.posts_month).to eq(1)+ expect(category.posts_week).to eq(1)endendcontext "with deleted topics" dobefore do- @category.topics << Fabricate(:deleted_topic, user: @category.user)+ category.topics << Fabricate(:deleted_topic, user: category.user)Category.update_stats- @category.reload+ category.reloadendit "does not count deleted topics" do- expect(@category.topics_week).to eq(0)- expect(@category.topic_count).to eq(0)- expect(@category.topics_month).to eq(0)- expect(@category.topics_year).to eq(0)- expect(@category.post_count).to eq(0)- expect(@category.posts_year).to eq(0)- expect(@category.posts_month).to eq(0)- expect(@category.posts_week).to eq(0)+ expect(category.topics_week).to eq(0)+ expect(category.topic_count).to eq(0)+ expect(category.topics_month).to eq(0)+ expect(category.topics_year).to eq(0)+ expect(category.post_count).to eq(0)+ expect(category.posts_year).to eq(0)+ expect(category.posts_month).to eq(0)+ expect(category.posts_week).to eq(0)endendcontext "with revised post" dobefore do- post = create_post(user: @category.user, category: @category.id)+ post = create_post(user: category.user, category:)SiteSetting.editing_grace_period = 1.minutepost.revise(post.user, { raw: "updated body" }, revised_at: post.updated_at + 2.minutes)Category.update_stats- @category.reload+ category.reloadendit "doesn't count each version of a post" do- expect(@category.post_count).to eq(1)- expect(@category.posts_year).to eq(1)- expect(@category.posts_month).to eq(1)- expect(@category.posts_week).to eq(1)+ expect(category.post_count).to eq(1)+ expect(category.posts_year).to eq(1)+ expect(category.posts_month).to eq(1)+ expect(category.posts_week).to eq(1)endendcontext "for uncategorized category" do+ let(:uncategorized) { Category.find(SiteSetting.uncategorized_category_id) }+before do- @uncategorized = Category.find(SiteSetting.uncategorized_category_id)- create_post(user: Fabricate(:user, refresh_auto_groups: true), category: @uncategorized.id)+ create_post(user: Fabricate(:user, refresh_auto_groups: true), category: uncategorized)Category.update_stats- @uncategorized.reload+ uncategorized.reloadendit "updates topic stats" do- expect(@uncategorized.topics_week).to eq(1)- expect(@uncategorized.topics_month).to eq(1)- expect(@uncategorized.topics_year).to eq(1)- expect(@uncategorized.topic_count).to eq(1)- expect(@uncategorized.post_count).to eq(1)- expect(@uncategorized.posts_year).to eq(1)- expect(@uncategorized.posts_month).to eq(1)- expect(@uncategorized.posts_week).to eq(1)+ expect(uncategorized.topics_week).to eq(1)+ expect(uncategorized.topics_month).to eq(1)+ expect(uncategorized.topics_year).to eq(1)+ expect(uncategorized.topic_count).to eq(1)+ expect(uncategorized.post_count).to eq(1)+ expect(uncategorized.posts_year).to eq(1)+ expect(uncategorized.posts_month).to eq(1)+ expect(uncategorized.posts_week).to eq(1)endendcontext "when there are no topics left" do- let!(:topic) { create_post(user: @category.user, category: @category.id).reload.topic }+ let!(:topic) { create_post(user: category.user, category:).reload.topic }it "can update the topic count to zero" do- @category.reload- expect(@category.topic_count).to eq(1)- expect(@category.topics.count).to eq(2)+ category.reload+ expect(category.topic_count).to eq(1)+ expect(category.topics.count).to eq(2)topic.delete # Delete so the post trash/destroy hook doesn't fireCategory.update_stats- @category.reload- expect(@category.topics.count).to eq(1)- expect(@category.topic_count).to eq(0)+ category.reload+ expect(category.topics.count).to eq(1)+ expect(category.topic_count).to eq(0)endendenddiff --git a/spec/models/directory_item_spec.rb b/spec/models/directory_item_spec.rbindex 1fec2148687..ca93b2548cc 100644--- a/spec/models/directory_item_spec.rb+++ b/spec/models/directory_item_spec.rb@@ -3,14 +3,12 @@RSpec.describe DirectoryItem dodescribe "#period_types" docontext "when verifying enum sequence" do- before { @period_types = DirectoryItem.period_types }-it "'all' should be at 1st position" do- expect(@period_types[:all]).to eq(1)+ expect(described_class.period_types[:all]).to eq(1)endit "'quarterly' should be at 6th position" do- expect(@period_types[:quarterly]).to eq(6)+ expect(described_class.period_types[:quarterly]).to eq(6)endendenddiff --git a/spec/models/discourse_connect_spec.rb b/spec/models/discourse_connect_spec.rbindex 6d3bef714e4..bb17c732523 100644--- a/spec/models/discourse_connect_spec.rb+++ b/spec/models/discourse_connect_spec.rb@@ -1,13 +1,13 @@# frozen_string_literal: trueRSpec.describe DiscourseConnect do- before do- @discourse_connect_url = "http://example.com/discourse_sso"- @discourse_connect_secret = "shjkfdhsfkjh"+ let(:discourse_connect_url) { "http://example.com/discourse_sso" }+ let(:discourse_connect_secret) { "shjkfdhsfkjh" }- SiteSetting.discourse_connect_url = @discourse_connect_url+ before do+ SiteSetting.discourse_connect_url = discourse_connect_urlSiteSetting.enable_discourse_connect = true- SiteSetting.discourse_connect_secret = @discourse_connect_secret+ SiteSetting.discourse_connect_secret = discourse_connect_secretSiteSetting.reserved_usernames = ""Jobs.run_immediately!end@@ -788,7 +788,7 @@ RSpec.describe DiscourseConnect doit "generates a correct sso url" dourl, payload = DiscourseConnect.generate_url(secure_session: secure_session).split("?")- expect(url).to eq @discourse_connect_url+ expect(url).to eq discourse_connect_urlsso = DiscourseConnect.parse(payload, secure_session: secure_session)expect(sso.nonce).to_not be_nildiff --git a/spec/models/email_token_spec.rb b/spec/models/email_token_spec.rbindex 214bcfa534d..3f1de4eadf5 100644--- a/spec/models/email_token_spec.rb+++ b/spec/models/email_token_spec.rb@@ -66,8 +66,8 @@ RSpec.describe EmailToken docontext "with taken email address" dobefore do- @other_user = Fabricate(:coding_horror)- email_token.update_attribute :email, @other_user.email+ other_user = Fabricate(:coding_horror)+ email_token.update_attribute :email, other_user.emailendit "returns nil when the email has been taken since the token has been generated" dodiff --git a/spec/models/group_associated_group_spec.rb b/spec/models/group_associated_group_spec.rbindex 89483205d50..16ba5133da2 100644--- a/spec/models/group_associated_group_spec.rb+++ b/spec/models/group_associated_group_spec.rb@@ -1,15 +1,16 @@# frozen_string_literal: trueRSpec.describe GroupAssociatedGroup do- let(:user) { Fabricate(:user) }- let(:group) { Fabricate(:group) }- let(:group2) { Fabricate(:group) }- let(:associated_group) { Fabricate(:associated_group) }- let(:associated_group2) { Fabricate(:associated_group) }-- before do+ fab!(:user)+ fab!(:group)+ fab!(:group2) { Fabricate(:group) }+ fab!(:associated_group)+ fab!(:associated_group2) { Fabricate(:associated_group) }+ fab!(:uag) doUserAssociatedGroup.create(user_id: user.id, associated_group_id: associated_group.id)- @gag = described_class.create(group_id: group.id, associated_group_id: associated_group.id)+ end+ fab!(:gag) do+ described_class.create(group_id: group.id, associated_group_id: associated_group.id)endit "adds users to group when created" do@@ -17,7 +18,7 @@ RSpec.describe GroupAssociatedGroup doendit "removes users from group when destroyed" do- @gag.destroy!+ gag.destroy!expect(group.users.include?(user)).to eq(false)end@@ -25,7 +26,7 @@ RSpec.describe GroupAssociatedGroup doUserAssociatedGroup.create(user_id: user.id, associated_group_id: associated_group2.id)described_class.create(group_id: group.id, associated_group_id: associated_group2.id)- @gag.destroy!+ gag.destroy!expect(group.users.include?(user)).to eq(true)end@@ -33,7 +34,7 @@ RSpec.describe GroupAssociatedGroup doUserAssociatedGroup.create(user_id: user.id, associated_group_id: associated_group2.id)described_class.create(group_id: group2.id, associated_group_id: associated_group2.id)- @gag.destroy!+ gag.destroy!expect(group.users.include?(user)).to eq(false)endenddiff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rbindex 27fd72e427d..4e8cef69b5f 100644--- a/spec/models/group_spec.rb+++ b/spec/models/group_spec.rb@@ -121,14 +121,12 @@ RSpec.describe Group dodescribe "#builtin" docontext "when verifying enum sequence" do- before { @builtin = Group.builtin }-it "'moderators' should be at 1st position" do- expect(@builtin[:moderators]).to eq(1)+ expect(described_class.builtin[:moderators]).to eq(1)endit "'trust_level_2' should be at 4th position" do- expect(@builtin[:trust_level_2]).to eq(4)+ expect(described_class.builtin[:trust_level_2]).to eq(4)endendend@@ -1189,17 +1187,18 @@ RSpec.describe Group dodescribe "IMAP" dolet(:group) { Fabricate(:group) }+ let(:mocked_imap_provider) do+ MockedImapProvider.new(+ group.imap_server,+ port: group.imap_port,+ ssl: group.imap_ssl,+ username: group.email_username,+ password: group.email_password,+ )+ enddef mock_imap- @mocked_imap_provider =- MockedImapProvider.new(- group.imap_server,- port: group.imap_port,- ssl: group.imap_ssl,- username: group.email_username,- password: group.email_password,- )- Imap::Providers::Detector.stubs(:init_with_detected_provider).returns(@mocked_imap_provider)+ Imap::Providers::Detector.stubs(:init_with_detected_provider).returns(mocked_imap_provider)enddef configure_imap@@ -1215,12 +1214,12 @@ RSpec.describe Group dodef enable_imapSiteSetting.enable_imap = true- @mocked_imap_provider.stubs(:connect!)- @mocked_imap_provider.stubs(:list_mailboxes_with_attributes).returns(+ mocked_imap_provider.stubs(:connect!)+ mocked_imap_provider.stubs(:list_mailboxes_with_attributes).returns([stub(attr: [], name: "Inbox")],)- @mocked_imap_provider.stubs(:list_mailboxes).returns(["Inbox"])- @mocked_imap_provider.stubs(:disconnect!)+ mocked_imap_provider.stubs(:list_mailboxes).returns(["Inbox"])+ mocked_imap_provider.stubs(:disconnect!)endbefore { Discourse.redis.del("group_imap_mailboxes_#{group.id}") }@@ -1240,7 +1239,7 @@ RSpec.describe Group doconfigure_imapmock_imapSiteSetting.enable_imap = true- @mocked_imap_provider.stubs(:connect!).raises(Net::IMAP::NoResponseError)+ mocked_imap_provider.stubs(:connect!).raises(Net::IMAP::NoResponseError)group.imap_mailboxesexpect(group.reload.imap_last_error).not_to eq(nil)enddiff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rbindex d84798c76dc..96f7b1e1aa8 100644--- a/spec/models/notification_spec.rb+++ b/spec/models/notification_spec.rb@@ -13,41 +13,41 @@ RSpec.describe Notification doit { is_expected.to belong_to :topic }describe "#types" do- context "when verifying enum sequence" do- before { @types = Notification.types }+ subject(:types) { Notification.types }+ context "when verifying enum sequence" doit "has a correct position for each type" do- expect(@types[:mentioned]).to eq(1)- expect(@types[:replied]).to eq(2)- expect(@types[:quoted]).to eq(3)- expect(@types[:edited]).to eq(4)- expect(@types[:liked]).to eq(5)- expect(@types[:private_message]).to eq(6)- expect(@types[:invited_to_private_message]).to eq(7)- expect(@types[:invitee_accepted]).to eq(8)- expect(@types[:posted]).to eq(9)- expect(@types[:moved_post]).to eq(10)- expect(@types[:linked]).to eq(11)- expect(@types[:granted_badge]).to eq(12)- expect(@types[:invited_to_topic]).to eq(13)- expect(@types[:custom]).to eq(14)- expect(@types[:group_mentioned]).to eq(15)- expect(@types[:group_message_summary]).to eq(16)- expect(@types[:watching_first_post]).to eq(17)- expect(@types[:topic_reminder]).to eq(18)- expect(@types[:liked_consolidated]).to eq(19)- expect(@types[:post_approved]).to eq(20)- expect(@types[:code_review_commit_approved]).to eq(21)- expect(@types[:membership_request_accepted]).to eq(22)- expect(@types[:membership_request_consolidated]).to eq(23)- expect(@types[:bookmark_reminder]).to eq(24)- expect(@types[:reaction]).to eq(25)- expect(@types[:votes_released]).to eq(26)- expect(@types[:event_reminder]).to eq(27)- expect(@types[:event_invitation]).to eq(28)- expect(@types[:chat_mention]).to eq(29)- expect(@types[:chat_message]).to eq(30)- expect(@types[:assigned]).to eq(34)+ expect(types[:mentioned]).to eq(1)+ expect(types[:replied]).to eq(2)+ expect(types[:quoted]).to eq(3)+ expect(types[:edited]).to eq(4)+ expect(types[:liked]).to eq(5)+ expect(types[:private_message]).to eq(6)+ expect(types[:invited_to_private_message]).to eq(7)+ expect(types[:invitee_accepted]).to eq(8)+ expect(types[:posted]).to eq(9)+ expect(types[:moved_post]).to eq(10)+ expect(types[:linked]).to eq(11)+ expect(types[:granted_badge]).to eq(12)+ expect(types[:invited_to_topic]).to eq(13)+ expect(types[:custom]).to eq(14)+ expect(types[:group_mentioned]).to eq(15)+ expect(types[:group_message_summary]).to eq(16)+ expect(types[:watching_first_post]).to eq(17)+ expect(types[:topic_reminder]).to eq(18)+ expect(types[:liked_consolidated]).to eq(19)+ expect(types[:post_approved]).to eq(20)+ expect(types[:code_review_commit_approved]).to eq(21)+ expect(types[:membership_request_accepted]).to eq(22)+ expect(types[:membership_request_consolidated]).to eq(23)+ expect(types[:bookmark_reminder]).to eq(24)+ expect(types[:reaction]).to eq(25)+ expect(types[:votes_released]).to eq(26)+ expect(types[:event_reminder]).to eq(27)+ expect(types[:event_invitation]).to eq(28)+ expect(types[:chat_mention]).to eq(29)+ expect(types[:chat_message]).to eq(30)+ expect(types[:assigned]).to eq(34)endendend@@ -256,31 +256,31 @@ RSpec.describe Notification doenddescribe "private message" do- before do- @topic = Fabricate(:private_message_topic)- @post = Fabricate(:post, topic: @topic, user: @topic.user)- @target = @post.topic.topic_allowed_users.reject { |a| a.user_id == @post.user_id }[0].user+ let(:topic) { Fabricate(:private_message_topic) }+ let(:post) { Fabricate(:post, topic:, user: topic.user) }+ let(:target) { post.topic.topic_allowed_users.reject { |a| a.user_id == post.user_id }[0].user }+ before doTopicUser.change(- @target.id,- @topic.id,+ target.id,+ topic.id,notification_level: TopicUser.notification_levels[:watching],)- PostAlerter.post_created(@post)+ PostAlerter.post_created(post)endit "should create and roll up private message notifications" do- expect(@target.notifications.first.notification_type).to eq(+ expect(target.notifications.first.notification_type).to eq(Notification.types[:private_message],)- expect(@post.user.unread_notifications).to eq(0)- expect(@post.user.total_unread_notifications).to eq(0)- expect(@target.unread_high_priority_notifications).to eq(1)+ expect(post.user.unread_notifications).to eq(0)+ expect(post.user.total_unread_notifications).to eq(0)+ expect(target.unread_high_priority_notifications).to eq(1)- Fabricate(:post, topic: @topic, user: @topic.user)- @target.reload- expect(@target.unread_high_priority_notifications).to eq(1)+ Fabricate(:post, topic:, user: topic.user)+ target.reload+ expect(target.unread_high_priority_notifications).to eq(1)endend@@ -623,10 +623,10 @@ RSpec.describe Notification dodescribe "#recent_report" dolet(:post) { Fabricate(:post) }+ let(:vars) { { i: 0 } }def fab(type, read)- @i ||= 0- @i += 1+ vars[:i] += 1Notification.create!(read: read,user_id: user.id,@@ -634,7 +634,7 @@ RSpec.describe Notification dopost_number: post.post_number,data: "[]",notification_type: type,- created_at: @i.days.from_now,+ created_at: vars[:i].days.from_now,)enddiff --git a/spec/models/post_action_type_spec.rb b/spec/models/post_action_type_spec.rbindex 7566a1b8f26..5eb4ea0c1f1 100644--- a/spec/models/post_action_type_spec.rb+++ b/spec/models/post_action_type_spec.rb@@ -19,10 +19,8 @@ RSpec.describe PostActionType dodescribe "#types" docontext "when verifying enum sequence" do- before { @types = PostActionType.types }-it "'spam' should be at 8th position" do- expect(@types[:spam]).to eq(8)+ expect(described_class.types[:spam]).to eq(8)endendenddiff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rbindex 3ab239f89bf..cfc36e1c71d 100644--- a/spec/models/post_mover_spec.rb+++ b/spec/models/post_mover_spec.rb@@ -6,14 +6,12 @@ RSpec.describe PostMover dodescribe "#move_types" docontext "when verifying enum sequence" do- before { @move_types = PostMover.move_types }-it "'new_topic' should be at 1st position" do- expect(@move_types[:new_topic]).to eq(1)+ expect(described_class.move_types[:new_topic]).to eq(1)endit "'existing_topic' should be at 2nd position" do- expect(@move_types[:existing_topic]).to eq(2)+ expect(described_class.move_types[:existing_topic]).to eq(2)endendenddiff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rbindex 403bcd53b15..ebf41095bad 100644--- a/spec/models/post_spec.rb+++ b/spec/models/post_spec.rb@@ -13,42 +13,36 @@ RSpec.describe Post dodescribe "#hidden_reasons" docontext "when verifying enum sequence" do- before { @hidden_reasons = Post.hidden_reasons }-it "'flag_threshold_reached' should be at 1st position" do- expect(@hidden_reasons[:flag_threshold_reached]).to eq(1)+ expect(described_class.hidden_reasons[:flag_threshold_reached]).to eq(1)endit "'flagged_by_tl3_user' should be at 4th position" do- expect(@hidden_reasons[:flagged_by_tl3_user]).to eq(4)+ expect(described_class.hidden_reasons[:flagged_by_tl3_user]).to eq(4)endendenddescribe "#types" docontext "when verifying enum sequence" do- before { @types = Post.types }-it "'regular' should be at 1st position" do- expect(@types[:regular]).to eq(1)+ expect(described_class.types[:regular]).to eq(1)endit "'whisper' should be at 4th position" do- expect(@types[:whisper]).to eq(4)+ expect(described_class.types[:whisper]).to eq(4)endendenddescribe "#cook_methods" docontext "when verifying enum sequence" do- before { @cook_methods = Post.cook_methods }-it "'regular' should be at 1st position" do- expect(@cook_methods[:regular]).to eq(1)+ expect(described_class.cook_methods[:regular]).to eq(1)endit "'email' should be at 3rd position" do- expect(@cook_methods[:email]).to eq(3)+ expect(described_class.cook_methods[:email]).to eq(3)endendenddiff --git a/spec/models/post_timing_spec.rb b/spec/models/post_timing_spec.rbindex 341e7385f59..8b3aacf8211 100644--- a/spec/models/post_timing_spec.rb+++ b/spec/models/post_timing_spec.rb@@ -126,48 +126,47 @@ RSpec.describe PostTiming doenddescribe "recording" do- before do- @topic = post.topic- @coding_horror = Fabricate(:coding_horror)- @timing_attrs = {+ let(:topic) { post.topic }+ let(:coding_horror) { Fabricate(:coding_horror) }+ let(:timing_attrs) do+ {msecs: 1234,topic_id: post.topic_id,- user_id: @coding_horror.id,+ user_id: coding_horror.id,post_number: post.post_number,}endit "adds a view to the post" doexpect {- PostTiming.record_timing(@timing_attrs)+ PostTiming.record_timing(timing_attrs)post.reload}.to change(post, :reads).by(1)endit "doesn't update the posts read count if the topic is a PM" dopm = Fabricate(:private_message_post).topic- @timing_attrs = @timing_attrs.merge(topic_id: pm.id)- PostTiming.record_timing(@timing_attrs)+ PostTiming.record_timing(timing_attrs.merge(topic_id: pm.id))- expect(@coding_horror.user_stat.posts_read_count).to eq(0)+ expect(coding_horror.user_stat.posts_read_count).to eq(0)enddescribe "multiple calls" doit "correctly works" do- PostTiming.record_timing(@timing_attrs)- PostTiming.record_timing(@timing_attrs)+ PostTiming.record_timing(timing_attrs)+ PostTiming.record_timing(timing_attrs)timing =PostTiming.find_by(topic_id: post.topic_id,- user_id: @coding_horror.id,+ user_id: coding_horror.id,post_number: post.post_number,)expect(timing).to be_presentexpect(timing.msecs).to eq(2468)- expect(@coding_horror.user_stat.posts_read_count).to eq(1)+ expect(coding_horror.user_stat.posts_read_count).to eq(1)endendenddiff --git a/spec/models/reviewable_spec.rb b/spec/models/reviewable_spec.rbindex e79e2c8b7a6..f6b22faf8a7 100644--- a/spec/models/reviewable_spec.rb+++ b/spec/models/reviewable_spec.rb@@ -224,16 +224,18 @@ RSpec.describe Reviewable, type: :model doenddescribe "Including pending queued posts even if they don't pass the minimum priority threshold" do+ let(:queued_post) do+ Fabricate(:reviewable_queued_post, score: 0, target: post, force_review: true)+ end+ let(:queued_user) { Fabricate(:reviewable_user, score: 0, force_review: true) }+before doSiteSetting.reviewable_default_visibility = :highReviewable.set_priorities(high: 10)- @queued_post =- Fabricate(:reviewable_queued_post, score: 0, target: post, force_review: true)- @queued_user = Fabricate(:reviewable_user, score: 0, force_review: true)endit "includes queued posts when searching for pending reviewables" do- expect(Reviewable.list_for(user)).to contain_exactly(@queued_post, @queued_user)+ expect(Reviewable.list_for(user)).to contain_exactly(queued_post, queued_user)endit "excludes pending queued posts when applying a different status filter" dodiff --git a/spec/models/reviewable_user_spec.rb b/spec/models/reviewable_user_spec.rbindex e0e56f718da..68fcfad236c 100644--- a/spec/models/reviewable_user_spec.rb+++ b/spec/models/reviewable_user_spec.rb@@ -202,21 +202,22 @@ RSpec.describe ReviewableUser, type: :model doenddescribe "when must_approve_users is true" do- before do+ let!(:reviewable) doSiteSetting.must_approve_users = trueJobs.run_immediately!- @reviewable = ReviewableUser.find_by(target: user)- Jobs.run_later!+ ReviewableUser.find_by(target: user)end+ before { Jobs.run_later! }+it "creates the ReviewableUser for a user, with moderator access" do- expect(@reviewable.reviewable_by_moderator).to eq(true)+ expect(reviewable.reviewable_by_moderator).to eq(true)endcontext "with email jobs" doit "enqueues a 'signup after approval' email if must_approve_users is true" doexpect_enqueued_with(job: :critical_user_email, args: { type: :signup_after_approval }) do- @reviewable.perform(admin, :approve_user)+ reviewable.perform(admin, :approve_user)endend@@ -228,7 +229,7 @@ RSpec.describe ReviewableUser, type: :model doargs: {type: :signup_after_approval,},- ) { @reviewable.perform(admin, :approve_user) }+ ) { reviewable.perform(admin, :approve_user) }endenddiff --git a/spec/models/screened_ip_address_spec.rb b/spec/models/screened_ip_address_spec.rbindex b6f47256067..4cf1b73ccb7 100644--- a/spec/models/screened_ip_address_spec.rb+++ b/spec/models/screened_ip_address_spec.rb@@ -408,11 +408,12 @@ RSpec.describe ScreenedIpAddress doendcontext "when allow_admin record exists" do+ let(:permitted_ip_address) { "111.234.23.11" }+before do- @permitted_ip_address = "111.234.23.11"Fabricate(:screened_ip_address,- ip_address: @permitted_ip_address,+ ip_address: permitted_ip_address,action_type: described_class.actions[:allow_admin],)end@@ -427,12 +428,12 @@ RSpec.describe ScreenedIpAddress dobefore { SiteSetting.use_admin_ip_allowlist = true }it "returns false when user is nil" do- expect(described_class.block_admin_login?(nil, @permitted_ip_address)).to eq(false)+ expect(described_class.block_admin_login?(nil, permitted_ip_address)).to eq(false)endit "returns false for an admin user at the allowed ip address" doexpect(- described_class.block_admin_login?(Fabricate.build(:admin), @permitted_ip_address),+ described_class.block_admin_login?(Fabricate.build(:admin), permitted_ip_address),).to eq(false)end@@ -444,7 +445,7 @@ RSpec.describe ScreenedIpAddress doit "returns false for regular user at allowed ip address" doexpect(- described_class.block_admin_login?(Fabricate.build(:user), @permitted_ip_address),+ described_class.block_admin_login?(Fabricate.build(:user), permitted_ip_address),).to eq(false)enddiff --git a/spec/models/screened_url_spec.rb b/spec/models/screened_url_spec.rbindex a6ad06cdb2b..ce2e69c6240 100644--- a/spec/models/screened_url_spec.rb+++ b/spec/models/screened_url_spec.rb@@ -25,28 +25,34 @@ RSpec.describe ScreenedUrl doenddescribe "normalize" do- subject(:normalized) do- record.normalize- record- end+ subject(:normalized) { record.tap(&:normalize) }- let(:record) { described_class.new(@params) }+ let(:record) { described_class.new(params) }%w[http:// HTTP:// https:// HTTPS://].each do |prefix|- it "strips #{prefix}" do- @params = valid_params.merge(url: url.gsub("http://", prefix))- expect(normalized.url).to eq(url.gsub("http://", ""))+ context "with #{prefix} prefix" do+ let(:params) { valid_params.merge(url: url.gsub("http://", prefix)) }++ it "strips it" do+ expect(normalized.url).to eq(url.gsub("http://", ""))+ endendend- it "strips trailing slash" do- @params = valid_params.merge(url: "silverbullet.in/")- expect(normalized.url).to eq("silverbullet.in")+ context "with a trailing slash" do+ let(:params) { valid_params.merge(url: "silverbullet.in/") }++ it "strips it" do+ expect(normalized.url).to eq("silverbullet.in")+ endend- it "strips trailing slashes" do- @params = valid_params.merge(url: "silverbullet.in/buy///")- expect(normalized.url).to eq("silverbullet.in/buy")+ context "with trailing slashes" do+ let(:params) { valid_params.merge(url: "silverbullet.in/buy///") }++ it "strips them" do+ expect(normalized.url).to eq("silverbullet.in/buy")+ endendit "downcases domains" dodiff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rbindex 7b7aa78ac55..ce7fa0b25dc 100644--- a/spec/models/tag_spec.rb+++ b/spec/models/tag_spec.rb@@ -2,11 +2,10 @@RSpec.describe Tag dodef make_some_tags(count: 3, tag_a_topic: false)- @tags = []if tag_a_topic- count.times { |i| @tags << Fabricate(:tag, topics: [Fabricate(:topic)]) }+ Fabricate.times(count, :tag, topics: [Fabricate(:topic)])else- count.times { |i| @tags << Fabricate(:tag) }+ Fabricate.times(count, :tag)endend@@ -84,68 +83,79 @@ RSpec.describe Tag doenddescribe "#top_tags" do- it "returns nothing if nothing has been tagged" do- make_some_tags(tag_a_topic: false)- expect(Tag.top_tags.sort).to be_empty+ context "when nothing has been tagged" do+ let!(:tags) { make_some_tags(tag_a_topic: false) }++ it "returns nothing" do+ expect(Tag.top_tags.sort).to be_empty+ endend- it "can return all tags" do- make_some_tags(tag_a_topic: true)- expect(Tag.top_tags.sort).to eq(@tags.map(&:name).sort)+ context "when something has been tagged" do+ let!(:tags) { make_some_tags(tag_a_topic: true) }++ it "returns all tags" do+ expect(Tag.top_tags.sort).to eq(tags.map(&:name).sort)+ endendcontext "with categories" do+ let(:tags) { make_some_tags(count: 4) }+ let(:category1) { Fabricate(:category) }+ let(:private_category) { Fabricate(:category) }+ let!(:topics) do+ [+ Fabricate(:topic, category: category1, tags: [tags[0]]),+ Fabricate(:topic, tags: [tags[1]]),+ Fabricate(:topic, category: private_category, tags: [tags[2]]),+ ]+ end+before do- make_some_tags(count: 4) # one tag that isn't used- @category1 = Fabricate(:category)- @private_category = Fabricate(:category)- @private_category.set_permissions(admins: :full)- @private_category.save!- @topics = []- @topics << Fabricate(:topic, category: @category1, tags: [@tags[0]])- @topics << Fabricate(:topic, tags: [@tags[1]])- @topics << Fabricate(:topic, category: @private_category, tags: [@tags[2]])+ private_category.set_permissions(admins: :full)+ private_category.save!endit "works correctly" do- expect(Tag.top_tags(category: @category1).sort).to eq([@tags[0].name].sort)+ expect(Tag.top_tags(category: category1).sort).to eq([tags[0].name].sort)expect(Tag.top_tags(guardian: Guardian.new(Fabricate(:admin))).sort).to eq(- [@tags[0].name, @tags[1].name, @tags[2].name].sort,+ [tags[0].name, tags[1].name, tags[2].name].sort,)expect(- Tag.top_tags(category: @private_category, guardian: Guardian.new(Fabricate(:admin))).sort,- ).to eq([@tags[2].name].sort)+ Tag.top_tags(category: private_category, guardian: Guardian.new(Fabricate(:admin))).sort,+ ).to eq([tags[2].name].sort)- expect(Tag.top_tags.sort).to eq([@tags[0].name, @tags[1].name].sort)- expect(Tag.top_tags(category: @private_category)).to be_empty+ expect(Tag.top_tags.sort).to eq([tags[0].name, tags[1].name].sort)+ expect(Tag.top_tags(category: private_category)).to be_empty- sub_category = Fabricate(:category, parent_category_id: @category1.id)- Fabricate(:topic, category: sub_category, tags: [@tags[1]])- expect(Tag.top_tags(category: @category1).sort).to eq([@tags[0].name, @tags[1].name].sort)+ sub_category = Fabricate(:category, parent_category_id: category1.id)+ Fabricate(:topic, category: sub_category, tags: [tags[1]])+ expect(Tag.top_tags(category: category1).sort).to eq([tags[0].name, tags[1].name].sort)endendcontext "with category-specific tags" do- before do- make_some_tags(count: 3)- @category1 = Fabricate(:category, tags: [@tags[0]]) # only one tag allowed in this category- @category2 = Fabricate(:category)- @topics = []- @topics << Fabricate(:topic, category: @category1, tags: [@tags[0]])- @topics << Fabricate(:topic, category: @category2, tags: [@tags[1], @tags[2]])- @topics << Fabricate(:topic, tags: [@tags[2]]) # uncategorized+ let(:tags) { make_some_tags(count: 3) }+ let(:category1) { Fabricate(:category, tags: [tags[0]]) } # only one tag allowed in this category+ let(:category2) { Fabricate(:category) }+ let!(:topics) do+ [+ Fabricate(:topic, category: category1, tags: [tags[0]]),+ Fabricate(:topic, category: category2, tags: [tags[1], tags[2]]),+ Fabricate(:topic, tags: [tags[2]]), # uncategorized+ ]endit "for category with restricted tags, lists those tags" do- expect(Tag.top_tags(category: @category1)).to eq([@tags[0].name])+ expect(Tag.top_tags(category: category1)).to eq([tags[0].name])endit "for category without tags, lists allowed tags" do- expect(Tag.top_tags(category: @category2).sort).to eq([@tags[1].name, @tags[2].name].sort)+ expect(Tag.top_tags(category: category2).sort).to eq([tags[1].name, tags[2].name].sort)endit "for no category arg, lists all tags" do- expect(Tag.top_tags.sort).to eq([@tags[0].name, @tags[1].name, @tags[2].name].sort)+ expect(Tag.top_tags.sort).to eq([tags[0].name, tags[1].name, tags[2].name].sort)endenddiff --git a/spec/models/top_topic_spec.rb b/spec/models/top_topic_spec.rbindex 33d59206ffc..969ecf95e17 100644--- a/spec/models/top_topic_spec.rb+++ b/spec/models/top_topic_spec.rb@@ -3,14 +3,12 @@RSpec.describe TopTopic dodescribe "#sorted_periods" docontext "when verifying enum sequence" do- before { @sorted_periods = TopTopic.sorted_periods }-it "'daily' should be at 1st position" do- expect(@sorted_periods[:daily]).to eq(1)+ expect(described_class.sorted_periods[:daily]).to eq(1)endit "'all' should be at 6th position" do- expect(@sorted_periods[:all]).to eq(6)+ expect(described_class.sorted_periods[:all]).to eq(6)endendenddiff --git a/spec/models/topic_converter_spec.rb b/spec/models/topic_converter_spec.rbindex 94d589f2710..1173c7f3141 100644--- a/spec/models/topic_converter_spec.rb+++ b/spec/models/topic_converter_spec.rb@@ -235,16 +235,17 @@ RSpec.describe TopicConverter doendcontext "when topic has replies" do+ let(:replied_user) { Fabricate(:coding_horror) }+before do- @replied_user = Fabricate(:coding_horror)- create_post(topic: topic, user: @replied_user)+ create_post(topic: topic, user: replied_user)topic.reloadendit "adds users who replied to topic in Private Message" dotopic.convert_to_private_message(admin)- expect(topic.reload.topic_allowed_users.where(user_id: @replied_user.id).count).to eq(1)+ expect(topic.reload.topic_allowed_users.where(user_id: replied_user.id).count).to eq(1)expect(topic.reload.user.user_stat.post_count).to eq(0)endenddiff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rbindex 4bef66b264f..a31109b1684 100644--- a/spec/models/topic_embed_spec.rb+++ b/spec/models/topic_embed_spec.rb@@ -89,23 +89,9 @@ RSpec.describe TopicEmbed doHTMLparsed = TopicEmbed.parse_html(html, "https://blog.discourse.com/somepost.html")+ expected = "<div><div> article content cats cats </div></div>"- expected = <<-HTML- <div><div>-- article content---- cats- cats-----</div></div>- HTML- expect(parsed.body.strip).to eq(expected.strip)+ expect(parsed.body.squish).to eq(expected.squish)endcontext "when creating a post" do@@ -559,31 +545,31 @@ RSpec.describe TopicEmbed dolet(:contents) do"my normal size emoji <p class='foo'>Hi</p> <img class='emoji other foo' src='/images/smiley.jpg'>"end+ let(:response) { TopicEmbed.find_remote(url) }before doSiteSetting.allowed_embed_classnames = "emoji, foo"stub_request(:get, url).to_return(status: 200, body: contents)- @response = TopicEmbed.find_remote(url)endit "has no author tag" do- expect(@response.author).to be_blank+ expect(response.author).to be_blankendit "img node has emoji class" do- expect(@response.body).to have_tag("img", with: { class: "emoji" })+ expect(response.body).to have_tag("img", with: { class: "emoji" })endit "img node has foo class" do- expect(@response.body).to have_tag("img", with: { class: "foo" })+ expect(response.body).to have_tag("img", with: { class: "foo" })endit "p node has foo class" do- expect(@response.body).to have_tag("p", with: { class: "foo" })+ expect(response.body).to have_tag("p", with: { class: "foo" })endit "nodes removes classes other than emoji" do- expect(@response.body).to have_tag("img", without: { class: "other" })+ expect(response.body).to have_tag("img", without: { class: "other" })endend@@ -609,27 +595,27 @@ RSpec.describe TopicEmbed dolet(:contents) do"my normal size emoji <p class='foo'>Hi</p> <img class='emoji other foo' src='/images/smiley.jpg'>"end+ let(:response) { TopicEmbed.find_remote(url) }before(:each) doSiteSetting.allowed_embed_classnames = ""stub_request(:get, url).to_return(status: 200, body: contents)- @response = TopicEmbed.find_remote(url)endit 'img node doesn\'t have emoji class' do- expect(@response.body).to have_tag("img", without: { class: "emoji" })+ expect(response.body).to have_tag("img", without: { class: "emoji" })endit 'img node doesn\'t have foo class' do- expect(@response.body).to have_tag("img", without: { class: "foo" })+ expect(response.body).to have_tag("img", without: { class: "foo" })endit 'p node doesn\'t foo class' do- expect(@response.body).to have_tag("p", without: { class: "foo" })+ expect(response.body).to have_tag("p", without: { class: "foo" })endit 'img node doesn\'t have other class' do- expect(@response.body).to have_tag("img", without: { class: "other" })+ expect(response.body).to have_tag("img", without: { class: "other" })endenddiff --git a/spec/models/topic_group_spec.rb b/spec/models/topic_group_spec.rbindex 0adf863c663..68c3e52e019 100644--- a/spec/models/topic_group_spec.rb+++ b/spec/models/topic_group_spec.rb@@ -4,60 +4,50 @@ RSpec.describe TopicGroup dodescribe "#update_last_read" dofab!(:group)fab!(:user)+ fab!(:topic) { Fabricate(:private_message_topic, allowed_groups: [group]) }- before do- @topic = Fabricate(:private_message_topic, allowed_groups: [group])- group.add(user)- end+ before { group.add(user) }it "does nothing if the user is not a member of an allowed group" doanother_user = Fabricate(:user)- described_class.update_last_read(another_user, @topic.id, @topic.highest_post_number)- created_topic_group = described_class.where(topic: @topic, group: group).exists?+ described_class.update_last_read(another_user, topic.id, topic.highest_post_number)+ created_topic_group = described_class.where(topic:, group:).exists?expect(created_topic_group).to eq(false)endit "creates a new record if the user is a member of an allowed group" do- described_class.update_last_read(user, @topic.id, @topic.highest_post_number)- created_topic_group = described_class.find_by(topic: @topic, group: group)+ described_class.update_last_read(user, topic.id, topic.highest_post_number)+ created_topic_group = described_class.find_by(topic:, group:)- expect(created_topic_group.last_read_post_number).to eq @topic.highest_post_number+ expect(created_topic_group.last_read_post_number).to eq topic.highest_post_numberendit "does nothing if the topic does not have allowed groups" do- @topic.update!(allowed_groups: [])+ topic.update!(allowed_groups: [])- described_class.update_last_read(user, @topic.id, @topic.highest_post_number)- created_topic_group = described_class.where(topic: @topic, group: group).exists?+ described_class.update_last_read(user, topic.id, topic.highest_post_number)+ created_topic_group = described_class.where(topic:, group:).exists?expect(created_topic_group).to eq(false)endit "updates an existing record with a higher post number" do- described_class.create!(- topic: @topic,- group: group,- last_read_post_number: @topic.highest_post_number - 1,- )+ described_class.create!(topic:, group:, last_read_post_number: topic.highest_post_number - 1)- described_class.update_last_read(user, @topic.id, @topic.highest_post_number)- created_topic_group = described_class.find_by(topic: @topic, group: group)+ described_class.update_last_read(user, topic.id, topic.highest_post_number)+ created_topic_group = described_class.find_by(topic:, group:)- expect(created_topic_group.last_read_post_number).to eq @topic.highest_post_number+ expect(created_topic_group.last_read_post_number).to eq topic.highest_post_numberendit "does nothing if the user read post number is lower than the current one" do- highest_read_number = @topic.highest_post_number + 1- described_class.create!(- topic: @topic,- group: group,- last_read_post_number: highest_read_number,- )+ highest_read_number = topic.highest_post_number + 1+ described_class.create!(topic:, group:, last_read_post_number: highest_read_number)- described_class.update_last_read(user, @topic.id, @topic.highest_post_number)- created_topic_group = described_class.find_by(topic: @topic, group: group)+ described_class.update_last_read(user, topic.id, topic.highest_post_number)+ created_topic_group = described_class.find_by(topic:, group:)expect(created_topic_group.last_read_post_number).to eq highest_read_numberend@@ -65,46 +55,38 @@ RSpec.describe TopicGroup doit "creates a new record if the list of allowed groups has changed" doanother_allowed_group = Fabricate(:group)another_allowed_group.add(user)- @topic.allowed_groups << another_allowed_group- described_class.create!(- topic: @topic,- group: group,- last_read_post_number: @topic.highest_post_number,- )+ topic.allowed_groups << another_allowed_group+ described_class.create!(topic:, group:, last_read_post_number: topic.highest_post_number)- described_class.update_last_read(user, @topic.id, @topic.highest_post_number)- created_topic_group = described_class.find_by(topic: @topic, group: another_allowed_group)+ described_class.update_last_read(user, topic.id, topic.highest_post_number)+ created_topic_group = described_class.find_by(topic:, group: another_allowed_group)- expect(created_topic_group.last_read_post_number).to eq @topic.highest_post_number+ expect(created_topic_group.last_read_post_number).to eq topic.highest_post_numberendit "Only updates the record that shares the same topic_id" donew_post_number = 100topic2 = Fabricate(:private_message_topic, allowed_groups: [group], topic_allowed_users: [])- described_class.create!(- topic: @topic,- group: group,- last_read_post_number: @topic.highest_post_number,- )+ described_class.create!(topic:, group:, last_read_post_number: topic.highest_post_number)described_class.create!(topic: topic2,group: group,last_read_post_number: topic2.highest_post_number,)- described_class.update_last_read(user, @topic.id, new_post_number)- created_topic_group = described_class.find_by(topic: @topic, group: group)- created_topic_group2 = described_class.find_by(topic: topic2, group: group)+ described_class.update_last_read(user, topic.id, new_post_number)+ created_topic_group = described_class.find_by(topic:, group:)+ created_topic_group2 = described_class.find_by(topic: topic2, group:)expect(created_topic_group.last_read_post_number).to eq new_post_numberexpect(created_topic_group2.last_read_post_number).to eq topic2.highest_post_numberendit "will not raise an error if a topic group already exists" do- TopicGroup.create_topic_group(user, @topic.id, 3, [])- expect(TopicGroup.find_by(group: group, topic: @topic).last_read_post_number).to eq(3)- TopicGroup.create_topic_group(user, @topic.id, 10, [])- expect(TopicGroup.find_by(group: group, topic: @topic).last_read_post_number).to eq(10)+ TopicGroup.create_topic_group(user, topic.id, 3, [])+ expect(TopicGroup.find_by(group: group, topic:).last_read_post_number).to eq(3)+ TopicGroup.create_topic_group(user, topic.id, 10, [])+ expect(TopicGroup.find_by(group:, topic:).last_read_post_number).to eq(10)endendenddiff --git a/spec/models/topic_link_click_spec.rb b/spec/models/topic_link_click_spec.rbindex 1217948f556..59f5f1917f4 100644--- a/spec/models/topic_link_click_spec.rb+++ b/spec/models/topic_link_click_spec.rb@@ -10,27 +10,27 @@ RSpec.describe TopicLinkClick doenddescribe "topic_links" do- before do- @topic = Fabricate(:topic, user: Fabricate(:user, refresh_auto_groups: true))- @post = Fabricate(:post_with_external_links, user: @topic.user, topic: @topic)- TopicLink.extract_from(@post)- @topic_link = @topic.topic_links.first- end+ fab!(:topic) { Fabricate(:topic, user: Fabricate(:user, refresh_auto_groups: true)) }+ fab!(:post) { Fabricate(:post_with_external_links, user: topic.user, topic:) }++ let(:topic_link) { topic.topic_links.first }++ before { TopicLink.extract_from(post) }it "has 0 clicks at first" do- expect(@topic_link.clicks).to eq(0)+ expect(topic_link.clicks).to eq(0)enddescribe ".create" do- before { TopicLinkClick.create(topic_link: @topic_link, ip_address: "192.168.1.1") }+ before { described_class.create(topic_link:, ip_address: "192.168.1.1") }it "creates the forum topic link click" do- expect(TopicLinkClick.count).to eq(1)+ expect(described_class.count).to eq(1)- @topic_link.reload- expect(@topic_link.clicks).to eq(1)+ topic_link.reload+ expect(topic_link.clicks).to eq(1)- expect(TopicLinkClick.first.ip_address.to_s).to eq("192.168.1.1")+ expect(described_class.first.ip_address.to_s).to eq("192.168.1.1")endend@@ -38,80 +38,79 @@ RSpec.describe TopicLinkClick doit "works correctly" do# returns nil to prevent exploitsclick =- TopicLinkClick.create_from(+ described_class.create_from(url: "http://url-that-doesnt-exist.com",- post_id: @post.id,+ post_id: post.id,ip: "127.0.0.1",)expect(click).to eq(nil)# redirects if allowlistedclick =- TopicLinkClick.create_from(+ described_class.create_from(url: "https://www.youtube.com/watch?v=jYd_5aggzd4",- post_id: @post.id,+ post_id: post.id,ip: "127.0.0.1",)expect(click).to eq("https://www.youtube.com/watch?v=jYd_5aggzd4")# does not change own linkexpect {- TopicLinkClick.create_from(- url: @topic_link.url,- post_id: @post.id,+ described_class.create_from(+ url: topic_link.url,+ post_id: post.id,ip: "127.0.0.0",- user_id: @post.user_id,+ user_id: post.user_id,)- }.not_to change(TopicLinkClick, :count)+ }.not_to change(described_class, :count)# can handle double # in a url# NOTE: this is not compliant but exists in the wildclick =- TopicLinkClick.create_from(+ described_class.create_from(url: "http://discourse.org#a#b",- post_id: @post.id,+ post_id: post.id,ip: "127.0.0.1",)expect(click).to eq("http://discourse.org#a#b")endcontext "with a valid url and post_id" do- before do- @url =- TopicLinkClick.create_from(url: @topic_link.url, post_id: @post.id, ip: "127.0.0.1")- @click = TopicLinkClick.last+ let!(:url) do+ described_class.create_from(url: topic_link.url, post_id: post.id, ip: "127.0.0.1")end+ let(:click) { described_class.last }it "creates a click" do- expect(@click).to be_present- expect(@click.topic_link).to eq(@topic_link)- expect(@url).to eq(@topic_link.url)+ expect(click).to be_present+ expect(click.topic_link).to eq(topic_link)+ expect(url).to eq(topic_link.url)# second click should not recordexpect {- TopicLinkClick.create_from(url: @topic_link.url, post_id: @post.id, ip: "127.0.0.1")- }.not_to change(TopicLinkClick, :count)+ described_class.create_from(url: topic_link.url, post_id: post.id, ip: "127.0.0.1")+ }.not_to change(described_class, :count)endendcontext "while logged in" dofab!(:other_user) { Fabricate(:user) }- before do- @url =- TopicLinkClick.create_from(- url: @topic_link.url,- post_id: @post.id,- ip: "127.0.0.1",- user_id: other_user.id,- )- @click = TopicLinkClick.last++ let!(:url) do+ described_class.create_from(+ url: topic_link.url,+ post_id: post.id,+ ip: "127.0.0.1",+ user_id: other_user.id,+ )end+ let(:click) { described_class.last }it "creates a click without an IP" do- expect(@click).to be_present- expect(@click.topic_link).to eq(@topic_link)- expect(@click.user_id).to eq(other_user.id)- expect(@click.ip_address).to eq(nil)+ expect(click).to be_present+ expect(click.topic_link).to eq(topic_link)+ expect(click.user_id).to eq(other_user.id)+ expect(click.ip_address).to eq(nil)endend@@ -119,19 +118,19 @@ RSpec.describe TopicLinkClick dolet(:host) { URI.parse(Discourse.base_url).host }it "returns the url" do- url = TopicLinkClick.create_from(url: "/relative-url", post_id: @post.id, ip: "127.0.0.1")+ url = described_class.create_from(url: "/relative-url", post_id: post.id, ip: "127.0.0.1")expect(url).to eq("/relative-url")endit "finds a protocol relative urls with a host" dourl = "//#{host}/relative-url"- redirect = TopicLinkClick.create_from(url: url)+ redirect = described_class.create_from(url: url)expect(redirect).to eq(url)endit "returns the url if it's on our host" dourl = "http://#{host}/relative-url"- redirect = TopicLinkClick.create_from(url: url)+ redirect = described_class.create_from(url: url)expect(redirect).to eq(url)end@@ -144,9 +143,9 @@ RSpec.describe TopicLinkClick doit "correctly handles cdn links" dourl =- TopicLinkClick.create_from(+ described_class.create_from(url: "https://cdn.discourse.org/stuff/my_link",- topic_id: @topic.id,+ topic_id: topic.id,ip: "127.0.0.3",)@@ -154,9 +153,9 @@ RSpec.describe TopicLinkClick do# cdn exploiturl =- TopicLinkClick.create_from(+ described_class.create_from(url: "https://cdn.discourse.org/bad/my_link",- topic_id: @topic.id,+ topic_id: topic.id,ip: "127.0.0.3",)@@ -165,11 +164,11 @@ RSpec.describe TopicLinkClick do# cdn better link trackpath = "/uploads/site/29/5b585f848d8761d5.xls"- post = Fabricate(:post, topic: @topic, raw: "[test](#{path})")+ post = Fabricate(:post, topic:, raw: "[test](#{path})")TopicLink.extract_from(post)url =- TopicLinkClick.create_from(+ described_class.create_from(url: "https://cdn.discourse.org/stuff#{path}",topic_id: post.topic_id,post_id: post.id,@@ -178,7 +177,7 @@ RSpec.describe TopicLinkClick doexpect(url).to eq("https://cdn.discourse.org/stuff#{path}")- click = TopicLinkClick.order("id desc").first+ click = described_class.order("id desc").firstexpect(click.topic_link_id).to eq(TopicLink.order("id desc").first.id)end@@ -190,17 +189,13 @@ RSpec.describe TopicLinkClick doSiteSetting.s3_cdn_url = "https://discourse-s3-cdn.global.ssl.fastly.net"post =- Fabricate(- :post,- topic: @topic,- raw: "[test](//test.localhost/uploads/default/my-test-link)",- )+ Fabricate(:post, topic:, raw: "[test](//test.localhost/uploads/default/my-test-link)")TopicLink.extract_from(post)url =- TopicLinkClick.create_from(+ described_class.create_from(url: "https://discourse-s3-cdn.global.ssl.fastly.net/my-test-link",- topic_id: @topic.id,+ topic_id: topic.id,ip: "127.0.0.3",)@@ -210,66 +205,64 @@ RSpec.describe TopicLinkClick doendcontext "with a HTTPS version of the same URL" do- before do- @url =- TopicLinkClick.create_from(- url: "https://twitter.com",- topic_id: @topic.id,- ip: "127.0.0.3",- )- @click = TopicLinkClick.last+ let!(:url) do+ described_class.create_from(+ url: "https://twitter.com",+ topic_id: topic.id,+ ip: "127.0.0.3",+ )end+ let(:click) { described_class.last }it "creates a click" do- expect(@click).to be_present- expect(@click.topic_link).to eq(@topic_link)- expect(@url).to eq("https://twitter.com")+ expect(click).to be_present+ expect(click.topic_link).to eq(topic_link)+ expect(url).to eq("https://twitter.com")endendcontext "with a google analytics tracking code" do- before do- @url =- TopicLinkClick.create_from(- url: "http://twitter.com?_ga=1.16846778.221554446.1071987018",- topic_id: @topic.id,- ip: "127.0.0.3",- )- @click = TopicLinkClick.last+ let!(:url) do+ described_class.create_from(+ url: "http://twitter.com?_ga=1.16846778.221554446.1071987018",+ topic_id: topic.id,+ ip: "127.0.0.3",+ )end+ let(:click) { described_class.last }it "creates a click" do- expect(@click).to be_present- expect(@click.topic_link).to eq(@topic_link)- expect(@url).to eq("http://twitter.com?_ga=1.16846778.221554446.1071987018")+ expect(click).to be_present+ expect(click.topic_link).to eq(topic_link)+ expect(url).to eq("http://twitter.com?_ga=1.16846778.221554446.1071987018")endendcontext "with a query param and google analytics" do- before do- @topic = Fabricate(:topic, user: Fabricate(:user, refresh_auto_groups: true))- @post =- Fabricate(- :post,- topic: @topic,- user: @topic.user,- raw: "Here's a link to twitter: http://twitter.com?ref=forum",- )- TopicLink.extract_from(@post)- @topic_link = @topic.topic_links.first+ let(:topic) { Fabricate(:topic, user: Fabricate(:user, refresh_auto_groups: true)) }+ let!(:post) do+ Fabricate(+ :post,+ topic:,+ user: topic.user,+ raw: "Here's a link to twitter: http://twitter.com?ref=forum",+ )end+ let(:topic_link) { topic.topic_links.first }++ before { TopicLink.extract_from(post) }it "creates a click" dourl =- TopicLinkClick.create_from(+ described_class.create_from(url: "http://twitter.com?ref=forum&_ga=1.16846778.221554446.1071987018",- topic_id: @topic.id,- post_id: @post.id,+ topic_id: topic.id,+ post_id: post.id,ip: "127.0.0.3",)- click = TopicLinkClick.last+ click = described_class.lastexpect(click).to be_present- expect(click.topic_link).to eq(@topic_link)+ expect(click.topic_link).to eq(topic_link)expect(url).to eq("http://twitter.com?ref=forum&_ga=1.16846778.221554446.1071987018")endend@@ -284,25 +277,25 @@ RSpec.describe TopicLinkClick doTopicLink.extract_from(post)- TopicLinkClick.create_from(+ described_class.create_from(url: "http://example.com/a",post_id: post.id,ip: "127.0.0.1",user: Fabricate(:user),)- TopicLinkClick.create_from(+ described_class.create_from(url: "http://example.com/a?b=c",post_id: post.id,ip: "127.0.0.2",user: Fabricate(:user),)- TopicLinkClick.create_from(+ described_class.create_from(url: "http://example.com/a?b=c&d=e",post_id: post.id,ip: "127.0.0.3",user: Fabricate(:user),)- TopicLinkClick.create_from(+ described_class.create_from(url: "http://example.com/a?b=c",post_id: post.id,ip: "127.0.0.4",@@ -320,33 +313,31 @@ RSpec.describe TopicLinkClick doendcontext "with a google analytics tracking code and a hash" do- before do- @url =- TopicLinkClick.create_from(- url: "http://discourse.org?_ga=1.16846778.221554446.1071987018#faq",- topic_id: @topic.id,- ip: "127.0.0.3",- )- @click = TopicLinkClick.last+ let!(:url) do+ described_class.create_from(+ url: "http://discourse.org?_ga=1.16846778.221554446.1071987018#faq",+ topic_id: topic.id,+ ip: "127.0.0.3",+ )end+ let(:click) { described_class.last }it "creates a click" do- expect(@click).to be_present- expect(@url).to eq("http://discourse.org?_ga=1.16846778.221554446.1071987018#faq")+ expect(click).to be_present+ expect(url).to eq("http://discourse.org?_ga=1.16846778.221554446.1071987018#faq")endendcontext "with a valid url and topic_id" do- before do- @url =- TopicLinkClick.create_from(url: @topic_link.url, topic_id: @topic.id, ip: "127.0.0.3")- @click = TopicLinkClick.last+ let!(:url) do+ described_class.create_from(url: topic_link.url, topic_id: topic.id, ip: "127.0.0.3")end+ let(:click) { described_class.last }it "creates a click" do- expect(@click).to be_present- expect(@click.topic_link).to eq(@topic_link)- expect(@url).to eq(@topic_link.url)+ expect(click).to be_present+ expect(click.topic_link).to eq(topic_link)+ expect(url).to eq(topic_link.url)endendenddiff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 326c0d50d0d..058129a8d1e 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -1333,7 +1333,7 @@ RSpec.describe Topic doenddescribe "bumping topics" do- let!(:topic) { Fabricate(:topic, bumped_at: 1.year.ago) }+ fab!(:topic) { Fabricate(:topic, bumped_at: 1.year.ago) }it "updates the bumped_at field when a new post is made" doexpect(topic.bumped_at).to be_present@@ -1344,19 +1344,18 @@ RSpec.describe Topic doendcontext "when editing posts" do- before do- @earlier_post = Fabricate(:post, topic: topic, user: topic.user)- @last_post = Fabricate(:post, topic: topic, user: topic.user)- topic.reload- end+ fab!(:earlier_post) { Fabricate(:post, topic:, user: topic.user) }+ fab!(:last_post) { Fabricate(:post, topic:, user: topic.user) }++ before { topic.reload }it "doesn't bump the topic on an edit to the last post that doesn't result in a new version" doexpect {SiteSetting.editing_grace_period = 5.minutes- @last_post.revise(- @last_post.user,- { raw: @last_post.raw + "a" },- revised_at: @last_post.created_at + 10.seconds,+ last_post.revise(+ last_post.user,+ { raw: last_post.raw + "a" },+ revised_at: last_post.created_at + 10.seconds,)topic.reload}.not_to change(topic, :bumped_at)@@ -1364,21 +1363,21 @@ RSpec.describe Topic doit "bumps the topic when a new version is made of the last post" doexpect {- @last_post.revise(moderator, raw: "updated contents")+ last_post.revise(moderator, raw: "updated contents")topic.reload}.to change(topic, :bumped_at)endit "doesn't bump the topic when a post that isn't the last post receives a new version" doexpect {- @earlier_post.revise(moderator, raw: "updated contents")+ earlier_post.revise(moderator, raw: "updated contents")topic.reload}.not_to change(topic, :bumped_at)endit "doesn't bump the topic when a post have invalid topic title while edit" doexpect {- @last_post.revise(moderator, title: "invalid title")+ last_post.revise(moderator, title: "invalid title")topic.reload}.not_to change(topic, :bumped_at)end@@ -1419,38 +1418,36 @@ RSpec.describe Topic dodescribe "update_status" dofab!(:post) { Fabricate(:post).tap { |p| p.topic.update!(bumped_at: 1.hour.ago) } }-fab!(:topic) { post.topic }- before do- @original_bumped_at = topic.bumped_at- @user = topic.user- @user.admin = true- end+ let(:user) { topic.user }+ let!(:original_bumped_at) { topic.bumped_at }++ before { user.admin = true }context "with visibility" dolet(:category) { Fabricate(:category_with_definition) }context "when disabled" doit "should not be visible and have correct counts" do- topic.update_status("visible", false, @user)+ topic.update_status("visible", false, user)topic.reloadexpect(topic).not_to be_visibleexpect(topic.moderator_posts_count).to eq(1)- expect(topic.bumped_at).to eq_time(@original_bumped_at)+ expect(topic.bumped_at).to eq_time(original_bumped_at)endit "decreases topic_count of topic category" dotopic.update!(category: category)Category.update_stats- expect do 2.times { topic.update_status("visible", false, @user) } end.to change {+ expect do 2.times { topic.update_status("visible", false, user) } end.to change {category.reload.topic_count}.by(-1)endit "decreases topic_count of user stat" do- expect do 2.times { topic.update_status("visible", false, @user) } end.to change {+ expect do 2.times { topic.update_status("visible", false, user) } end.to change {post.user.user_stat.reload.topic_count}.from(1).to(0)end@@ -1459,35 +1456,35 @@ RSpec.describe Topic douser.user_profile.update(featured_topic_id: topic.id)expect(user.user_profile.featured_topic).to eq(topic)- topic.update_status("visible", false, @user)+ topic.update_status("visible", false, user)expect(user.user_profile.reload.featured_topic).to eq(nil)endendcontext "when enabled" dobefore do- topic.update_status("visible", false, @user)+ topic.update_status("visible", false, user)topic.reloadendit "should be visible with correct counts" do- topic.update_status("visible", true, @user)+ topic.update_status("visible", true, user)expect(topic).to be_visibleexpect(topic.moderator_posts_count).to eq(2)- expect(topic.bumped_at).to eq_time(@original_bumped_at)+ expect(topic.bumped_at).to eq_time(original_bumped_at)endit "increases topic_count of topic category" dotopic.update!(category: category)- expect do 2.times { topic.update_status("visible", true, @user) } end.to change {+ expect do 2.times { topic.update_status("visible", true, user) } end.to change {category.reload.topic_count}.by(1)endit "increases topic_count of user stat" do- expect do 2.times { topic.update_status("visible", true, @user) } end.to change {+ expect do 2.times { topic.update_status("visible", true, user) } end.to change {post.user.user_stat.reload.topic_count}.from(0).to(1)end@@ -1497,27 +1494,27 @@ RSpec.describe Topic docontext "with pinned" docontext "when disabled" dobefore do- topic.update_status("pinned", false, @user)+ topic.update_status("pinned", false, user)topic.reloadendit "doesn't have a pinned_at but has correct dates" doexpect(topic.pinned_at).to be_blankexpect(topic.moderator_posts_count).to eq(1)- expect(topic.bumped_at).to eq_time(@original_bumped_at)+ expect(topic.bumped_at).to eq_time(original_bumped_at)endendcontext "when enabled" dobefore dotopic.update_attribute :pinned_at, nil- topic.update_status("pinned", true, @user)+ topic.update_status("pinned", true, user)topic.reloadendit "should enable correctly" doexpect(topic.pinned_at).to be_present- expect(topic.bumped_at).to eq_time(@original_bumped_at)+ expect(topic.bumped_at).to eq_time(original_bumped_at)expect(topic.moderator_posts_count).to eq(1)endend@@ -1525,67 +1522,69 @@ RSpec.describe Topic docontext "with archived" doit "should create a staff action log entry" do- expect { topic.update_status("archived", true, @user) }.to change {+ expect { topic.update_status("archived", true, user) }.to change {UserHistory.where(action: UserHistory.actions[:topic_archived]).count}.by(1)endcontext "when disabled" do+ let(:archived_topic) { Fabricate(:topic, archived: true, bumped_at: 1.hour.ago) }+ let!(:original_bumped_at) { archived_topic.bumped_at }+before do- @archived_topic = Fabricate(:topic, archived: true, bumped_at: 1.hour.ago)- @original_bumped_at = @archived_topic.bumped_at- @archived_topic.update_status("archived", false, @user)- @archived_topic.reload+ archived_topic.update_status("archived", false, user)+ archived_topic.reloadendit "should archive correctly" do- expect(@archived_topic).not_to be_archived- expect(@archived_topic.bumped_at).to eq_time(@original_bumped_at)- expect(@archived_topic.moderator_posts_count).to eq(1)+ expect(archived_topic).not_to be_archived+ expect(archived_topic.bumped_at).to eq_time(original_bumped_at)+ expect(archived_topic.moderator_posts_count).to eq(1)endendcontext "when enabled" dobefore dotopic.update_attribute :archived, false- topic.update_status("archived", true, @user)+ topic.update_status("archived", true, user)topic.reloadendit "should be archived" doexpect(topic).to be_archivedexpect(topic.moderator_posts_count).to eq(1)- expect(topic.bumped_at).to eq_time(@original_bumped_at)+ expect(topic.bumped_at).to eq_time(original_bumped_at)endendendshared_examples_for "a status that closes a topic" docontext "when disabled" do+ let(:closed_topic) { Fabricate(:topic, closed: true, bumped_at: 1.hour.ago) }+ let!(:original_bumped_at) { closed_topic.bumped_at }+before do- @closed_topic = Fabricate(:topic, closed: true, bumped_at: 1.hour.ago)- @original_bumped_at = @closed_topic.bumped_at- @closed_topic.update_status(status, false, @user)- @closed_topic.reload+ closed_topic.update_status(status, false, user)+ closed_topic.reloadendit "should not be pinned" do- expect(@closed_topic).not_to be_closed- expect(@closed_topic.moderator_posts_count).to eq(1)- expect(@closed_topic.bumped_at).not_to eq_time(@original_bumped_at)+ expect(closed_topic).not_to be_closed+ expect(closed_topic.moderator_posts_count).to eq(1)+ expect(closed_topic.bumped_at).not_to eq_time(original_bumped_at)endendcontext "when enabled" dobefore dotopic.update_attribute :closed, false- topic.update_status(status, true, @user)+ topic.update_status(status, true, user)topic.reloadendit "should be closed" doexpect(topic).to be_closed- expect(topic.bumped_at).to eq_time(@original_bumped_at)+ expect(topic.bumped_at).to eq_time(original_bumped_at)expect(topic.moderator_posts_count).to eq(1)expect(topic.topic_timers.first).to eq(nil)end@@ -1597,17 +1596,17 @@ RSpec.describe Topic doit_behaves_like "a status that closes a topic"it "should archive group message" do- group.add(@user)+ group.add(user)topic = Fabricate(:private_message_topic, allowed_groups: [group])- expect { topic.update_status(status, true, @user) }.to change(+ expect { topic.update_status(status, true, user) }.to change(topic.group_archived_messages,:count,).by(1)endit "should create a staff action log entry" do- expect { topic.update_status(status, true, @user) }.to change {+ expect { topic.update_status(status, true, user) }.to change {UserHistory.where(action: UserHistory.actions[:topic_closed]).count}.by(1)end@@ -1621,7 +1620,7 @@ RSpec.describe Topic doit "includes the autoclose duration in the moderator post" dofreeze_time(Time.new(2000, 1, 1))topic.created_at = 3.days.ago- topic.update_status(status, true, @user)+ topic.update_status(status, true, user)expect(topic.posts.last.raw).to include "closed after 3 days"endend@@ -1639,7 +1638,7 @@ RSpec.describe Topic dofreeze_time(2.days.from_now)- topic.update_status(status, true, @user)+ topic.update_status(status, true, user)expect(topic.posts.last.raw).to include "closed after 2 days"endend@@ -1713,47 +1712,44 @@ RSpec.describe Topic doendcontext "with last_poster info" do- before do- @post = create_post- @user = @post.user- @topic = @post.topic- end+ let(:post) { create_post }+ let!(:user) { post.user }+ let!(:topic) { post.topic }it "initially has the last_post_user_id of the OP" do- expect(@topic.last_post_user_id).to eq(@user.id)+ expect(topic.last_post_user_id).to eq(user.id)endcontext "after a second post" do- before do- @second_user = coding_horror- @new_post = create_post(topic: @topic, user: @second_user)- @topic.reload- end+ let(:second_user) { coding_horror }+ let!(:new_post) { create_post(topic:, user: second_user) }++ before { topic.reload }it "updates the last_post_user_id to the second_user" do- expect(@topic.last_post_user_id).to eq(@second_user.id)- expect(@topic.last_posted_at.to_i).to eq(@new_post.created_at.to_i)- topic_user = @second_user.topic_users.find_by(topic_id: @topic.id)+ expect(topic.last_post_user_id).to eq(second_user.id)+ expect(topic.last_posted_at.to_i).to eq(new_post.created_at.to_i)+ topic_user = second_user.topic_users.find_by(topic_id: topic.id)expect(topic_user.posted?).to eq(true)endendenddescribe "with category" do- before { @category = Fabricate(:category_with_definition) }+ fab!(:category) { Fabricate(:category_with_definition) }it "should not increase the topic_count with no category" doexpect {- Fabricate(:topic, user: @category.user)- @category.reload- }.not_to change(@category, :topic_count)+ Fabricate(:topic, user: category.user)+ category.reload+ }.not_to change(category, :topic_count)endit "should increase the category's topic_count" doexpect {- Fabricate(:topic, user: @category.user, category_id: @category.id)- @category.reload- }.to change(@category, :topic_count).by(1)+ Fabricate(:topic, user: category.user, category_id: category.id)+ category.reload+ }.to change(category, :topic_count).by(1)endend@@ -2903,16 +2899,16 @@ RSpec.describe Topic dotopic = Fabricate(:topic)user = topic.useruser.admin = true- @topic_status_event_triggered = false+ topic_status_event = spy- blk = Proc.new { @topic_status_event_triggered = true }+ blk = Proc.new { topic_status_event.triggered }DiscourseEvent.on(:topic_status_updated, &blk)topic.update_status("closed", true, user)topic.reload- expect(@topic_status_event_triggered).to eq(true)+ expect(topic_status_event).to have_received(:triggered)ensureDiscourseEvent.off(:topic_status_updated, &blk)end@@ -3336,38 +3332,40 @@ RSpec.describe Topic doenddescribe "#auto_close_threshold_reached?" do+ fab!(:post)+ fab!(:reviewable) { Fabricate(:reviewable_flagged_post, target: post, topic: post.topic) }++ let(:topic) { post.topic }+before doReviewable.set_priorities(low: 2.0, medium: 6.0, high: 9.0)SiteSetting.num_flaggers_to_close_topic = 2SiteSetting.reviewable_default_visibility = "medium"SiteSetting.auto_close_topic_sensitivity = Reviewable.sensitivities[:high]- post = Fabricate(:post)- @topic = post.topic- @reviewable = Fabricate(:reviewable_flagged_post, target: post, topic: @topic)endit "ignores flags with a low score" do5.times do- @reviewable.add_score(+ reviewable.add_score(Fabricate(:user, trust_level: TrustLevel[0]),PostActionType.types[:spam],created_at: 1.minute.ago,)end- expect(@topic.auto_close_threshold_reached?).to eq(false)+ expect(topic.auto_close_threshold_reached?).to eq(false)endit "returns true when the flags have a high score" do5.times do- @reviewable.add_score(+ reviewable.add_score(Fabricate(:user, admin: true),PostActionType.types[:spam],created_at: 1.minute.ago,)end- expect(@topic.auto_close_threshold_reached?).to eq(true)+ expect(topic.auto_close_threshold_reached?).to eq(true)endenddiff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rbindex 93ba9870862..19c44967603 100644--- a/spec/models/topic_user_spec.rb+++ b/spec/models/topic_user_spec.rb@@ -43,28 +43,28 @@ RSpec.describe TopicUser dodescribe "#notification_levels" docontext "when verifying enum sequence" do- before { @notification_levels = TopicUser.notification_levels }+ let(:notification_levels) { TopicUser.notification_levels }it "'muted' should be at 0 position" do- expect(@notification_levels[:muted]).to eq(0)+ expect(notification_levels[:muted]).to eq(0)endit "'watching' should be at 3rd position" do- expect(@notification_levels[:watching]).to eq(3)+ expect(notification_levels[:watching]).to eq(3)endendenddescribe "#notification_reasons" docontext "when verifying enum sequence" do- before { @notification_reasons = TopicUser.notification_reasons }+ let(:notification_reasons) { TopicUser.notification_reasons }it "'created_topic' should be at 1st position" do- expect(@notification_reasons[:created_topic]).to eq(1)+ expect(notification_reasons[:created_topic]).to eq(1)endit "'plugin_changed' should be at 9th position" do- expect(@notification_reasons[:plugin_changed]).to eq(9)+ expect(notification_reasons[:plugin_changed]).to eq(9)endendenddiff --git a/spec/models/user_action_spec.rb b/spec/models/user_action_spec.rbindex 71ebe054b3a..f6fa740873c 100644--- a/spec/models/user_action_spec.rb+++ b/spec/models/user_action_spec.rb@@ -188,31 +188,30 @@ RSpec.describe UserAction doenddescribe "when user likes" do- fab!(:post)- let(:likee) { post.user }- fab!(:liker) { coding_horror }-def likee_streamUserAction.stream(user_id: likee.id, guardian: Guardian.new)end- before { @old_count = likee_stream.count }+ fab!(:post)+ fab!(:liker) { coding_horror }++ let(:likee) { post.user }+ let!(:old_count) { likee_stream.count }it "creates a new stream entry" doPostActionCreator.like(liker, post)- expect(likee_stream.count).to eq(@old_count + 1)+ expect(likee_stream.count).to eq(old_count + 1)endcontext "with successful like" do- before do- PostActionCreator.like(liker, post)- @liker_action = liker.user_actions.find_by(action_type: UserAction::LIKE)- @likee_action = likee.user_actions.find_by(action_type: UserAction::WAS_LIKED)- end+ let(:liker_action) { liker.user_actions.find_by(action_type: UserAction::LIKE) }+ let(:likee_action) { likee.user_actions.find_by(action_type: UserAction::WAS_LIKED) }++ before { PostActionCreator.like(liker, post) }it "should result in correct data assignment" do- expect(@liker_action).not_to eq(nil)- expect(@likee_action).not_to eq(nil)+ expect(liker_action).not_to eq(nil)+ expect(likee_action).not_to eq(nil)expect(likee.user_stat.reload.likes_received).to eq(1)expect(liker.user_stat.reload.likes_given).to eq(1)@@ -227,9 +226,9 @@ RSpec.describe UserAction dolet(:liker) { post.topic.topic_allowed_users.last.user }it "should not increase user stats" do- expect(@liker_action).not_to eq(nil)+ expect(liker_action).not_to eq(nil)expect(liker.user_stat.reload.likes_given).to eq(0)- expect(@likee_action).not_to eq(nil)+ expect(likee_action).not_to eq(nil)expect(likee.user_stat.reload.likes_received).to eq(0)PostActionDestroyer.destroy(liker, post, :like)@@ -244,61 +243,57 @@ RSpec.describe UserAction doit "doesn't add the entry to the stream" doPostActionCreator.like(liker, post)- expect(likee_stream.count).not_to eq(@old_count + 1)+ expect(likee_stream.count).not_to eq(old_count + 1)endendenddescribe "when a user posts a new topic" do- before do- freeze_time(100.days.ago) do- @post = create_post- PostAlerter.post_created(@post)- end- end+ let(:post) { Post.last }++ before { freeze_time(100.days.ago) { PostAlerter.post_created(create_post) } }describe "topic action" do- it "should exist" do- @action = @post.user.user_actions.find_by(action_type: UserAction::NEW_TOPIC)+ let(:action) { post.user.user_actions.find_by(action_type: UserAction::NEW_TOPIC) }- expect(@action).not_to eq(nil)- expect(@action.created_at).to eq_time(@post.topic.created_at)+ it "should exist" do+ expect(action).not_to eq(nil)+ expect(action.created_at).to eq_time(post.topic.created_at)endendit "should not log a post user action" do- expect(@post.user.user_actions.find_by(action_type: UserAction::REPLY)).to eq(nil)+ expect(post.user.user_actions.find_by(action_type: UserAction::REPLY)).to eq(nil)enddescribe "when another user posts on the topic" do- before do- @other_user = coding_horror- @mentioned = Fabricate(:admin)-- @response =- PostCreator.new(- @other_user,- reply_to_post_number: 1,- topic_id: @post.topic_id,- raw: "perhaps @#{@mentioned.username} knows how this works?",- ).create-- PostAlerter.post_created(@response)+ fab!(:mentioned) { Fabricate(:admin) }++ let(:other_user) { coding_horror }+ let(:response) do+ PostCreator.new(+ other_user,+ reply_to_post_number: 1,+ topic_id: post.topic_id,+ raw: "perhaps @#{mentioned.username} knows how this works?",+ ).createend+ before { PostAlerter.post_created(response) }+it "should log user actions correctly" do- expect(@response.user.user_actions.find_by(action_type: UserAction::REPLY)).not_to eq(nil)- expect(@post.user.user_actions.find_by(action_type: UserAction::RESPONSE)).not_to eq(nil)- expect(@mentioned.user_actions.find_by(action_type: UserAction::MENTION)).not_to eq(nil)+ expect(response.user.user_actions.find_by(action_type: UserAction::REPLY)).not_to eq(nil)+ expect(post.user.user_actions.find_by(action_type: UserAction::RESPONSE)).not_to eq(nil)+ expect(mentioned.user_actions.find_by(action_type: UserAction::MENTION)).not_to eq(nil)expect(- @post.user.user_actions.joins(:target_post).where("posts.post_number = 2").count,+ post.user.user_actions.joins(:target_post).where("posts.post_number = 2").count,).to eq(1)endit "should not log a double notification for a post edit" do- @response.raw = "here it goes again"- @response.save!- expect(@response.user.user_actions.where(action_type: UserAction::REPLY).count).to eq(1)+ response.raw = "here it goes again"+ response.save!+ expect(response.user.user_actions.where(action_type: UserAction::REPLY).count).to eq(1)endendenddiff --git a/spec/models/user_associated_group_spec.rb b/spec/models/user_associated_group_spec.rbindex 15df0d33b84..42a33f63d6b 100644--- a/spec/models/user_associated_group_spec.rb+++ b/spec/models/user_associated_group_spec.rb@@ -1,23 +1,23 @@# frozen_string_literal: trueRSpec.describe UserAssociatedGroup do- let(:user) { Fabricate(:user) }- let(:group) { Fabricate(:group) }- let(:group2) { Fabricate(:group) }- let(:associated_group) { Fabricate(:associated_group) }- let(:associated_group2) { Fabricate(:associated_group) }-- before do+ fab!(:group)+ fab!(:associated_group)+ fab!(:user)+ fab!(:gag) doGroupAssociatedGroup.create(group_id: group.id, associated_group_id: associated_group.id)- @uag = described_class.create(user_id: user.id, associated_group_id: associated_group.id)end+ fab!(:uag) { described_class.create(user_id: user.id, associated_group_id: associated_group.id) }++ let(:group2) { Fabricate(:group) }+ let(:associated_group2) { Fabricate(:associated_group) }it "adds user to group when created" doexpect(group.users.include?(user)).to eq(true)endit "removes user from group when destroyed" do- @uag.destroy!+ uag.destroy!expect(group.users.include?(user)).to eq(false)end@@ -25,7 +25,7 @@ RSpec.describe UserAssociatedGroup doGroupAssociatedGroup.create(group_id: group.id, associated_group_id: associated_group2.id)described_class.create(user_id: user.id, associated_group_id: associated_group2.id)- @uag.destroy!+ uag.destroy!expect(group.users.include?(user)).to eq(true)end@@ -33,7 +33,7 @@ RSpec.describe UserAssociatedGroup doGroupAssociatedGroup.create(group_id: group2.id, associated_group_id: associated_group2.id)described_class.create(user_id: user.id, associated_group_id: associated_group2.id)- @uag.destroy!+ uag.destroy!expect(group.users.include?(user)).to eq(false)endenddiff --git a/spec/models/user_history_spec.rb b/spec/models/user_history_spec.rbindex 60987f7c723..90622336540 100644--- a/spec/models/user_history_spec.rb+++ b/spec/models/user_history_spec.rb@@ -3,14 +3,14 @@RSpec.describe UserHistory dodescribe "#actions" docontext "when verifying enum sequence" do- before { @actions = UserHistory.actions }+ let!(:actions) { described_class.actions }it "'delete_user' should be at 1st position" do- expect(@actions[:delete_user]).to eq(1)+ expect(actions[:delete_user]).to eq(1)endit "'change_site_text' should be at 29th position" do- expect(@actions[:change_site_text]).to eq(29)+ expect(actions[:change_site_text]).to eq(29)endendend@@ -18,23 +18,25 @@ RSpec.describe UserHistory dodescribe "#staff_action_records" docontext "with some records" dofab!(:admin)- let(:custom_type) { "confirmed_ham" }- before do- @change_site_setting =- UserHistory.create!(- action: UserHistory.actions[:change_site_setting],- subject: "title",- previous_value: "Old",- new_value: "New",- )- @change_trust_level =- UserHistory.create!(- action: UserHistory.actions[:change_trust_level],- target_user_id: Fabricate(:user).id,- details: "stuff happened",- )- @custom_history = StaffActionLogger.new(admin).log_custom("confirmed_ham", admin_only: true)+ let(:custom_type) { "confirmed_ham" }+ let!(:change_site_setting) do+ UserHistory.create!(+ action: UserHistory.actions[:change_site_setting],+ subject: "title",+ previous_value: "Old",+ new_value: "New",+ )+ end+ let!(:change_trust_level) do+ UserHistory.create!(+ action: UserHistory.actions[:change_trust_level],+ target_user_id: Fabricate(:user).id,+ details: "stuff happened",+ )+ end+ let!(:custom_history) do+ StaffActionLogger.new(admin).log_custom("confirmed_ham", admin_only: true)endit "returns all records for admins" do@@ -44,24 +46,24 @@ RSpec.describe UserHistory doit "doesn't return records to moderators that only admins should see" dorecords = described_class.staff_action_records(Fabricate(:moderator)).to_a- expect(records).not_to include([@change_site_setting])+ expect(records).not_to include([change_site_setting])endit "filters by action" dorecords =described_class.staff_action_records(admin,- action_id: @change_site_setting.action_before_type_cast,+ action_id: change_site_setting.action_before_type_cast,).to_aexpect(records.size).to eq(1)- expect(records.first).to eq(@change_site_setting)+ expect(records.first).to eq(change_site_setting)endit "filters by action_name" dorecords =described_class.staff_action_records(admin, action_name: "change_site_setting").to_aexpect(records.size).to eq(1)- expect(records.first).to eq(@change_site_setting)+ expect(records.first).to eq(change_site_setting)endit "Uses action_name as custom_type when searching for custom_staff logs" do@@ -73,7 +75,7 @@ RSpec.describe UserHistory do).to_aexpect(records.size).to eq(1)- expect(records.first).to eq(@custom_history)+ expect(records.first).to eq(custom_history)endendenddiff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex 53ab2b0d68e..139e3e71d22 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -793,32 +793,32 @@ RSpec.describe User doenddescribe "email_hash" do- before_all { @user = Fabricate(:user) }+ fab!(:user)+ fab!(:user2) { Fabricate(:user) }it "should have a sane email hash" do- expect(@user.email_hash).to match(/^[0-9a-f]{32}$/)+ expect(user.email_hash).to match(/^[0-9a-f]{32}$/)endit "should use downcase email" do- @user.email = "[email protected]"- @user2 = Fabricate(:user)- @user2.email = "[email protected]"+ user.email = "[email protected]"+ user2.email = "[email protected]"- expect(@user.email_hash).to eq(@user2.email_hash)+ expect(user.email_hash).to eq(user2.email_hash)endit "should trim whitespace before hashing" do- @user.email = "[email protected]"- @user2 = Fabricate(:user)- @user2.email = " [email protected] "+ user.email = "[email protected]"+ user2.email = " [email protected] "- expect(@user.email_hash).to eq(@user2.email_hash)+ expect(user.email_hash).to eq(user2.email_hash)endenddescribe "associated_accounts" do+ fab!(:user)+it "should correctly find social associations" do- user = Fabricate(:user)expect(user.associated_accounts).to eq([])UserAssociatedAccount.create(@@ -981,20 +981,18 @@ RSpec.describe User doenddescribe "username uniqueness" do- before_all do- @user = Fabricate.build(:user)- @user.save!- @codinghorror = Fabricate.build(:coding_horror)- end+ fab!(:user)++ let!(:codinghorror) { Fabricate.build(:coding_horror) }it "should not allow saving if username is reused" do- @codinghorror.username = @user.username- expect(@codinghorror.save).to eq(false)+ codinghorror.username = user.username+ expect(codinghorror.save).to eq(false)endit "should not allow saving if username is reused in different casing" do- @codinghorror.username = @user.username.upcase- expect(@codinghorror.save).to eq(false)+ codinghorror.username = user.username.upcase+ expect(codinghorror.save).to eq(false)endend@@ -1200,23 +1198,24 @@ RSpec.describe User doenddescribe "passwords" do+ let(:user) { Fabricate.build(:user, active: false) }+it "should not have an active account with a good password" do- @user = Fabricate.build(:user, active: false)- @user.password = "ilovepasta"- @user.save!+ user.password = "ilovepasta"+ user.save!- expect(@user.active).to eq(false)- expect(@user.confirm_password?("ilovepasta")).to eq(true)+ expect(user.active).to eq(false)+ expect(user.confirm_password?("ilovepasta")).to eq(true)- email_token = Fabricate(:email_token, user: @user, email: "[email protected]")+ email_token = Fabricate(:email_token, user:, email: "[email protected]")- UserAuthToken.generate!(user_id: @user.id)+ UserAuthToken.generate!(user_id: user.id)- @user.password = "passwordT0"- @user.save!+ user.password = "passwordT0"+ user.save!# must expire old token on password change- expect(@user.user_auth_tokens.count).to eq(0)+ expect(user.user_auth_tokens.count).to eq(0)email_token.reloadexpect(email_token.expired).to eq(true)