mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-21 00:18:33 +08:00
97 lines
3.3 KiB
Ruby
Vendored
97 lines
3.3 KiB
Ruby
Vendored
# frozen_string_literal: true
|
||
|
||
RSpec.describe Chat::DirectMessagesController do
|
||
fab!(:user)
|
||
fab!(:user1, :user)
|
||
fab!(:user2, :user)
|
||
fab!(:user3, :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 = "" }
|
||
|
||
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 a category channel exists with the same chatable_id as the direct message" do
|
||
it "returns the direct message channel, not the category channel" do
|
||
category = Fabricate(:category, name: "Secret Category")
|
||
cat_channel =
|
||
Chat::CategoryChannel.create!(
|
||
chatable: category,
|
||
chatable_type: "Category",
|
||
name: "Secret Category",
|
||
)
|
||
|
||
dm = Chat::DirectMessage.create!
|
||
dm.direct_message_users.create!(user_id: user.id)
|
||
dm.direct_message_users.create!(user_id: user1.id)
|
||
dm_channel = Chat::DirectMessageChannel.create!(chatable: dm)
|
||
|
||
DB.exec(
|
||
"UPDATE chat_channels SET chatable_id = :target_id WHERE id = :channel_id",
|
||
target_id: dm.id,
|
||
channel_id: cat_channel.id,
|
||
)
|
||
|
||
get "/chat/direct_messages.json", params: { usernames: user1.username }
|
||
expect(response.status).to eq(200)
|
||
expect(response.parsed_body["channel"]["id"]).to eq(dm_channel.id)
|
||
expect(response.parsed_body["channel"]["chatable_type"]).to eq("DirectMessage")
|
||
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, :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
|