This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/chat/spec/requests/api/chat_channels_controller_spec.rb
Joffrey JAFFEUX d2e24f9569
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 13:21:02 +01:00

526 lines
15 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::Api::ChatChannelsController do
before do
SiteSetting.chat_enabled = true
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
end
describe "#index" do
context "as anonymous user" do
it "returns an error" do
get "/chat/api/channels"
expect(response.status).to eq(403)
end
end
context "as disallowed user" do
fab!(:current_user) { Fabricate(:user) }
before do
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:staff]
sign_in(current_user)
end
it "returns an error" do
get "/chat/api/channels"
expect(response.status).to eq(403)
end
end
context "as allowed user" do
fab!(:current_user) { Fabricate(:user) }
before { sign_in(current_user) }
context "with category channels" do
context "when channel is public" do
fab!(:channel_1) { Fabricate(:category_channel) }
it "returns the channel" do
get "/chat/api/channels"
expect(response.status).to eq(200)
expect(response.parsed_body["channels"].map { |channel| channel["id"] }).to eq(
[channel_1.id],
)
end
context "when chatable is destroyed" do
before { channel_1.chatable.destroy! }
it "returns nothing" do
get "/chat/api/channels"
expect(response.status).to eq(200)
expect(response.parsed_body["channels"]).to be_blank
end
end
end
context "when channel has limited access" do
fab!(:group_1) { Fabricate(:group) }
fab!(:channel_1) { Fabricate(:private_category_channel, group: group_1) }
context "when user has access" do
before { group_1.add(current_user) }
it "returns the channel" do
get "/chat/api/channels"
expect(response.status).to eq(200)
expect(response.parsed_body["channels"].map { |channel| channel["id"] }).to eq(
[channel_1.id],
)
end
end
context "when user has no access" do
it "returns nothing" do
get "/chat/api/channels"
expect(response.status).to eq(200)
expect(response.parsed_body["channels"]).to be_blank
end
context "when user is admin" do
before { sign_in(Fabricate(:admin)) }
it "returns the channels" do
get "/chat/api/channels"
expect(response.status).to eq(200)
expect(response.parsed_body["channels"].map { |channel| channel["id"] }).to eq(
[channel_1.id],
)
end
end
end
end
end
context "with direct message channels" do
fab!(:dm_channel_1) { Fabricate(:direct_message_channel, users: [current_user]) }
it "doesnt return direct message channels" do
get "/chat/api/channels"
expect(response.parsed_body["channels"]).to be_blank
end
end
end
end
describe "#show" do
context "when anonymous" do
it "returns an error" do
get "/chat/api/channels/-999"
expect(response.status).to eq(403)
end
end
context "when user cannot access channel" do
fab!(:channel_1) { Fabricate(:private_category_channel) }
before { sign_in(Fabricate(:user)) }
it "returns an error" do
get "/chat/api/channels/#{channel_1.id}"
expect(response.status).to eq(403)
end
end
context "when user can access channel" do
fab!(:current_user) { Fabricate(:user) }
before { sign_in(current_user) }
context "when channel doesnt exist" do
it "returns an error" do
get "/chat/api/channels/-999"
expect(response.status).to eq(404)
end
end
context "when channel exists" do
fab!(:channel_1) { Fabricate(:category_channel) }
it "can find channel by id" do
get "/chat/api/channels/#{channel_1.id}"
expect(response.status).to eq(200)
expect(response.parsed_body["channel"]["id"]).to eq(channel_1.id)
end
end
end
end
describe "#destroy" do
fab!(:channel_1) { Fabricate(:category_channel) }
context "when user is not staff" do
fab!(:current_user) { Fabricate(:user) }
before { sign_in(current_user) }
it "returns an error" do
delete "/chat/api/channels/#{channel_1.id}",
params: {
channel: {
name_confirmation: channel_1.title(current_user),
},
}
expect(response.status).to eq(403)
end
end
context "when user is admin" do
fab!(:current_user) { Fabricate(:admin) }
before { sign_in(current_user) }
context "when the channel doesnt exist" do
before { channel_1.destroy! }
it "returns an error" do
delete "/chat/api/channels/#{channel_1.id}",
params: {
channel: {
name_confirmation: channel_1.title(current_user),
},
}
expect(response.status).to eq(404)
end
end
context "when the confirmation doesnt match the channel name" do
it "returns an error" do
delete "/chat/api/channels/#{channel_1.id}",
params: {
channel: {
name_confirmation: channel_1.title(current_user) + "foo",
},
}
expect(response.status).to eq(400)
end
end
context "with valid params" do
it "properly destroys the channel" do
delete "/chat/api/channels/#{channel_1.id}",
params: {
channel: {
name_confirmation: channel_1.title(current_user),
},
}
expect(response.status).to eq(200)
expect(channel_1.reload.trashed?).to eq(true)
expect(
job_enqueued?(job: :chat_channel_delete, args: { chat_channel_id: channel_1.id }),
).to eq(true)
expect(
UserHistory.exists?(
acting_user_id: current_user.id,
action: UserHistory.actions[:custom_staff],
custom_type: "chat_channel_delete",
),
).to eq(true)
end
end
end
end
describe "#create" do
fab!(:admin) { Fabricate(:admin) }
fab!(:category) { Fabricate(:category) }
let(:params) do
{
channel: {
type: category.class.name,
chatable_id: category.id,
name: "channel name",
description: "My new channel",
},
}
end
before { sign_in(admin) }
it "creates a channel associated to a category" do
post "/chat/api/channels", params: params
new_channel = ChatChannel.last
expect(new_channel.name).to eq(params[:channel][:name])
expect(new_channel.description).to eq(params[:channel][:description])
expect(new_channel.chatable_type).to eq(category.class.name)
expect(new_channel.chatable_id).to eq(category.id)
end
it "creates a channel sets auto_join_users to false by default" do
post "/chat/api/channels", params: params
new_channel = ChatChannel.last
expect(new_channel.auto_join_users).to eq(false)
end
it "creates a channel with auto_join_users set to true" do
params[:channel][:auto_join_users] = true
post "/chat/api/channels", params: params
new_channel = ChatChannel.last
expect(new_channel.auto_join_users).to eq(true)
end
describe "triggers the auto-join process" do
fab!(:chatters_group) { Fabricate(:group) }
fab!(:user) { Fabricate(:user, last_seen_at: 15.minute.ago) }
before do
Jobs.run_immediately!
Fabricate(:category_group, category: category, group: chatters_group)
chatters_group.add(user)
end
it "joins the user when auto_join_users is true" do
params[:channel][:auto_join_users] = true
post "/chat/api/channels", params: params
created_channel_id = response.parsed_body.dig("channel", "id")
membership_exists =
UserChatChannelMembership.find_by(
user: user,
chat_channel_id: created_channel_id,
following: true,
)
expect(membership_exists).to be_present
end
it "doesn't join the user when auto_join_users is false" do
params[:channel][:auto_join_users] = false
post "/chat/api/channels", params: params
created_channel_id = response.parsed_body.dig("channel", "id")
membership_exists =
UserChatChannelMembership.find_by(
user: user,
chat_channel_id: created_channel_id,
following: true,
)
expect(membership_exists).to be_nil
end
end
end
describe "#update" do
include_examples "channel access example", :put
context "when user cant edit channel" do
fab!(:channel) { Fabricate(:category_channel) }
before { sign_in(Fabricate(:user)) }
it "returns a 403" do
put "/chat/api/channels/#{channel.id}"
expect(response.status).to eq(403)
end
end
context "when user provided invalid params" do
fab!(:channel) { Fabricate(:category_channel, user_count: 10) }
before { sign_in(Fabricate(:admin)) }
it "doesnt change invalid properties" do
put "/chat/api/channels/#{channel.id}", params: { user_count: 40 }
expect(channel.reload.user_count).to eq(10)
end
end
context "when user provided an empty name" do
fab!(:user) { Fabricate(:admin) }
fab!(:channel) do
Fabricate(:category_channel, name: "something", description: "something else")
end
before { sign_in(user) }
it "nullifies the field and doesnt store an empty string" do
put "/chat/api/channels/#{channel.id}", params: { channel: { name: " " } }
expect(channel.reload.name).to be_nil
end
it "doesnt nullify the description" do
put "/chat/api/channels/#{channel.id}", params: { channel: { name: " " } }
expect(channel.reload.description).to eq("something else")
end
end
context "when user provides an empty description" do
fab!(:user) { Fabricate(:admin) }
fab!(:channel) do
Fabricate(:category_channel, name: "something else", description: "something")
end
before { sign_in(user) }
it "nullifies the field and doesnt store an empty string" do
put "/chat/api/channels/#{channel.id}", params: { channel: { description: " " } }
expect(channel.reload.description).to be_nil
end
it "doesnt nullify the name" do
put "/chat/api/channels/#{channel.id}", params: { channel: { description: " " } }
expect(channel.reload.name).to eq("something else")
end
end
context "when channel is a direct message channel" do
fab!(:user) { Fabricate(:admin) }
fab!(:channel) { Fabricate(:direct_message_channel) }
before { sign_in(user) }
it "raises a 403" do
put "/chat/api/channels/#{channel.id}"
expect(response.status).to eq(403)
end
end
context "when user provides valid params" do
fab!(:user) { Fabricate(:admin) }
fab!(:channel) { Fabricate(:category_channel) }
before { sign_in(user) }
it "sets properties" do
put "/chat/api/channels/#{channel.id}",
params: {
channel: {
name: "joffrey",
description: "cat owner",
},
}
expect(channel.reload.name).to eq("joffrey")
expect(channel.reload.description).to eq("cat owner")
end
it "publishes an update" do
messages =
MessageBus.track_publish("/chat/channel-edits") do
put "/chat/api/channels/#{channel.id}",
params: {
channel: {
name: "A new cat overlord",
},
}
end
expect(messages[0].data[:chat_channel_id]).to eq(channel.id)
end
it "returns a valid chat channel" do
put "/chat/api/channels/#{channel.id}", params: { channel: { name: "A new cat is born" } }
expect(response.parsed_body["channel"]).to match_response_schema("category_chat_channel")
end
describe "when updating allow_channel_wide_mentions" do
it "sets the new value" do
put "/chat/api/channels/#{channel.id}",
params: {
channel: {
allow_channel_wide_mentions: false,
},
}
expect(response.parsed_body["channel"]["allow_channel_wide_mentions"]).to eq(false)
end
end
describe "Updating a channel to add users automatically" do
it "sets the channel to auto-update users automatically" do
put "/chat/api/channels/#{channel.id}", params: { channel: { auto_join_users: true } }
expect(response.parsed_body["channel"]["auto_join_users"]).to eq(true)
end
it "tells staff members to slow down when toggling auto-update multiple times" do
RateLimiter.enable
put "/chat/api/channels/#{channel.id}", params: { channel: { auto_join_users: true } }
put "/chat/api/channels/#{channel.id}", params: { channel: { auto_join_users: false } }
put "/chat/api/channels/#{channel.id}", params: { channel: { auto_join_users: true } }
expect(response.status).to eq(429)
end
describe "triggers the auto-join process" do
fab!(:chatters_group) { Fabricate(:group) }
fab!(:another_user) { Fabricate(:user, last_seen_at: 15.minute.ago) }
before do
Jobs.run_immediately!
Fabricate(:category_group, category: channel.chatable, group: chatters_group)
chatters_group.add(another_user)
end
it "joins the user when auto_join_users is true" do
put "/chat/api/channels/#{channel.id}", params: { channel: { auto_join_users: true } }
created_channel_id = response.parsed_body["channel"]["id"]
membership_exists =
UserChatChannelMembership.find_by(
user: another_user,
chat_channel_id: created_channel_id,
following: true,
)
expect(membership_exists).to be_present
end
it "doesn't join the user when auto_join_users is false" do
put "/chat/api/channels/#{channel.id}", params: { channel: { auto_join_users: false } }
created_channel_id = response.parsed_body["channel"]["id"]
expect(created_channel_id).to be_present
membership_exists =
UserChatChannelMembership.find_by(
user: another_user,
chat_channel_id: created_channel_id,
following: true,
)
expect(membership_exists).to be_nil
end
end
end
end
end
end