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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

166 lines
5.5 KiB
Ruby
Raw Normal View History

# 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)
DEV: start glimmer-ification and optimisations of chat plugin (#19531) Note this is a very large PR, and some of it could have been splited, but keeping it one chunk made it to merge conflicts and to revert if necessary. Actual new code logic is also not that much, as most of the changes are removing js tests, adding system specs or moving things around. To make it possible this commit is doing the following changes: - converting (and adding new) existing js acceptances tests into system tests. This change was necessary to ensure as little regressions as possible while changing paradigm - moving away from store. Using glimmer and tracked properties requires to have class objects everywhere and as a result works well with models. However store/adapters are suffering from many bugs and limitations. As a workaround the `chat-api` and `chat-channels-manager` are an answer to this problem by encapsulating backend calls and frontend storage logic; while still using js models. - dropping `appEvents` as much as possible. Using tracked properties and a better local storage of channel models, allows to be much more reactive and doesn’t require arbitrary manual updates everywhere in the app. - while working on replacing store, the existing work of a chat api (backend) has been continued to support more cases. - removing code from the `chat` service to separate concerns, `chat-subscriptions-manager` and `chat-channels-manager`, being the largest examples of where the code has been rewritten/moved. Future wok: - improve behavior when closing/deleting a channel, it's already slightly buggy on live, it's rare enough that it's not a big issue, but should be improved - improve page objects used in chat - move more endpoints to the API - finish temporarily skipped tests - extract more code from the `chat` service - use glimmer for `chat-messages` - separate concerns in `chat-live-pane` - eventually add js tests for `chat-api`, `chat-channels-manager` and `chat-subscriptions-manager`, they are indirectly heavy tested through system tests but it would be nice to at least test the public API <!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2022-12-21 20:21:02 +08:00
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)
DEV: start glimmer-ification and optimisations of chat plugin (#19531) Note this is a very large PR, and some of it could have been splited, but keeping it one chunk made it to merge conflicts and to revert if necessary. Actual new code logic is also not that much, as most of the changes are removing js tests, adding system specs or moving things around. To make it possible this commit is doing the following changes: - converting (and adding new) existing js acceptances tests into system tests. This change was necessary to ensure as little regressions as possible while changing paradigm - moving away from store. Using glimmer and tracked properties requires to have class objects everywhere and as a result works well with models. However store/adapters are suffering from many bugs and limitations. As a workaround the `chat-api` and `chat-channels-manager` are an answer to this problem by encapsulating backend calls and frontend storage logic; while still using js models. - dropping `appEvents` as much as possible. Using tracked properties and a better local storage of channel models, allows to be much more reactive and doesn’t require arbitrary manual updates everywhere in the app. - while working on replacing store, the existing work of a chat api (backend) has been continued to support more cases. - removing code from the `chat` service to separate concerns, `chat-subscriptions-manager` and `chat-channels-manager`, being the largest examples of where the code has been rewritten/moved. Future wok: - improve behavior when closing/deleting a channel, it's already slightly buggy on live, it's rare enough that it's not a big issue, but should be improved - improve page objects used in chat - move more endpoints to the API - finish temporarily skipped tests - extract more code from the `chat` service - use glimmer for `chat-messages` - separate concerns in `chat-live-pane` - eventually add js tests for `chat-api`, `chat-channels-manager` and `chat-subscriptions-manager`, they are indirectly heavy tested through system tests but it would be nice to at least test the public API <!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2022-12-21 20:21:02 +08:00
expect(response.parsed_body["channel"]["id"]).to eq(channel.id)
end
end
end
end
describe "#create" do
before { Group.refresh_automatic_groups! }
shared_examples "creating dms" do
it "creates a new dm channel with username(s) provided" do
expect {
post "/chat/direct_messages/create.json", params: { usernames: [usernames] }
}.to change { Chat::DirectMessage.count }.by(1)
expect(Chat::DirectMessage.last.direct_message_users.map(&:user_id)).to match_array(
direct_message_user_ids,
)
end
it "returns existing dm channel if one exists for username(s)" do
create_dm_channel(direct_message_user_ids)
expect {
post "/chat/direct_messages/create.json", params: { usernames: [usernames] }
}.not_to change { Chat::DirectMessage.count }
end
end
describe "dm with one other user" do
let(:usernames) { user1.username }
let(:direct_message_user_ids) { [user.id, user1.id] }
include_examples "creating dms"
end
describe "dm with myself" do
let(:usernames) { [user.username] }
let(:direct_message_user_ids) { [user.id] }
include_examples "creating dms"
end
describe "dm with two other users" do
let(:usernames) { [user1, user2, user3].map(&:username) }
let(:direct_message_user_ids) { [user.id, user1.id, user2.id, user3.id] }
include_examples "creating dms"
end
it "creates Chat::UserChatChannelMembership records" do
users = [user2, user3]
usernames = users.map(&:username)
expect {
post "/chat/direct_messages/create.json", params: { usernames: usernames }
}.to change { Chat::UserChatChannelMembership.count }.by(3)
end
context "when one of the users I am messaging has ignored, muted, or prevented DMs from the acting user creating the channel" do
let(:usernames) { [user1, user2, user3].map(&:username) }
let(:direct_message_user_ids) { [user.id, user1.id, user2.id, user3.id] }
shared_examples "creating dms with communication error" do
it "responds with a friendly error" do
expect {
post "/chat/direct_messages/create.json", params: { usernames: [usernames] }
}.not_to change { Chat::DirectMessage.count }
expect(response.status).to eq(422)
expect(response.parsed_body["errors"]).to eq(
[I18n.t("chat.errors.not_accepting_dms", username: user1.username)],
)
end
end
describe "user ignoring the actor" do
before do
Fabricate(:ignored_user, user: user1, ignored_user: user, expiring_at: 1.day.from_now)
end
include_examples "creating dms with communication error"
end
describe "user muting the actor" do
before { Fabricate(:muted_user, user: user1, muted_user: user) }
include_examples "creating dms with communication error"
end
describe "user preventing all DMs" do
before { user1.user_option.update(allow_private_messages: false) }
include_examples "creating dms with communication error"
end
describe "user only allowing DMs from certain users" do
before { user1.user_option.update(enable_allowed_pm_users: true) }
include_examples "creating dms with communication error"
end
end
end
end