Consolidate JSON parsing in serializers specs (#27693)

main
Matt Jankowski 2023-11-07 10:20:24 -05:00 committed by GitHub
parent a688a9ed20
commit 1d51e10510
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 25 additions and 84 deletions

View File

@ -8,13 +8,11 @@ describe EmojisController do
let(:emoji) { Fabricate(:custom_emoji) } let(:emoji) { Fabricate(:custom_emoji) }
describe 'GET #show' do describe 'GET #show' do
subject(:body) { JSON.parse(response.body, symbolize_names: true) }
let(:response) { get :show, params: { id: emoji.id, format: :json } } let(:response) { get :show, params: { id: emoji.id, format: :json } }
it 'returns the right response' do it 'returns the right response' do
expect(response).to have_http_status 200 expect(response).to have_http_status 200
expect(body[:name]).to eq ':coolcat:' expect(body_as_json[:name]).to eq ':coolcat:'
end end
end end
end end

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe ActivityPub::DeviceSerializer do describe ActivityPub::DeviceSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { Fabricate(:device) } let(:record) { Fabricate(:device) }
describe 'type' do describe 'type' do

View File

@ -3,7 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe ActivityPub::NoteSerializer do 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!(:account) { Fabricate(:account) }
let!(:other) { 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_third) { Fabricate(:status, account: account, thread: parent, visibility: :public) }
let!(:reply_by_account_visibility_direct) { Fabricate(:status, account: account, thread: parent, visibility: :direct) } 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 it 'has the expected shape' do
expect(subject).to include({ expect(subject).to include({
'@context' => include('https://www.w3.org/ns/activitystreams'), '@context' => include('https://www.w3.org/ns/activitystreams'),

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe ActivityPub::OneTimeKeySerializer do describe ActivityPub::OneTimeKeySerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { Fabricate(:one_time_key) } let(:record) { Fabricate(:one_time_key) }
describe 'type' do describe 'type' do

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe ActivityPub::UndoLikeSerializer do describe ActivityPub::UndoLikeSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { Fabricate(:favourite) } let(:record) { Fabricate(:favourite) }
describe 'type' do describe 'type' do

View File

@ -3,16 +3,12 @@
require 'rails_helper' require 'rails_helper'
describe ActivityPub::UpdatePollSerializer do 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(:account) { Fabricate(:account) }
let(:poll) { Fabricate(:poll, account: account) } let(:poll) { Fabricate(:poll, account: account) }
let!(:status) { Fabricate(:status, account: account, poll: poll) } 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 it 'has a Update type' do
expect(subject['type']).to eql('Update') expect(subject['type']).to eql('Update')
end end

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe ActivityPub::VoteSerializer do describe ActivityPub::VoteSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { Fabricate(:poll_vote) } let(:record) { Fabricate(:poll_vote) }
describe 'type' do describe 'type' do

View File

@ -3,7 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe REST::AccountSerializer do 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(:role) { Fabricate(:user_role, name: 'Role', highlighted: true) }
let(:user) { Fabricate(:user, role: role) } let(:user) { Fabricate(:user, role: role) }

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe REST::EncryptedMessageSerializer do describe REST::EncryptedMessageSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { Fabricate(:encrypted_message) } let(:record) { Fabricate(:encrypted_message) }
describe 'account' do describe 'account' do

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe REST::InstanceSerializer do describe REST::InstanceSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { InstancePresenter.new } let(:record) { InstancePresenter.new }
describe 'usage' do describe 'usage' do

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe REST::Keys::ClaimResultSerializer do describe REST::Keys::ClaimResultSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { Keys::ClaimService::Result.new(Account.new(id: 123), 456) } let(:record) { Keys::ClaimService::Result.new(Account.new(id: 123), 456) }
describe 'account' do describe 'account' do

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe REST::Keys::DeviceSerializer do describe REST::Keys::DeviceSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { Device.new(name: 'Device name') } let(:record) { Device.new(name: 'Device name') }
describe 'name' do describe 'name' do

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe REST::Keys::QueryResultSerializer do describe REST::Keys::QueryResultSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) { Keys::QueryService::Result.new(Account.new(id: 123), []) } let(:record) { Keys::QueryService::Result.new(Account.new(id: 123), []) }
describe 'account' do describe 'account' do

View File

@ -3,13 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe REST::SuggestionSerializer do describe REST::SuggestionSerializer do
let(:serialization) do let(:serialization) { serialized_record_json(record, described_class) }
JSON.parse(
ActiveModelSerializers::SerializableResource.new(
record, serializer: described_class
).to_json
)
end
let(:record) do let(:record) do
AccountSuggestions::Suggestion.new( AccountSuggestions::Suggestion.new(
account: account, account: account,

View File

@ -52,6 +52,17 @@ def json_str_to_hash(str)
JSON.parse(str, symbolize_names: true) JSON.parse(str, symbolize_names: true)
end 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) def expect_push_bulk_to_match(klass, matcher)
allow(Sidekiq::Client).to receive(:push_bulk) allow(Sidekiq::Client).to receive(:push_bulk)
yield yield