mirror of
https://github.com/discourse/discourse.git
synced 2024-12-05 09:56:30 +08:00
3f1024de76
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>
72 lines
2.3 KiB
Ruby
72 lines
2.3 KiB
Ruby
# 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 doesn’t 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
|