mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: ensures users can open channel invites (#24067)
We were incorrectly generating URLs with message id even when it was not provided, resulting in a route ending with "undefined", which was causing an error. This commit also uses this opportunity to: - move `invite_users` into a proper controller inside the API namespace - refactors the code into a service: `Chat::InviteUsersToChannel`
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe Chat::Api::ChannelsInvitesController do
|
||||
fab!(:current_user) { Fabricate(:user) }
|
||||
fab!(:channel_1) { Fabricate(:chat_channel) }
|
||||
fab!(:user_1) { Fabricate(:user) }
|
||||
fab!(:user_2) { Fabricate(:user) }
|
||||
|
||||
before do
|
||||
SiteSetting.chat_enabled = true
|
||||
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
|
||||
channel_1.add(current_user)
|
||||
sign_in(current_user)
|
||||
end
|
||||
|
||||
describe "create" do
|
||||
describe "success" do
|
||||
it "works" do
|
||||
expect {
|
||||
post "/chat/api/channels/#{channel_1.id}/invites?user_ids=#{user_1.id},#{user_2.id}"
|
||||
}.to change {
|
||||
Notification.where(
|
||||
notification_type: Notification.types[:chat_invitation],
|
||||
user_id: [user_1.id, user_2.id],
|
||||
).count
|
||||
}.by(2)
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
end
|
||||
end
|
||||
|
||||
describe "missing user_ids" do
|
||||
fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel_1) }
|
||||
|
||||
it "returns a 400" do
|
||||
post "/chat/api/channels/#{channel_1.id}/invites"
|
||||
|
||||
expect(response.status).to eq(400)
|
||||
end
|
||||
end
|
||||
|
||||
describe "message_id param" do
|
||||
fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel_1) }
|
||||
|
||||
it "works" do
|
||||
post "/chat/api/channels/#{channel_1.id}/invites?user_ids=#{user_1.id},#{user_2.id}&message_id=#{message_1.id}"
|
||||
|
||||
expect(JSON.parse(Notification.last.data)["chat_message_id"]).to eq(message_1.id)
|
||||
expect(response.status).to eq(200)
|
||||
end
|
||||
end
|
||||
|
||||
describe "current user can't view channel" do
|
||||
fab!(:channel_1) { Fabricate(:private_category_channel) }
|
||||
|
||||
it "returns a 403" do
|
||||
post "/chat/api/channels/#{channel_1.id}/invites?user_ids=#{user_1.id},#{user_2.id}"
|
||||
|
||||
expect(response.status).to eq(403)
|
||||
end
|
||||
end
|
||||
|
||||
describe "channel doesn’t exist" do
|
||||
it "returns a 404" do
|
||||
post "/chat/api/channels/-1/invites?user_ids=#{user_1.id},#{user_2.id}"
|
||||
|
||||
expect(response.status).to eq(404)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -370,61 +370,6 @@ RSpec.describe Chat::ChatController do
|
||||
end
|
||||
end
|
||||
|
||||
describe "invite_users" do
|
||||
fab!(:chat_channel) { Fabricate(:category_channel) }
|
||||
fab!(:chat_message) { Fabricate(:chat_message, chat_channel: chat_channel, user: admin) }
|
||||
fab!(:user2) { Fabricate(:user) }
|
||||
|
||||
before do
|
||||
sign_in(admin)
|
||||
|
||||
[user, user2].each { |u| u.user_option.update(chat_enabled: true) }
|
||||
end
|
||||
|
||||
it "doesn't invite users who cannot chat" do
|
||||
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:admins]
|
||||
|
||||
expect {
|
||||
put "/chat/#{chat_channel.id}/invite.json", params: { user_ids: [user.id] }
|
||||
}.not_to change {
|
||||
user.notifications.where(notification_type: Notification.types[:chat_invitation]).count
|
||||
}
|
||||
end
|
||||
|
||||
it "creates an invitation notification for users who can chat" do
|
||||
expect {
|
||||
put "/chat/#{chat_channel.id}/invite.json", params: { user_ids: [user.id] }
|
||||
}.to change {
|
||||
user.notifications.where(notification_type: Notification.types[:chat_invitation]).count
|
||||
}.by(1)
|
||||
notification =
|
||||
user.notifications.where(notification_type: Notification.types[:chat_invitation]).last
|
||||
parsed_data = JSON.parse(notification[:data])
|
||||
expect(parsed_data["chat_channel_title"]).to eq(chat_channel.title(user))
|
||||
expect(parsed_data["chat_channel_slug"]).to eq(chat_channel.slug)
|
||||
end
|
||||
|
||||
it "creates multiple invitations" do
|
||||
expect {
|
||||
put "/chat/#{chat_channel.id}/invite.json", params: { user_ids: [user.id, user2.id] }
|
||||
}.to change {
|
||||
Notification.where(
|
||||
notification_type: Notification.types[:chat_invitation],
|
||||
user_id: [user.id, user2.id],
|
||||
).count
|
||||
}.by(2)
|
||||
end
|
||||
|
||||
it "adds chat_message_id when param is present" do
|
||||
put "/chat/#{chat_channel.id}/invite.json",
|
||||
params: {
|
||||
user_ids: [user.id],
|
||||
chat_message_id: chat_message.id,
|
||||
}
|
||||
expect(JSON.parse(Notification.last.data)["chat_message_id"]).to eq(chat_message.id.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#dismiss_retention_reminder" do
|
||||
it "errors for anon" do
|
||||
post "/chat/dismiss-retention-reminder.json", params: { chatable_type: "Category" }
|
||||
|
||||
Reference in New Issue
Block a user