From 1d51e10510115a22c4f68b28facb03266130da59 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 7 Nov 2023 10:20:24 -0500 Subject: [PATCH] Consolidate JSON parsing in serializers specs (#27693) --- spec/controllers/emojis_controller_spec.rb | 4 +--- .../serializers/activitypub/device_serializer_spec.rb | 8 +------- spec/serializers/activitypub/note_serializer_spec.rb | 6 +----- .../activitypub/one_time_key_serializer_spec.rb | 8 +------- .../activitypub/undo_like_serializer_spec.rb | 8 +------- .../activitypub/update_poll_serializer_spec.rb | 6 +----- spec/serializers/activitypub/vote_serializer_spec.rb | 8 +------- spec/serializers/rest/account_serializer_spec.rb | 2 +- .../rest/encrypted_message_serializer_spec.rb | 8 +------- spec/serializers/rest/instance_serializer_spec.rb | 8 +------- .../rest/keys/claim_result_serializer_spec.rb | 8 +------- spec/serializers/rest/keys/device_serializer_spec.rb | 8 +------- .../rest/keys/query_result_serializer_spec.rb | 8 +------- spec/serializers/rest/suggestion_serializer_spec.rb | 8 +------- spec/spec_helper.rb | 11 +++++++++++ 15 files changed, 25 insertions(+), 84 deletions(-) diff --git a/spec/controllers/emojis_controller_spec.rb b/spec/controllers/emojis_controller_spec.rb index 249dfd9d51..3fe19ee5ce 100644 --- a/spec/controllers/emojis_controller_spec.rb +++ b/spec/controllers/emojis_controller_spec.rb @@ -8,13 +8,11 @@ describe EmojisController do let(:emoji) { Fabricate(:custom_emoji) } describe 'GET #show' do - subject(:body) { JSON.parse(response.body, symbolize_names: true) } - let(:response) { get :show, params: { id: emoji.id, format: :json } } it 'returns the right response' do expect(response).to have_http_status 200 - expect(body[:name]).to eq ':coolcat:' + expect(body_as_json[:name]).to eq ':coolcat:' end end end diff --git a/spec/serializers/activitypub/device_serializer_spec.rb b/spec/serializers/activitypub/device_serializer_spec.rb index 2a3be82121..23f0b24c4e 100644 --- a/spec/serializers/activitypub/device_serializer_spec.rb +++ b/spec/serializers/activitypub/device_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe ActivityPub::DeviceSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { Fabricate(:device) } describe 'type' do diff --git a/spec/serializers/activitypub/note_serializer_spec.rb b/spec/serializers/activitypub/note_serializer_spec.rb index 311a4573d9..338d66b308 100644 --- a/spec/serializers/activitypub/note_serializer_spec.rb +++ b/spec/serializers/activitypub/note_serializer_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' describe ActivityPub::NoteSerializer do - subject { JSON.parse(@serialization.to_json) } + subject { serialized_record_json(parent, described_class, adapter: ActivityPub::Adapter) } let!(:account) { Fabricate(:account) } let!(:other) { Fabricate(:account) } @@ -14,10 +14,6 @@ describe ActivityPub::NoteSerializer do let!(:reply_by_account_third) { Fabricate(:status, account: account, thread: parent, visibility: :public) } let!(:reply_by_account_visibility_direct) { Fabricate(:status, account: account, thread: parent, visibility: :direct) } - before do - @serialization = ActiveModelSerializers::SerializableResource.new(parent, serializer: described_class, adapter: ActivityPub::Adapter) - end - it 'has the expected shape' do expect(subject).to include({ '@context' => include('https://www.w3.org/ns/activitystreams'), diff --git a/spec/serializers/activitypub/one_time_key_serializer_spec.rb b/spec/serializers/activitypub/one_time_key_serializer_spec.rb index 6fe1f06185..89efe95c8c 100644 --- a/spec/serializers/activitypub/one_time_key_serializer_spec.rb +++ b/spec/serializers/activitypub/one_time_key_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe ActivityPub::OneTimeKeySerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { Fabricate(:one_time_key) } describe 'type' do diff --git a/spec/serializers/activitypub/undo_like_serializer_spec.rb b/spec/serializers/activitypub/undo_like_serializer_spec.rb index 43cf7192e4..3d61e86751 100644 --- a/spec/serializers/activitypub/undo_like_serializer_spec.rb +++ b/spec/serializers/activitypub/undo_like_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe ActivityPub::UndoLikeSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { Fabricate(:favourite) } describe 'type' do diff --git a/spec/serializers/activitypub/update_poll_serializer_spec.rb b/spec/serializers/activitypub/update_poll_serializer_spec.rb index 9a13fdd576..8ff4fd2701 100644 --- a/spec/serializers/activitypub/update_poll_serializer_spec.rb +++ b/spec/serializers/activitypub/update_poll_serializer_spec.rb @@ -3,16 +3,12 @@ require 'rails_helper' describe ActivityPub::UpdatePollSerializer do - subject { JSON.parse(@serialization.to_json) } + subject { serialized_record_json(status, described_class, adapter: ActivityPub::Adapter) } let(:account) { Fabricate(:account) } let(:poll) { Fabricate(:poll, account: account) } let!(:status) { Fabricate(:status, account: account, poll: poll) } - before do - @serialization = ActiveModelSerializers::SerializableResource.new(status, serializer: described_class, adapter: ActivityPub::Adapter) - end - it 'has a Update type' do expect(subject['type']).to eql('Update') end diff --git a/spec/serializers/activitypub/vote_serializer_spec.rb b/spec/serializers/activitypub/vote_serializer_spec.rb index c329542d79..b7c0b8928b 100644 --- a/spec/serializers/activitypub/vote_serializer_spec.rb +++ b/spec/serializers/activitypub/vote_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe ActivityPub::VoteSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { Fabricate(:poll_vote) } describe 'type' do diff --git a/spec/serializers/rest/account_serializer_spec.rb b/spec/serializers/rest/account_serializer_spec.rb index e399e88f37..15939e484d 100644 --- a/spec/serializers/rest/account_serializer_spec.rb +++ b/spec/serializers/rest/account_serializer_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' describe REST::AccountSerializer do - subject { JSON.parse(ActiveModelSerializers::SerializableResource.new(account, serializer: described_class).to_json) } + subject { serialized_record_json(account, described_class) } let(:role) { Fabricate(:user_role, name: 'Role', highlighted: true) } let(:user) { Fabricate(:user, role: role) } diff --git a/spec/serializers/rest/encrypted_message_serializer_spec.rb b/spec/serializers/rest/encrypted_message_serializer_spec.rb index e0e70a3b84..01db1149af 100644 --- a/spec/serializers/rest/encrypted_message_serializer_spec.rb +++ b/spec/serializers/rest/encrypted_message_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe REST::EncryptedMessageSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { Fabricate(:encrypted_message) } describe 'account' do diff --git a/spec/serializers/rest/instance_serializer_spec.rb b/spec/serializers/rest/instance_serializer_spec.rb index 15a5de18dd..8ac32f2247 100644 --- a/spec/serializers/rest/instance_serializer_spec.rb +++ b/spec/serializers/rest/instance_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe REST::InstanceSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { InstancePresenter.new } describe 'usage' do diff --git a/spec/serializers/rest/keys/claim_result_serializer_spec.rb b/spec/serializers/rest/keys/claim_result_serializer_spec.rb index cf9416f032..7f7fb850cd 100644 --- a/spec/serializers/rest/keys/claim_result_serializer_spec.rb +++ b/spec/serializers/rest/keys/claim_result_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe REST::Keys::ClaimResultSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { Keys::ClaimService::Result.new(Account.new(id: 123), 456) } describe 'account' do diff --git a/spec/serializers/rest/keys/device_serializer_spec.rb b/spec/serializers/rest/keys/device_serializer_spec.rb index c15e197cb8..28177a3db5 100644 --- a/spec/serializers/rest/keys/device_serializer_spec.rb +++ b/spec/serializers/rest/keys/device_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe REST::Keys::DeviceSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { Device.new(name: 'Device name') } describe 'name' do diff --git a/spec/serializers/rest/keys/query_result_serializer_spec.rb b/spec/serializers/rest/keys/query_result_serializer_spec.rb index 983780ae98..ef67d70675 100644 --- a/spec/serializers/rest/keys/query_result_serializer_spec.rb +++ b/spec/serializers/rest/keys/query_result_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe REST::Keys::QueryResultSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) { Keys::QueryService::Result.new(Account.new(id: 123), []) } describe 'account' do diff --git a/spec/serializers/rest/suggestion_serializer_spec.rb b/spec/serializers/rest/suggestion_serializer_spec.rb index b3c086208d..60420d8023 100644 --- a/spec/serializers/rest/suggestion_serializer_spec.rb +++ b/spec/serializers/rest/suggestion_serializer_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe REST::SuggestionSerializer do - let(:serialization) do - JSON.parse( - ActiveModelSerializers::SerializableResource.new( - record, serializer: described_class - ).to_json - ) - end + let(:serialization) { serialized_record_json(record, described_class) } let(:record) do AccountSuggestions::Suggestion.new( account: account, diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4c4e31dd2a..39c4abe5c9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -52,6 +52,17 @@ def json_str_to_hash(str) JSON.parse(str, symbolize_names: true) end +def serialized_record_json(record, serializer, adapter: nil) + options = { serializer: serializer } + options[:adapter] = adapter if adapter.present? + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, + options + ).to_json + ) +end + def expect_push_bulk_to_match(klass, matcher) allow(Sidekiq::Client).to receive(:push_bulk) yield