Add coverage for misc serializers (#32781)

This commit is contained in:
Matt Jankowski 2024-11-07 10:37:26 -05:00 committed by GitHub
parent 41227aeb95
commit 870bb06994
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 344 additions and 0 deletions

View File

@ -0,0 +1,39 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ActivityPub::ActorSerializer do
subject { serialized_record_json(record, described_class) }
describe '#type' do
context 'with the instance actor' do
let(:record) { Account.find(Account::INSTANCE_ACTOR_ID) }
it { is_expected.to include('type' => 'Application') }
end
context 'with an application actor' do
let(:record) { Fabricate :account, actor_type: 'Application' }
it { is_expected.to include('type' => 'Service') }
end
context 'with a service actor' do
let(:record) { Fabricate :account, actor_type: 'Service' }
it { is_expected.to include('type' => 'Service') }
end
context 'with a Group actor' do
let(:record) { Fabricate :account, actor_type: 'Group' }
it { is_expected.to include('type' => 'Group') }
end
context 'with a Person actor' do
let(:record) { Fabricate :account, actor_type: 'Person' }
it { is_expected.to include('type' => 'Person') }
end
end
end

View File

@ -0,0 +1,27 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ActivityPub::AddSerializer do
describe '.serializer_for' do
subject { described_class.serializer_for(model, {}) }
context 'with a Status model' do
let(:model) { Status.new }
it { is_expected.to eq(described_class::UriSerializer) }
end
context 'with a FeaturedTag model' do
let(:model) { FeaturedTag.new }
it { is_expected.to eq(ActivityPub::HashtagSerializer) }
end
context 'with an Array' do
let(:model) { [] }
it { is_expected.to eq(ActiveModel::Serializer::CollectionSerializer) }
end
end
end

View File

@ -0,0 +1,39 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ActivityPub::CollectionSerializer do
describe '.serializer_for' do
subject { described_class.serializer_for(model, {}) }
context 'with a Status model' do
let(:model) { Status.new }
it { is_expected.to eq(ActivityPub::NoteSerializer) }
end
context 'with a FeaturedTag model' do
let(:model) { FeaturedTag.new }
it { is_expected.to eq(ActivityPub::HashtagSerializer) }
end
context 'with an ActivityPub::CollectionPresenter' do
let(:model) { ActivityPub::CollectionPresenter.new }
it { is_expected.to eq(described_class) }
end
context 'with a String' do
let(:model) { '' }
it { is_expected.to eq(described_class::StringSerializer) }
end
context 'with an Array' do
let(:model) { [] }
it { is_expected.to eq(ActiveModel::Serializer::CollectionSerializer) }
end
end
end

View File

@ -0,0 +1,27 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ActivityPub::RemoveSerializer do
describe '.serializer_for' do
subject { described_class.serializer_for(model, {}) }
context 'with a Status model' do
let(:model) { Status.new }
it { is_expected.to eq(described_class::UriSerializer) }
end
context 'with a FeaturedTag model' do
let(:model) { FeaturedTag.new }
it { is_expected.to eq(ActivityPub::HashtagSerializer) }
end
context 'with an Array' do
let(:model) { [] }
it { is_expected.to eq(ActiveModel::Serializer::CollectionSerializer) }
end
end
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::AccountRelationshipSeveranceEventSerializer do
subject { serialized_record_json(record, described_class) }
let(:record) { Fabricate.build :account_relationship_severance_event, id: 123 }
describe 'serialization' do
it 'returns expected values' do
expect(subject)
.to include(
'id' => be_a(String).and(eq('123'))
)
end
end
end

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::AccountWarningSerializer do
subject { serialized_record_json(record, described_class) }
let(:record) { Fabricate :account_warning, id: 123, status_ids: [456, 789] }
describe 'serialization' do
it 'returns expected values' do
expect(subject)
.to include(
'id' => be_a(String).and(eq('123')),
'status_ids' => be_a(Array).and(eq(['456', '789']))
)
end
end
end

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::Admin::AccountSerializer do
subject { serialized_record_json(record, described_class) }
describe 'created_by_application_id' do
context 'when account is application-created' do
let(:record) { Fabricate :account, user: Fabricate(:user, created_by_application: application) }
let(:application) { Fabricate :application }
it { is_expected.to include('created_by_application_id' => application.id.to_s) }
end
end
describe 'invited_by_account_id' do
context 'when account was invited' do
let(:record) { Fabricate :account, user: Fabricate(:user, invite: invite) }
let(:invite) { Fabricate :invite }
it { is_expected.to include('invited_by_account_id' => invite.user.account.id.to_s) }
end
end
end

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::Admin::CohortSerializer do
subject { serialized_record_json(record, described_class) }
let(:record) { Admin::Metrics::Retention.new('2024-01-01', '2024-01-02', 'day').cohorts.first }
describe 'serialization' do
it 'returns expected values' do
expect(subject)
.to include(
'data' => be_a(Array),
'period' => /2024-01-01/
)
end
end
end

View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::Admin::WebhookEventSerializer do
describe '.serializer_for' do
subject { described_class.serializer_for(model, {}) }
context 'with an Account model' do
let(:model) { Account.new }
it { is_expected.to eq(REST::Admin::AccountSerializer) }
end
context 'with a Report model' do
let(:model) { Report.new }
it { is_expected.to eq(REST::Admin::ReportSerializer) }
end
context 'with a Status model' do
let(:model) { Status.new }
it { is_expected.to eq(REST::StatusSerializer) }
end
context 'with an Array' do
let(:model) { [] }
it { is_expected.to eq(ActiveModel::Serializer::CollectionSerializer) }
end
end
end

View File

@ -0,0 +1,27 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::AppealSerializer do
subject { serialized_record_json(record, described_class) }
describe 'state' do
context 'when appeal is approved' do
let(:record) { Fabricate.build :appeal, approved_at: 2.days.ago }
it { is_expected.to include('state' => 'approved') }
end
context 'when appeal is rejected' do
let(:record) { Fabricate.build :appeal, rejected_at: 2.days.ago }
it { is_expected.to include('state' => 'rejected') }
end
context 'when appeal is not approved or rejected' do
let(:record) { Fabricate.build :appeal, approved_at: nil, rejected_at: nil }
it { is_expected.to include('state' => 'pending') }
end
end
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::CustomEmojiSerializer do
subject { serialized_record_json(record, described_class) }
let(:record) { Fabricate.build :custom_emoji, id: 123, category: Fabricate(:custom_emoji_category, name: 'Category Name') }
describe 'serialization' do
it 'returns expected values' do
expect(subject)
.to include(
'category' => be_a(String).and(eq('Category Name'))
)
end
end
end

View File

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::ExtendedDescriptionSerializer do
subject { serialized_record_json(record, described_class) }
describe 'serialization' do
context 'with text present' do
let(:record) { ExtendedDescription.new text: 'Hello world', updated_at: Date.new(2024, 1, 1) }
it 'returns expected values' do
expect(subject)
.to include(
'content' => eq(<<~HTML),
<p>Hello world</p>
HTML
'updated_at' => eq('2024-01-01')
)
end
end
context 'with text missing' do
let(:record) { ExtendedDescription.new text: nil, updated_at: Date.new(2024, 1, 1) }
it 'returns expected values' do
expect(subject)
.to include(
'content' => eq(''),
'updated_at' => eq('2024-01-01')
)
end
end
end
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe REST::RuleSerializer do
subject { serialized_record_json(record, described_class) }
let(:record) { Fabricate.build :rule, id: 123 }
describe 'serialization' do
it 'returns expected values' do
expect(subject)
.to include(
'id' => be_a(String).and(eq('123'))
)
end
end
end