DEV: Use run_successfully matcher in service specs

This commit is contained in:
Loïc Guitaut
2024-08-28 14:54:10 +02:00
committed by Loïc Guitaut
parent 0977f9fb47
commit d26d45540e
40 changed files with 189 additions and 181 deletions

View File

@ -28,6 +28,8 @@ RSpec.describe Chat::AddUsersToChannel do
context "when all steps pass" do
before { channel.add(current_user) }
it { is_expected.to run_successfully }
it "fetches users to add" do
expect(result.target_users.map(&:username)).to contain_exactly(*users.map(&:username))
end

View File

@ -86,6 +86,8 @@ describe Chat::AutoJoinChannelBatch do
context "when more than one membership is created" do
let(:user_ids) { Fabricate.times(2, :user).map(&:id) }
it { is_expected.to run_successfully }
it "does not recalculate user count" do
::Chat::ChannelMembershipManager.any_instance.expects(:recalculate_user_count).never
result
@ -101,6 +103,8 @@ describe Chat::AutoJoinChannelBatch do
end
context "when only one membership is created" do
it { is_expected.to run_successfully }
it "recalculates user count" do
::Chat::ChannelMembershipManager.any_instance.expects(:recalculate_user_count).once
result

View File

@ -60,6 +60,8 @@ RSpec.describe Chat::CreateCategoryChannel do
end
context "when all steps pass" do
it { is_expected.to run_successfully }
it "creates the channel" do
expect { result }.to change { Chat::Channel.count }.by(1)
expect(result.channel).to have_attributes(

View File

@ -17,6 +17,7 @@ RSpec.describe Chat::CreateDirectMessageChannel do
expect(contract.target_usernames).to eq(%w[lechuck elaine])
end
end
context "when the target_groups argument is a string" do
let(:params) { { target_groups: "admins,moderators" } }
@ -42,6 +43,8 @@ RSpec.describe Chat::CreateDirectMessageChannel do
let(:params) { { guardian: guardian, target_usernames: target_usernames, name: name } }
context "when all steps pass" do
it { is_expected.to run_successfully }
it "sets the service result as successful" do
expect(result).to be_a_success
end

View File

@ -237,7 +237,7 @@ RSpec.describe Chat::CreateMessage do
context "when user is a bot" do
fab!(:user) { Discourse.system_user }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when membership is enforced" do
@ -248,7 +248,7 @@ RSpec.describe Chat::CreateMessage do
params[:enforce_membership] = true
end
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when user can join channel" do
@ -291,6 +291,8 @@ RSpec.describe Chat::CreateMessage do
it_behaves_like "creating a new message"
it_behaves_like "a message in a thread"
it { is_expected.to run_successfully }
it "assigns the thread to the new message" do
expect(message).to have_attributes(
in_reply_to: an_object_having_attributes(thread: thread),
@ -312,6 +314,8 @@ RSpec.describe Chat::CreateMessage do
let(:original_user) { reply_to.user }
end
it { is_expected.to run_successfully }
it "creates a new thread" do
expect { result }.to change { Chat::Thread.count }.by(1)
expect(message).to have_attributes(
@ -389,6 +393,8 @@ RSpec.describe Chat::CreateMessage do
it_behaves_like "creating a new message"
it_behaves_like "a message in a thread"
it { is_expected.to run_successfully }
it "does not publish the thread" do
Chat::Publisher.expects(:publish_thread_created!).never
result
@ -400,6 +406,8 @@ RSpec.describe Chat::CreateMessage do
it_behaves_like "creating a new message"
it_behaves_like "a message in a thread"
it { is_expected.to run_successfully }
it "does not publish the thread" do
Chat::Publisher.expects(:publish_thread_created!).never
result
@ -418,6 +426,8 @@ RSpec.describe Chat::CreateMessage do
context "when message is valid" do
it_behaves_like "creating a new message"
it { is_expected.to run_successfully }
it "updates membership last_read_message attribute" do
expect { result }.to change { membership.reload.last_read_message }
end

View File

@ -25,9 +25,7 @@ RSpec.describe Chat::CreateThread do
end
context "when all steps pass" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "creates a thread" do
result

View File

@ -44,6 +44,8 @@ RSpec.describe Chat::FlagMessage do
context "when all steps pass" do
fab!(:current_user) { Fabricate(:admin) }
it { is_expected.to run_successfully }
it "flags the message" do
expect { result }.to change { Reviewable.count }.by(1)

View File

@ -29,9 +29,7 @@ RSpec.describe Chat::InviteUsersToChannel do
end
context "when all steps pass" do
it "sets the service result as successful" do
expect(result).to run_service_successfully
end
it { is_expected.to run_successfully }
it "creates the notifications for allowed users" do
result

View File

@ -28,6 +28,8 @@ RSpec.describe Chat::LeaveChannel do
Chat::Channel.ensure_consistency!
end
it { is_expected.to run_successfully }
it "unfollows the channel" do
membership = channel_1.membership_for(current_user)
@ -40,6 +42,8 @@ RSpec.describe Chat::LeaveChannel do
end
context "with no existing membership" do
it { is_expected.to run_successfully }
it "does nothing" do
expect { result }.to_not change { Chat::UserChatChannelMembership }
end
@ -54,6 +58,8 @@ RSpec.describe Chat::LeaveChannel do
before { Chat::Channel.ensure_consistency! }
it { is_expected.to run_successfully }
it "leaves the channel" do
membership = channel_1.membership_for(current_user)
@ -71,6 +77,8 @@ RSpec.describe Chat::LeaveChannel do
end
context "with no existing membership" do
it { is_expected.to run_successfully }
it "does nothing" do
expect { result }.to_not change { Chat::UserChatChannelMembership }
end
@ -85,6 +93,8 @@ RSpec.describe Chat::LeaveChannel do
before { Chat::Channel.ensure_consistency! }
it { is_expected.to run_successfully }
it "unfollows the channel" do
membership = channel_1.membership_for(current_user)
@ -100,6 +110,8 @@ RSpec.describe Chat::LeaveChannel do
end
context "with no existing membership" do
it { is_expected.to run_successfully }
it "does nothing" do
expect { result }.to_not change { Chat::UserChatChannelMembership }
end

View File

@ -29,6 +29,8 @@ RSpec.describe Chat::ListChannelMessages do
end
context "when channel exists" do
it { is_expected.to run_successfully }
it "finds the correct channel" do
expect(result.channel).to eq(channel)
end
@ -37,6 +39,8 @@ RSpec.describe Chat::ListChannelMessages do
context "when fetch_eventual_membership" do
context "when user has membership" do
it { is_expected.to run_successfully }
it "finds the correct membership" do
expect(result.membership).to eq(channel.membership_for(user))
end
@ -45,6 +49,8 @@ RSpec.describe Chat::ListChannelMessages do
context "when user has no membership" do
before { channel.membership_for(user).destroy! }
it { is_expected.to run_successfully }
it "finds no membership" do
expect(result.membership).to be_blank
end
@ -86,7 +92,7 @@ RSpec.describe Chat::ListChannelMessages do
context "when target_message_exists" do
context "when no target_message_id is given" do
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when target message is not found" do
@ -99,7 +105,7 @@ RSpec.describe Chat::ListChannelMessages do
fab!(:target_message) { Fabricate(:chat_message, chat_channel: channel) }
let(:optional_params) { { target_message_id: target_message.id } }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when target message is trashed" do
@ -117,13 +123,13 @@ RSpec.describe Chat::ListChannelMessages do
context "when user is the message creator" do
fab!(:target_message) { Fabricate(:chat_message, chat_channel: channel, user: user) }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when user is admin" do
fab!(:user) { Fabricate(:admin) }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
end
end
@ -132,6 +138,8 @@ RSpec.describe Chat::ListChannelMessages do
context "with no params" do
fab!(:messages) { Fabricate.times(20, :chat_message, chat_channel: channel) }
it { is_expected.to be_a_success }
it "returns messages" do
expect(result.can_load_more_past).to eq(false)
expect(result.can_load_more_future).to eq(false)
@ -149,6 +157,8 @@ RSpec.describe Chat::ListChannelMessages do
let(:optional_params) { { target_date: 2.days.ago } }
it { is_expected.to be_a_success }
it "includes past and future messages" do
expect(result.messages).to eq([past_message, future_message])
end
@ -164,6 +174,8 @@ RSpec.describe Chat::ListChannelMessages do
thread_1.add(user)
end
it { is_expected.to be_a_success }
it "returns tracking" do
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)
@ -175,6 +187,8 @@ RSpec.describe Chat::ListChannelMessages do
context "when thread is forced" do
before { thread_1.update!(force: true) }
it { is_expected.to be_a_success }
it "returns tracking" do
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)
@ -193,6 +207,8 @@ RSpec.describe Chat::ListChannelMessages do
thread_1.add(user)
end
it { is_expected.to be_a_success }
it "returns tracking" do
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)

