discourse/plugins/chat/spec/requests/direct_messages_controller_spec.rb
Daniel Waterworth 6e161d3e75
DEV: Allow fab! without block (#24314)
The most common thing that we do with fab! is:

    fab!(:thing) { Fabricate(:thing) }

This commit adds a shorthand for this which is just simply:

    fab!(:thing)

i.e. If you omit the block, then, by default, you'll get a `Fabricate`d object using the fabricator of the same name.
2023-11-09 16:47:59 -06: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)
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