Consolidate JSON parsing in serializers specs (#27693)

This commit is contained in:
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) }
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

View File

@ -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

View File

@ -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'),

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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) }

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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