View File

@ -29,6 +29,8 @@ RSpec.describe Chat::ListChannelThreadMessages do
end
context "when thread exists" do
it { is_expected.to run_successfully }
it "finds the correct channel" do
expect(result.thread).to eq(thread)
end
@ -44,7 +46,7 @@ RSpec.describe Chat::ListChannelThreadMessages do
context "with system user" do
fab!(:user) { Discourse.system_user }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
end
end
@ -80,7 +82,7 @@ RSpec.describe Chat::ListChannelThreadMessages do
context "when target_message_exists" do
context "when no target_message_id is given" do
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when target message is not found" do
@ -95,7 +97,7 @@ RSpec.describe Chat::ListChannelThreadMessages do
end
let(:optional_params) { { target_message_id: target_message.id } }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when target message is trashed" do
@ -115,13 +117,13 @@ RSpec.describe Chat::ListChannelThreadMessages do
Fabricate(:chat_message, chat_channel: thread.channel, thread: thread, user: user)
end
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when user is admin" do
fab!(:user) { Fabricate(:admin) }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
end
end

View File

@ -11,6 +11,8 @@ RSpec.describe Chat::ListUserChannels do
before { channel_1.add(current_user) }
it { is_expected.to run_successfully }
it "returns the structured data" do
expect(result.structured[:post_allowed_category_ids]).to eq(nil)
expect(result.structured[:unread_thread_overview]).to eq({})

