discourse/plugins/chat/spec/requests/api/chat_chatables_controller_s...

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

202 lines
8.7 KiB
Ruby
Raw Normal View History

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 07:21:02 -05:00
# frozen_string_literal: true
require "rails_helper"
RSpec.describe Chat::Api::ChatChatablesController do
before do
SiteSetting.chat_enabled = true
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
end
describe "#index" do
fab!(:user) { Fabricate(:user, username: "johndoe", name: "John Doe") }
describe "without chat permissions" do
it "errors errors for anon" do
get "/chat/api/chatables", params: { filter: "so" }
expect(response.status).to eq(403)
end
it "errors when user cannot chat" do
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:staff]
sign_in(user)
get "/chat/api/chatables", params: { filter: "so" }
expect(response.status).to eq(403)
end
end
describe "with chat permissions" do
fab!(:other_user) { Fabricate(:user, username: "janemay", name: "Jane May") }
fab!(:admin) { Fabricate(:admin, username: "andyjones", name: "Andy Jones") }
fab!(:category) { Fabricate(:category) }
fab!(:chat_channel) { Fabricate(:category_channel, chatable: category) }
fab!(:dm_chat_channel) { Fabricate(:direct_message_channel, users: [user, admin]) }
before do
chat_channel.update(name: "something")
sign_in(user)
end
it "returns the correct channels with filter 'so'" do
get "/chat/api/chatables", params: { filter: "so" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"][0]["id"]).to eq(chat_channel.id)
expect(response.parsed_body["direct_message_channels"].count).to eq(0)
expect(response.parsed_body["users"].count).to eq(0)
end
it "returns the correct channels with filter 'something'" do
get "/chat/api/chatables", params: { filter: "something" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"][0]["id"]).to eq(chat_channel.id)
expect(response.parsed_body["direct_message_channels"].count).to eq(0)
expect(response.parsed_body["users"].count).to eq(0)
end
it "returns the correct channels with filter 'andyjones'" do
get "/chat/api/chatables", params: { filter: "andyjones" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"].count).to eq(0)
expect(response.parsed_body["direct_message_channels"][0]["id"]).to eq(dm_chat_channel.id)
expect(response.parsed_body["users"].count).to eq(0)
end
it "returns the current user inside the users array if their username matches the filter too" do
user.update!(username: "andysmith")
get "/chat/api/chatables", params: { filter: "andy" }
expect(response.status).to eq(200)
expect(response.parsed_body["direct_message_channels"][0]["id"]).to eq(dm_chat_channel.id)
expect(response.parsed_body["users"].map { |u| u["id"] }).to match_array([user.id])
end
it "returns no channels with a whacky filter" do
get "/chat/api/chatables", params: { filter: "hello good sir" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"].count).to eq(0)
expect(response.parsed_body["direct_message_channels"].count).to eq(0)
expect(response.parsed_body["users"].count).to eq(0)
end
it "only returns open channels" do
chat_channel.update(status: ChatChannel.statuses[:closed])
get "/chat/api/chatables", params: { filter: "so" }
expect(response.parsed_body["public_channels"].count).to eq(0)
chat_channel.update(status: ChatChannel.statuses[:read_only])
get "/chat/api/chatables", params: { filter: "so" }
expect(response.parsed_body["public_channels"].count).to eq(0)
chat_channel.update(status: ChatChannel.statuses[:archived])
get "/chat/api/chatables", params: { filter: "so" }
expect(response.parsed_body["public_channels"].count).to eq(0)
# Now set status to open and the channel is there!
chat_channel.update(status: ChatChannel.statuses[:open])
get "/chat/api/chatables", params: { filter: "so" }
expect(response.parsed_body["public_channels"][0]["id"]).to eq(chat_channel.id)
end
it "only finds users by username_lower if not enable_names" do
SiteSetting.enable_names = false
get "/chat/api/chatables", params: { filter: "Andy J" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"].count).to eq(0)
expect(response.parsed_body["direct_message_channels"].count).to eq(0)
get "/chat/api/chatables", params: { filter: "andyjones" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"].count).to eq(0)
expect(response.parsed_body["direct_message_channels"][0]["id"]).to eq(dm_chat_channel.id)
end
it "only finds users by username if prioritize_username_in_ux" do
SiteSetting.prioritize_username_in_ux = true
get "/chat/api/chatables", params: { filter: "Andy J" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"].count).to eq(0)
expect(response.parsed_body["direct_message_channels"].count).to eq(0)
get "/chat/api/chatables", params: { filter: "andyjones" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"].count).to eq(0)
expect(response.parsed_body["direct_message_channels"][0]["id"]).to eq(dm_chat_channel.id)
end
it "can find users by name or username if not prioritize_username_in_ux and enable_names" do
SiteSetting.prioritize_username_in_ux = false
SiteSetting.enable_names = true
get "/chat/api/chatables", params: { filter: "Andy J" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"].count).to eq(0)
expect(response.parsed_body["direct_message_channels"][0]["id"]).to eq(dm_chat_channel.id)
get "/chat/api/chatables", params: { filter: "andyjones" }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"].count).to eq(0)
expect(response.parsed_body["direct_message_channels"][0]["id"]).to eq(dm_chat_channel.id)
end
it "does not return DM channels for users who do not have chat enabled" do
admin.user_option.update!(chat_enabled: false)
get "/chat/api/chatables", params: { filter: "andyjones" }
expect(response.status).to eq(200)
expect(response.parsed_body["direct_message_channels"].count).to eq(0)
end
it "does not return DM channels for users who are not in the chat allowed group" do
group = Fabricate(:group, name: "chatpeeps")
SiteSetting.chat_allowed_groups = group.id
GroupUser.create(user: user, group: group)
dm_chat_channel_2 = Fabricate(:direct_message_channel, users: [user, other_user])
get "/chat/api/chatables", params: { filter: "janemay" }
expect(response.status).to eq(200)
expect(response.parsed_body["direct_message_channels"].count).to eq(0)
GroupUser.create(user: other_user, group: group)
get "/chat/api/chatables", params: { filter: "janemay" }
expect(response.status).to eq(200)
expect(response.parsed_body["direct_message_channels"][0]["id"]).to eq(dm_chat_channel_2.id)
end
it "returns DM channels for staff users even if they are not in chat_allowed_groups" do
group = Fabricate(:group, name: "chatpeeps")
SiteSetting.chat_allowed_groups = group.id
GroupUser.create(user: user, group: group)
get "/chat/api/chatables", params: { filter: "andyjones" }
expect(response.status).to eq(200)
expect(response.parsed_body["direct_message_channels"][0]["id"]).to eq(dm_chat_channel.id)
end
it "returns followed channels" do
Fabricate(
:user_chat_channel_membership,
user: user,
chat_channel: chat_channel,
following: true,
)
get "/chat/api/chatables", params: { filter: chat_channel.name }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"][0]["id"]).to eq(chat_channel.id)
end
it "returns not followed channels" do
Fabricate(
:user_chat_channel_membership,
user: user,
chat_channel: chat_channel,
following: false,
)
get "/chat/api/chatables", params: { filter: chat_channel.name }
expect(response.status).to eq(200)
expect(response.parsed_body["public_channels"][0]["id"]).to eq(chat_channel.id)
end
end
end
end