mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 07:11:34 +08:00
DEV: Move channel creation for category into service (#21167)
This commit moves the category channel creation out of the Chat::Api::Channel controller and into a dedicated CreateCategoryChannel service. A follow up commit will move the DM channel creation out of the old DirectMessageChannelCreator service. Also includes a new on_model_errors helper for chat service class usage, that collects model validation errors to present in a nice way. --------- Co-authored-by: Loïc Guitaut <loic@discourse.org>
This commit is contained in:
@ -155,7 +155,7 @@ RSpec.describe Chat::Api::ChannelsController 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)
|
||||
expect(response.parsed_body.dig("channel", "id")).to eq(channel_1.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -248,8 +248,9 @@ RSpec.describe Chat::Api::ChannelsController do
|
||||
|
||||
it "creates a channel associated to a category" do
|
||||
post "/chat/api/channels", params: params
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
new_channel = Chat::Channel.last
|
||||
new_channel = Chat::Channel.find(response.parsed_body.dig("channel", "id"))
|
||||
|
||||
expect(new_channel.name).to eq(params[:channel][:name])
|
||||
expect(new_channel.slug).to eq("channel-name")
|
||||
@ -262,16 +263,31 @@ RSpec.describe Chat::Api::ChannelsController do
|
||||
new_params = params.dup
|
||||
new_params[:channel][:slug] = "wow-so-cool"
|
||||
post "/chat/api/channels", params: new_params
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
new_channel = Chat::Channel.last
|
||||
new_channel = Chat::Channel.find(response.parsed_body.dig("channel", "id"))
|
||||
|
||||
expect(new_channel.slug).to eq("wow-so-cool")
|
||||
end
|
||||
|
||||
context "when the user-provided slug already exists for a channel" do
|
||||
before do
|
||||
params[:channel][:slug] = "wow-so-cool"
|
||||
post "/chat/api/channels", params: params
|
||||
params[:channel][:name] = "new name"
|
||||
end
|
||||
|
||||
it "returns an error" do
|
||||
post "/chat/api/channels", params: params
|
||||
expect(response).to have_http_status :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
it "creates a channel sets auto_join_users to false by default" do
|
||||
post "/chat/api/channels", params: params
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
new_channel = Chat::Channel.last
|
||||
new_channel = Chat::Channel.find(response.parsed_body.dig("channel", "id"))
|
||||
|
||||
expect(new_channel.auto_join_users).to eq(false)
|
||||
end
|
||||
@ -279,8 +295,9 @@ RSpec.describe Chat::Api::ChannelsController do
|
||||
it "creates a channel with auto_join_users set to true" do
|
||||
params[:channel][:auto_join_users] = true
|
||||
post "/chat/api/channels", params: params
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
new_channel = Chat::Channel.last
|
||||
new_channel = Chat::Channel.find(response.parsed_body.dig("channel", "id"))
|
||||
|
||||
expect(new_channel.auto_join_users).to eq(true)
|
||||
end
|
||||
@ -298,6 +315,7 @@ RSpec.describe Chat::Api::ChannelsController do
|
||||
it "joins the user when auto_join_users is true" do
|
||||
params[:channel][:auto_join_users] = true
|
||||
post "/chat/api/channels", params: params
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
created_channel_id = response.parsed_body.dig("channel", "id")
|
||||
membership_exists =
|
||||
@ -313,6 +331,7 @@ RSpec.describe Chat::Api::ChannelsController do
|
||||
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
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
created_channel_id = response.parsed_body.dig("channel", "id")
|
||||
membership_exists =
|
||||
@ -519,7 +538,7 @@ RSpec.describe Chat::Api::ChannelsController do
|
||||
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"]
|
||||
created_channel_id = response.parsed_body.dig("channel", "id")
|
||||
membership_exists =
|
||||
Chat::UserChatChannelMembership.find_by(
|
||||
user: another_user,
|
||||
@ -533,7 +552,7 @@ RSpec.describe Chat::Api::ChannelsController do
|
||||
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"]
|
||||
created_channel_id = response.parsed_body.dig("channel", "id")
|
||||
|
||||
expect(created_channel_id).to be_present
|
||||
|
||||
|
Reference in New Issue
Block a user