View File

@ -125,7 +125,7 @@ RSpec.describe ::Chat::LookupChannelThreads do
describe "model - threads" do
before { channel_1.add(current_user) }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
it "orders threads by the last reply created_at timestamp" do
[

View File

@ -19,9 +19,7 @@ RSpec.describe Chat::LookupThread do
let(:params) { { guardian: guardian, thread_id: thread.id, channel_id: thread.channel_id } }
context "when all steps pass" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "fetches the thread" do
expect(result.thread).to eq(thread)
@ -60,7 +58,7 @@ RSpec.describe Chat::LookupThread do
context "when thread is forced" do
before { thread.update!(force: true) }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
end
end

View File

@ -5,6 +5,7 @@ RSpec.describe ::Chat::LookupUserThreads do
fab!(:current_user) { Fabricate(:user) }
fab!(:channel_1) { Fabricate(:chat_channel, threading_enabled: true) }
fab!(:thread_1) { Fabricate(:chat_thread, channel: channel_1, with_replies: 1) }
let(:guardian) { Guardian.new(current_user) }
let(:channel_id) { channel_1.id }
@ -12,15 +13,15 @@ RSpec.describe ::Chat::LookupUserThreads do
let(:offset) { 0 }
let(:params) { { guardian: guardian, limit: limit, offset: offset } }
before { channel_1.add(current_user) }
before do
channel_1.add(current_user)
thread_1.add(current_user)
end
context "when all steps pass" do
it "returns threads" do
thread_1 =
Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|
thread.add(current_user)
end
it { is_expected.to run_successfully }
it "returns threads" do
expect(result.threads).to eq([thread_1])
end
@ -65,6 +66,7 @@ RSpec.describe ::Chat::LookupUserThreads do
it "can offset" do
params[:offset] = 1
Chat::Thread.destroy_all
threads =
Fabricate
.times(2, :chat_thread, channel: channel_1, with_replies: 1)
@ -77,21 +79,16 @@ RSpec.describe ::Chat::LookupUserThreads do
it "has a min offset" do
params[:offset] = -99
threads =
Fabricate
.times(2, :chat_thread, channel: channel_1, with_replies: 1)
.each { |thread| thread.add(current_user) }
Chat::Thread.destroy_all
Fabricate
.times(2, :chat_thread, channel: channel_1, with_replies: 1)
.each { |thread| thread.add(current_user) }
# 0 because we sort by last_message.created_at, so the last created thread is the first one
expect(result.threads.length).to eq(2)
end
it "fetches tracking" do
thread_1 =
Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|
thread.add(current_user)
end
expect(result.tracking).to eq(
::Chat::TrackingStateReportQuery.call(
guardian: current_user.guardian,
@ -102,58 +99,39 @@ RSpec.describe ::Chat::LookupUserThreads do
end
it "fetches memberships" do
thread_1 =
Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|
thread.add(current_user)
end
expect(result.memberships).to eq([thread_1.membership_for(current_user)])
end
it "fetches participants" do
thread_1 =
Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|
thread.add(current_user)
end
expect(result.participants).to eq(
::Chat::ThreadParticipantQuery.call(thread_ids: [thread_1.id]),
)
end
it "builds a load_more_url" do
Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|
thread.add(current_user)
end
expect(result.load_more_url).to eq("/chat/api/me/threads?limit=10&offset=10")
end
end
it "doesn't return threads with no replies" do
thread_1 = Fabricate(:chat_thread, channel: channel_1)
thread_1.add(current_user)
Fabricate(:chat_thread, channel: channel_1).tap { _1.add(current_user) }
expect(result.threads).to eq([])
expect(result.threads).to eq([thread_1])
end
it "doesn't return threads with no membership" do
thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)
Fabricate(:chat_thread, channel: channel_1, with_replies: 1)
expect(result.threads).to eq([])
expect(result.threads).to eq([thread_1])
end
it "doesn't return threads when the channel has not threading enabled" do
channel_1.update!(threading_enabled: false)
thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)
thread_1.add(current_user)
expect(result.threads).to eq([])
end
it "doesn't return muted threads" do
thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)
thread_1.add(current_user)
thread_1.membership_for(current_user).update!(
notification_level: ::Chat::UserChatThreadMembership.notification_levels[:muted],
)
@ -163,16 +141,11 @@ RSpec.describe ::Chat::LookupUserThreads do
it "doesn't return threads when the channel it not open" do
channel_1.update!(status: Chat::Channel.statuses[:closed])
thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)
thread_1.add(current_user)
expect(result.threads).to eq([])
end
it "returns threads from muted channels" do
thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)
thread_1.add(current_user)
channel_1.membership_for(current_user).update!(muted: true)
expect(result.threads).to eq([thread_1])

