diff --git a/.streerc b/.streerc index 3669f107c57..8a004597732 100644 --- a/.streerc +++ b/.streerc @@ -5,6 +5,5 @@ --ignore-files=config/* --ignore-files=db/* --ignore-files=lib/* ---ignore-files=plugins/* --ignore-files=script/* --ignore-files=spec/* diff --git a/plugins/chat/app/controllers/api/category_chatables_controller.rb b/plugins/chat/app/controllers/api/category_chatables_controller.rb index d3f93907d49..352326cda6a 100644 --- a/plugins/chat/app/controllers/api/category_chatables_controller.rb +++ b/plugins/chat/app/controllers/api/category_chatables_controller.rb @@ -9,7 +9,10 @@ class Chat::Api::CategoryChatablesController < ApplicationController Group .joins(:category_groups) .where(category_groups: { category_id: category.id }) - .where("category_groups.permission_type IN (?)", [CategoryGroup.permission_types[:full], CategoryGroup.permission_types[:create_post]]) + .where( + "category_groups.permission_type IN (?)", + [CategoryGroup.permission_types[:full], CategoryGroup.permission_types[:create_post]], + ) .joins("LEFT OUTER JOIN group_users ON groups.id = group_users.group_id") .group("groups.id", "groups.name") .pluck("groups.name", "COUNT(group_users.user_id)") diff --git a/plugins/chat/app/controllers/api/hints_controller.rb b/plugins/chat/app/controllers/api/hints_controller.rb index b06d325c532..3a0635114bf 100644 --- a/plugins/chat/app/controllers/api/hints_controller.rb +++ b/plugins/chat/app/controllers/api/hints_controller.rb @@ -9,16 +9,17 @@ class Chat::Api::HintsController < ApplicationController raise Discourse::InvalidParameters.new(:mentions) if group_names.blank? - visible_groups = Group - .where("LOWER(name) IN (?)", group_names) - .visible_groups(current_user) - .pluck(:name) + visible_groups = + Group.where("LOWER(name) IN (?)", group_names).visible_groups(current_user).pluck(:name) mentionable_groups = filter_mentionable_groups(visible_groups) result = { unreachable: visible_groups - mentionable_groups.map(&:name), - over_members_limit: mentionable_groups.select { |g| g.user_count > SiteSetting.max_users_notified_per_group_mention }.map(&:name), + over_members_limit: + mentionable_groups + .select { |g| g.user_count > SiteSetting.max_users_notified_per_group_mention } + .map(&:name), } result[:invalid] = (group_names - result[:unreachable]) - result[:over_members_limit] diff --git a/plugins/chat/app/jobs/regular/delete_user_messages.rb b/plugins/chat/app/jobs/regular/delete_user_messages.rb index ca52ba73860..22c35624ef9 100644 --- a/plugins/chat/app/jobs/regular/delete_user_messages.rb +++ b/plugins/chat/app/jobs/regular/delete_user_messages.rb @@ -5,8 +5,9 @@ module Jobs def execute(args) return if args[:user_id].nil? - ChatMessageDestroyer.new - .destroy_in_batches(ChatMessage.with_deleted.where(user_id: args[:user_id])) + ChatMessageDestroyer.new.destroy_in_batches( + ChatMessage.with_deleted.where(user_id: args[:user_id]), + ) end end end diff --git a/plugins/chat/app/jobs/scheduled/delete_old_chat_messages.rb b/plugins/chat/app/jobs/scheduled/delete_old_chat_messages.rb index 6fa85197197..0fbc06141be 100644 --- a/plugins/chat/app/jobs/scheduled/delete_old_chat_messages.rb +++ b/plugins/chat/app/jobs/scheduled/delete_old_chat_messages.rb @@ -15,10 +15,9 @@ module Jobs return unless valid_day_value?(:chat_channel_retention_days) ChatMessageDestroyer.new.destroy_in_batches( - ChatMessage - .in_public_channel - .with_deleted - .created_before(SiteSetting.chat_channel_retention_days.days.ago) + ChatMessage.in_public_channel.with_deleted.created_before( + SiteSetting.chat_channel_retention_days.days.ago, + ), ) end @@ -26,10 +25,9 @@ module Jobs return unless valid_day_value?(:chat_dm_retention_days) ChatMessageDestroyer.new.destroy_in_batches( - ChatMessage - .in_dm_channel - .with_deleted - .created_before(SiteSetting.chat_dm_retention_days.days.ago) + ChatMessage.in_dm_channel.with_deleted.created_before( + SiteSetting.chat_dm_retention_days.days.ago, + ), ) end diff --git a/plugins/chat/app/models/chat_channel.rb b/plugins/chat/app/models/chat_channel.rb index f46746592d3..389a16a4c12 100644 --- a/plugins/chat/app/models/chat_channel.rb +++ b/plugins/chat/app/models/chat_channel.rb @@ -89,7 +89,6 @@ class ChatChannel < ActiveRecord::Base # TODO (martin) Move UpdateUserCountsForChatChannels into here def self.update_counts - # NOTE: ChatChannel#messages_count is not updated every time # a message is created or deleted in a channel, so it should not # be displayed in the UI. It is updated eventually via Jobs::ChatPeriodicalUpdates diff --git a/plugins/chat/app/services/chat_message_destroyer.rb b/plugins/chat/app/services/chat_message_destroyer.rb index 311e1f96110..f5f159b8169 100644 --- a/plugins/chat/app/services/chat_message_destroyer.rb +++ b/plugins/chat/app/services/chat_message_destroyer.rb @@ -2,11 +2,13 @@ class ChatMessageDestroyer def destroy_in_batches(chat_messages_query, batch_size: 200) - chat_messages_query.in_batches(of: batch_size).each do |relation| - destroyed_ids = relation.destroy_all.pluck(:id) - reset_last_read(destroyed_ids) - delete_flags(destroyed_ids) - end + chat_messages_query + .in_batches(of: batch_size) + .each do |relation| + destroyed_ids = relation.destroy_all.pluck(:id) + reset_last_read(destroyed_ids) + delete_flags(destroyed_ids) + end end private diff --git a/plugins/chat/db/migrate/20221122070108_save_chat_allowed_groups_site_setting.rb b/plugins/chat/db/migrate/20221122070108_save_chat_allowed_groups_site_setting.rb index 32d446ffc2d..cea003abed7 100644 --- a/plugins/chat/db/migrate/20221122070108_save_chat_allowed_groups_site_setting.rb +++ b/plugins/chat/db/migrate/20221122070108_save_chat_allowed_groups_site_setting.rb @@ -2,10 +2,12 @@ class SaveChatAllowedGroupsSiteSetting < ActiveRecord::Migration[7.0] def up - chat_enabled = DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_enabled' AND value = 't'") + chat_enabled = + DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_enabled' AND value = 't'") return if chat_enabled.blank? - chat_allowed_groups = DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_allowed_groups'") + chat_allowed_groups = + DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_allowed_groups'") return if chat_allowed_groups.present? # The original default was auto group ID 3 (staff) so we are diff --git a/plugins/chat/lib/chat_channel_fetcher.rb b/plugins/chat/lib/chat_channel_fetcher.rb index f0e90fb3dc9..028e0f3643e 100644 --- a/plugins/chat/lib/chat_channel_fetcher.rb +++ b/plugins/chat/lib/chat_channel_fetcher.rb @@ -30,10 +30,14 @@ module Chat::ChatChannelFetcher end def self.generate_allowed_channel_ids_sql(guardian, exclude_dm_channels: false) - category_channel_sql = Category.post_create_allowed(guardian) - .joins("INNER JOIN chat_channels ON chat_channels.chatable_id = categories.id AND chat_channels.chatable_type = 'Category'") - .select("chat_channels.id") - .to_sql + category_channel_sql = + Category + .post_create_allowed(guardian) + .joins( + "INNER JOIN chat_channels ON chat_channels.chatable_id = categories.id AND chat_channels.chatable_type = 'Category'", + ) + .select("chat_channels.id") + .to_sql dm_channel_sql = "" if !exclude_dm_channels dm_channel_sql = <<~SQL @@ -75,8 +79,7 @@ module Chat::ChatChannelFetcher end def self.secured_public_channel_search(guardian, options = {}) - allowed_channel_ids = - generate_allowed_channel_ids_sql(guardian, exclude_dm_channels: true) + allowed_channel_ids = generate_allowed_channel_ids_sql(guardian, exclude_dm_channels: true) channels = ChatChannel.includes(chatable: [:topic_only_relative_url]) channels = channels.includes(:chat_channel_archive) if options[:include_archives] diff --git a/plugins/chat/lib/chat_mailer.rb b/plugins/chat/lib/chat_mailer.rb index 8c914b497d8..7600a25fe4c 100644 --- a/plugins/chat/lib/chat_mailer.rb +++ b/plugins/chat/lib/chat_mailer.rb @@ -32,10 +32,11 @@ class Chat::ChatMailer when_away_frequency = UserOption.chat_email_frequencies[:when_away] allowed_group_ids = Chat.allowed_group_ids - users = User - .joins(:user_option) - .where(user_options: { chat_enabled: true, chat_email_frequency: when_away_frequency }) - .where("users.last_seen_at < ?", 15.minutes.ago) + users = + User + .joins(:user_option) + .where(user_options: { chat_enabled: true, chat_email_frequency: when_away_frequency }) + .where("users.last_seen_at < ?", 15.minutes.ago) if !allowed_group_ids.include?(Group::AUTO_GROUPS[:everyone]) users = users.joins(:groups).where(groups: { id: allowed_group_ids }) diff --git a/plugins/chat/lib/chat_notifier.rb b/plugins/chat/lib/chat_notifier.rb index ff34fdecba3..017aa1b3200 100644 --- a/plugins/chat/lib/chat_notifier.rb +++ b/plugins/chat/lib/chat_notifier.rb @@ -41,7 +41,7 @@ class Chat::ChatNotifier :send_message_notifications, chat_message_id: chat_message.id, timestamp: timestamp.iso8601(6), - reason: :edit + reason: :edit, ) end @@ -50,7 +50,7 @@ class Chat::ChatNotifier :send_message_notifications, chat_message_id: chat_message.id, timestamp: timestamp.iso8601(6), - reason: :new + reason: :new, ) end end @@ -112,8 +112,7 @@ class Chat::ChatNotifier group_mentions_count = group_name_mentions.length skip_notifications = - (direct_mentions_count + group_mentions_count) > - SiteSetting.max_mentions_per_chat_message + (direct_mentions_count + group_mentions_count) > SiteSetting.max_mentions_per_chat_message {}.tap do |to_notify| # The order of these methods is the precedence @@ -248,24 +247,21 @@ class Chat::ChatNotifier end def visible_groups - @visible_groups ||= - Group - .where("LOWER(name) IN (?)", group_name_mentions) - .visible_groups(@user) + @visible_groups ||= Group.where("LOWER(name) IN (?)", group_name_mentions).visible_groups(@user) end def expand_group_mentions(to_notify, already_covered_ids, skip) return [] if skip || visible_groups.empty? - mentionable_groups = Group - .mentionable(@user, include_public: false) - .where(id: visible_groups.map(&:id)) + mentionable_groups = + Group.mentionable(@user, include_public: false).where(id: visible_groups.map(&:id)) mentions_disabled = visible_groups - mentionable_groups - too_many_members, mentionable = mentionable_groups.partition do |group| - group.user_count > SiteSetting.max_users_notified_per_group_mention - end + too_many_members, mentionable = + mentionable_groups.partition do |group| + group.user_count > SiteSetting.max_users_notified_per_group_mention + end to_notify[:group_mentions_disabled] = mentions_disabled to_notify[:too_many_members] = too_many_members @@ -275,7 +271,9 @@ class Chat::ChatNotifier reached_by_group = chat_users .includes(:groups) - .joins(:groups).where(groups: mentionable).where.not(id: already_covered_ids) + .joins(:groups) + .where(groups: mentionable) + .where.not(id: already_covered_ids) grouped = group_users_to_notify(reached_by_group) @@ -295,7 +293,13 @@ class Chat::ChatNotifier end def notify_creator_of_inaccessible_mentions(to_notify) - inaccessible = to_notify.extract!(:unreachable, :welcome_to_join, :too_many_members, :group_mentions_disabled) + inaccessible = + to_notify.extract!( + :unreachable, + :welcome_to_join, + :too_many_members, + :group_mentions_disabled, + ) return if inaccessible.values.all?(&:blank?) ChatPublisher.publish_inaccessible_mentions( @@ -304,7 +308,7 @@ class Chat::ChatNotifier inaccessible[:unreachable].to_a, inaccessible[:welcome_to_join].to_a, inaccessible[:too_many_members].to_a, - inaccessible[:group_mentions_disabled].to_a + inaccessible[:group_mentions_disabled].to_a, ) end @@ -321,9 +325,7 @@ class Chat::ChatNotifier to_notify .except(:unreachable, :welcome_to_join) .each do |key, user_ids| - to_notify[key] = user_ids.reject do |user_id| - screener.ignoring_or_muting_actor?(user_id) - end + to_notify[key] = user_ids.reject { |user_id| screener.ignoring_or_muting_actor?(user_id) } end # :welcome_to_join contains users because it's serialized by MB. @@ -351,11 +353,7 @@ class Chat::ChatNotifier def notify_watching_users(except: []) Jobs.enqueue( :chat_notify_watching, - { - chat_message_id: @chat_message.id, - except_user_ids: except, - timestamp: @timestamp, - }, + { chat_message_id: @chat_message.id, except_user_ids: except, timestamp: @timestamp }, ) end end diff --git a/plugins/chat/lib/duplicate_message_validator.rb b/plugins/chat/lib/duplicate_message_validator.rb index c66420f9d7d..7b094692ff4 100644 --- a/plugins/chat/lib/duplicate_message_validator.rb +++ b/plugins/chat/lib/duplicate_message_validator.rb @@ -22,11 +22,11 @@ class Chat::DuplicateMessageValidator # Check if the same duplicate message has been posted in the last N seconds by any user if !chat_message - .chat_channel - .chat_messages - .where("created_at > ?", matrix[:min_past_seconds].seconds.ago) - .where(message: chat_message.message) - .exists? + .chat_channel + .chat_messages + .where("created_at > ?", matrix[:min_past_seconds].seconds.ago) + .where(message: chat_message.message) + .exists? return end diff --git a/plugins/chat/lib/extensions/user_notifications_extension.rb b/plugins/chat/lib/extensions/user_notifications_extension.rb index 8b3723cdb8b..a054b108d85 100644 --- a/plugins/chat/lib/extensions/user_notifications_extension.rb +++ b/plugins/chat/lib/extensions/user_notifications_extension.rb @@ -85,27 +85,27 @@ module Chat::UserNotificationsExtension "user_notifications.chat_summary.subject.chat_channel_more", email_prefix: @email_prefix, channel: channels.first.title, - count: total_count - 1 + count: total_count - 1, ) elsif channels.size == 1 && dm_users.size == 0 I18n.t( "user_notifications.chat_summary.subject.chat_channel_1", email_prefix: @email_prefix, - channel: channels.first.title + channel: channels.first.title, ) elsif channels.size == 1 && dm_users.size == 1 I18n.t( "user_notifications.chat_summary.subject.chat_channel_and_direct_message", email_prefix: @email_prefix, channel: channels.first.title, - username: dm_users.first.username + username: dm_users.first.username, ) elsif channels.size == 2 I18n.t( "user_notifications.chat_summary.subject.chat_channel_2", email_prefix: @email_prefix, channel1: channels.first.title, - channel2: channels.second.title + channel2: channels.second.title, ) end end @@ -116,21 +116,21 @@ module Chat::UserNotificationsExtension I18n.t( "user_notifications.chat_summary.subject.direct_message_from_1", email_prefix: @email_prefix, - username: dm_users.first.username + username: dm_users.first.username, ) when 2 I18n.t( "user_notifications.chat_summary.subject.direct_message_from_2", email_prefix: @email_prefix, username1: dm_users.first.username, - username2: dm_users.second.username + username2: dm_users.second.username, ) else I18n.t( "user_notifications.chat_summary.subject.direct_message_from_more", email_prefix: @email_prefix, username: dm_users.first.username, - count: dm_users.size - 1 + count: dm_users.size - 1, ) end end diff --git a/plugins/chat/spec/components/chat_message_creator_spec.rb b/plugins/chat/spec/components/chat_message_creator_spec.rb index 6f87534fa29..45f3a4ad2e3 100644 --- a/plugins/chat/spec/components/chat_message_creator_spec.rb +++ b/plugins/chat/spec/components/chat_message_creator_spec.rb @@ -32,10 +32,7 @@ describe Chat::ChatMessageCreator do ) end let(:direct_message_channel) do - Chat::DirectMessageChannelCreator.create!( - acting_user: user1, - target_users: [user1, user2], - ) + Chat::DirectMessageChannelCreator.create!(acting_user: user1, target_users: [user1, user2]) end before do @@ -135,13 +132,14 @@ describe Chat::ChatMessageCreator do end it "publishes a DiscourseEvent for new messages" do - events = DiscourseEvent.track_events { - Chat::ChatMessageCreator.create( - chat_channel: public_chat_channel, - user: user1, - content: "this is a message", - ) - } + events = + DiscourseEvent.track_events do + Chat::ChatMessageCreator.create( + chat_channel: public_chat_channel, + user: user1, + content: "this is a message", + ) + end expect(events.map { _1[:event_name] }).to include(:chat_message_created) end @@ -368,8 +366,8 @@ describe Chat::ChatMessageCreator do content: "hello @#{admin_group.name}", ) }.to change { admin1.chat_mentions.count }.by(1).and change { - admin2.chat_mentions.count - }.by(1) + admin2.chat_mentions.count + }.by(1) end it "doesn't mention users twice if they are direct mentioned and group mentioned" do @@ -380,8 +378,8 @@ describe Chat::ChatMessageCreator do content: "hello @#{admin_group.name} @#{admin1.username} and @#{admin2.username}", ) }.to change { admin1.chat_mentions.count }.by(1).and change { - admin2.chat_mentions.count - }.by(1) + admin2.chat_mentions.count + }.by(1) end it "creates chat mentions for group mentions and direct mentions" do @@ -392,8 +390,8 @@ describe Chat::ChatMessageCreator do content: "hello @#{admin_group.name} @#{user2.username}", ) }.to change { admin1.chat_mentions.count }.by(1).and change { - admin2.chat_mentions.count - }.by(1).and change { user2.chat_mentions.count }.by(1) + admin2.chat_mentions.count + }.by(1).and change { user2.chat_mentions.count }.by(1) end it "creates chat mentions for group mentions and direct mentions" do @@ -404,10 +402,10 @@ describe Chat::ChatMessageCreator do content: "hello @#{admin_group.name} @#{user_group.name}", ) }.to change { admin1.chat_mentions.count }.by(1).and change { - admin2.chat_mentions.count - }.by(1).and change { user2.chat_mentions.count }.by(1).and change { - user3.chat_mentions.count - }.by(1) + admin2.chat_mentions.count + }.by(1).and change { user2.chat_mentions.count }.by(1).and change { + user3.chat_mentions.count + }.by(1) end it "doesn't create chat mentions for group mentions where the group is un-mentionable" do @@ -475,8 +473,8 @@ describe Chat::ChatMessageCreator do upload_ids: [upload1.id, upload2.id], ) }.to change { ChatUpload.where(upload_id: upload1.id).count }.by(1).and change { - ChatUpload.where(upload_id: upload2.id).count - }.by(1) + ChatUpload.where(upload_id: upload2.id).count + }.by(1) end it "filters out uploads that weren't uploaded by the user" do diff --git a/plugins/chat/spec/components/chat_message_rate_limiter_spec.rb b/plugins/chat/spec/components/chat_message_rate_limiter_spec.rb index b91616a3592..fa73e927819 100644 --- a/plugins/chat/spec/components/chat_message_rate_limiter_spec.rb +++ b/plugins/chat/spec/components/chat_message_rate_limiter_spec.rb @@ -64,11 +64,11 @@ describe Chat::ChatMessageRateLimiter do limiter.run! expect { limiter.run! }.to raise_error(RateLimiter::LimitExceeded).and change { - UserHistory.where( - target_user: user, - acting_user: Discourse.system_user, - action: UserHistory.actions[:silence_user], - ).count - }.by(1) + UserHistory.where( + target_user: user, + acting_user: Discourse.system_user, + action: UserHistory.actions[:silence_user], + ).count + }.by(1) end end diff --git a/plugins/chat/spec/fabricators/chat_fabricator.rb b/plugins/chat/spec/fabricators/chat_fabricator.rb index 4ecd7bfab32..e123c06bc54 100644 --- a/plugins/chat/spec/fabricators/chat_fabricator.rb +++ b/plugins/chat/spec/fabricators/chat_fabricator.rb @@ -76,9 +76,7 @@ end Fabricator(:chat_upload) do transient :user - user do - Fabricate(:user) - end + user { Fabricate(:user) } chat_message { |attrs| Fabricate(:chat_message, user: attrs[:user]) } upload { |attrs| Fabricate(:upload, user: attrs[:user]) } diff --git a/plugins/chat/spec/integration/post_chat_quote_spec.rb b/plugins/chat/spec/integration/post_chat_quote_spec.rb index 51d5c327cfe..a49ae55665f 100644 --- a/plugins/chat/spec/integration/post_chat_quote_spec.rb +++ b/plugins/chat/spec/integration/post_chat_quote_spec.rb @@ -219,9 +219,19 @@ martin channel = Fabricate(:chat_channel) message1 = Fabricate(:chat_message, chat_channel: channel, user: post.user) message2 = Fabricate(:chat_message, chat_channel: channel, user: post.user) - md = ChatTranscriptService.new(channel, message2.user, messages_or_ids: [message2.id]).generate_markdown + md = + ChatTranscriptService.new( + channel, + message2.user, + messages_or_ids: [message2.id], + ).generate_markdown message1.update!(message: md) - md_for_post = ChatTranscriptService.new(channel, message1.user, messages_or_ids: [message1.id]).generate_markdown + md_for_post = + ChatTranscriptService.new( + channel, + message1.user, + messages_or_ids: [message1.id], + ).generate_markdown post.update!(raw: md_for_post) expect(post.cooked.chomp).to eq(<<~COOKED.chomp)
diff --git a/plugins/chat/spec/jobs/chat_channel_delete_spec.rb b/plugins/chat/spec/jobs/chat_channel_delete_spec.rb index 55176fdd606..033274f04c0 100644 --- a/plugins/chat/spec/jobs/chat_channel_delete_spec.rb +++ b/plugins/chat/spec/jobs/chat_channel_delete_spec.rb @@ -56,23 +56,23 @@ describe Jobs::ChatChannelDelete do expect { described_class.new.execute(chat_channel_id: chat_channel.id) }.to change { IncomingChatWebhook.where(chat_channel_id: chat_channel.id).count }.by(-1).and change { - ChatWebhookEvent.where(incoming_chat_webhook_id: @incoming_chat_webhook_id).count - }.by(-1).and change { ChatDraft.where(chat_channel: chat_channel).count }.by( + ChatWebhookEvent.where(incoming_chat_webhook_id: @incoming_chat_webhook_id).count + }.by(-1).and change { ChatDraft.where(chat_channel: chat_channel).count }.by( -1, ).and change { UserChatChannelMembership.where(chat_channel: chat_channel).count }.by(-3).and change { - ChatMessageRevision.where(chat_message_id: @message_ids).count - }.by(-1).and change { - ChatMention.where(chat_message_id: @message_ids).count - }.by(-1).and change { - ChatUpload.where(chat_message_id: @message_ids).count - }.by(-10).and change { - ChatMessage.where(id: @message_ids).count - }.by(-20).and change { - ChatMessageReaction.where( - chat_message_id: @message_ids, - ).count - }.by(-10) + ChatMessageRevision.where(chat_message_id: @message_ids).count + }.by(-1).and change { + ChatMention.where(chat_message_id: @message_ids).count + }.by(-1).and change { + ChatUpload.where(chat_message_id: @message_ids).count + }.by(-10).and change { + ChatMessage.where(id: @message_ids).count + }.by(-20).and change { + ChatMessageReaction.where( + chat_message_id: @message_ids, + ).count + }.by(-10) end end diff --git a/plugins/chat/spec/jobs/regular/send_message_notifications_spec.rb b/plugins/chat/spec/jobs/regular/send_message_notifications_spec.rb index eee9303438e..e00bad83f5c 100644 --- a/plugins/chat/spec/jobs/regular/send_message_notifications_spec.rb +++ b/plugins/chat/spec/jobs/regular/send_message_notifications_spec.rb @@ -21,7 +21,7 @@ RSpec.describe Jobs::SendMessageNotifications do subject.execute( chat_message_id: chat_message.id, reason: "invalid", - timestamp: 1.minute.ago + timestamp: 1.minute.ago, ) end @@ -29,32 +29,21 @@ RSpec.describe Jobs::SendMessageNotifications do Chat::ChatNotifier.any_instance.expects(:notify_new).never Chat::ChatNotifier.any_instance.expects(:notify_edit).never - subject.execute( - chat_message_id: chat_message.id, - reason: "new" - ) + subject.execute(chat_message_id: chat_message.id, reason: "new") end it "calls notify_new when the reason is 'new'" do Chat::ChatNotifier.any_instance.expects(:notify_new).once Chat::ChatNotifier.any_instance.expects(:notify_edit).never - subject.execute( - chat_message_id: chat_message.id, - reason: "new", - timestamp: 1.minute.ago - ) + subject.execute(chat_message_id: chat_message.id, reason: "new", timestamp: 1.minute.ago) end it "calls notify_edit when the reason is 'edit'" do Chat::ChatNotifier.any_instance.expects(:notify_new).never Chat::ChatNotifier.any_instance.expects(:notify_edit).once - subject.execute( - chat_message_id: chat_message.id, - reason: "edit", - timestamp: 1.minute.ago - ) + subject.execute(chat_message_id: chat_message.id, reason: "edit", timestamp: 1.minute.ago) end end end diff --git a/plugins/chat/spec/lib/chat_channel_fetcher_spec.rb b/plugins/chat/spec/lib/chat_channel_fetcher_spec.rb index 7de79fe043c..46a1f394197 100644 --- a/plugins/chat/spec/lib/chat_channel_fetcher_spec.rb +++ b/plugins/chat/spec/lib/chat_channel_fetcher_spec.rb @@ -142,17 +142,38 @@ describe Chat::ChatChannelFetcher do fab!(:group_user) { Fabricate(:group_user, group: group, user: user1) } it "does not include the category channel for member of group with readonly access" do - category_channel.update!(chatable: Fabricate(:private_category, group: group, permission_type: CategoryGroup.permission_types[:readonly])) + category_channel.update!( + chatable: + Fabricate( + :private_category, + group: group, + permission_type: CategoryGroup.permission_types[:readonly], + ), + ) expect(subject.all_secured_channel_ids(guardian)).to be_empty end it "includes the category channel for member of group with create_post access" do - category_channel.update!(chatable: Fabricate(:private_category, group: group, permission_type: CategoryGroup.permission_types[:create_post])) + category_channel.update!( + chatable: + Fabricate( + :private_category, + group: group, + permission_type: CategoryGroup.permission_types[:create_post], + ), + ) expect(subject.all_secured_channel_ids(guardian)).to match_array([category_channel.id]) end it "includes the category channel for member of group with full access" do - category_channel.update!(chatable: Fabricate(:private_category, group: group, permission_type: CategoryGroup.permission_types[:full])) + category_channel.update!( + chatable: + Fabricate( + :private_category, + group: group, + permission_type: CategoryGroup.permission_types[:full], + ), + ) expect(subject.all_secured_channel_ids(guardian)).to match_array([category_channel.id]) end end diff --git a/plugins/chat/spec/lib/chat_message_reactor_spec.rb b/plugins/chat/spec/lib/chat_message_reactor_spec.rb index 3245f248f03..565fab80db1 100644 --- a/plugins/chat/spec/lib/chat_message_reactor_spec.rb +++ b/plugins/chat/spec/lib/chat_message_reactor_spec.rb @@ -9,7 +9,7 @@ describe Chat::ChatMessageReactor do fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel, user: reacting_user) } let(:subject) { described_class.new(reacting_user, channel) } - it 'calls guardian ensure_can_join_chat_channel!' do + it "calls guardian ensure_can_join_chat_channel!" do Guardian.any_instance.expects(:ensure_can_join_chat_channel!).once subject.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") end diff --git a/plugins/chat/spec/lib/chat_notifier_spec.rb b/plugins/chat/spec/lib/chat_notifier_spec.rb index 4b6ddbf0b62..e9f413cfd68 100644 --- a/plugins/chat/spec/lib/chat_notifier_spec.rb +++ b/plugins/chat/spec/lib/chat_notifier_spec.rb @@ -275,7 +275,7 @@ describe Chat::ChatNotifier do include_examples "ensure only channel members are notified" - it 'calls guardian can_join_chat_channel?' do + it "calls guardian can_join_chat_channel?" do Guardian.any_instance.expects(:can_join_chat_channel?).at_least_once msg = build_cooked_msg("Hello @#{group.name} and @#{user_2.username}", user_1) to_notify = described_class.new(msg, msg.created_at).notify_new @@ -463,7 +463,8 @@ describe Chat::ChatNotifier do expect(not_participating_msg).to be_present expect(not_participating_msg.data[:cannot_see]).to be_empty - not_participating_users = not_participating_msg.data[:without_membership].map { |u| u["id"] } + not_participating_users = + not_participating_msg.data[:without_membership].map { |u| u["id"] } expect(not_participating_users).to contain_exactly(user_3.id) end @@ -515,7 +516,8 @@ describe Chat::ChatNotifier do expect(not_participating_msg).to be_present expect(not_participating_msg.data[:cannot_see]).to be_empty - not_participating_users = not_participating_msg.data[:without_membership].map { |u| u["id"] } + not_participating_users = + not_participating_msg.data[:without_membership].map { |u| u["id"] } expect(not_participating_users).to contain_exactly(user_3.id) end @@ -539,7 +541,8 @@ describe Chat::ChatNotifier do expect(not_participating_msg).to be_present expect(not_participating_msg.data[:cannot_see]).to be_empty - not_participating_users = not_participating_msg.data[:without_membership].map { |u| u["id"] } + not_participating_users = + not_participating_msg.data[:without_membership].map { |u| u["id"] } expect(not_participating_users).to contain_exactly(user_3.id) end @@ -598,11 +601,12 @@ describe Chat::ChatNotifier do SiteSetting.max_users_notified_per_group_mention = (group.user_count - 1) msg = build_cooked_msg("Hello @#{group.name}", user_1) - messages = MessageBus.track_publish("/chat/#{channel.id}") do - to_notify = described_class.new(msg, msg.created_at).notify_new + messages = + MessageBus.track_publish("/chat/#{channel.id}") do + to_notify = described_class.new(msg, msg.created_at).notify_new - expect(to_notify[group.name]).to be_nil - end + expect(to_notify[group.name]).to be_nil + end too_many_members_msg = messages.first expect(too_many_members_msg).to be_present @@ -614,11 +618,12 @@ describe Chat::ChatNotifier do group.update!(mentionable_level: Group::ALIAS_LEVELS[:only_admins]) msg = build_cooked_msg("Hello @#{group.name}", user_1) - messages = MessageBus.track_publish("/chat/#{channel.id}") do - to_notify = described_class.new(msg, msg.created_at).notify_new + messages = + MessageBus.track_publish("/chat/#{channel.id}") do + to_notify = described_class.new(msg, msg.created_at).notify_new - expect(to_notify[group.name]).to be_nil - end + expect(to_notify[group.name]).to be_nil + end mentions_disabled_msg = messages.first expect(mentions_disabled_msg).to be_present diff --git a/plugins/chat/spec/lib/chat_review_queue_spec.rb b/plugins/chat/spec/lib/chat_review_queue_spec.rb index 23433dab010..5559543c52a 100644 --- a/plugins/chat/spec/lib/chat_review_queue_spec.rb +++ b/plugins/chat/spec/lib/chat_review_queue_spec.rb @@ -46,7 +46,7 @@ describe Chat::ChatReviewQueue do it "returns an error" do expect(second_flag_result).to include success: false, - errors: [I18n.t("chat.reviewables.message_already_handled")] + errors: [I18n.t("chat.reviewables.message_already_handled")] end it "returns an error when trying to use notify_moderators and the previous flag is still pending" do @@ -59,7 +59,7 @@ describe Chat::ChatReviewQueue do ) expect(notify_moderators_result).to include success: false, - errors: [I18n.t("chat.reviewables.message_already_handled")] + errors: [I18n.t("chat.reviewables.message_already_handled")] end end @@ -87,7 +87,7 @@ describe Chat::ChatReviewQueue do queue.flag_message(message, admin_guardian, ReviewableScore.types[:spam]) expect(second_flag_result).to include success: false, - errors: [I18n.t("chat.reviewables.message_already_handled")] + errors: [I18n.t("chat.reviewables.message_already_handled")] end end @@ -105,7 +105,7 @@ describe Chat::ChatReviewQueue do it "raises an error when we are inside the cooldown window" do expect(second_flag_result).to include success: false, - errors: [I18n.t("chat.reviewables.message_already_handled")] + errors: [I18n.t("chat.reviewables.message_already_handled")] end it "allows the user to re-flag after the cooldown period" do diff --git a/plugins/chat/spec/lib/guardian_extensions_spec.rb b/plugins/chat/spec/lib/guardian_extensions_spec.rb index 02da5c577b3..e2e4c4cbc70 100644 --- a/plugins/chat/spec/lib/guardian_extensions_spec.rb +++ b/plugins/chat/spec/lib/guardian_extensions_spec.rb @@ -92,17 +92,32 @@ RSpec.describe Chat::GuardianExtensions do fab!(:group_user) { Fabricate(:group_user, group: group, user: user) } it "returns true if the user can join the category" do - category = Fabricate(:private_category, group: group, permission_type: CategoryGroup.permission_types[:readonly]) + category = + Fabricate( + :private_category, + group: group, + permission_type: CategoryGroup.permission_types[:readonly], + ) channel.update(chatable: category) guardian = Guardian.new(user) expect(guardian.can_join_chat_channel?(channel)).to eq(false) - category = Fabricate(:private_category, group: group, permission_type: CategoryGroup.permission_types[:create_post]) + category = + Fabricate( + :private_category, + group: group, + permission_type: CategoryGroup.permission_types[:create_post], + ) channel.update(chatable: category) guardian = Guardian.new(user) expect(guardian.can_join_chat_channel?(channel)).to eq(true) - category = Fabricate(:private_category, group: group, permission_type: CategoryGroup.permission_types[:full]) + category = + Fabricate( + :private_category, + group: group, + permission_type: CategoryGroup.permission_types[:full], + ) channel.update(chatable: category) guardian = Guardian.new(user) expect(guardian.can_join_chat_channel?(channel)).to eq(true) diff --git a/plugins/chat/spec/mailers/user_notifications_spec.rb b/plugins/chat/spec/mailers/user_notifications_spec.rb index 943739bb826..fa282a8a133 100644 --- a/plugins/chat/spec/mailers/user_notifications_spec.rb +++ b/plugins/chat/spec/mailers/user_notifications_spec.rb @@ -25,7 +25,7 @@ describe UserNotifications do Chat::DirectMessageChannelCreator.create!(acting_user: sender, target_users: [sender, user]) end - it 'calls guardian can_join_chat_channel?' do + it "calls guardian can_join_chat_channel?" do Fabricate(:chat_message, user: sender, chat_channel: channel) Guardian.any_instance.expects(:can_join_chat_channel?).once email = described_class.chat_summary(user, {}) @@ -34,11 +34,12 @@ describe UserNotifications do describe "email subject" do it "includes the sender username in the subject" do - expected_subject = I18n.t( - "user_notifications.chat_summary.subject.direct_message_from_1", - email_prefix: SiteSetting.title, - username: sender.username - ) + expected_subject = + I18n.t( + "user_notifications.chat_summary.subject.direct_message_from_1", + email_prefix: SiteSetting.title, + username: sender.username, + ) Fabricate(:chat_message, user: sender, chat_channel: channel) email = described_class.chat_summary(user, {}) @@ -54,11 +55,12 @@ describe UserNotifications do chat_channel: channel, ) DirectMessageUser.create!(direct_message: channel.chatable, user: another_participant) - expected_subject = I18n.t( - "user_notifications.chat_summary.subject.direct_message_from_1", - email_prefix: SiteSetting.title, - username: sender.username - ) + expected_subject = + I18n.t( + "user_notifications.chat_summary.subject.direct_message_from_1", + email_prefix: SiteSetting.title, + username: sender.username, + ) Fabricate(:chat_message, user: sender, chat_channel: channel) email = described_class.chat_summary(user, {}) @@ -80,12 +82,13 @@ describe UserNotifications do Fabricate(:chat_message, user: sender, chat_channel: channel) email = described_class.chat_summary(user, {}) - expected_subject = I18n.t( - "user_notifications.chat_summary.subject.direct_message_from_2", - email_prefix: SiteSetting.title, - username1: another_dm_user.username, - username2: sender.username - ) + expected_subject = + I18n.t( + "user_notifications.chat_summary.subject.direct_message_from_2", + email_prefix: SiteSetting.title, + username1: another_dm_user.username, + username2: sender.username, + ) expect(email.subject).to eq(expected_subject) expect(email.subject).to include(sender.username) @@ -116,12 +119,13 @@ describe UserNotifications do email = described_class.chat_summary(user, {}) - expected_subject = I18n.t( - "user_notifications.chat_summary.subject.direct_message_from_more", - email_prefix: SiteSetting.title, - username: senders.first.username, - count: 2 - ) + expected_subject = + I18n.t( + "user_notifications.chat_summary.subject.direct_message_from_more", + email_prefix: SiteSetting.title, + username: senders.first.username, + count: 2, + ) expect(email.subject).to eq(expected_subject) end @@ -162,11 +166,12 @@ describe UserNotifications do before { Fabricate(:chat_mention, user: user, chat_message: chat_message) } it "includes the sender username in the subject" do - expected_subject = I18n.t( - "user_notifications.chat_summary.subject.chat_channel_1", - email_prefix: SiteSetting.title, - channel: channel.title(user) - ) + expected_subject = + I18n.t( + "user_notifications.chat_summary.subject.chat_channel_1", + email_prefix: SiteSetting.title, + channel: channel.title(user), + ) email = described_class.chat_summary(user, {}) @@ -193,12 +198,13 @@ describe UserNotifications do email = described_class.chat_summary(user, {}) - expected_subject = I18n.t( - "user_notifications.chat_summary.subject.chat_channel_2", - email_prefix: SiteSetting.title, - channel1: channel.title(user), - channel2: another_chat_channel.title(user) - ) + expected_subject = + I18n.t( + "user_notifications.chat_summary.subject.chat_channel_2", + email_prefix: SiteSetting.title, + channel1: channel.title(user), + channel2: another_chat_channel.title(user), + ) expect(email.subject).to eq(expected_subject) expect(email.subject).to include(channel.title(user)) @@ -224,12 +230,13 @@ describe UserNotifications do Fabricate(:chat_mention, user: user, chat_message: another_chat_message) end - expected_subject = I18n.t( - "user_notifications.chat_summary.subject.chat_channel_more", - email_prefix: SiteSetting.title, - channel: channel.title(user), - count: 2 - ) + expected_subject = + I18n.t( + "user_notifications.chat_summary.subject.chat_channel_more", + email_prefix: SiteSetting.title, + channel: channel.title(user), + count: 2, + ) email = described_class.chat_summary(user, {}) @@ -250,12 +257,13 @@ describe UserNotifications do end it "always includes the DM second" do - expected_subject = I18n.t( - "user_notifications.chat_summary.subject.chat_channel_and_direct_message", - email_prefix: SiteSetting.title, - channel: channel.title(user), - username: sender.username - ) + expected_subject = + I18n.t( + "user_notifications.chat_summary.subject.chat_channel_and_direct_message", + email_prefix: SiteSetting.title, + channel: channel.title(user), + username: sender.username, + ) email = described_class.chat_summary(user, {}) diff --git a/plugins/chat/spec/models/chat_message_spec.rb b/plugins/chat/spec/models/chat_message_spec.rb index 89d1920eac2..1ce6a39ed36 100644 --- a/plugins/chat/spec/models/chat_message_spec.rb +++ b/plugins/chat/spec/models/chat_message_spec.rb @@ -517,7 +517,8 @@ describe ChatMessage do it "keeps the same hashtags the user has permission to after rebake" do group.add(chat_message.user) chat_message.update!( - message: "this is the message ##{category.slug} ##{secure_category.slug} ##{chat_message.chat_channel.slug}", + message: + "this is the message ##{category.slug} ##{secure_category.slug} ##{chat_message.chat_channel.slug}", ) chat_message.cook chat_message.save! diff --git a/plugins/chat/spec/models/deleted_chat_user_spec.rb b/plugins/chat/spec/models/deleted_chat_user_spec.rb index 92617c58f73..387eb5c89f9 100644 --- a/plugins/chat/spec/models/deleted_chat_user_spec.rb +++ b/plugins/chat/spec/models/deleted_chat_user_spec.rb @@ -11,9 +11,7 @@ describe DeletedChatUser do describe "#avatar_template" do it "returns a default path" do - expect(subject.avatar_template).to eq( - "/plugins/chat/images/deleted-chat-user-avatar.png", - ) + expect(subject.avatar_template).to eq("/plugins/chat/images/deleted-chat-user-avatar.png") end end end diff --git a/plugins/chat/spec/requests/api/chat_channels_current_user_notifications_settings_controller_spec.rb b/plugins/chat/spec/requests/api/chat_channels_current_user_notifications_settings_controller_spec.rb index bf0dc2c3536..f5fe1f3ae34 100644 --- a/plugins/chat/spec/requests/api/chat_channels_current_user_notifications_settings_controller_spec.rb +++ b/plugins/chat/spec/requests/api/chat_channels_current_user_notifications_settings_controller_spec.rb @@ -12,11 +12,7 @@ RSpec.describe Chat::Api::ChatChannelsCurrentUserNotificationsSettingsController include_examples "channel access example", :put, "/notifications-settings/me", - { - notifications_settings: { - muted: true, - }, - } + { notifications_settings: { muted: true } } context "when category channel has invalid params" do fab!(:channel_1) { Fabricate(:category_channel) } diff --git a/plugins/chat/spec/requests/api/chat_channels_status_controller_spec.rb b/plugins/chat/spec/requests/api/chat_channels_status_controller_spec.rb index 942d063a7ed..c2e687da594 100644 --- a/plugins/chat/spec/requests/api/chat_channels_status_controller_spec.rb +++ b/plugins/chat/spec/requests/api/chat_channels_status_controller_spec.rb @@ -61,9 +61,9 @@ RSpec.describe Chat::Api::ChatChannelsStatusController do context "when changing from open to closed" do it "changes the status" do - expect { put "/chat/api/channels/#{channel_1.id}/status", params: status("closed") }.to change { - channel_1.reload.status - }.to("closed").from("open") + expect { + put "/chat/api/channels/#{channel_1.id}/status", params: status("closed") + }.to change { channel_1.reload.status }.to("closed").from("open") expect(response.status).to eq(200) channel = response.parsed_body["channel"] @@ -75,9 +75,9 @@ RSpec.describe Chat::Api::ChatChannelsStatusController do before { channel_1.update!(status: "closed") } it "changes the status" do - expect { put "/chat/api/channels/#{channel_1.id}/status", params: status("open") }.to change { - channel_1.reload.status - }.to("open").from("closed") + expect { + put "/chat/api/channels/#{channel_1.id}/status", params: status("open") + }.to change { channel_1.reload.status }.to("open").from("closed") expect(response.status).to eq(200) channel = response.parsed_body["channel"] diff --git a/plugins/chat/spec/requests/chat_controller_spec.rb b/plugins/chat/spec/requests/chat_controller_spec.rb index 8b08203a697..8ff5912df5b 100644 --- a/plugins/chat/spec/requests/chat_controller_spec.rb +++ b/plugins/chat/spec/requests/chat_controller_spec.rb @@ -1114,7 +1114,11 @@ RSpec.describe Chat::ChatController do it "returns a 403 if the user can't see the channel" do category.update!(read_restricted: true) group = Fabricate(:group) - CategoryGroup.create(group: group, category: category, permission_type: CategoryGroup.permission_types[:create_post]) + CategoryGroup.create( + group: group, + category: category, + permission_type: CategoryGroup.permission_types[:create_post], + ) sign_in(user) post "/chat/#{channel.id}/quote.json", params: { diff --git a/plugins/chat/spec/requests/core_ext/categories_controller_spec.rb b/plugins/chat/spec/requests/core_ext/categories_controller_spec.rb index 431ded15610..3ecc2011ff2 100644 --- a/plugins/chat/spec/requests/core_ext/categories_controller_spec.rb +++ b/plugins/chat/spec/requests/core_ext/categories_controller_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe CategoriesController do - describe '#destroy' do + describe "#destroy" do subject(:destroy_category) { delete "/categories/#{category.slug}.json" } fab!(:admin) { Fabricate(:admin) } diff --git a/plugins/chat/spec/services/chat_message_destroyer_spec.rb b/plugins/chat/spec/services/chat_message_destroyer_spec.rb index 66e58a85aad..e6016b7bca3 100644 --- a/plugins/chat/spec/services/chat_message_destroyer_spec.rb +++ b/plugins/chat/spec/services/chat_message_destroyer_spec.rb @@ -23,11 +23,7 @@ RSpec.describe ChatMessageDestroyer do it "deletes flags associated to deleted chat messages" do guardian = Guardian.new(Discourse.system_user) - Chat::ChatReviewQueue.new.flag_message( - message_1, - guardian, - ReviewableScore.types[:off_topic], - ) + Chat::ChatReviewQueue.new.flag_message(message_1, guardian, ReviewableScore.types[:off_topic]) reviewable = ReviewableChatMessage.last expect(reviewable).to be_present diff --git a/plugins/chat/spec/support/examples/chatable_model.rb b/plugins/chat/spec/support/examples/chatable_model.rb index 78237d7937d..8c274b38537 100644 --- a/plugins/chat/spec/support/examples/chatable_model.rb +++ b/plugins/chat/spec/support/examples/chatable_model.rb @@ -6,8 +6,8 @@ RSpec.shared_examples "a chatable model" do it "returns a new chat channel model" do expect(chat_channel).to have_attributes persisted?: false, - class: channel_class, - chatable: chatable + class: channel_class, + chatable: chatable end end diff --git a/plugins/chat/spec/system/hashtag_autocomplete_spec.rb b/plugins/chat/spec/system/hashtag_autocomplete_spec.rb index ad613449436..47d4401c7a4 100644 --- a/plugins/chat/spec/system/hashtag_autocomplete_spec.rb +++ b/plugins/chat/spec/system/hashtag_autocomplete_spec.rb @@ -32,7 +32,9 @@ describe "Using #hashtag autocompletion to search for and lookup channels", count: 3, ) hashtag_results = page.all(".hashtag-autocomplete__link", count: 3) - expect(hashtag_results.map(&:text).map { |r| r.gsub("\n", " ") }).to eq(["Random", "Raspberry", "razed (x0)"]) + expect(hashtag_results.map(&:text).map { |r| r.gsub("\n", " ") }).to eq( + ["Random", "Raspberry", "razed (x0)"], + ) end it "searches for channels as well with # in a topic composer and deprioritises them" do @@ -44,18 +46,26 @@ describe "Using #hashtag autocompletion to search for and lookup channels", count: 3, ) hashtag_results = page.all(".hashtag-autocomplete__link", count: 3) - expect(hashtag_results.map(&:text).map { |r| r.gsub("\n", " ") }).to eq(["Raspberry", "razed (x0)", "Random"]) + expect(hashtag_results.map(&:text).map { |r| r.gsub("\n", " ") }).to eq( + ["Raspberry", "razed (x0)", "Random"], + ) end it "cooks the hashtags for channels, categories, and tags serverside when the chat message is saved to the database" do chat_page.visit_channel(channel1) expect(chat_channel_page).to have_no_loading_skeleton - chat_channel_page.type_in_composer("this is #random and this is #raspberry-beret and this is #razed which is cool") + chat_channel_page.type_in_composer( + "this is #random and this is #raspberry-beret and this is #razed which is cool", + ) chat_channel_page.click_send_message message = nil try_until_success do - message = ChatMessage.find_by(user: user, message: "this is #random and this is #raspberry-beret and this is #razed which is cool") + message = + ChatMessage.find_by( + user: user, + message: "this is #random and this is #raspberry-beret and this is #razed which is cool", + ) expect(message).not_to eq(nil) end expect(chat_channel_page).to have_message(id: message.id) diff --git a/plugins/chat/spec/system/list_channels/mobile_spec.rb b/plugins/chat/spec/system/list_channels/mobile_spec.rb index 073ca8533df..59744187b7b 100644 --- a/plugins/chat/spec/system/list_channels/mobile_spec.rb +++ b/plugins/chat/spec/system/list_channels/mobile_spec.rb @@ -43,8 +43,12 @@ RSpec.describe "List channels | mobile", type: :system, js: true, mobile: true d it "sorts them alphabetically" do visit("/chat") - expect(page.find("#public-channels a:nth-child(1)")["data-chat-channel-id"]).to eq(channel_2.id.to_s) - expect(page.find("#public-channels a:nth-child(2)")["data-chat-channel-id"]).to eq(channel_1.id.to_s) + expect(page.find("#public-channels a:nth-child(1)")["data-chat-channel-id"]).to eq( + channel_2.id.to_s, + ) + expect(page.find("#public-channels a:nth-child(2)")["data-chat-channel-id"]).to eq( + channel_1.id.to_s, + ) end end diff --git a/plugins/chat/spec/system/list_channels/no_sidebar_spec.rb b/plugins/chat/spec/system/list_channels/no_sidebar_spec.rb index 37266c51a2d..9f751df0471 100644 --- a/plugins/chat/spec/system/list_channels/no_sidebar_spec.rb +++ b/plugins/chat/spec/system/list_channels/no_sidebar_spec.rb @@ -44,8 +44,12 @@ RSpec.describe "List channels | no sidebar", type: :system, js: true do it "sorts them alphabetically" do visit("/chat") - expect(page.find("#public-channels a:nth-child(1)")["data-chat-channel-id"]).to eq(channel_2.id.to_s) - expect(page.find("#public-channels a:nth-child(2)")["data-chat-channel-id"]).to eq(channel_1.id.to_s) + expect(page.find("#public-channels a:nth-child(1)")["data-chat-channel-id"]).to eq( + channel_2.id.to_s, + ) + expect(page.find("#public-channels a:nth-child(2)")["data-chat-channel-id"]).to eq( + channel_1.id.to_s, + ) end end diff --git a/plugins/chat/spec/system/list_channels/sidebar_spec.rb b/plugins/chat/spec/system/list_channels/sidebar_spec.rb index aae03c9c44a..f7c4889c569 100644 --- a/plugins/chat/spec/system/list_channels/sidebar_spec.rb +++ b/plugins/chat/spec/system/list_channels/sidebar_spec.rb @@ -53,8 +53,12 @@ RSpec.describe "List channels | sidebar", type: :system, js: true do it "sorts them alphabetically" do visit("/") - expect(page.find("#sidebar-section-content-chat-channels li:nth-child(1)")).to have_css(".channel-#{channel_2.id}") - expect(page.find("#sidebar-section-content-chat-channels li:nth-child(2)")).to have_css(".channel-#{channel_1.id}") + expect(page.find("#sidebar-section-content-chat-channels li:nth-child(1)")).to have_css( + ".channel-#{channel_2.id}", + ) + expect(page.find("#sidebar-section-content-chat-channels li:nth-child(2)")).to have_css( + ".channel-#{channel_1.id}", + ) end end diff --git a/plugins/chat/spec/system/navigating_to_message_spec.rb b/plugins/chat/spec/system/navigating_to_message_spec.rb index f23097373e4..7dc60d697e2 100644 --- a/plugins/chat/spec/system/navigating_to_message_spec.rb +++ b/plugins/chat/spec/system/navigating_to_message_spec.rb @@ -43,14 +43,20 @@ RSpec.describe "Navigating to message", type: :system, js: true do context "when clicking a link to a message from the current channel" do before do - Fabricate(:chat_message, chat_channel: channel_1, message: "[#{link}](/chat/channel/#{channel_1.id}/-?messageId=#{first_message.id})") + Fabricate( + :chat_message, + chat_channel: channel_1, + message: "[#{link}](/chat/channel/#{channel_1.id}/-?messageId=#{first_message.id})", + ) end it "highglights the correct message" do chat_page.visit_channel(channel_1) click_link(link) - expect(page).to have_css(".chat-message-container.highlighted[data-id='#{first_message.id}']") + expect(page).to have_css( + ".chat-message-container.highlighted[data-id='#{first_message.id}']", + ) end it "highlights the correct message after using the bottom arrow" do @@ -59,7 +65,9 @@ RSpec.describe "Navigating to message", type: :system, js: true do click_link(I18n.t("js.chat.scroll_to_bottom")) click_link(link) - expect(page).to have_css(".chat-message-container.highlighted[data-id='#{first_message.id}']") + expect(page).to have_css( + ".chat-message-container.highlighted[data-id='#{first_message.id}']", + ) end end @@ -67,7 +75,11 @@ RSpec.describe "Navigating to message", type: :system, js: true do fab!(:channel_2) { Fabricate(:category_channel) } before do - Fabricate(:chat_message, chat_channel: channel_2, message: "[#{link}](/chat/channel/#{channel_1.id}/-?messageId=#{first_message.id})") + Fabricate( + :chat_message, + chat_channel: channel_2, + message: "[#{link}](/chat/channel/#{channel_1.id}/-?messageId=#{first_message.id})", + ) channel_2.add(current_user) end @@ -75,7 +87,9 @@ RSpec.describe "Navigating to message", type: :system, js: true do chat_page.visit_channel(channel_2) click_link(link) - expect(page).to have_css(".chat-message-container.highlighted[data-id='#{first_message.id}']") + expect(page).to have_css( + ".chat-message-container.highlighted[data-id='#{first_message.id}']", + ) end end @@ -83,7 +97,9 @@ RSpec.describe "Navigating to message", type: :system, js: true do it "highglights the correct message" do visit("/chat/channel/#{channel_1.id}/-?messageId=#{first_message.id}") - expect(page).to have_css(".chat-message-container.highlighted[data-id='#{first_message.id}']") + expect(page).to have_css( + ".chat-message-container.highlighted[data-id='#{first_message.id}']", + ) end end end @@ -113,7 +129,11 @@ RSpec.describe "Navigating to message", type: :system, js: true do context "when clicking a link to a message from the current channel" do before do - Fabricate(:chat_message, chat_channel: channel_1, message: "[#{link}](/chat/channel/#{channel_1.id}/-?messageId=#{first_message.id})") + Fabricate( + :chat_message, + chat_channel: channel_1, + message: "[#{link}](/chat/channel/#{channel_1.id}/-?messageId=#{first_message.id})", + ) end it "highglights the correct message" do @@ -122,7 +142,9 @@ RSpec.describe "Navigating to message", type: :system, js: true do chat_drawer_page.open_channel(channel_1) click_link(link) - expect(page).to have_css(".chat-message-container.highlighted[data-id='#{first_message.id}']") + expect(page).to have_css( + ".chat-message-container.highlighted[data-id='#{first_message.id}']", + ) end it "highlights the correct message after using the bottom arrow" do @@ -133,7 +155,9 @@ RSpec.describe "Navigating to message", type: :system, js: true do click_link(I18n.t("js.chat.scroll_to_bottom")) click_link(link) - expect(page).to have_css(".chat-message-container.highlighted[data-id='#{first_message.id}']") + expect(page).to have_css( + ".chat-message-container.highlighted[data-id='#{first_message.id}']", + ) end end end diff --git a/plugins/chat/spec/system/page_objects/chat/chat.rb b/plugins/chat/spec/system/page_objects/chat/chat.rb index a070c700a10..38e341e46d2 100644 --- a/plugins/chat/spec/system/page_objects/chat/chat.rb +++ b/plugins/chat/spec/system/page_objects/chat/chat.rb @@ -4,7 +4,9 @@ module PageObjects module Pages class Chat < PageObjects::Pages::Base def prefers_full_page - page.execute_script("window.localStorage.setItem('discourse_chat_preferred_mode', '\"FULL_PAGE_CHAT\"');") + page.execute_script( + "window.localStorage.setItem('discourse_chat_preferred_mode', '\"FULL_PAGE_CHAT\"');", + ) end def open_from_header diff --git a/plugins/discourse-details/plugin.rb b/plugins/discourse-details/plugin.rb index 130bd58a325..6d14a1dba67 100644 --- a/plugins/discourse-details/plugin.rb +++ b/plugins/discourse-details/plugin.rb @@ -12,32 +12,34 @@ hide_plugin if self.respond_to?(:hide_plugin) register_asset "stylesheets/details.scss" after_initialize do - Email::Styles.register_plugin_style do |fragment| # remove all elided content fragment.css("details.elided").each(&:remove) # replace all details with their summary in emails - fragment.css("details").each do |details| - summary = details.css("summary") - if summary && summary[0] - summary = summary[0] - if summary && summary.respond_to?(:name) - summary.name = "p" - details.replace(summary) + fragment + .css("details") + .each do |details| + summary = details.css("summary") + if summary && summary[0] + summary = summary[0] + if summary && summary.respond_to?(:name) + summary.name = "p" + details.replace(summary) + end end end - end end on(:reduce_cooked) do |fragment, post| - fragment.css("details").each do |el| - text = el.css("summary").text - link = fragment.document.create_element("a") - link["href"] = post.url if post - link.content = I18n.t("details.excerpt_details") - el.replace CGI.escapeHTML(text) + " " + link.to_html - end + fragment + .css("details") + .each do |el| + text = el.css("summary").text + link = fragment.document.create_element("a") + link["href"] = post.url if post + link.content = I18n.t("details.excerpt_details") + el.replace CGI.escapeHTML(text) + " " + link.to_html + end end - end diff --git a/plugins/discourse-details/spec/components/pretty_text_spec.rb b/plugins/discourse-details/spec/components/pretty_text_spec.rb index bbbcb6ba277..20b84acfc79 100644 --- a/plugins/discourse-details/spec/components/pretty_text_spec.rb +++ b/plugins/discourse-details/spec/components/pretty_text_spec.rb @@ -1,10 +1,9 @@ # frozen_string_literal: true -require 'rails_helper' -require 'pretty_text' +require "rails_helper" +require "pretty_text" RSpec.describe PrettyText do - let(:post) { Fabricate(:post) } it "supports details tag" do @@ -17,17 +16,19 @@ RSpec.describe PrettyText do HTML expect(cooked_html).to match_html(cooked_html) - expect(PrettyText.cook("[details=foo]\nbar\n[/details]").gsub("\n", "")).to match_html(cooked_html) + expect(PrettyText.cook("[details=foo]\nbar\n[/details]").gsub("\n", "")).to match_html( + cooked_html, + ) end it "deletes elided content" do cooked_html = PrettyText.cook("Hello World\n\n
42
") - mail_html = "

Hello World

\n(click for more details)" + mail_html = "

Hello World

\n(click for more details)" expect(PrettyText.format_for_email(cooked_html)).to match_html(mail_html) end - it 'can replace spoilers in emails' do + it "can replace spoilers in emails" do md = PrettyText.cook(<<~MD) hello @@ -41,7 +42,7 @@ RSpec.describe PrettyText do expect(md).to eq(html) end - it 'properly handles multiple spoiler blocks in a post' do + it "properly handles multiple spoiler blocks in a post" do md = PrettyText.cook(<<~MD) [details="First"] body secret stuff very long @@ -58,13 +59,13 @@ RSpec.describe PrettyText do MD md = PrettyText.format_for_email(md, post) - expect(md).not_to include('secret stuff') + expect(md).not_to include("secret stuff") expect(md.scan(/First/).size).to eq(1) expect(md.scan(/Third/).size).to eq(1) - expect(md.scan(I18n.t('details.excerpt_details')).size).to eq(3) + expect(md.scan(I18n.t("details.excerpt_details")).size).to eq(3) end - it 'escapes summary text' do + it "escapes summary text" do md = PrettyText.cook(<<~MD) [details=""] @@ -73,7 +74,6 @@ RSpec.describe PrettyText do MD md = PrettyText.format_for_email(md, post) - expect(md).not_to include(']\n- A\n- B\n[/poll]" - }, format: :json + post :create, + params: { + title: title, + raw: "[poll name=]\n- A\n- B\n[/poll]", + }, + format: :json expect(response.status).to eq(200) json = response.parsed_body expect(json["cooked"]).to match("data-poll-") expect(json["cooked"]).to include("<script>") - expect(Poll.find_by(post_id: json["id"]).name).to eq("<script>alert('xss')</script>") + expect(Poll.find_by(post_id: json["id"]).name).to eq( + "<script>alert('xss')</script>", + ) end it "also works when there is a link starting with '[poll'" do - post :create, params: { - title: title, raw: "[Polls are awesome](/foobar)\n[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, + params: { + title: title, + raw: "[Polls are awesome](/foobar)\n[poll]\n- A\n- B\n[/poll]", + }, + format: :json expect(response.status).to eq(200) json = response.parsed_body @@ -139,9 +151,12 @@ RSpec.describe PostsController do end it "prevents poll-inception" do - post :create, params: { - title: title, raw: "[poll name=1]\n- A\n[poll name=2]\n- B\n- C\n[/poll]\n- D\n[/poll]" - }, format: :json + post :create, + params: { + title: title, + raw: "[poll name=1]\n- A\n[poll name=2]\n- B\n- C\n[/poll]\n- D\n[/poll]", + }, + format: :json expect(response.status).to eq(200) json = response.parsed_body @@ -150,9 +165,12 @@ RSpec.describe PostsController do end it "accepts polls with titles" do - post :create, params: { - title: title, raw: "[poll]\n# What's up?\n- one\n[/poll]" - }, format: :json + post :create, + params: { + title: title, + raw: "[poll]\n# What's up?\n- one\n[/poll]", + }, + format: :json expect(response).to be_successful poll = Poll.last @@ -161,23 +179,24 @@ RSpec.describe PostsController do end describe "edit window" do - describe "within the first 5 minutes" do - let(:post_id) do freeze_time(4.minutes.ago) do - post :create, params: { - title: title, raw: "[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json response.parsed_body["id"] end end it "can be changed" do - put :update, params: { - id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" } - }, format: :json + put :update, + params: { + id: post_id, + post: { + raw: "[poll]\n- A\n- B\n- C\n[/poll]", + }, + }, + format: :json expect(response.status).to eq(200) json = response.parsed_body @@ -187,28 +206,29 @@ RSpec.describe PostsController do it "resets the votes" do DiscoursePoll::Poll.vote(user, post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"]) - put :update, params: { - id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" } - }, format: :json + put :update, + params: { + id: post_id, + post: { + raw: "[poll]\n- A\n- B\n- C\n[/poll]", + }, + }, + format: :json expect(response.status).to eq(200) json = response.parsed_body expect(json["post"]["polls_votes"]).to_not be end - end describe "after the poll edit window has expired" do - let(:poll) { "[poll]\n- A\n- B\n[/poll]" } let(:new_option) { "[poll]\n- A\n- C\n[/poll]" } let(:updated) { "before\n\n[poll]\n- A\n- B\n[/poll]\n\nafter" } let(:post_id) do freeze_time(6.minutes.ago) do - post :create, params: { - title: title, raw: poll - }, format: :json + post :create, params: { title: title, raw: poll }, format: :json response.parsed_body["id"] end @@ -216,16 +236,11 @@ RSpec.describe PostsController do let(:poll_edit_window_mins) { 6 } - before do - SiteSetting.poll_edit_window_mins = poll_edit_window_mins - end + before { SiteSetting.poll_edit_window_mins = poll_edit_window_mins } describe "with no vote" do - it "can change the options" do - put :update, params: { - id: post_id, post: { raw: new_option } - }, format: :json + put :update, params: { id: post_id, post: { raw: new_option } }, format: :json expect(response.status).to eq(200) json = response.parsed_body @@ -238,26 +253,24 @@ RSpec.describe PostsController do json = response.parsed_body expect(json["post"]["cooked"]).to match("before") end - end describe "with at least one vote" do - before do DiscoursePoll::Poll.vote(user, post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"]) end it "cannot change the options" do - put :update, params: { - id: post_id, post: { raw: new_option } - }, format: :json + put :update, params: { id: post_id, post: { raw: new_option } }, format: :json expect(response).not_to be_successful json = response.parsed_body - expect(json["errors"][0]).to eq(I18n.t( - "poll.edit_window_expired.cannot_edit_default_poll_with_votes", - minutes: poll_edit_window_mins - )) + expect(json["errors"][0]).to eq( + I18n.t( + "poll.edit_window_expired.cannot_edit_default_poll_with_votes", + minutes: poll_edit_window_mins, + ), + ) end it "support changes on the post" do @@ -266,45 +279,49 @@ RSpec.describe PostsController do json = response.parsed_body expect(json["post"]["cooked"]).to match("before") end - end - end - end - end describe "named polls" do - it "should have different options" do - post :create, params: { - title: title, raw: "[poll name=""foo""]\n- A\n- A\n[/poll]" - }, format: :json + post :create, + params: { + title: title, + raw: + "[poll name=" \ + "foo" \ + "]\n- A\n- A\n[/poll]", + }, + format: :json expect(response).not_to be_successful json = response.parsed_body - expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_different_options", name: "foo")) + expect(json["errors"][0]).to eq( + I18n.t("poll.named_poll_must_have_different_options", name: "foo"), + ) end it "should have at least 1 option" do - post :create, params: { - title: title, raw: "[poll name='foo']\n[/poll]" - }, format: :json + post :create, params: { title: title, raw: "[poll name='foo']\n[/poll]" }, format: :json expect(response).not_to be_successful json = response.parsed_body - expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_at_least_1_option", name: "foo")) + expect(json["errors"][0]).to eq( + I18n.t("poll.named_poll_must_have_at_least_1_option", name: "foo"), + ) end - end describe "multiple polls" do - it "works" do - post :create, params: { - title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]" - }, format: :json + post :create, + params: { + title: title, + raw: "[poll]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]", + }, + format: :json expect(response.status).to eq(200) json = response.parsed_body @@ -313,9 +330,12 @@ RSpec.describe PostsController do end it "should have a name" do - post :create, params: { - title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, + params: { + title: title, + raw: "[poll]\n- A\n- B\n[/poll]\n[poll]\n- A\n- B\n[/poll]", + }, + format: :json expect(response).not_to be_successful json = response.parsed_body @@ -323,46 +343,42 @@ RSpec.describe PostsController do end it "should have unique name" do - post :create, params: { - title: title, raw: "[poll name=foo]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]" - }, format: :json + post :create, + params: { + title: title, + raw: "[poll name=foo]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]", + }, + format: :json expect(response).not_to be_successful json = response.parsed_body expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_with_same_name", name: "foo")) end - end describe "disabled polls" do - before do - SiteSetting.poll_enabled = false - end + before { SiteSetting.poll_enabled = false } it "doesn’t cook the poll" do log_in_user(Fabricate(:user, admin: true, trust_level: 4)) - post :create, params: { - title: title, raw: "[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json expect(response.status).to eq(200) json = response.parsed_body - expect(json["cooked"]).to eq("

[poll]

\n") + expect(json["cooked"]).to eq( + "

[poll]

\n", + ) end end describe "regular user with insufficient trust level" do - before do - SiteSetting.poll_minimum_trust_level_to_create = 2 - end + before { SiteSetting.poll_minimum_trust_level_to_create = 2 } it "invalidates the post" do log_in_user(Fabricate(:user, trust_level: 1)) - post :create, params: { - title: title, raw: "[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json expect(response).not_to be_successful json = response.parsed_body @@ -371,33 +387,31 @@ RSpec.describe PostsController do it "skips the check in PMs with bots" do user = Fabricate(:user, trust_level: 1) - topic = Fabricate(:private_message_topic, topic_allowed_users: [ - Fabricate.build(:topic_allowed_user, user: user), - Fabricate.build(:topic_allowed_user, user: Discourse.system_user) - ]) + topic = + Fabricate( + :private_message_topic, + topic_allowed_users: [ + Fabricate.build(:topic_allowed_user, user: user), + Fabricate.build(:topic_allowed_user, user: Discourse.system_user), + ], + ) Fabricate(:post, topic_id: topic.id, user_id: Discourse::SYSTEM_USER_ID) log_in_user(user) - post :create, params: { - topic_id: topic.id, raw: "[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, params: { topic_id: topic.id, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json expect(response.parsed_body["errors"]).to eq(nil) end end describe "regular user with equal trust level" do - before do - SiteSetting.poll_minimum_trust_level_to_create = 2 - end + before { SiteSetting.poll_minimum_trust_level_to_create = 2 } it "validates the post" do log_in_user(Fabricate(:user, trust_level: 2)) - post :create, params: { - title: title, raw: "[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json expect(response.status).to eq(200) json = response.parsed_body @@ -407,16 +421,12 @@ RSpec.describe PostsController do end describe "regular user with superior trust level" do - before do - SiteSetting.poll_minimum_trust_level_to_create = 2 - end + before { SiteSetting.poll_minimum_trust_level_to_create = 2 } it "validates the post" do log_in_user(Fabricate(:user, trust_level: 3)) - post :create, params: { - title: title, raw: "[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json expect(response.status).to eq(200) json = response.parsed_body @@ -426,16 +436,12 @@ RSpec.describe PostsController do end describe "staff with insufficient trust level" do - before do - SiteSetting.poll_minimum_trust_level_to_create = 2 - end + before { SiteSetting.poll_minimum_trust_level_to_create = 2 } it "validates the post" do log_in_user(Fabricate(:user, moderator: true, trust_level: 1)) - post :create, params: { - title: title, raw: "[poll]\n- A\n- B\n[/poll]" - }, format: :json + post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json expect(response.status).to eq(200) json = response.parsed_body @@ -445,9 +451,7 @@ RSpec.describe PostsController do end describe "staff editing posts of users with insufficient trust level" do - before do - SiteSetting.poll_minimum_trust_level_to_create = 2 - end + before { SiteSetting.poll_minimum_trust_level_to_create = 2 } it "validates the post" do log_in_user(Fabricate(:user, trust_level: 1)) @@ -459,9 +463,14 @@ RSpec.describe PostsController do log_in_user(Fabricate(:admin)) - put :update, params: { - id: post_id, post: { raw: "#{title}\n[poll]\n- A\n- B\n- C\n[/poll]" } - }, format: :json + put :update, + params: { + id: post_id, + post: { + raw: "#{title}\n[poll]\n- A\n- B\n- C\n[/poll]", + }, + }, + format: :json expect(response.status).to eq(200) expect(response.parsed_body["post"]["polls"][0]["options"][2]["html"]).to eq("C") diff --git a/plugins/poll/spec/integration/poll_endpoints_spec.rb b/plugins/poll/spec/integration/poll_endpoints_spec.rb index 9fff1972a88..e8b62dae9f9 100644 --- a/plugins/poll/spec/integration/poll_endpoints_spec.rb +++ b/plugins/poll/spec/integration/poll_endpoints_spec.rb @@ -7,31 +7,25 @@ RSpec.describe "DiscoursePoll endpoints" do fab!(:user) { Fabricate(:user) } fab!(:post) { Fabricate(:post, raw: "[poll public=true]\n- A\n- B\n[/poll]") } - fab!(:post_with_multiple_poll) do - Fabricate(:post, raw: <<~SQL) + fab!(:post_with_multiple_poll) { Fabricate(:post, raw: <<~SQL) } [poll type=multiple public=true min=1 max=2] - A - B - C [/poll] SQL - end let(:option_a) { "5c24fc1df56d764b550ceae1b9319125" } let(:option_b) { "e89dec30bbd9bf50fabf6a05b4324edf" } it "should return the right response" do - DiscoursePoll::Poll.vote( - user, - post.id, - DiscoursePoll::DEFAULT_POLL_NAME, - [option_a] - ) + DiscoursePoll::Poll.vote(user, post.id, DiscoursePoll::DEFAULT_POLL_NAME, [option_a]) - get "/polls/voters.json", params: { - post_id: post.id, - poll_name: DiscoursePoll::DEFAULT_POLL_NAME - } + get "/polls/voters.json", + params: { + post_id: post.id, + poll_name: DiscoursePoll::DEFAULT_POLL_NAME, + } expect(response.status).to eq(200) @@ -43,19 +37,20 @@ RSpec.describe "DiscoursePoll endpoints" do expect(option.first["username"]).to eq(user.username) end - it 'should return the right response for a single option' do + it "should return the right response for a single option" do DiscoursePoll::Poll.vote( user, post_with_multiple_poll.id, DiscoursePoll::DEFAULT_POLL_NAME, - [option_a, option_b] + [option_a, option_b], ) - get "/polls/voters.json", params: { - post_id: post_with_multiple_poll.id, - poll_name: DiscoursePoll::DEFAULT_POLL_NAME, - option_id: option_b - } + get "/polls/voters.json", + params: { + post_id: post_with_multiple_poll.id, + poll_name: DiscoursePoll::DEFAULT_POLL_NAME, + option_id: option_b, + } expect(response.status).to eq(200) @@ -70,56 +65,60 @@ RSpec.describe "DiscoursePoll endpoints" do expect(option.first["username"]).to eq(user.username) end - describe 'when post_id is blank' do - it 'should raise the right error' do + describe "when post_id is blank" do + it "should raise the right error" do get "/polls/voters.json", params: { poll_name: DiscoursePoll::DEFAULT_POLL_NAME } expect(response.status).to eq(400) end end - describe 'when post_id is not valid' do - it 'should raise the right error' do - get "/polls/voters.json", params: { - post_id: -1, - poll_name: DiscoursePoll::DEFAULT_POLL_NAME - } + describe "when post_id is not valid" do + it "should raise the right error" do + get "/polls/voters.json", + params: { + post_id: -1, + poll_name: DiscoursePoll::DEFAULT_POLL_NAME, + } expect(response.status).to eq(400) - expect(response.body).to include('post_id') + expect(response.body).to include("post_id") end end - describe 'when poll_name is blank' do - it 'should raise the right error' do + describe "when poll_name is blank" do + it "should raise the right error" do get "/polls/voters.json", params: { post_id: post.id } expect(response.status).to eq(400) end end - describe 'when poll_name is not valid' do - it 'should raise the right error' do - get "/polls/voters.json", params: { post_id: post.id, poll_name: 'wrongpoll' } + describe "when poll_name is not valid" do + it "should raise the right error" do + get "/polls/voters.json", params: { post_id: post.id, poll_name: "wrongpoll" } expect(response.status).to eq(400) - expect(response.body).to include('poll_name') + expect(response.body).to include("poll_name") end end context "with number poll" do - let(:post) { Fabricate(:post, raw: "[poll type=number min=1 max=20 step=1 public=true]\n[/poll]") } + let(:post) do + Fabricate(:post, raw: "[poll type=number min=1 max=20 step=1 public=true]\n[/poll]") + end - it 'should return the right response' do + it "should return the right response" do post DiscoursePoll::Poll.vote( user, post.id, DiscoursePoll::DEFAULT_POLL_NAME, - ["4d8a15e3cc35750f016ce15a43937620"] + ["4d8a15e3cc35750f016ce15a43937620"], ) - get "/polls/voters.json", params: { - post_id: post.id, - poll_name: DiscoursePoll::DEFAULT_POLL_NAME - } + get "/polls/voters.json", + params: { + post_id: post.id, + poll_name: DiscoursePoll::DEFAULT_POLL_NAME, + } expect(response.status).to eq(200) @@ -137,31 +136,25 @@ RSpec.describe "DiscoursePoll endpoints" do fab!(:user3) { Fabricate(:user) } fab!(:user4) { Fabricate(:user) } - fab!(:post) do - Fabricate(:post, raw: <<~SQL) + fab!(:post) { Fabricate(:post, raw: <<~SQL) } [poll type=multiple public=true min=1 max=2] - A - B [/poll] SQL - end let(:option_a) { "5c24fc1df56d764b550ceae1b9319125" } let(:option_b) { "e89dec30bbd9bf50fabf6a05b4324edf" } before do - user_votes = { - user_0: option_a, - user_1: option_a, - user_2: option_b, - } + user_votes = { user_0: option_a, user_1: option_a, user_2: option_b } [user1, user2, user3].each_with_index do |user, index| DiscoursePoll::Poll.vote( user, post.id, DiscoursePoll::DEFAULT_POLL_NAME, - [user_votes["user_#{index}".to_sym]] + [user_votes["user_#{index}".to_sym]], ) UserCustomField.create(user_id: user.id, name: "something", value: "value#{index}") end @@ -171,7 +164,7 @@ RSpec.describe "DiscoursePoll endpoints" do user4, post.id, DiscoursePoll::DEFAULT_POLL_NAME, - [option_a, option_b] + [option_a, option_b], ) UserCustomField.create(user_id: user4.id, name: "something", value: "value1") end @@ -179,32 +172,52 @@ RSpec.describe "DiscoursePoll endpoints" do it "returns grouped poll results based on user field" do SiteSetting.poll_groupable_user_fields = "something" - get "/polls/grouped_poll_results.json", params: { - post_id: post.id, - poll_name: DiscoursePoll::DEFAULT_POLL_NAME, - user_field_name: "something" - } + get "/polls/grouped_poll_results.json", + params: { + post_id: post.id, + poll_name: DiscoursePoll::DEFAULT_POLL_NAME, + user_field_name: "something", + } expect(response.status).to eq(200) expect(response.parsed_body.deep_symbolize_keys).to eq( grouped_results: [ - { group: "Value0", options: [{ digest: option_a, html: "A", votes: 1 }, { digest: option_b, html: "B", votes: 0 }] }, - { group: "Value1", options: [{ digest: option_a, html: "A", votes: 2 }, { digest: option_b, html: "B", votes: 1 }] }, - { group: "Value2", options: [{ digest: option_a, html: "A", votes: 0 }, { digest: option_b, html: "B", votes: 1 }] }, - ] + { + group: "Value0", + options: [ + { digest: option_a, html: "A", votes: 1 }, + { digest: option_b, html: "B", votes: 0 }, + ], + }, + { + group: "Value1", + options: [ + { digest: option_a, html: "A", votes: 2 }, + { digest: option_b, html: "B", votes: 1 }, + ], + }, + { + group: "Value2", + options: [ + { digest: option_a, html: "A", votes: 0 }, + { digest: option_b, html: "B", votes: 1 }, + ], + }, + ], ) end it "returns an error when poll_groupable_user_fields is empty" do SiteSetting.poll_groupable_user_fields = "" - get "/polls/grouped_poll_results.json", params: { - post_id: post.id, - poll_name: DiscoursePoll::DEFAULT_POLL_NAME, - user_field_name: "something" - } + get "/polls/grouped_poll_results.json", + params: { + post_id: post.id, + poll_name: DiscoursePoll::DEFAULT_POLL_NAME, + user_field_name: "something", + } expect(response.status).to eq(400) - expect(response.body).to include('user_field_name') + expect(response.body).to include("user_field_name") end end end diff --git a/plugins/poll/spec/jobs/regular/close_poll_spec.rb b/plugins/poll/spec/jobs/regular/close_poll_spec.rb index 891b35c9c14..08734f9691f 100644 --- a/plugins/poll/spec/jobs/regular/close_poll_spec.rb +++ b/plugins/poll/spec/jobs/regular/close_poll_spec.rb @@ -5,15 +5,17 @@ require "rails_helper" RSpec.describe Jobs::ClosePoll do let(:post) { Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]") } - describe 'missing arguments' do - it 'should raise the right error' do - expect do - Jobs::ClosePoll.new.execute(post_id: post.id) - end.to raise_error(Discourse::InvalidParameters, "poll_name") + describe "missing arguments" do + it "should raise the right error" do + expect do Jobs::ClosePoll.new.execute(post_id: post.id) end.to raise_error( + Discourse::InvalidParameters, + "poll_name", + ) - expect do - Jobs::ClosePoll.new.execute(poll_name: "poll") - end.to raise_error(Discourse::InvalidParameters, "post_id") + expect do Jobs::ClosePoll.new.execute(poll_name: "poll") end.to raise_error( + Discourse::InvalidParameters, + "post_id", + ) end end @@ -24,5 +26,4 @@ RSpec.describe Jobs::ClosePoll do expect(post.polls.first.closed?).to eq(true) end - end diff --git a/plugins/poll/spec/lib/new_post_manager_spec.rb b/plugins/poll/spec/lib/new_post_manager_spec.rb index fb10d9cf2de..5a051697cbc 100644 --- a/plugins/poll/spec/lib/new_post_manager_spec.rb +++ b/plugins/poll/spec/lib/new_post_manager_spec.rb @@ -7,9 +7,7 @@ RSpec.describe NewPostManager do let(:admin) { Fabricate(:admin) } describe "when new post containing a poll is queued for approval" do - before do - SiteSetting.poll_minimum_trust_level_to_create = 0 - end + before { SiteSetting.poll_minimum_trust_level_to_create = 0 } let(:params) do { @@ -23,9 +21,10 @@ RSpec.describe NewPostManager do is_warning: false, title: "This is a test post with a poll", ip_address: "127.0.0.1", - user_agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36", + user_agent: + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36", referrer: "http://localhost:3000/", - first_post_checks: true + first_post_checks: true, } end @@ -38,7 +37,7 @@ RSpec.describe NewPostManager do expect(Poll.where(post: review_result.created_post).exists?).to eq(true) end - it 're-validates the poll when the approve_post event is triggered' do + it "re-validates the poll when the approve_post event is triggered" do invalid_raw_poll = <<~MD [poll type=multiple min=0] * 1 diff --git a/plugins/poll/spec/lib/poll_spec.rb b/plugins/poll/spec/lib/poll_spec.rb index 1b885947f7b..9e42fd55f45 100644 --- a/plugins/poll/spec/lib/poll_spec.rb +++ b/plugins/poll/spec/lib/poll_spec.rb @@ -4,17 +4,14 @@ RSpec.describe DiscoursePoll::Poll do fab!(:user) { Fabricate(:user) } fab!(:user_2) { Fabricate(:user) } - fab!(:post_with_regular_poll) do - Fabricate(:post, raw: <<~RAW) + fab!(:post_with_regular_poll) { Fabricate(:post, raw: <<~RAW) } [poll] * 1 * 2 [/poll] RAW - end - fab!(:post_with_multiple_poll) do - Fabricate(:post, raw: <<~RAW) + fab!(:post_with_multiple_poll) { Fabricate(:post, raw: <<~RAW) } [poll type=multiple min=2 max=3] * 1 * 2 @@ -23,10 +20,9 @@ RSpec.describe DiscoursePoll::Poll do * 5 [/poll] RAW - end - describe '.vote' do - it 'should only allow one vote per user for a regular poll' do + describe ".vote" do + it "should only allow one vote per user for a regular poll" do poll = post_with_regular_poll.polls.first expect do @@ -34,46 +30,35 @@ RSpec.describe DiscoursePoll::Poll do user, post_with_regular_poll.id, "poll", - poll.poll_options.map(&:digest) + poll.poll_options.map(&:digest), ) end.to raise_error(DiscoursePoll::Error, I18n.t("poll.one_vote_per_user")) end - it 'should clean up bad votes for a regular poll' do + it "should clean up bad votes for a regular poll" do poll = post_with_regular_poll.polls.first - PollVote.create!( - poll: poll, - poll_option: poll.poll_options.first, - user: user - ) + PollVote.create!(poll: poll, poll_option: poll.poll_options.first, user: user) - PollVote.create!( - poll: poll, - poll_option: poll.poll_options.last, - user: user - ) + PollVote.create!(poll: poll, poll_option: poll.poll_options.last, user: user) DiscoursePoll::Poll.vote( user, post_with_regular_poll.id, "poll", - [poll.poll_options.first.digest] + [poll.poll_options.first.digest], ) - expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id)) - .to contain_exactly(poll.poll_options.first.id) + expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id)).to contain_exactly( + poll.poll_options.first.id, + ) end - it 'allows user to vote on multiple options correctly for a multiple poll' do + it "allows user to vote on multiple options correctly for a multiple poll" do poll = post_with_multiple_poll.polls.first poll_options = poll.poll_options - [ - poll_options.first, - poll_options.second, - poll_options.third, - ].each do |poll_option| + [poll_options.first, poll_options.second, poll_options.third].each do |poll_option| PollVote.create!(poll: poll, poll_option: poll_option, user: user) end @@ -81,24 +66,28 @@ RSpec.describe DiscoursePoll::Poll do user, post_with_multiple_poll.id, "poll", - [poll_options.first.digest, poll_options.second.digest] + [poll_options.first.digest, poll_options.second.digest], ) DiscoursePoll::Poll.vote( user_2, post_with_multiple_poll.id, "poll", - [poll_options.third.digest, poll_options.fourth.digest] + [poll_options.third.digest, poll_options.fourth.digest], ) - expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id)) - .to contain_exactly(poll_options.first.id, poll_options.second.id) + expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id)).to contain_exactly( + poll_options.first.id, + poll_options.second.id, + ) - expect(PollVote.where(poll: poll, user: user_2).pluck(:poll_option_id)) - .to contain_exactly(poll_options.third.id, poll_options.fourth.id) + expect(PollVote.where(poll: poll, user: user_2).pluck(:poll_option_id)).to contain_exactly( + poll_options.third.id, + poll_options.fourth.id, + ) end - it 'should respect the min/max votes per user for a multiple poll' do + it "should respect the min/max votes per user for a multiple poll" do poll = post_with_multiple_poll.polls.first expect do @@ -106,27 +95,21 @@ RSpec.describe DiscoursePoll::Poll do user, post_with_multiple_poll.id, "poll", - poll.poll_options.map(&:digest) + poll.poll_options.map(&:digest), ) - end.to raise_error( - DiscoursePoll::Error, - I18n.t("poll.max_vote_per_user", count: poll.max) - ) + end.to raise_error(DiscoursePoll::Error, I18n.t("poll.max_vote_per_user", count: poll.max)) expect do DiscoursePoll::Poll.vote( user, post_with_multiple_poll.id, "poll", - [poll.poll_options.first.digest] + [poll.poll_options.first.digest], ) - end.to raise_error( - DiscoursePoll::Error, - I18n.t("poll.min_vote_per_user", count: poll.min) - ) + end.to raise_error(DiscoursePoll::Error, I18n.t("poll.min_vote_per_user", count: poll.min)) end - it 'should allow user to vote on a multiple poll even if min option is not configured' do + it "should allow user to vote on a multiple poll even if min option is not configured" do post_with_multiple_poll = Fabricate(:post, raw: <<~RAW) [poll type=multiple max=3] * 1 @@ -143,14 +126,15 @@ RSpec.describe DiscoursePoll::Poll do user, post_with_multiple_poll.id, "poll", - [poll.poll_options.first.digest] + [poll.poll_options.first.digest], ) - expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id)) - .to contain_exactly(poll.poll_options.first.id) + expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id)).to contain_exactly( + poll.poll_options.first.id, + ) end - it 'should allow user to vote on a multiple poll even if max option is not configured' do + it "should allow user to vote on a multiple poll even if max option is not configured" do post_with_multiple_poll = Fabricate(:post, raw: <<~RAW) [poll type=multiple min=1] * 1 @@ -167,11 +151,13 @@ RSpec.describe DiscoursePoll::Poll do user, post_with_multiple_poll.id, "poll", - [poll.poll_options.first.digest, poll.poll_options.second.digest] + [poll.poll_options.first.digest, poll.poll_options.second.digest], ) - expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id)) - .to contain_exactly(poll.poll_options.first.id, poll.poll_options.second.id) + expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id)).to contain_exactly( + poll.poll_options.first.id, + poll.poll_options.second.id, + ) end end @@ -179,19 +165,14 @@ RSpec.describe DiscoursePoll::Poll do it "publishes on message bus if a there are polls" do first_post = Fabricate(:post) topic = first_post.topic - creator = PostCreator.new(user, - topic_id: topic.id, - raw: <<~RAW + creator = PostCreator.new(user, topic_id: topic.id, raw: <<~RAW) [poll] * 1 * 2 [/poll] RAW - ) - messages = MessageBus.track_publish("/polls/#{topic.id}") do - creator.create! - end + messages = MessageBus.track_publish("/polls/#{topic.id}") { creator.create! } expect(messages.count).to eq(1) end @@ -199,20 +180,16 @@ RSpec.describe DiscoursePoll::Poll do it "does not publish on message bus when a post with no polls is created" do first_post = Fabricate(:post) topic = first_post.topic - creator = PostCreator.new(user, - topic_id: topic.id, - raw: "Just a post with definitely no polls" - ) + creator = + PostCreator.new(user, topic_id: topic.id, raw: "Just a post with definitely no polls") - messages = MessageBus.track_publish("/polls/#{topic.id}") do - creator.create! - end + messages = MessageBus.track_publish("/polls/#{topic.id}") { creator.create! } expect(messages.count).to eq(0) end end - describe '.extract' do + describe ".extract" do it "skips the polls inside quote" do raw = <<~RAW [quote="username, post:1, topic:2"] @@ -230,18 +207,17 @@ RSpec.describe DiscoursePoll::Poll do Post with a poll and a quoted poll. RAW - expect(DiscoursePoll::Poll.extract(raw, 2)).to contain_exactly({ - "name" => "poll", - "options" => [{ - "html" => "3", - "id" => "68b434ff88aeae7054e42cd05a4d9056" - }, { - "html" => "4", - "id" => "aa2393b424f2f395abb63bf785760a3b" - }], - "status" => "open", - "type" => "regular" - }) + expect(DiscoursePoll::Poll.extract(raw, 2)).to contain_exactly( + { + "name" => "poll", + "options" => [ + { "html" => "3", "id" => "68b434ff88aeae7054e42cd05a4d9056" }, + { "html" => "4", "id" => "aa2393b424f2f395abb63bf785760a3b" }, + ], + "status" => "open", + "type" => "regular", + }, + ) end end end diff --git a/plugins/poll/spec/lib/polls_updater_spec.rb b/plugins/poll/spec/lib/polls_updater_spec.rb index b6dde6817a6..f42ec701861 100644 --- a/plugins/poll/spec/lib/polls_updater_spec.rb +++ b/plugins/poll/spec/lib/polls_updater_spec.rb @@ -1,68 +1,54 @@ # frozen_string_literal: true RSpec.describe DiscoursePoll::PollsUpdater do - def update(post, polls) DiscoursePoll::PollsUpdater.update(post, polls) end let(:user) { Fabricate(:user) } - let(:post) { - Fabricate(:post, raw: <<~RAW) + let(:post) { Fabricate(:post, raw: <<~RAW) } [poll] * 1 * 2 [/poll] RAW - } - let(:post_with_3_options) { - Fabricate(:post, raw: <<~RAW) + let(:post_with_3_options) { Fabricate(:post, raw: <<~RAW) } [poll] - a - b - c [/poll] RAW - } - let(:post_with_some_attributes) { - Fabricate(:post, raw: <<~RAW) + let(:post_with_some_attributes) { Fabricate(:post, raw: <<~RAW) } [poll close=#{1.week.from_now.to_formatted_s(:iso8601)} results=on_close] - A - B - C [/poll] RAW - } - let(:polls) { - DiscoursePoll::PollsValidator.new(post).validate_polls - } + let(:polls) { DiscoursePoll::PollsValidator.new(post).validate_polls } - let(:polls_with_3_options) { + let(:polls_with_3_options) do DiscoursePoll::PollsValidator.new(post_with_3_options).validate_polls - } + end - let(:polls_with_some_attributes) { + let(:polls_with_some_attributes) do DiscoursePoll::PollsValidator.new(post_with_some_attributes).validate_polls - } + end describe "update" do - it "does nothing when there are no changes" do - message = MessageBus.track_publish("/polls/#{post.topic_id}") do - update(post, polls) - end.first + message = MessageBus.track_publish("/polls/#{post.topic_id}") { update(post, polls) }.first expect(message).to be(nil) end describe "when editing" do - - let(:raw) do - <<~RAW + let(:raw) { <<~RAW } This is a new poll with three options. [poll type=multiple results=always min=1 max=2] @@ -71,7 +57,6 @@ RSpec.describe DiscoursePoll::PollsUpdater do * third [/poll] RAW - end let(:post) { Fabricate(:post, raw: raw) } @@ -84,11 +69,9 @@ RSpec.describe DiscoursePoll::PollsUpdater do expect(post.errors[:base].size).to equal(0) end - end describe "deletes polls" do - it "that were removed" do update(post, {}) @@ -97,19 +80,15 @@ RSpec.describe DiscoursePoll::PollsUpdater do expect(Poll.where(post: post).exists?).to eq(false) expect(post.custom_fields[DiscoursePoll::HAS_POLLS]).to eq(nil) end - end describe "creates polls" do - it "that were added" do post = Fabricate(:post) expect(Poll.find_by(post: post)).to_not be - message = MessageBus.track_publish("/polls/#{post.topic_id}") do - update(post, polls) - end.first + message = MessageBus.track_publish("/polls/#{post.topic_id}") { update(post, polls) }.first poll = Poll.find_by(post: post) @@ -121,21 +100,19 @@ RSpec.describe DiscoursePoll::PollsUpdater do expect(message.data[:post_id]).to eq(post.id) expect(message.data[:polls][0][:name]).to eq(poll.name) end - end describe "updates polls" do - describe "when there are no votes" do - it "at any time" do post # create the post freeze_time 1.month.from_now - message = MessageBus.track_publish("/polls/#{post.topic_id}") do - update(post, polls_with_some_attributes) - end.first + message = + MessageBus + .track_publish("/polls/#{post.topic_id}") { update(post, polls_with_some_attributes) } + .first poll = Poll.find_by(post: post) @@ -150,11 +127,9 @@ RSpec.describe DiscoursePoll::PollsUpdater do expect(message.data[:post_id]).to eq(post.id) expect(message.data[:polls][0][:name]).to eq(poll.name) end - end describe "when there are votes" do - before do expect { DiscoursePoll::Poll.vote(user, post.id, "poll", [polls["poll"]["options"][0]["id"]]) @@ -162,11 +137,13 @@ RSpec.describe DiscoursePoll::PollsUpdater do end describe "inside the edit window" do - it "and deletes the votes" do - message = MessageBus.track_publish("/polls/#{post.topic_id}") do - update(post, polls_with_some_attributes) - end.first + message = + MessageBus + .track_publish("/polls/#{post.topic_id}") do + update(post, polls_with_some_attributes) + end + .first poll = Poll.find_by(post: post) @@ -181,11 +158,9 @@ RSpec.describe DiscoursePoll::PollsUpdater do expect(message.data[:post_id]).to eq(post.id) expect(message.data[:polls][0][:name]).to eq(poll.name) end - end describe "outside the edit window" do - it "throws an error" do edit_window = SiteSetting.poll_edit_window_mins @@ -204,17 +179,12 @@ RSpec.describe DiscoursePoll::PollsUpdater do expect(post.errors[:base]).to include( I18n.t( "poll.edit_window_expired.cannot_edit_default_poll_with_votes", - minutes: edit_window - ) + minutes: edit_window, + ), ) end - end - end - end - end - end diff --git a/plugins/poll/spec/lib/polls_validator_spec.rb b/plugins/poll/spec/lib/polls_validator_spec.rb index 2ad690c81c5..583a96c80e7 100644 --- a/plugins/poll/spec/lib/polls_validator_spec.rb +++ b/plugins/poll/spec/lib/polls_validator_spec.rb @@ -18,9 +18,15 @@ RSpec.describe ::DiscoursePoll::PollsValidator do post.raw = raw expect(post.valid?).to eq(false) - expect(post.errors[:base]).to include(I18n.t("poll.invalid_argument", argument: "type", value: "not_good1")) - expect(post.errors[:base]).to include(I18n.t("poll.invalid_argument", argument: "status", value: "not_good2")) - expect(post.errors[:base]).to include(I18n.t("poll.invalid_argument", argument: "results", value: "not_good3")) + expect(post.errors[:base]).to include( + I18n.t("poll.invalid_argument", argument: "type", value: "not_good1"), + ) + expect(post.errors[:base]).to include( + I18n.t("poll.invalid_argument", argument: "status", value: "not_good2"), + ) + expect(post.errors[:base]).to include( + I18n.t("poll.invalid_argument", argument: "results", value: "not_good3"), + ) end it "ensures that all possible values are valid" do @@ -70,9 +76,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do post.raw = raw expect(post.valid?).to eq(false) - expect(post.errors[:base]).to include( - I18n.t("poll.multiple_polls_without_name") - ) + expect(post.errors[:base]).to include(I18n.t("poll.multiple_polls_without_name")) raw = <<~RAW [poll name=test] @@ -90,7 +94,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.multiple_polls_with_same_name", name: "test") + I18n.t("poll.multiple_polls_with_same_name", name: "test"), ) end @@ -105,9 +109,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do post.raw = raw expect(post.valid?).to eq(false) - expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_must_have_different_options") - ) + expect(post.errors[:base]).to include(I18n.t("poll.default_poll_must_have_different_options")) raw = <<~RAW [poll name=test] @@ -120,7 +122,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.named_poll_must_have_different_options", name: "test") + I18n.t("poll.named_poll_must_have_different_options", name: "test"), ) end @@ -136,7 +138,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_must_not_have_any_empty_options") + I18n.t("poll.default_poll_must_not_have_any_empty_options"), ) raw = <<~RAW @@ -150,7 +152,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.named_poll_must_not_have_any_empty_options", name: "test") + I18n.t("poll.named_poll_must_not_have_any_empty_options", name: "test"), ) end @@ -163,9 +165,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do post.raw = raw expect(post.valid?).to eq(false) - expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_must_have_at_least_1_option") - ) + expect(post.errors[:base]).to include(I18n.t("poll.default_poll_must_have_at_least_1_option")) raw = <<~RAW [poll name=test] @@ -176,7 +176,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.named_poll_must_have_at_least_1_option", name: "test") + I18n.t("poll.named_poll_must_have_at_least_1_option", name: "test"), ) end @@ -194,10 +194,9 @@ RSpec.describe ::DiscoursePoll::PollsValidator do post.raw = raw expect(post.valid?).to eq(false) - expect(post.errors[:base]).to include(I18n.t( - "poll.default_poll_must_have_less_options", - count: SiteSetting.poll_maximum_options - )) + expect(post.errors[:base]).to include( + I18n.t("poll.default_poll_must_have_less_options", count: SiteSetting.poll_maximum_options), + ) raw = <<~RAW [poll name=test] @@ -210,10 +209,13 @@ RSpec.describe ::DiscoursePoll::PollsValidator do post.raw = raw expect(post.valid?).to eq(false) - expect(post.errors[:base]).to include(I18n.t( - "poll.named_poll_must_have_less_options", - name: "test", count: SiteSetting.poll_maximum_options - )) + expect(post.errors[:base]).to include( + I18n.t( + "poll.named_poll_must_have_less_options", + name: "test", + count: SiteSetting.poll_maximum_options, + ), + ) end describe "multiple type polls" do @@ -230,7 +232,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") + I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"), ) raw = <<~RAW @@ -245,7 +247,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.named_poll_with_multiple_choices_has_invalid_parameters", name: "test") + I18n.t("poll.named_poll_with_multiple_choices_has_invalid_parameters", name: "test"), ) end @@ -261,7 +263,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") + I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"), ) end @@ -277,7 +279,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") + I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"), ) end @@ -293,7 +295,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") + I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"), ) end @@ -321,7 +323,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") + I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"), ) end @@ -337,7 +339,7 @@ RSpec.describe ::DiscoursePoll::PollsValidator do expect(post.valid?).to eq(false) expect(post.errors[:base]).to include( - I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") + I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"), ) end end @@ -350,9 +352,15 @@ RSpec.describe ::DiscoursePoll::PollsValidator do post.raw = raw expect(post.valid?).to eq(false) - expect(post.errors[:base]).to include("Min #{I18n.t("errors.messages.greater_than", count: 0)}") - expect(post.errors[:base]).to include("Max #{I18n.t("errors.messages.greater_than", count: "min")}") - expect(post.errors[:base]).to include("Step #{I18n.t("errors.messages.greater_than", count: 0)}") + expect(post.errors[:base]).to include( + "Min #{I18n.t("errors.messages.greater_than", count: 0)}", + ) + expect(post.errors[:base]).to include( + "Max #{I18n.t("errors.messages.greater_than", count: "min")}", + ) + expect(post.errors[:base]).to include( + "Step #{I18n.t("errors.messages.greater_than", count: 0)}", + ) raw = <<~RAW [poll type=number min=9999999999 max=9999999999 step=1] @@ -361,8 +369,12 @@ RSpec.describe ::DiscoursePoll::PollsValidator do post.raw = raw expect(post.valid?).to eq(false) - expect(post.errors[:base]).to include("Min #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}") - expect(post.errors[:base]).to include("Max #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}") + expect(post.errors[:base]).to include( + "Min #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}", + ) + expect(post.errors[:base]).to include( + "Max #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}", + ) expect(post.errors[:base]).to include(I18n.t("poll.default_poll_must_have_at_least_1_option")) end end diff --git a/plugins/poll/spec/lib/pretty_text_spec.rb b/plugins/poll/spec/lib/pretty_text_spec.rb index 6c4c48c188e..9f6b6763ddd 100644 --- a/plugins/poll/spec/lib/pretty_text_spec.rb +++ b/plugins/poll/spec/lib/pretty_text_spec.rb @@ -1,12 +1,11 @@ # frozen_string_literal: true RSpec.describe PrettyText do - def n(html) html.strip end - it 'supports multi choice polls' do + it "supports multi choice polls" do cooked = PrettyText.cook <<~MD [poll type=multiple min=1 max=3 public=true] * option 1 @@ -24,17 +23,16 @@ RSpec.describe PrettyText do expect(cooked).to include('data-poll-public="true"') end - it 'can dynamically generate a poll' do - + it "can dynamically generate a poll" do cooked = PrettyText.cook <<~MD [poll type=number min=1 max=20 step=1] [/poll] MD - expect(cooked.scan('test @@ -83,7 +81,7 @@ RSpec.describe PrettyText do expect(tight_hashes).to eq(loose_hashes) end - it 'can correctly cook polls' do + it "can correctly cook polls" do md = <<~MD [poll type=multiple] 1. test 1 :) test @@ -115,10 +113,9 @@ RSpec.describe PrettyText do # note, hashes should remain stable even if emoji changes cause text content is hashed expect(n cooked).to eq(n expected) - end - it 'can onebox posts' do + it "can onebox posts" do post = Fabricate(:post, raw: <<~MD) A post with a poll @@ -129,13 +126,13 @@ RSpec.describe PrettyText do MD onebox = Oneboxer.onebox_raw(post.full_url, user_id: Fabricate(:user).id) - doc = Nokogiri::HTML5(onebox[:preview]) + doc = Nokogiri.HTML5(onebox[:preview]) expect(onebox[:preview]).to include("A post with a poll") expect(onebox[:preview]).to include("poll") end - it 'can reduce excerpts' do + it "can reduce excerpts" do post = Fabricate(:post, raw: <<~MD) A post with a poll @@ -187,8 +184,12 @@ RSpec.describe PrettyText do
HTML - expect(cooked).to include("

\nPre-heading

") - expect(cooked).to include("

\nPost-heading

") + expect(cooked).to include( + "

\nPre-heading

", + ) + expect(cooked).to include( + "

\nPost-heading

", + ) end it "does not break when there are headings before/after a poll without a title" do @@ -209,7 +210,11 @@ RSpec.describe PrettyText do
HTML - expect(cooked).to include("

\nPre-heading

") - expect(cooked).to include("

\nPost-heading

") + expect(cooked).to include( + "

\nPre-heading

", + ) + expect(cooked).to include( + "

\nPost-heading

", + ) end end diff --git a/plugins/poll/spec/models/poll_spec.rb b/plugins/poll/spec/models/poll_spec.rb index 9b72a4a9ec9..2afc37cc59c 100644 --- a/plugins/poll/spec/models/poll_spec.rb +++ b/plugins/poll/spec/models/poll_spec.rb @@ -5,13 +5,17 @@ RSpec.describe ::DiscoursePoll::Poll do it "Transforms UserField name if a matching CustomUserField is present" do user_field_name = "Something Cool" user_field = Fabricate(:user_field, name: user_field_name) - expect(::DiscoursePoll::Poll.transform_for_user_field_override(user_field_name)).to eq("user_field_#{user_field.id}") + expect(::DiscoursePoll::Poll.transform_for_user_field_override(user_field_name)).to eq( + "user_field_#{user_field.id}", + ) end it "does not transform UserField name if a matching CustomUserField is not present" do user_field_name = "Something Cool" user_field = Fabricate(:user_field, name: "Something Else!") - expect(::DiscoursePoll::Poll.transform_for_user_field_override(user_field_name)).to eq(user_field_name) + expect(::DiscoursePoll::Poll.transform_for_user_field_override(user_field_name)).to eq( + user_field_name, + ) end end @@ -61,14 +65,14 @@ RSpec.describe ::DiscoursePoll::Poll do option = poll.poll_options.first expect(poll.can_see_results?(user)).to eq(false) - poll.poll_votes.create!(poll_option_id: option.id , user_id: user.id) + poll.poll_votes.create!(poll_option_id: option.id, user_id: user.id) expect(poll.can_see_results?(user)).to eq(false) user.update!(moderator: true) expect(poll.can_see_results?(user)).to eq(true) end end - describe 'when post is trashed' do + describe "when post is trashed" do it "maintains the association" do user = Fabricate(:user) post = Fabricate(:post, raw: "[poll results=staff_only]\n- A\n- B\n[/poll]", user: user) diff --git a/plugins/poll/spec/requests/users_controller_spec.rb b/plugins/poll/spec/requests/users_controller_spec.rb index 78e387de6eb..b1a01267297 100644 --- a/plugins/poll/spec/requests/users_controller_spec.rb +++ b/plugins/poll/spec/requests/users_controller_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Admin::UsersController do before { sign_in(admin) } - describe '#destroy' do + describe "#destroy" do let(:delete_me) { Fabricate(:user) } context "when user has voted" do diff --git a/plugins/poll/spec/serializers/poll_option_serializer_spec.rb b/plugins/poll/spec/serializers/poll_option_serializer_spec.rb index ad46e779619..a6eff33c33b 100644 --- a/plugins/poll/spec/serializers/poll_option_serializer_spec.rb +++ b/plugins/poll/spec/serializers/poll_option_serializer_spec.rb @@ -4,7 +4,9 @@ def serialize_option(option, user) PollOptionSerializer.new( option, root: false, - scope: { can_see_results: poll.can_see_results?(user) } + scope: { + can_see_results: poll.can_see_results?(user), + }, ) end @@ -12,17 +14,15 @@ RSpec.describe PollOptionSerializer do let(:voter) { Fabricate(:user) } let(:poll) { post.polls.first } - before do - poll.poll_votes.create!(poll_option_id: poll.poll_options.first.id, user_id: voter.id) - end + before { poll.poll_votes.create!(poll_option_id: poll.poll_options.first.id, user_id: voter.id) } - context 'when poll results are public' do + context "when poll results are public" do let(:post) { Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]") } - context 'when user is not staff' do + context "when user is not staff" do let(:user) { Fabricate(:user) } - it 'include votes' do + it "include votes" do serializer = serialize_option(poll.poll_options.first, user) expect(serializer.include_votes?).to eq(true) @@ -30,23 +30,23 @@ RSpec.describe PollOptionSerializer do end end - context 'when poll results are staff only' do + context "when poll results are staff only" do let(:post) { Fabricate(:post, raw: "[poll results=staff_only]\n- A\n- B\n[/poll]") } - context 'when user is not staff' do + context "when user is not staff" do let(:user) { Fabricate(:user) } - it 'doesn’t include votes' do + it "doesn’t include votes" do serializer = serialize_option(poll.poll_options.first, user) expect(serializer.include_votes?).to eq(false) end end - context 'when user is staff' do + context "when user is staff" do let(:admin) { Fabricate(:admin) } - it 'includes votes' do + it "includes votes" do serializer = serialize_option(poll.poll_options.first, admin) expect(serializer.include_votes?).to eq(true) diff --git a/plugins/styleguide/app/controllers/styleguide/styleguide_controller.rb b/plugins/styleguide/app/controllers/styleguide/styleguide_controller.rb index 94f853c15f5..ebdab9b8f1c 100644 --- a/plugins/styleguide/app/controllers/styleguide/styleguide_controller.rb +++ b/plugins/styleguide/app/controllers/styleguide/styleguide_controller.rb @@ -8,7 +8,7 @@ module Styleguide def index ensure_admin if SiteSetting.styleguide_admin_only - render 'default/empty' + render "default/empty" end end end diff --git a/plugins/styleguide/config/routes.rb b/plugins/styleguide/config/routes.rb index 57efad00149..46812dae1c0 100644 --- a/plugins/styleguide/config/routes.rb +++ b/plugins/styleguide/config/routes.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true Styleguide::Engine.routes.draw do - get "/" => 'styleguide#index' - get "/:category/:section" => 'styleguide#index' + get "/" => "styleguide#index" + get "/:category/:section" => "styleguide#index" end diff --git a/plugins/styleguide/plugin.rb b/plugins/styleguide/plugin.rb index 4c3244b1f67..0611793c0cc 100644 --- a/plugins/styleguide/plugin.rb +++ b/plugins/styleguide/plugin.rb @@ -8,14 +8,12 @@ register_asset "stylesheets/styleguide.scss" enabled_site_setting :styleguide_enabled -load File.expand_path('../lib/styleguide/engine.rb', __FILE__) +load File.expand_path("../lib/styleguide/engine.rb", __FILE__) -Discourse::Application.routes.append do - mount ::Styleguide::Engine, at: '/styleguide' -end +Discourse::Application.routes.append { mount ::Styleguide::Engine, at: "/styleguide" } after_initialize do register_asset_filter do |type, request, opts| - (opts[:path] || '').start_with?("#{Discourse.base_path}/styleguide") + (opts[:path] || "").start_with?("#{Discourse.base_path}/styleguide") end end diff --git a/plugins/styleguide/spec/integration/access_spec.rb b/plugins/styleguide/spec/integration/access_spec.rb index 568d35eafa3..96e5a36c399 100644 --- a/plugins/styleguide/spec/integration/access_spec.rb +++ b/plugins/styleguide/spec/integration/access_spec.rb @@ -1,62 +1,48 @@ # frozen_string_literal: true -RSpec.describe 'SiteSetting.styleguide_admin_only' do - before do - SiteSetting.styleguide_enabled = true - end +RSpec.describe "SiteSetting.styleguide_admin_only" do + before { SiteSetting.styleguide_enabled = true } - context 'when styleguide is admin only' do - before do - SiteSetting.styleguide_admin_only = true - end + context "when styleguide is admin only" do + before { SiteSetting.styleguide_admin_only = true } - context 'when user is admin' do - before do - sign_in(Fabricate(:admin)) - end + context "when user is admin" do + before { sign_in(Fabricate(:admin)) } - it 'shows the styleguide' do - get '/styleguide' + it "shows the styleguide" do + get "/styleguide" expect(response.status).to eq(200) end end - context 'when user is not admin' do - before do - sign_in(Fabricate(:user)) - end + context "when user is not admin" do + before { sign_in(Fabricate(:user)) } - it 'doesn’t allow access' do - get '/styleguide' + it "doesn’t allow access" do + get "/styleguide" expect(response.status).to eq(403) end end end end -RSpec.describe 'SiteSetting.styleguide_enabled' do - before do - sign_in(Fabricate(:admin)) - end +RSpec.describe "SiteSetting.styleguide_enabled" do + before { sign_in(Fabricate(:admin)) } - context 'when style is enabled' do - before do - SiteSetting.styleguide_enabled = true - end + context "when style is enabled" do + before { SiteSetting.styleguide_enabled = true } - it 'shows the styleguide' do - get '/styleguide' + it "shows the styleguide" do + get "/styleguide" expect(response.status).to eq(200) end end - context 'when styleguide is disabled' do - before do - SiteSetting.styleguide_enabled = false - end + context "when styleguide is disabled" do + before { SiteSetting.styleguide_enabled = false } - it 'returns a page not found' do - get '/styleguide' + it "returns a page not found" do + get "/styleguide" expect(response.status).to eq(404) end end diff --git a/plugins/styleguide/spec/integration/assets_spec.rb b/plugins/styleguide/spec/integration/assets_spec.rb index e4d6f2cfa0f..95fd56df960 100644 --- a/plugins/styleguide/spec/integration/assets_spec.rb +++ b/plugins/styleguide/spec/integration/assets_spec.rb @@ -1,22 +1,22 @@ # frozen_string_literal: true -RSpec.describe 'Styleguide assets' do +RSpec.describe "Styleguide assets" do before do SiteSetting.styleguide_enabled = true sign_in(Fabricate(:admin)) end - context 'when visiting homepage' do - it 'doesn’t load styleguide assets' do - get '/' - expect(response.body).to_not include('styleguide') + context "when visiting homepage" do + it "doesn’t load styleguide assets" do + get "/" + expect(response.body).to_not include("styleguide") end end - context 'when visiting styleguide' do - it 'loads styleguide assets' do - get '/styleguide' - expect(response.body).to include('styleguide') + context "when visiting styleguide" do + it "loads styleguide assets" do + get "/styleguide" + expect(response.body).to include("styleguide") end end end