From 7bfcdde9eb97189a4fe0acd3f4fb318885cc531d Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 2 Jan 2025 16:36:13 +0000 Subject: [PATCH] specs --- spec/models/notification_spec.rb | 57 +++++-------------- .../notification_serializer_spec.rb | 19 ++++++- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index edfa6a0584a..af0f45ff73a 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -806,52 +806,21 @@ RSpec.describe Notification do ) expect(notification.shelved_notification).to be_nil end - end - describe ".populate_acting_user" do - SiteSetting.enable_names = true + describe ".acting_user" do + fab!(:users) { Fabricate.times(6, :user) } + fab!(:notifications) do + [ + { username: users[0].username }, + { display_username: users[1].username }, + { mentioned_by_username: users[2].username }, + { invited_by_username: users[3].username }, + { original_username: users[4].username }, + ].map { |data| Fabricate(:notification, user: user, data: data.to_json) } + end - fab!(:user1) { Fabricate(:user) } - fab!(:user2) { Fabricate(:user) } - fab!(:user3) { Fabricate(:user) } - fab!(:user4) { Fabricate(:user) } - fab!(:user5) { Fabricate(:user) } - fab!(:user6) { Fabricate(:user) } - fab!(:notification1) do - Fabricate(:notification, user: user, data: { username: user1.username }.to_json) - end - fab!(:notification2) do - Fabricate(:notification, user: user, data: { display_username: user2.username }.to_json) - end - fab!(:notification3) do - Fabricate(:notification, user: user, data: { mentioned_by_username: user3.username }.to_json) - end - fab!(:notification4) do - Fabricate(:notification, user: user, data: { invited_by_username: user4.username }.to_json) - end - fab!(:notification5) do - Fabricate(:notification, user: user, data: { original_username: user5.username }.to_json) - end - fab!(:notification6) do - Fabricate(:notification, user: user, data: { original_username: user6.username }.to_json) - end - - it "Sets the acting_user correctly for each notification" do - # TODO: remove this spec - expect(notification1.acting_user).to eq(user1) - expect(notification2.acting_user).to eq(user2) - expect(notification3.acting_user).to eq(user3) - expect(notification4.acting_user).to eq(user4) - expect(notification5.acting_user).to eq(user5) - expect(notification5.data_hash[:original_name]).to eq user5.name - end - - context "with SiteSettings.enable_names=false" do - it "doesn't set the :original_name property" do - SiteSetting.enable_names = false - # todo: refactor spec - expect(notification6.data_hash[:original_name]).to be_nil - SiteSetting.enable_names = true + it "sets the acting_user correctly for each notification" do + 5.times { |i| expect(notifications[i].acting_user).to eq(users[i]) } end end end diff --git a/spec/serializers/notification_serializer_spec.rb b/spec/serializers/notification_serializer_spec.rb index 76fbf1db7c9..c301245cb10 100644 --- a/spec/serializers/notification_serializer_spec.rb +++ b/spec/serializers/notification_serializer_spec.rb @@ -3,7 +3,14 @@ RSpec.describe NotificationSerializer do describe "#as_json" do fab!(:user) - let(:notification) { Fabricate(:notification, user: user) } + fab!(:acting_user) { Fabricate(:user) } + fab!(:notification) do + Fabricate( + :notification, + user: user, + data: { original_username: acting_user.username }.to_json, + ) + end let(:serializer) { NotificationSerializer.new(notification) } let(:json) { serializer.as_json } @@ -14,6 +21,16 @@ RSpec.describe NotificationSerializer do it "does not include external_id when sso is disabled" do expect(json[:notification].key?(:external_id)).to eq(false) end + + it "includes original_name when enable_names=true" do + SiteSetting.enable_names = true + expect(json.dig(:notification, :data, :original_name)).to eq(acting_user.name) + end + + it "excludes original_name when enable_names=false" do + SiteSetting.enable_names = false + expect(json.dig(:notification, :data, :original_name)).to be_nil + end end describe "#sso_enabled" do