From 14af5b47ac2230937e9244bec41390301c7b0067 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 4 Sep 2024 01:12:40 -0400 Subject: [PATCH] Add coverage for model normalizations (#31734) --- spec/models/account_alias_spec.rb | 11 +++++++ spec/models/account_migration_spec.rb | 6 ++++ spec/models/account_spec.rb | 11 +++---- spec/models/account_warning_spec.rb | 11 +++++++ spec/models/custom_emoji_spec.rb | 19 ++----------- spec/models/custom_filter_spec.rb | 6 ++-- spec/models/featured_tag_spec.rb | 11 +++++++ spec/models/relay_spec.rb | 11 +++++++ spec/models/user_spec.rb | 41 +++++---------------------- spec/models/webhook_spec.rb | 6 ++-- 10 files changed, 70 insertions(+), 63 deletions(-) create mode 100644 spec/models/account_alias_spec.rb create mode 100644 spec/models/account_warning_spec.rb create mode 100644 spec/models/featured_tag_spec.rb create mode 100644 spec/models/relay_spec.rb diff --git a/spec/models/account_alias_spec.rb b/spec/models/account_alias_spec.rb new file mode 100644 index 0000000000..fc8c6bd250 --- /dev/null +++ b/spec/models/account_alias_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AccountAlias do + describe 'Normalizations' do + describe 'acct' do + it { is_expected.to normalize(:acct).from(' @username@domain ').to('username@domain') } + end + end +end diff --git a/spec/models/account_migration_spec.rb b/spec/models/account_migration_spec.rb index 1f32c6082e..d658915ce3 100644 --- a/spec/models/account_migration_spec.rb +++ b/spec/models/account_migration_spec.rb @@ -3,6 +3,12 @@ require 'rails_helper' RSpec.describe AccountMigration do + describe 'Normalizations' do + describe 'acct' do + it { is_expected.to normalize(:acct).from(' @username@domain ').to('username@domain') } + end + end + describe 'validations' do subject { described_class.new(account: source_account, acct: target_acct) } diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 83f1585b61..27707fa897 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -723,14 +723,15 @@ RSpec.describe Account do end end + describe 'Normalizations' do + describe 'username' do + it { is_expected.to normalize(:username).from(" \u3000bob \t \u00a0 \n ").to('bob') } + end + end + describe 'validations' do it { is_expected.to validate_presence_of(:username) } - it 'squishes the username before validation' do - account = Fabricate(:account, domain: nil, username: " \u3000bob \t \u00a0 \n ") - expect(account.username).to eq 'bob' - end - context 'when is local' do it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do _account = Fabricate(:account, username: 'the_doctor') diff --git a/spec/models/account_warning_spec.rb b/spec/models/account_warning_spec.rb new file mode 100644 index 0000000000..37866ce3da --- /dev/null +++ b/spec/models/account_warning_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AccountWarning do + describe 'Normalizations' do + describe 'text' do + it { is_expected.to normalize(:text).from(nil).to('') } + end + end +end diff --git a/spec/models/custom_emoji_spec.rb b/spec/models/custom_emoji_spec.rb index cb8cb5c11b..87b111441a 100644 --- a/spec/models/custom_emoji_spec.rb +++ b/spec/models/custom_emoji_spec.rb @@ -79,22 +79,9 @@ RSpec.describe CustomEmoji, :attachment_processing do end describe 'Normalizations' do - describe 'downcase domain value' do - context 'with a mixed case domain value' do - it 'normalizes the value to downcased' do - custom_emoji = Fabricate.build(:custom_emoji, domain: 'wWw.MaStOdOn.CoM') - - expect(custom_emoji.domain).to eq('www.mastodon.com') - end - end - - context 'with a nil domain value' do - it 'leaves the value as nil' do - custom_emoji = Fabricate.build(:custom_emoji, domain: nil) - - expect(custom_emoji.domain).to be_nil - end - end + describe 'domain' do + it { is_expected.to normalize(:domain).from('wWw.MaStOdOn.CoM').to('www.mastodon.com') } + it { is_expected.to normalize(:domain).from(nil).to(nil) } end end end diff --git a/spec/models/custom_filter_spec.rb b/spec/models/custom_filter_spec.rb index 8ac9dbb896..5bb615bb37 100644 --- a/spec/models/custom_filter_spec.rb +++ b/spec/models/custom_filter_spec.rb @@ -34,10 +34,8 @@ RSpec.describe CustomFilter do end describe 'Normalizations' do - it 'cleans up context values' do - record = described_class.new(context: ['home', 'notifications', 'public ', '']) - - expect(record.context).to eq(%w(home notifications public)) + describe 'context' do + it { is_expected.to normalize(:context).from(['home', 'notifications', 'public ', '']).to(%w(home notifications public)) } end end end diff --git a/spec/models/featured_tag_spec.rb b/spec/models/featured_tag_spec.rb new file mode 100644 index 0000000000..6056e645e0 --- /dev/null +++ b/spec/models/featured_tag_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe FeaturedTag do + describe 'Normalizations' do + describe 'name' do + it { is_expected.to normalize(:name).from(' #hashtag ').to('hashtag') } + end + end +end diff --git a/spec/models/relay_spec.rb b/spec/models/relay_spec.rb new file mode 100644 index 0000000000..4b95c596e4 --- /dev/null +++ b/spec/models/relay_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Relay do + describe 'Normalizations' do + describe 'inbox_url' do + it { is_expected.to normalize(:inbox_url).from(' http://host.example ').to('http://host.example') } + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0f3e25576e..5c2af4dc39 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -59,45 +59,18 @@ RSpec.describe User do describe 'Normalizations' do describe 'locale' do - it 'preserves valid locale' do - user = Fabricate.build(:user, locale: 'en') - - expect(user.locale).to eq('en') - end - - it 'cleans out invalid locale' do - user = Fabricate.build(:user, locale: 'toto') - - expect(user.locale).to be_nil - end + it { is_expected.to_not normalize(:locale).from('en') } + it { is_expected.to normalize(:locale).from('toto').to(nil) } end describe 'time_zone' do - it 'preserves valid timezone' do - user = Fabricate.build(:user, time_zone: 'UTC') - - expect(user.time_zone).to eq('UTC') - end - - it 'cleans out invalid timezone' do - user = Fabricate.build(:user, time_zone: 'toto') - - expect(user.time_zone).to be_nil - end + it { is_expected.to_not normalize(:time_zone).from('UTC') } + it { is_expected.to normalize(:time_zone).from('toto').to(nil) } end - describe 'languages' do - it 'preserves valid options for languages' do - user = Fabricate.build(:user, chosen_languages: ['en', 'fr', '']) - - expect(user.chosen_languages).to eq(['en', 'fr']) - end - - it 'cleans out empty string from languages' do - user = Fabricate.build(:user, chosen_languages: ['']) - - expect(user.chosen_languages).to be_nil - end + describe 'chosen_languages' do + it { is_expected.to normalize(:chosen_languages).from(['en', 'fr', '']).to(%w(en fr)) } + it { is_expected.to normalize(:chosen_languages).from(['']).to(nil) } end end diff --git a/spec/models/webhook_spec.rb b/spec/models/webhook_spec.rb index 864baf2e1a..1b2d803bd7 100644 --- a/spec/models/webhook_spec.rb +++ b/spec/models/webhook_spec.rb @@ -29,10 +29,8 @@ RSpec.describe Webhook do end describe 'Normalizations' do - it 'cleans up events values' do - record = described_class.new(events: ['account.approved', 'account.created ', '']) - - expect(record.events).to eq(%w(account.approved account.created)) + describe 'events' do + it { is_expected.to normalize(:events).from(['account.approved', 'account.created ', '']).to(%w(account.approved account.created)) } end end