From 870bb06994753023cde350bc94c4c317091d3d7b Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 7 Nov 2024 10:37:26 -0500 Subject: [PATCH] Add coverage for misc serializers (#32781) --- .../activitypub/actor_serializer_spec.rb | 39 +++++++++++++++++++ .../activitypub/add_serializer_spec.rb | 27 +++++++++++++ .../activitypub/collection_serializer_spec.rb | 39 +++++++++++++++++++ .../activitypub/remove_serializer_spec.rb | 27 +++++++++++++ ...ionship_severance_event_serializer_spec.rb | 18 +++++++++ .../rest/account_warning_serializer_spec.rb | 19 +++++++++ .../rest/admin/account_serializer_spec.rb | 25 ++++++++++++ .../rest/admin/cohort_serializer_spec.rb | 19 +++++++++ .../admin/webhook_event_serializer_spec.rb | 33 ++++++++++++++++ .../rest/appeal_serializer_spec.rb | 27 +++++++++++++ .../rest/custom_emoji_serializer_spec.rb | 18 +++++++++ .../extended_description_serializer_spec.rb | 35 +++++++++++++++++ spec/serializers/rest/rule_serializer_spec.rb | 18 +++++++++ 13 files changed, 344 insertions(+) create mode 100644 spec/serializers/activitypub/actor_serializer_spec.rb create mode 100644 spec/serializers/activitypub/add_serializer_spec.rb create mode 100644 spec/serializers/activitypub/collection_serializer_spec.rb create mode 100644 spec/serializers/activitypub/remove_serializer_spec.rb create mode 100644 spec/serializers/rest/account_relationship_severance_event_serializer_spec.rb create mode 100644 spec/serializers/rest/account_warning_serializer_spec.rb create mode 100644 spec/serializers/rest/admin/account_serializer_spec.rb create mode 100644 spec/serializers/rest/admin/cohort_serializer_spec.rb create mode 100644 spec/serializers/rest/admin/webhook_event_serializer_spec.rb create mode 100644 spec/serializers/rest/appeal_serializer_spec.rb create mode 100644 spec/serializers/rest/custom_emoji_serializer_spec.rb create mode 100644 spec/serializers/rest/extended_description_serializer_spec.rb create mode 100644 spec/serializers/rest/rule_serializer_spec.rb diff --git a/spec/serializers/activitypub/actor_serializer_spec.rb b/spec/serializers/activitypub/actor_serializer_spec.rb new file mode 100644 index 0000000000..ad24455953 --- /dev/null +++ b/spec/serializers/activitypub/actor_serializer_spec.rb @@ -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 diff --git a/spec/serializers/activitypub/add_serializer_spec.rb b/spec/serializers/activitypub/add_serializer_spec.rb new file mode 100644 index 0000000000..3b3eaeb1b0 --- /dev/null +++ b/spec/serializers/activitypub/add_serializer_spec.rb @@ -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 diff --git a/spec/serializers/activitypub/collection_serializer_spec.rb b/spec/serializers/activitypub/collection_serializer_spec.rb new file mode 100644 index 0000000000..7726df914f --- /dev/null +++ b/spec/serializers/activitypub/collection_serializer_spec.rb @@ -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 diff --git a/spec/serializers/activitypub/remove_serializer_spec.rb b/spec/serializers/activitypub/remove_serializer_spec.rb new file mode 100644 index 0000000000..0e4b199838 --- /dev/null +++ b/spec/serializers/activitypub/remove_serializer_spec.rb @@ -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 diff --git a/spec/serializers/rest/account_relationship_severance_event_serializer_spec.rb b/spec/serializers/rest/account_relationship_severance_event_serializer_spec.rb new file mode 100644 index 0000000000..95b4d7dcd2 --- /dev/null +++ b/spec/serializers/rest/account_relationship_severance_event_serializer_spec.rb @@ -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 diff --git a/spec/serializers/rest/account_warning_serializer_spec.rb b/spec/serializers/rest/account_warning_serializer_spec.rb new file mode 100644 index 0000000000..0f335d1215 --- /dev/null +++ b/spec/serializers/rest/account_warning_serializer_spec.rb @@ -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 diff --git a/spec/serializers/rest/admin/account_serializer_spec.rb b/spec/serializers/rest/admin/account_serializer_spec.rb new file mode 100644 index 0000000000..58f58a997b --- /dev/null +++ b/spec/serializers/rest/admin/account_serializer_spec.rb @@ -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 diff --git a/spec/serializers/rest/admin/cohort_serializer_spec.rb b/spec/serializers/rest/admin/cohort_serializer_spec.rb new file mode 100644 index 0000000000..ed6067c0d2 --- /dev/null +++ b/spec/serializers/rest/admin/cohort_serializer_spec.rb @@ -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 diff --git a/spec/serializers/rest/admin/webhook_event_serializer_spec.rb b/spec/serializers/rest/admin/webhook_event_serializer_spec.rb new file mode 100644 index 0000000000..3cbfbd92a3 --- /dev/null +++ b/spec/serializers/rest/admin/webhook_event_serializer_spec.rb @@ -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 diff --git a/spec/serializers/rest/appeal_serializer_spec.rb b/spec/serializers/rest/appeal_serializer_spec.rb new file mode 100644 index 0000000000..1ae6617de5 --- /dev/null +++ b/spec/serializers/rest/appeal_serializer_spec.rb @@ -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 diff --git a/spec/serializers/rest/custom_emoji_serializer_spec.rb b/spec/serializers/rest/custom_emoji_serializer_spec.rb new file mode 100644 index 0000000000..a694ca515a --- /dev/null +++ b/spec/serializers/rest/custom_emoji_serializer_spec.rb @@ -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 diff --git a/spec/serializers/rest/extended_description_serializer_spec.rb b/spec/serializers/rest/extended_description_serializer_spec.rb new file mode 100644 index 0000000000..d7bd078e1d --- /dev/null +++ b/spec/serializers/rest/extended_description_serializer_spec.rb @@ -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), +

Hello world

+ 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 diff --git a/spec/serializers/rest/rule_serializer_spec.rb b/spec/serializers/rest/rule_serializer_spec.rb new file mode 100644 index 0000000000..4d801e77d3 --- /dev/null +++ b/spec/serializers/rest/rule_serializer_spec.rb @@ -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