discourse/plugins/chat/spec/services/chat/trash_channel_spec.rb
Joffrey JAFFEUX 12a18d4d55
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. -->
2023-03-17 14:24:38 +01:00

55 lines
1.6 KiB
Ruby

# frozen_string_literal: true
RSpec.describe(Chat::TrashChannel) do
subject(:result) { described_class.call(guardian: guardian) }
let(:guardian) { Guardian.new(current_user) }
context "when channel_id is not provided" do
fab!(:current_user) { Fabricate(:admin) }
it { is_expected.to fail_to_find_a_model(:channel) }
end
context "when channel_id is provided" do
subject(:result) { described_class.call(channel_id: channel.id, guardian: guardian) }
fab!(:channel) { Fabricate(:chat_channel) }
context "when user is not allowed to perform the action" do
fab!(:current_user) { Fabricate(:user) }
it { is_expected.to fail_a_policy(:invalid_access) }
end
context "when user is allowed to perform the action" do
fab!(:current_user) { Fabricate(:admin) }
it "sets the service result as successful" do
expect(result).to be_a_success
end
it "trashes the channel" do
expect(result[:channel]).to be_trashed
end
it "logs the action" do
expect { result }.to change { UserHistory.count }.by(1)
expect(UserHistory.last).to have_attributes(
custom_type: "chat_channel_delete",
details:
"chat_channel_id: #{result[:channel].id}\nchat_channel_name: #{result[:channel].title(guardian.user)}",
)
end
it "changes the slug to prevent colisions" do
expect(result[:channel].slug).to include("deleted")
end
it "queues a job to delete channel relations" do
expect { result }.to change(Jobs::Chat::ChannelDelete.jobs, :size).by(1)
end
end
end
end