View File

@ -56,12 +56,10 @@ RSpec.describe Chat::MarkAllUserChannelsRead do
context "when the user has no memberships" do
let(:guardian) { Guardian.new(Fabricate(:user)) }
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "returns the updated_memberships in context" do
expect(result.updated_memberships).to eq([])
expect(result.updated_memberships).to be_empty
end
end
@ -96,9 +94,7 @@ RSpec.describe Chat::MarkAllUserChannelsRead do
)
end
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "updates the last_read_message_ids" do
result

View File

@ -23,9 +23,7 @@ RSpec.describe Chat::MarkThreadTitlePromptSeen do
before { thread.update!(last_message: last_reply) }
context "when all steps pass" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
context "when the user is a member of the thread" do
fab!(:membership) { thread.add(current_user) }

View File

@ -41,9 +41,7 @@ RSpec.describe Chat::RestoreMessage do
end
context "when the user has permission to restore" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "restores the message" do
result

View File

@ -43,9 +43,7 @@ RSpec.describe Chat::SearchChatable do
end
context "when all steps pass" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "cleans the term" do
params[:term] = "#bob"

View File

@ -19,7 +19,7 @@ RSpec.describe Chat::StopMessageStreaming do
context "with valid params" do
fab!(:current_user) { Fabricate(:admin) }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
it "updates the streaming attribute to false" do
expect { result }.to change { message_1.reload.streaming }.to eq(false)
@ -42,7 +42,7 @@ RSpec.describe Chat::StopMessageStreaming do
context "when the user is a bot" do
fab!(:current_user) { Discourse.system_user }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
end
@ -69,7 +69,7 @@ RSpec.describe Chat::StopMessageStreaming do
before { params[:message_id] = reply.id }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when the OM is not from current user" do
@ -89,13 +89,13 @@ RSpec.describe Chat::StopMessageStreaming do
context "when current user is a bot" do
fab!(:current_user) { Discourse.system_user }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when current user is an admin" do
fab!(:current_user) { Fabricate(:admin) }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
end
end
@ -112,13 +112,13 @@ RSpec.describe Chat::StopMessageStreaming do
context "when current user is a bot" do
fab!(:current_user) { Discourse.system_user }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
context "when current user is an admin" do
fab!(:current_user) { Fabricate(:admin) }
it { is_expected.to be_a_success }
it { is_expected.to run_successfully }
end
end
end

