discourse/plugins/chat/spec/requests/direct_messages_controller_spec.rb

70 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
RSpec.describe Chat::DirectMessagesController do
fab!(: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