discourse/plugins/chat/spec/requests/direct_messages_controller_spec.rb
Martin Brennan 3f1024de76
DEV: Refactor DM channel creation into new service pattern (#22144)
This will be used when we move the channel creation for DMs
to happen when we first send a message in a DM channel to avoid
a double-request. For now we can just have a new API endpoint
for creating this that the existing frontend code can use,
that uses the new service pattern.

This also uses the new policy pattern for services where the policy
can be defined in a class so a more dynamic reason for the policy
failing can be sent to the controller.

Co-authored-by: Loïc Guitaut <loic@discourse.org>
2023-07-03 10:18:37 +10:00

72 lines
2.3 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
require "rails_helper"
RSpec.describe Chat::DirectMessagesController do
fab!(:user) { Fabricate(:user) }
fab!(:user1) { Fabricate(:user) }
fab!(:user2) { Fabricate(:user) }
fab!(:user3) { Fabricate(:user) }
before do
SiteSetting.chat_enabled = true
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
sign_in(user)
end
def create_dm_channel(user_ids)
direct_messages_channel = Chat::DirectMessage.create!
user_ids.each do |user_id|
direct_messages_channel.direct_message_users.create!(user_id: user_id)
end
Chat::DirectMessageChannel.create!(chatable: direct_messages_channel)
end
describe "#index" do
context "when user is not allowed to chat" do
before { SiteSetting.chat_allowed_groups = nil }
it "returns a forbidden error" do
get "/chat/direct_messages.json", params: { usernames: user1.username }
expect(response.status).to eq(403)
end
end
context "when channel doesnt exists" do
it "returns a not found error" do
get "/chat/direct_messages.json", params: { usernames: user1.username }
expect(response.status).to eq(404)
end
end
context "when channel exists" do
let!(:channel) do
direct_messages_channel = Chat::DirectMessage.create!
direct_messages_channel.direct_message_users.create!(user_id: user.id)
direct_messages_channel.direct_message_users.create!(user_id: user1.id)
Chat::DirectMessageChannel.create!(chatable: direct_messages_channel)
end
it "returns the channel" do
get "/chat/direct_messages.json", params: { usernames: user1.username }
expect(response.status).to eq(200)
expect(response.parsed_body["channel"]["id"]).to eq(channel.id)
end
context "with more than two users" do
fab!(:user3) { Fabricate(:user) }
before { channel.chatable.direct_message_users.create!(user_id: user3.id) }
it "returns the channel" do
get "/chat/direct_messages.json",
params: {
usernames: [user1.username, user.username, user3.username].join(","),
}
expect(response.status).to eq(200)
expect(response.parsed_body["channel"]["id"]).to eq(channel.id)
end
end
end
end
end