View File

@ -25,9 +25,7 @@ RSpec.describe(Chat::TrashChannel) do
context "when user is allowed to perform the action" do
fab!(:current_user) { Fabricate(:admin) }
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "trashes the channel" do
expect(result[:channel]).to be_trashed

View File

@ -34,9 +34,7 @@ RSpec.describe Chat::TrashMessage do
end
context "when the user has permission to delete" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "trashes the message" do
result

View File

@ -40,9 +40,7 @@ RSpec.describe Chat::TrashMessages do
end
context "when the user has permission to delete" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "trashes the messages" do
result

View File

@ -24,6 +24,8 @@ RSpec.describe Chat::UnfollowChannel do
context "with existing membership" do
before { channel_1.add(current_user) }
it { is_expected.to run_successfully }
it "unfollows the channel" do
membership = channel_1.membership_for(current_user)
@ -32,6 +34,8 @@ RSpec.describe Chat::UnfollowChannel do
end
context "with no existing membership" do
it { is_expected.to run_successfully }
it "does nothing" do
expect { result }.to_not change { Chat::UserChatChannelMembership }
end

View File

@ -31,9 +31,7 @@ RSpec.describe Chat::UpdateChannel do
.first
end
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "updates the channel accordingly" do
result

View File

@ -42,9 +42,7 @@ RSpec.describe(Chat::UpdateChannelStatus) do
context "when status is allowed" do
let(:status) { "closed" }
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "changes the status" do
result

View File

@ -879,9 +879,7 @@ RSpec.describe Chat::UpdateMessage do
end
context "when all steps pass" do
it "sets the service result as successful" do
expect(result).to run_service_successfully
end
it { is_expected.to run_successfully }
it "updates the message" do
expect(result.message.message).to eq("new")

View File

@ -29,9 +29,7 @@ RSpec.describe Chat::UpdateThreadNotificationSettings do
before { thread.update!(last_message: last_reply) }
context "when all steps pass" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
context "when the user is a member of the thread" do
fab!(:membership) { thread.add(current_user) }

View File

@ -19,9 +19,7 @@ RSpec.describe Chat::UpdateThread do
let(:params) { { guardian: guardian, thread_id: thread.id, title: title } }
context "when all steps pass" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "updates the title of the thread" do
result

View File

@ -91,9 +91,7 @@ RSpec.describe Chat::UpdateUserChannelLastRead do
)
end
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "updates the last_read message id" do
expect { result }.to change { membership.reload.last_read_message_id }.to(message_1.id)

View File

@ -55,9 +55,7 @@ RSpec.describe Chat::UpdateUserThreadLastRead do
end
context "when params are valid" do
it "sets the service result as successful" do
expect(result).to be_a_success
end
it { is_expected.to run_successfully }
it "publishes new last read to clients" do
messages = MessageBus.track_publish { result }

View File

@ -29,6 +29,8 @@ RSpec.describe Chat::UpsertDraft do
end
context "when all steps pass" do
it { is_expected.to run_successfully }
it "creates draft if data provided and not existing draft" do
params[:data] = MultiJson.dump(message: "a")