DEV: properly namespace chat (#20690)

This commit main goal was to comply with Zeitwerk and properly rely on autoloading. To achieve this, most resources have been namespaced under the `Chat` module.

- Given all models are now namespaced with `Chat::` and would change the stored types in DB when using polymorphism or STI (single table inheritance), this commit uses various Rails methods to ensure proper class is loaded and the stored name in DB is unchanged, eg: `Chat::Message` model will be stored as `"ChatMessage"`, and `"ChatMessage"` will correctly load `Chat::Message` model.
- Jobs are now using constants only, eg: `Jobs::Chat::Foo` and should only be enqueued this way

Notes:
- This commit also used this opportunity to limit the number of registered css files in plugin.rb
- `discourse_dev` support has been removed within this commit and will be reintroduced later

<!-- 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. -->
This commit is contained in:
Joffrey JAFFEUX
2023-03-17 14:24:38 +01:00
committed by GitHub
parent 74349e17c9
commit 12a18d4d55
343 changed files with 9077 additions and 8745 deletions

View File

@ -2,7 +2,7 @@
require "rails_helper"
RSpec.describe Chat::AdminIncomingChatWebhooksController do
RSpec.describe Chat::Admin::IncomingWebhooksController do
fab!(:admin) { Fabricate(:admin) }
fab!(:user) { Fabricate(:user) }
fab!(:chat_channel1) { Fabricate(:category_channel) }
@ -56,7 +56,7 @@ RSpec.describe Chat::AdminIncomingChatWebhooksController do
post "/admin/plugins/chat/hooks.json",
params: {
name: "test1a",
chat_channel_id: ChatChannel.last.id + 1,
chat_channel_id: Chat::Channel.last.id + 1,
}
expect(response.status).to eq(404)
end
@ -64,7 +64,7 @@ RSpec.describe Chat::AdminIncomingChatWebhooksController do
it "creates a new incoming_chat_webhook record" do
sign_in(admin)
expect { post "/admin/plugins/chat/hooks.json", params: attrs }.to change {
IncomingChatWebhook.count
Chat::IncomingWebhook.count
}.by(1)
expect(response.parsed_body["name"]).to eq(attrs[:name])
expect(response.parsed_body["chat_channel"]["id"]).to eq(attrs[:chat_channel_id])
@ -117,7 +117,7 @@ RSpec.describe Chat::AdminIncomingChatWebhooksController do
end
end
describe "#update" do
describe "#delete" do
fab!(:existing) { Fabricate(:incoming_chat_webhook, chat_channel: chat_channel1) }
it "errors for non-staff" do
@ -129,7 +129,7 @@ RSpec.describe Chat::AdminIncomingChatWebhooksController do
it "destroys incoming_chat_webhook records" do
sign_in(admin)
expect { delete "/admin/plugins/chat/hooks/#{existing.id}.json" }.to change {
IncomingChatWebhook.count
Chat::IncomingWebhook.count
}.by(-1)
end
end

View File

@ -2,7 +2,7 @@
require "rails_helper"
RSpec.describe Chat::Api::ChatChannelThreadsController do
RSpec.describe Chat::Api::ChannelThreadsController do
fab!(:current_user) { Fabricate(:user) }
fab!(:public_channel) { Fabricate(:chat_channel, threading_enabled: true) }

View File

@ -2,7 +2,7 @@
require "rails_helper"
RSpec.describe Chat::Api::ChatChannelsArchivesController do
RSpec.describe Chat::Api::ChannelsArchivesController do
fab!(:user) { Fabricate(:user) }
fab!(:admin) { Fabricate(:admin) }
fab!(:category) { Fabricate(:category) }
@ -62,10 +62,10 @@ RSpec.describe Chat::Api::ChatChannelsArchivesController do
it "starts the archive process using a new topic" do
sign_in(admin)
post "/chat/api/channels/#{channel.id}/archives", params: new_topic_params
channel_archive = ChatChannelArchive.find_by(chat_channel: channel)
channel_archive = Chat::ChannelArchive.find_by(chat_channel: channel)
expect(
job_enqueued?(
job: :chat_channel_archive,
job: Jobs::Chat::ChannelArchive,
args: {
chat_channel_archive_id: channel_archive.id,
},
@ -77,10 +77,10 @@ RSpec.describe Chat::Api::ChatChannelsArchivesController do
it "starts the archive process using an existing topic" do
sign_in(admin)
post "/chat/api/channels/#{channel.id}/archives", params: existing_topic_params
channel_archive = ChatChannelArchive.find_by(chat_channel: channel)
channel_archive = Chat::ChannelArchive.find_by(chat_channel: channel)
expect(
job_enqueued?(
job: :chat_channel_archive,
job: Jobs::Chat::ChannelArchive,
args: {
chat_channel_archive_id: channel_archive.id,
},
@ -95,7 +95,7 @@ RSpec.describe Chat::Api::ChatChannelsArchivesController do
expect(response.status).to eq(200)
expect {
post "/chat/api/channels/#{channel.id}/archives", params: new_topic_params
}.not_to change { ChatChannelArchive.count }
}.not_to change { Chat::ChannelArchive.count }
end
context "when archiving to a new topic" do
@ -108,7 +108,7 @@ RSpec.describe Chat::Api::ChatChannelsArchivesController do
sign_in(admin)
expect {
post "/chat/api/channels/#{channel.id}/archives", params: new_topic_params_invalid
}.not_to change { ChatChannelArchive.count }
}.not_to change { Chat::ChannelArchive.count }
expect(response.status).to eq(400)
expect(response.parsed_body["errors"]).to eq(["Title can't have more than 1 emoji"])
end
@ -117,7 +117,7 @@ RSpec.describe Chat::Api::ChatChannelsArchivesController do
describe "when retrying the archive process" do
fab!(:channel) { Fabricate(:category_channel, chatable: category, status: :read_only) }
fab!(:archive) do
ChatChannelArchive.create!(
Chat::ChannelArchive.create!(
chat_channel: channel,
destination_topic_title: "test archive topic title",
archived_by: admin,
@ -151,7 +151,7 @@ RSpec.describe Chat::Api::ChatChannelsArchivesController do
archive.update!(archive_error: "bad stuff", archived_messages: 1)
expect { post "/chat/api/channels/#{channel.id}/archives" }.to change(
Jobs::ChatChannelArchive.jobs,
Jobs::Chat::ChannelArchive.jobs,
:size,
).by (1)
expect(response.status).to eq(200)

View File

@ -2,7 +2,7 @@
require "rails_helper"
RSpec.describe Chat::Api::ChatChannelsController do
RSpec.describe Chat::Api::ChannelsController do
before do
SiteSetting.chat_enabled = true
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
@ -198,7 +198,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
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 }),
job_enqueued?(job: Jobs::Chat::ChannelDelete, args: { chat_channel_id: channel_1.id }),
).to eq(true)
expect(
UserHistory.exists?(
@ -249,7 +249,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
it "creates a channel associated to a category" do
post "/chat/api/channels", params: params
new_channel = ChatChannel.last
new_channel = Chat::Channel.last
expect(new_channel.name).to eq(params[:channel][:name])
expect(new_channel.slug).to eq("channel-name")
@ -263,7 +263,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
new_params[:channel][:slug] = "wow-so-cool"
post "/chat/api/channels", params: new_params
new_channel = ChatChannel.last
new_channel = Chat::Channel.last
expect(new_channel.slug).to eq("wow-so-cool")
end
@ -271,7 +271,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
it "creates a channel sets auto_join_users to false by default" do
post "/chat/api/channels", params: params
new_channel = ChatChannel.last
new_channel = Chat::Channel.last
expect(new_channel.auto_join_users).to eq(false)
end
@ -280,7 +280,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
params[:channel][:auto_join_users] = true
post "/chat/api/channels", params: params
new_channel = ChatChannel.last
new_channel = Chat::Channel.last
expect(new_channel.auto_join_users).to eq(true)
end
@ -301,7 +301,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
created_channel_id = response.parsed_body.dig("channel", "id")
membership_exists =
UserChatChannelMembership.find_by(
Chat::UserChatChannelMembership.find_by(
user: user,
chat_channel_id: created_channel_id,
following: true,
@ -316,7 +316,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
created_channel_id = response.parsed_body.dig("channel", "id")
membership_exists =
UserChatChannelMembership.find_by(
Chat::UserChatChannelMembership.find_by(
user: user,
chat_channel_id: created_channel_id,
following: true,
@ -521,7 +521,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
created_channel_id = response.parsed_body["channel"]["id"]
membership_exists =
UserChatChannelMembership.find_by(
Chat::UserChatChannelMembership.find_by(
user: another_user,
chat_channel_id: created_channel_id,
following: true,
@ -538,7 +538,7 @@ RSpec.describe Chat::Api::ChatChannelsController do
expect(created_channel_id).to be_present
membership_exists =
UserChatChannelMembership.find_by(
Chat::UserChatChannelMembership.find_by(
user: another_user,
chat_channel_id: created_channel_id,
following: true,

View File

@ -2,7 +2,7 @@
require "rails_helper"
describe Chat::Api::ChatChannelsCurrentUserMembershipController do
describe Chat::Api::ChannelsCurrentUserMembershipController do
fab!(:current_user) { Fabricate(:user) }
fab!(:channel_1) { Fabricate(:category_channel) }
@ -16,7 +16,7 @@ describe Chat::Api::ChatChannelsCurrentUserMembershipController do
context "when no existing membership exists" do
it "creates a membership" do
expect { post "/chat/api/channels/#{channel_1.id}/memberships/me" }.to change {
UserChatChannelMembership.where(user_id: current_user.id, following: true).count
Chat::UserChatChannelMembership.where(user_id: current_user.id, following: true).count
}.by(1)
expect(response.status).to eq(200)
expect(response.parsed_body["membership"]["following"]).to eq(true)
@ -30,7 +30,7 @@ describe Chat::Api::ChatChannelsCurrentUserMembershipController do
it "fails" do
expect { post "/chat/api/channels/#{channel_2.id}/memberships/me" }.not_to change {
UserChatChannelMembership.where(user_id: current_user.id).count
Chat::UserChatChannelMembership.where(user_id: current_user.id).count
}
expect(response.status).to eq(403)
end
@ -54,7 +54,7 @@ describe Chat::Api::ChatChannelsCurrentUserMembershipController do
it "fails" do
expect { post "/chat/api/channels/#{channel_2.id}/memberships/me" }.not_to change {
UserChatChannelMembership.where(user_id: current_user.id).count
Chat::UserChatChannelMembership.where(user_id: current_user.id).count
}
expect(response.status).to eq(403)
end
@ -77,7 +77,7 @@ describe Chat::Api::ChatChannelsCurrentUserMembershipController do
it "works" do
expect { post "/chat/api/channels/#{channel_2.id}/memberships/me" }.to change {
UserChatChannelMembership.where(user_id: current_user.id).count
Chat::UserChatChannelMembership.where(user_id: current_user.id).count
}.by(1)
expect(response.status).to eq(200)
end
@ -87,7 +87,7 @@ describe Chat::Api::ChatChannelsCurrentUserMembershipController do
context "when an existing membership exists" do
it "enforces 'following' to true" do
membership_record =
UserChatChannelMembership.create!(
Chat::UserChatChannelMembership.create!(
chat_channel_id: channel_1.id,
user_id: current_user.id,
following: false,
@ -125,7 +125,7 @@ describe Chat::Api::ChatChannelsCurrentUserMembershipController do
it "fails" do
expect { delete "/chat/api/channels/#{channel_2.id}/memberships/me" }.not_to change {
UserChatChannelMembership.where(user_id: current_user.id).count
Chat::UserChatChannelMembership.where(user_id: current_user.id).count
}
expect(response.status).to eq(403)
end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
RSpec.describe Chat::Api::ChatChannelsCurrentUserNotificationsSettingsController do
RSpec.describe Chat::Api::ChannelsCurrentUserNotificationsSettingsController do
fab!(:current_user) { Fabricate(:user) }
before do
@ -23,7 +23,11 @@ RSpec.describe Chat::Api::ChatChannelsCurrentUserNotificationsSettingsController
end
it "doesn’t use invalid params" do
UserChatChannelMembership.any_instance.expects(:update!).with({ "muted" => "true" }).once
Chat::UserChatChannelMembership
.any_instance
.expects(:update!)
.with({ "muted" => "true" })
.once
put "/chat/api/channels/#{channel_1.id}/notifications-settings/me",
params: {
@ -91,7 +95,11 @@ RSpec.describe Chat::Api::ChatChannelsCurrentUserNotificationsSettingsController
before { sign_in(current_user) }
it "doesn’t use invalid params" do
UserChatChannelMembership.any_instance.expects(:update!).with({ "muted" => "true" }).once
Chat::UserChatChannelMembership
.any_instance
.expects(:update!)
.with({ "muted" => "true" })
.once
put "/chat/api/channels/#{dm_channel_1.id}/notifications-settings/me",
params: {

View File

@ -2,7 +2,7 @@
require "rails_helper"
RSpec.describe Chat::Api::ChatChannelsMessagesMovesController do
RSpec.describe Chat::Api::ChannelsMessagesMovesController do
fab!(:channel) { Fabricate(:category_channel) }
70.times { |n| fab!("message_#{n}") { Fabricate(:chat_message, chat_channel: channel) } }
@ -13,7 +13,7 @@ RSpec.describe Chat::Api::ChatChannelsMessagesMovesController do
end
def flag_message(message, flagger, flag_type: ReviewableScore.types[:off_topic])
Chat::ChatReviewQueue.new.flag_message(message, Guardian.new(flagger), flag_type)[:reviewable]
Chat::ReviewQueue.new.flag_message(message, Guardian.new(flagger), flag_type)[:reviewable]
end
describe "#create" do

View File

@ -2,7 +2,7 @@
require "rails_helper"
RSpec.describe Chat::Api::ChatChannelsStatusController do
RSpec.describe Chat::Api::ChannelsStatusController do
fab!(:channel_1) { Fabricate(:category_channel, status: :open) }
before do

View File

@ -2,7 +2,7 @@
require "rails_helper"
RSpec.describe Chat::Api::ChatChatablesController do
RSpec.describe Chat::Api::ChatablesController do
before do
SiteSetting.chat_enabled = true
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
@ -78,20 +78,20 @@ RSpec.describe Chat::Api::ChatChatablesController do
end
it "only returns open channels" do
chat_channel.update(status: ChatChannel.statuses[:closed])
chat_channel.update(status: Chat::Channel.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])
chat_channel.update(status: Chat::Channel.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])
chat_channel.update(status: Chat::Channel.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])
chat_channel.update(status: Chat::Channel.statuses[:open])
get "/chat/api/chatables", params: { filter: "so" }
expect(response.parsed_body["public_channels"][0]["id"]).to eq(chat_channel.id)
end

View File

@ -2,7 +2,7 @@
require "rails_helper"
describe Chat::Api::ChatCurrentUserChannelsController do
describe Chat::Api::CurrentUserChannelsController do
fab!(:current_user) { Fabricate(:user) }
before do

View File

@ -29,7 +29,7 @@ RSpec.describe Chat::ChatController do
end
def flag_message(message, flagger, flag_type: ReviewableScore.types[:off_topic])
Chat::ChatReviewQueue.new.flag_message(message, Guardian.new(flagger), flag_type)[:reviewable]
Chat::ReviewQueue.new.flag_message(message, Guardian.new(flagger), flag_type)[:reviewable]
end
describe "#messages" do
@ -102,6 +102,7 @@ RSpec.describe Chat::ChatController do
score = reviewable.reviewable_scores.last
get "/chat/#{chat_channel.id}/messages.json", params: { page_size: page_size }
expect(response.parsed_body["chat_messages"].last["user_flag_status"]).to eq(
score.status_for_database,
)
@ -252,7 +253,7 @@ RSpec.describe Chat::ChatController do
Guardian.any_instance.expects(:can_join_chat_channel?).with(channel)
sign_in(admin)
post "/chat/enable.json", params: { chatable_type: "category", chatable_id: category.id }
post "/chat/enable.json", params: { chatable_type: "Category", chatable_id: category.id }
end
# TODO: rewrite specs to ensure no exception is raised
@ -260,7 +261,7 @@ RSpec.describe Chat::ChatController do
Guardian.any_instance.expects(:can_join_chat_channel?)
sign_in(admin)
post "/chat/enable.json", params: { chatable_type: "category", chatable_id: category.id }
post "/chat/enable.json", params: { chatable_type: "Category", chatable_id: category.id }
end
end
end
@ -275,7 +276,7 @@ RSpec.describe Chat::ChatController do
Guardian.any_instance.expects(:can_join_chat_channel?).with(channel)
sign_in(admin)
post "/chat/disable.json", params: { chatable_type: "category", chatable_id: category.id }
post "/chat/disable.json", params: { chatable_type: "Category", chatable_id: category.id }
end
end
end
@ -288,7 +289,11 @@ RSpec.describe Chat::ChatController do
context "when current user is silenced" do
before do
UserChatChannelMembership.create(user: user, chat_channel: chat_channel, following: true)
Chat::UserChatChannelMembership.create(
user: user,
chat_channel: chat_channel,
following: true,
)
sign_in(user)
UserSilencer.new(user).silence
end
@ -344,13 +349,17 @@ RSpec.describe Chat::ChatController do
it "sends a message for regular user when staff-only is disabled and they are following channel" do
sign_in(user)
UserChatChannelMembership.create(user: user, chat_channel: chat_channel, following: true)
Chat::UserChatChannelMembership.create(
user: user,
chat_channel: chat_channel,
following: true,
)
expect { post "/chat/#{chat_channel.id}.json", params: { message: message } }.to change {
ChatMessage.count
Chat::Message.count
}.by(1)
expect(response.status).to eq(200)
expect(ChatMessage.last.message).to eq(message)
expect(Chat::Message.last.message).to eq(message)
end
end
@ -363,22 +372,22 @@ RSpec.describe Chat::ChatController do
it "forces users to follow the channel" do
direct_message_channel.remove(user2)
ChatPublisher.expects(:publish_new_channel).once
Chat::Publisher.expects(:publish_new_channel).once
sign_in(user1)
post "/chat/#{direct_message_channel.id}.json", params: { message: message }
expect(UserChatChannelMembership.find_by(user_id: user2.id).following).to be true
expect(Chat::UserChatChannelMembership.find_by(user_id: user2.id).following).to be true
end
it "errors when the user is not part of the direct message channel" do
DirectMessageUser.find_by(user: user1, direct_message: chatable).destroy!
Chat::DirectMessageUser.find_by(user: user1, direct_message: chatable).destroy!
sign_in(user1)
post "/chat/#{direct_message_channel.id}.json", params: { message: message }
expect(response.status).to eq(403)
UserChatChannelMembership.find_by(user_id: user2.id).update!(following: true)
Chat::UserChatChannelMembership.find_by(user_id: user2.id).update!(following: true)
sign_in(user2)
post "/chat/#{direct_message_channel.id}.json", params: { message: message }
expect(response.status).to eq(200)
@ -404,12 +413,12 @@ RSpec.describe Chat::ChatController do
it "does not force them to follow the channel or send a publish_new_channel message" do
direct_message_channel.remove(user2)
ChatPublisher.expects(:publish_new_channel).never
Chat::Publisher.expects(:publish_new_channel).never
sign_in(user1)
post "/chat/#{direct_message_channel.id}.json", params: { message: message }
expect(UserChatChannelMembership.find_by(user_id: user2.id).following).to be false
expect(Chat::UserChatChannelMembership.find_by(user_id: user2.id).following).to be false
end
end
end
@ -423,7 +432,7 @@ RSpec.describe Chat::ChatController do
sign_in(Fabricate(:admin))
expect_enqueued_with(
job: :process_chat_message,
job: Jobs::Chat::ProcessMessage,
args: {
chat_message_id: chat_message.id,
},
@ -457,7 +466,7 @@ RSpec.describe Chat::ChatController do
chat_message.update!(message: "new content")
expect_enqueued_with(
job: :process_chat_message,
job: Jobs::Chat::ProcessMessage,
args: {
chat_message_id: chat_message.id,
is_dirty: true,
@ -554,7 +563,7 @@ RSpec.describe Chat::ChatController do
it "doesn't allow a user to delete another user's message" do
sign_in(other_user)
delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json"
delete "/chat/#{chat_channel.id}/#{Chat::Message.last.id}.json"
expect(response.status).to eq(403)
end
@ -573,25 +582,25 @@ RSpec.describe Chat::ChatController do
expect do
events =
DiscourseEvent.track_events do
delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json"
delete "/chat/#{chat_channel.id}/#{Chat::Message.last.id}.json"
end
end.to change { ChatMessage.count }.by(-1)
end.to change { Chat::Message.count }.by(-1)
expect(response.status).to eq(200)
expect(events.map { |event| event[:event_name] }).to include(:chat_message_trashed)
end
it "does not allow message delete when chat channel is read_only" do
sign_in(ChatMessage.last.user)
sign_in(Chat::Message.last.user)
chat_channel.update!(status: :read_only)
expect { delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json" }.not_to change {
ChatMessage.count
expect { delete "/chat/#{chat_channel.id}/#{Chat::Message.last.id}.json" }.not_to change {
Chat::Message.count
}
expect(response.status).to eq(403)
sign_in(admin)
delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json"
delete "/chat/#{chat_channel.id}/#{Chat::Message.last.id}.json"
expect(response.status).to eq(403)
end
@ -599,14 +608,14 @@ RSpec.describe Chat::ChatController do
sign_in(admin)
chat_channel.update!(status: :read_only)
expect { delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json" }.not_to change {
ChatMessage.count
expect { delete "/chat/#{chat_channel.id}/#{Chat::Message.last.id}.json" }.not_to change {
Chat::Message.count
}
expect(response.status).to eq(403)
chat_channel.update!(status: :closed)
expect { delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json" }.to change {
ChatMessage.count
expect { delete "/chat/#{chat_channel.id}/#{Chat::Message.last.id}.json" }.to change {
Chat::Message.count
}.by(-1)
expect(response.status).to eq(200)
end
@ -619,7 +628,7 @@ RSpec.describe Chat::ChatController do
end
before do
ChatMessage.create!(user: user, message: "this is a message", chat_channel: chat_channel)
Chat::Message.create!(user: user, message: "this is a message", chat_channel: chat_channel)
end
describe "for category" do
@ -632,8 +641,8 @@ RSpec.describe Chat::ChatController do
it "Allows users to delete their own messages" do
sign_in(user)
expect { delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json" }.to change {
ChatMessage.count
expect { delete "/chat/#{chat_channel.id}/#{Chat::Message.last.id}.json" }.to change {
Chat::Message.count
}.by(-1)
expect(response.status).to eq(200)
end
@ -644,14 +653,14 @@ RSpec.describe Chat::ChatController do
it "doesn't allow a user to restore another user's message" do
sign_in(other_user)
put "/chat/#{chat_channel.id}/restore/#{ChatMessage.unscoped.last.id}.json"
put "/chat/#{chat_channel.id}/restore/#{Chat::Message.unscoped.last.id}.json"
expect(response.status).to eq(403)
end
it "allows a user to restore their own posts" do
sign_in(user)
deleted_message = ChatMessage.unscoped.last
deleted_message = Chat::Message.unscoped.last
put "/chat/#{chat_channel.id}/restore/#{deleted_message.id}.json"
expect(response.status).to eq(200)
expect(deleted_message.reload.deleted_at).to be_nil
@ -660,18 +669,18 @@ RSpec.describe Chat::ChatController do
it "allows admin to restore others' posts" do
sign_in(admin)
deleted_message = ChatMessage.unscoped.last
deleted_message = Chat::Message.unscoped.last
put "/chat/#{chat_channel.id}/restore/#{deleted_message.id}.json"
expect(response.status).to eq(200)
expect(deleted_message.reload.deleted_at).to be_nil
end
it "does not allow message restore when chat channel is read_only" do
sign_in(ChatMessage.last.user)
sign_in(Chat::Message.last.user)
chat_channel.update!(status: :read_only)
deleted_message = ChatMessage.unscoped.last
deleted_message = Chat::Message.unscoped.last
put "/chat/#{chat_channel.id}/restore/#{deleted_message.id}.json"
expect(response.status).to eq(403)
expect(deleted_message.reload.deleted_at).not_to be_nil
@ -686,7 +695,7 @@ RSpec.describe Chat::ChatController do
chat_channel.update!(status: :read_only)
deleted_message = ChatMessage.unscoped.last
deleted_message = Chat::Message.unscoped.last
put "/chat/#{chat_channel.id}/restore/#{deleted_message.id}.json"
expect(response.status).to eq(403)
expect(deleted_message.reload.deleted_at).not_to be_nil
@ -703,7 +712,7 @@ RSpec.describe Chat::ChatController do
before do
message =
ChatMessage.create(user: user, message: "this is a message", chat_channel: chat_channel)
Chat::Message.create(user: user, message: "this is a message", chat_channel: chat_channel)
message.trash!
end
@ -777,7 +786,7 @@ RSpec.describe Chat::ChatController do
it "updates timing records" do
expect { put "/chat/#{chat_channel.id}/read/#{message_1.id}.json" }.not_to change {
UserChatChannelMembership.count
Chat::UserChatChannelMembership.count
}
membership.reload
@ -803,7 +812,7 @@ RSpec.describe Chat::ChatController do
}.to_json,
)
.tap do |notification|
ChatMention.create!(user: user, chat_message: msg, notification: notification)
Chat::Mention.create!(user: user, chat_message: msg, notification: notification)
end
end
@ -878,7 +887,7 @@ RSpec.describe Chat::ChatController do
emoji: ":heart:",
react_action: "add",
}
}.to change { UserChatChannelMembership.count }.by(1)
}.to change { Chat::UserChatChannelMembership.count }.by(1)
expect(response.status).to eq(200)
end
@ -1188,7 +1197,7 @@ RSpec.describe Chat::ChatController do
chat_message_id: admin_chat_message.id,
flag_type_id: ReviewableScore.types[:off_topic],
}
}.to change { ReviewableChatMessage.where(target: admin_chat_message).count }.by(1)
}.to change { Chat::ReviewableMessage.where(target: admin_chat_message).count }.by(1)
expect(response.status).to eq(200)
end
@ -1264,10 +1273,10 @@ RSpec.describe Chat::ChatController do
it "can create and destroy chat drafts" do
expect {
post "/chat/drafts.json", params: { chat_channel_id: chat_channel.id, data: "{}" }
}.to change { ChatDraft.count }.by(1)
}.to change { Chat::Draft.count }.by(1)
expect { post "/chat/drafts.json", params: { chat_channel_id: chat_channel.id } }.to change {
ChatDraft.count
Chat::Draft.count
}.by(-1)
end
@ -1282,17 +1291,17 @@ RSpec.describe Chat::ChatController do
GroupUser.create!(user: user, group: group)
expect {
post "/chat/drafts.json", params: { chat_channel_id: chat_channel.id, data: "{}" }
}.to change { ChatDraft.count }.by(1)
}.to change { Chat::Draft.count }.by(1)
end
it "cannot create chat drafts for a direct message channel the user cannot access" do
post "/chat/drafts.json", params: { chat_channel_id: dm_channel.id, data: "{}" }
expect(response.status).to eq(403)
DirectMessageUser.create(user: user, direct_message: dm_channel.chatable)
Chat::DirectMessageUser.create(user: user, direct_message: dm_channel.chatable)
expect {
post "/chat/drafts.json", params: { chat_channel_id: dm_channel.id, data: "{}" }
}.to change { ChatDraft.count }.by(1)
}.to change { Chat::Draft.count }.by(1)
end
it "cannot create a too long chat draft" do
@ -1372,7 +1381,7 @@ RSpec.describe Chat::ChatController do
get "/chat/lookup/#{message.id}.json", params: { chat_channel_id: channel.id }
expect(response.status).to eq(403)
DirectMessageUser.create!(user: user, direct_message: chatable)
Chat::DirectMessageUser.create!(user: user, direct_message: chatable)
get "/chat/lookup/#{message.id}.json", params: { chat_channel_id: channel.id }
expect(response.status).to eq(200)
expect(response.parsed_body["chat_messages"][0]["id"]).to eq(message.id)

View File

@ -26,7 +26,7 @@ RSpec.describe CategoriesController do
end
it "deletes the associated channel" do
expect { destroy_category }.to change { CategoryChannel.count }.by(-1)
expect { destroy_category }.to change { Chat::CategoryChannel.count }.by(-1)
end
end

View File

@ -18,7 +18,7 @@ describe UsersController do
put "/u/activate-account/#{email_token.token}"
expect(response.status).to eq(200)
membership = UserChatChannelMembership.find_by(user: user, chat_channel: channel)
membership = Chat::UserChatChannelMembership.find_by(user: user, chat_channel: channel)
expect(membership.following).to eq(true)
end
end

View File

@ -15,11 +15,11 @@ RSpec.describe Chat::DirectMessagesController do
end
def create_dm_channel(user_ids)
direct_messages_channel = DirectMessage.create!
direct_messages_channel = Chat::DirectMessage.create!
user_ids.each do |user_id|
direct_messages_channel.direct_message_users.create!(user_id: user_id)
end
DirectMessageChannel.create!(chatable: direct_messages_channel)
Chat::DirectMessageChannel.create!(chatable: direct_messages_channel)
end
describe "#index" do
@ -41,10 +41,10 @@ RSpec.describe Chat::DirectMessagesController do
context "when channel exists" do
let!(:channel) do
direct_messages_channel = DirectMessage.create!
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)
DirectMessageChannel.create!(chatable: direct_messages_channel)
Chat::DirectMessageChannel.create!(chatable: direct_messages_channel)
end
it "returns the channel" do
@ -76,8 +76,8 @@ RSpec.describe Chat::DirectMessagesController do
it "creates a new dm channel with username(s) provided" do
expect {
post "/chat/direct_messages/create.json", params: { usernames: [usernames] }
}.to change { DirectMessage.count }.by(1)
expect(DirectMessage.last.direct_message_users.map(&:user_id)).to match_array(
}.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
@ -86,7 +86,7 @@ RSpec.describe Chat::DirectMessagesController do
create_dm_channel(direct_message_user_ids)
expect {
post "/chat/direct_messages/create.json", params: { usernames: [usernames] }
}.not_to change { DirectMessage.count }
}.not_to change { Chat::DirectMessage.count }
end
end
@ -111,12 +111,12 @@ RSpec.describe Chat::DirectMessagesController do
include_examples "creating dms"
end
it "creates UserChatChannelMembership records" do
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 { UserChatChannelMembership.count }.by(3)
}.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
@ -127,7 +127,7 @@ RSpec.describe Chat::DirectMessagesController do
it "responds with a friendly error" do
expect {
post "/chat/direct_messages/create.json", params: { usernames: [usernames] }
}.not_to change { DirectMessage.count }
}.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)],

View File

@ -2,7 +2,7 @@
require "rails_helper"
RSpec.describe Chat::IncomingChatWebhooksController do
RSpec.describe Chat::IncomingWebhooksController do
fab!(:chat_channel) { Fabricate(:category_channel) }
fab!(:webhook) { Fabricate(:incoming_chat_webhook, chat_channel: chat_channel) }
@ -30,10 +30,10 @@ RSpec.describe Chat::IncomingChatWebhooksController do
it "creates a new chat message" do
expect {
post "/chat/hooks/#{webhook.key}.json", params: { text: "A new signup woo!" }
}.to change { ChatMessage.where(chat_channel: chat_channel).count }.by(1)
}.to change { Chat::Message.where(chat_channel: chat_channel).count }.by(1)
expect(response.status).to eq(200)
chat_webhook_event = ChatWebhookEvent.last
expect(chat_webhook_event.chat_message_id).to eq(ChatMessage.last.id)
chat_webhook_event = Chat::WebhookEvent.last
expect(chat_webhook_event.chat_message_id).to eq(Chat::Message.last.id)
end
it "handles create message failures gracefully and does not create the chat message" do
@ -41,7 +41,7 @@ RSpec.describe Chat::IncomingChatWebhooksController do
expect {
post "/chat/hooks/#{webhook.key}.json", params: { text: "hey #{watched_word.word}" }
}.not_to change { ChatMessage.where(chat_channel: chat_channel).count }
}.not_to change { Chat::Message.where(chat_channel: chat_channel).count }
expect(response.status).to eq(422)
expect(response.parsed_body["errors"]).to include(
"Sorry, you can't post the word '#{watched_word.word}'; it's not allowed.",
@ -52,7 +52,7 @@ RSpec.describe Chat::IncomingChatWebhooksController do
chat_channel.update!(status: :read_only)
expect {
post "/chat/hooks/#{webhook.key}.json", params: { text: "hey this is a message" }
}.not_to change { ChatMessage.where(chat_channel: chat_channel).count }
}.not_to change { Chat::Message.where(chat_channel: chat_channel).count }
expect(response.status).to eq(422)
expect(response.parsed_body["errors"]).to include(
I18n.t("chat.errors.channel_new_message_disallowed.read_only"),
@ -74,9 +74,9 @@ RSpec.describe Chat::IncomingChatWebhooksController do
it "processes the text param with SlackCompatibility" do
expect {
post "/chat/hooks/#{webhook.key}/slack.json", params: { text: "A new signup woo <!here>!" }
}.to change { ChatMessage.where(chat_channel: chat_channel).count }.by(1)
}.to change { Chat::Message.where(chat_channel: chat_channel).count }.by(1)
expect(response.status).to eq(200)
expect(ChatMessage.last.message).to eq("A new signup woo @here!")
expect(Chat::Message.last.message).to eq("A new signup woo @here!")
end
it "processes the attachments param with SlackCompatibility, using the fallback" do
@ -94,14 +94,14 @@ RSpec.describe Chat::IncomingChatWebhooksController do
],
}
expect { post "/chat/hooks/#{webhook.key}/slack.json", params: payload_data }.to change {
ChatMessage.where(chat_channel: chat_channel).count
Chat::Message.where(chat_channel: chat_channel).count
}.by(1)
expect(ChatMessage.last.message).to eq(
expect(Chat::Message.last.message).to eq(
"New alert: \"[StatusCake] https://www.test_notification.com (StatusCake Test Alert): Down,\" [46353](https://eu.opsg.in/a/i/test/blahguid)\nTags: ",
)
expect {
post "/chat/hooks/#{webhook.key}/slack.json", params: { payload: payload_data }
}.to change { ChatMessage.where(chat_channel: chat_channel).count }.by(1)
}.to change { Chat::Message.where(chat_channel: chat_channel).count }.by(1)
end
it "can process the payload when it's a JSON string" do
@ -120,8 +120,8 @@ RSpec.describe Chat::IncomingChatWebhooksController do
}
expect {
post "/chat/hooks/#{webhook.key}/slack.json", params: { payload: payload_data.to_json }
}.to change { ChatMessage.where(chat_channel: chat_channel).count }.by(1)
expect(ChatMessage.last.message).to eq(
}.to change { Chat::Message.where(chat_channel: chat_channel).count }.by(1)
expect(Chat::Message.last.message).to eq(
"New alert: \"[StatusCake] https://www.test_notification.com (StatusCake Test Alert): Down,\" [46353](https://eu.opsg.in/a/i/test/blahguid)\nTags: ",
)
end