mirror of
https://github.com/discourse/discourse.git
synced 2024-12-15 08:13:45 +08:00
abcaa1a961
This is a followup of the previous refactor where we created two new models to handle all the dedicated logic that was present in the `ChatChannel` model. For the sake of consistency, `DMChannel` has been renamed to `DirectMessageChannel` and the previous `DirectMessageChannel` model is now named `DirectMessage`. This should help reasoning about direct messages.
53 lines
1.6 KiB
Ruby
53 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "rails_helper"
|
|
|
|
describe DirectMessageSerializer do
|
|
describe "#user" do
|
|
it "returns you when there are two of us" do
|
|
me = Fabricate.build(:user)
|
|
you = Fabricate.build(:user)
|
|
direct_message = Fabricate.build(:direct_message, users: [me, you])
|
|
|
|
serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false)
|
|
|
|
expect(serializer.users).to eq([you])
|
|
end
|
|
|
|
it "returns you both if there are three of us" do
|
|
me = Fabricate.build(:user)
|
|
you = Fabricate.build(:user)
|
|
other_you = Fabricate.build(:user)
|
|
direct_message = Fabricate.build(:direct_message, users: [me, you, other_you])
|
|
|
|
serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false)
|
|
|
|
expect(serializer.users).to match_array([you, other_you])
|
|
end
|
|
|
|
it "returns me if there is only me" do
|
|
me = Fabricate.build(:user)
|
|
direct_message = Fabricate.build(:direct_message, users: [me])
|
|
|
|
serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false)
|
|
|
|
expect(serializer.users).to eq([me])
|
|
end
|
|
|
|
context "when a user is destroyed" do
|
|
it "returns a placeholder user" do
|
|
me = Fabricate(:user)
|
|
you = Fabricate(:user)
|
|
direct_message = Fabricate(:direct_message, users: [me, you])
|
|
|
|
you.destroy!
|
|
|
|
serializer =
|
|
described_class.new(direct_message.reload, scope: Guardian.new(me), root: false).as_json
|
|
|
|
expect(serializer[:users][0][:username]).to eq(I18n.t("chat.deleted_chat_username"))
|
|
end
|
|
end
|
|
end
|
|
end
|