From 84cc805caea566d4fb0fafce411cd07f83cfd0e2 Mon Sep 17 00:00:00 2001 From: Nick Schonning Date: Tue, 21 Feb 2023 19:55:31 -0500 Subject: Enable Style/FrozenStringLiteralComment for specs (#23790) --- spec/helpers/statuses_helper_spec.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'spec/helpers/statuses_helper_spec.rb') diff --git a/spec/helpers/statuses_helper_spec.rb b/spec/helpers/statuses_helper_spec.rb index cba659bfb..ce2a4680e 100644 --- a/spec/helpers/statuses_helper_spec.rb +++ b/spec/helpers/statuses_helper_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe StatusesHelper, type: :helper do -- cgit From 2f606ba1220edf29e805296939f4e5612721bbf0 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Sat, 4 Mar 2023 10:58:11 -0500 Subject: Helpers specs coverage improvement (#23937) --- .../admin/account_moderation_notes_helper_spec.rb | 10 ++-- spec/helpers/admin/dashboard_helper_spec.rb | 69 ++++++++++++++++++++++ spec/helpers/languages_helper_spec.rb | 48 +++++++++++++-- spec/helpers/settings_helper_spec.rb | 37 ++++++++++++ spec/helpers/statuses_helper_spec.rb | 62 +++++++++++++++++++ 5 files changed, 216 insertions(+), 10 deletions(-) create mode 100644 spec/helpers/admin/dashboard_helper_spec.rb create mode 100644 spec/helpers/settings_helper_spec.rb (limited to 'spec/helpers/statuses_helper_spec.rb') diff --git a/spec/helpers/admin/account_moderation_notes_helper_spec.rb b/spec/helpers/admin/account_moderation_notes_helper_spec.rb index 622ce8806..e01eba51d 100644 --- a/spec/helpers/admin/account_moderation_notes_helper_spec.rb +++ b/spec/helpers/admin/account_moderation_notes_helper_spec.rb @@ -42,13 +42,11 @@ RSpec.describe Admin::AccountModerationNotesHelper, type: :helper do let(:account) { Fabricate(:account) } it 'calls #link_to' do - expect(helper).to receive(:link_to).with( - admin_account_path(account.id), - class: name_tag_classes(account, true), - title: account.acct - ) + result = helper.admin_account_inline_link_to(account) - helper.admin_account_inline_link_to(account) + expect(result).to match(name_tag_classes(account, true)) + expect(result).to match(account.acct) + expect(result).to match(admin_account_path(account.id)) end end end diff --git a/spec/helpers/admin/dashboard_helper_spec.rb b/spec/helpers/admin/dashboard_helper_spec.rb new file mode 100644 index 000000000..59062e483 --- /dev/null +++ b/spec/helpers/admin/dashboard_helper_spec.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Admin::DashboardHelper do + describe 'relevant_account_timestamp' do + context 'with an account with older sign in' do + let(:account) { Fabricate(:account) } + let(:stamp) { 10.days.ago } + + it 'returns a time element' do + account.user.update(current_sign_in_at: stamp) + result = helper.relevant_account_timestamp(account) + + expect(result).to match('time-ago') + expect(result).to match(I18n.l(stamp)) + end + end + + context 'with an account with newer sign in' do + let(:account) { Fabricate(:account) } + + it 'returns a time element' do + account.user.update(current_sign_in_at: 10.hours.ago) + result = helper.relevant_account_timestamp(account) + + expect(result).to eq(I18n.t('generic.today')) + end + end + + context 'with an account where the user is pending' do + let(:account) { Fabricate(:account) } + + it 'returns a time element' do + account.user.update(current_sign_in_at: nil) + account.user.update(approved: false) + result = helper.relevant_account_timestamp(account) + + expect(result).to match('time-ago') + expect(result).to match(I18n.l(account.user.created_at)) + end + end + + context 'with an account with a last status value' do + let(:account) { Fabricate(:account) } + let(:stamp) { 5.minutes.ago } + + it 'returns a time element' do + account.user.update(current_sign_in_at: nil) + account.account_stat.update(last_status_at: stamp) + result = helper.relevant_account_timestamp(account) + + expect(result).to match('time-ago') + expect(result).to match(I18n.l(stamp)) + end + end + + context 'with an account without sign in or last status or pending' do + let(:account) { Fabricate(:account) } + + it 'returns a time element' do + account.user.update(current_sign_in_at: nil) + result = helper.relevant_account_timestamp(account) + + expect(result).to eq('-') + end + end + end +end diff --git a/spec/helpers/languages_helper_spec.rb b/spec/helpers/languages_helper_spec.rb index 217c9b239..98c8064a3 100644 --- a/spec/helpers/languages_helper_spec.rb +++ b/spec/helpers/languages_helper_spec.rb @@ -10,14 +10,54 @@ describe LanguagesHelper do end describe 'native_locale_name' do - it 'finds the human readable native name from a key' do - expect(helper.native_locale_name(:de)).to eq('Deutsch') + context 'with a blank locale' do + it 'defaults to a generic value' do + expect(helper.native_locale_name(nil)).to eq(I18n.t('generic.none')) + end + end + + context 'with a locale of `und`' do + it 'defaults to a generic value' do + expect(helper.native_locale_name('und')).to eq(I18n.t('generic.none')) + end + end + + context 'with a supported locale' do + it 'finds the human readable native name from a key' do + expect(helper.native_locale_name(:de)).to eq('Deutsch') + end + end + + context 'with a regional locale' do + it 'finds the human readable regional name from a key' do + expect(helper.native_locale_name('en-GB')).to eq('English (British)') + end + end + + context 'with a non-existent locale' do + it 'returns the supplied locale value' do + expect(helper.native_locale_name(:xxx)).to eq(:xxx) + end end end describe 'standard_locale_name' do - it 'finds the human readable standard name from a key' do - expect(helper.standard_locale_name(:de)).to eq('German') + context 'with a blank locale' do + it 'defaults to a generic value' do + expect(helper.standard_locale_name(nil)).to eq(I18n.t('generic.none')) + end + end + + context 'with a non-existent locale' do + it 'returns the supplied locale value' do + expect(helper.standard_locale_name(:xxx)).to eq(:xxx) + end + end + + context 'with a supported locale' do + it 'finds the human readable standard name from a key' do + expect(helper.standard_locale_name(:de)).to eq('German') + end end end end diff --git a/spec/helpers/settings_helper_spec.rb b/spec/helpers/settings_helper_spec.rb new file mode 100644 index 000000000..cba5c6ee8 --- /dev/null +++ b/spec/helpers/settings_helper_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe SettingsHelper do + describe 'session_device_icon' do + context 'with a mobile device' do + let(:session) { SessionActivation.new(user_agent: 'Mozilla/5.0 (iPhone)') } + + it 'detects the device and returns a descriptive string' do + result = helper.session_device_icon(session) + + expect(result).to eq('mobile') + end + end + + context 'with a tablet device' do + let(:session) { SessionActivation.new(user_agent: 'Mozilla/5.0 (iPad)') } + + it 'detects the device and returns a descriptive string' do + result = helper.session_device_icon(session) + + expect(result).to eq('tablet') + end + end + + context 'with a desktop device' do + let(:session) { SessionActivation.new(user_agent: 'Mozilla/5.0 (Macintosh)') } + + it 'detects the device and returns a descriptive string' do + result = helper.session_device_icon(session) + + expect(result).to eq('desktop') + end + end + end +end diff --git a/spec/helpers/statuses_helper_spec.rb b/spec/helpers/statuses_helper_spec.rb index ce2a4680e..c8ca2ed32 100644 --- a/spec/helpers/statuses_helper_spec.rb +++ b/spec/helpers/statuses_helper_spec.rb @@ -3,6 +3,68 @@ require 'rails_helper' RSpec.describe StatusesHelper, type: :helper do + describe 'link_to_newer' do + it 'returns a link to newer content' do + url = 'https://example.com' + result = helper.link_to_newer(url) + + expect(result).to match('load-more') + expect(result).to match(I18n.t('statuses.show_newer')) + end + end + + describe 'link_to_older' do + it 'returns a link to older content' do + url = 'https://example.com' + result = helper.link_to_older(url) + + expect(result).to match('load-more') + expect(result).to match(I18n.t('statuses.show_older')) + end + end + + describe 'fa_visibility_icon' do + context 'with a status that is public' do + let(:status) { Status.new(visibility: 'public') } + + it 'returns the correct fa icon' do + result = helper.fa_visibility_icon(status) + + expect(result).to match('fa-globe') + end + end + + context 'with a status that is unlisted' do + let(:status) { Status.new(visibility: 'unlisted') } + + it 'returns the correct fa icon' do + result = helper.fa_visibility_icon(status) + + expect(result).to match('fa-unlock') + end + end + + context 'with a status that is private' do + let(:status) { Status.new(visibility: 'private') } + + it 'returns the correct fa icon' do + result = helper.fa_visibility_icon(status) + + expect(result).to match('fa-lock') + end + end + + context 'with a status that is direct' do + let(:status) { Status.new(visibility: 'direct') } + + it 'returns the correct fa icon' do + result = helper.fa_visibility_icon(status) + + expect(result).to match('fa-at') + end + end + end + describe '#stream_link_target' do it 'returns nil if it is not an embedded view' do set_not_embedded_view -- cgit From 688287c59d526ef76089322a368789f5846c6ac3 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 10 Mar 2023 07:33:30 -0500 Subject: Coverage improvement round-out following up previous work (#23987) --- .rubocop_todo.yml | 1 + .../activitypub/claims_controller_spec.rb | 19 ++++++++++ .../api/v2/instances_controller_spec.rb | 22 ++++++++++++ .../api/v2/suggestions_controller_spec.rb | 22 ++++++++++++ spec/controllers/auth/setup_controller_spec.rb | 25 +++++++++++++ spec/controllers/custom_css_controller_spec.rb | 14 ++++++++ .../filters/statuses_controller_spec.rb | 41 ++++++++++++++++++++++ spec/controllers/filters_controller_spec.rb | 27 ++++++++++++++ spec/controllers/health_controller_spec.rb | 14 ++++++++ spec/controllers/privacy_controller_spec.rb | 14 ++++++++ spec/helpers/statuses_helper_spec.rb | 28 ++++++++++++++- spec/lib/importer/base_importer_spec.rb | 14 ++++++++ spec/lib/search_query_transformer_spec.rb | 18 ++++++++++ spec/models/admin/appeal_filter_spec.rb | 16 +++++++++ spec/models/form/admin_settings_spec.rb | 36 +++++++++++++++++++ .../models/form/status_filter_batch_action_spec.rb | 13 +++++++ spec/workers/verify_account_links_worker_spec.rb | 13 +++++++ 17 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 spec/controllers/activitypub/claims_controller_spec.rb create mode 100644 spec/controllers/api/v2/instances_controller_spec.rb create mode 100644 spec/controllers/api/v2/suggestions_controller_spec.rb create mode 100644 spec/controllers/auth/setup_controller_spec.rb create mode 100644 spec/controllers/custom_css_controller_spec.rb create mode 100644 spec/controllers/filters/statuses_controller_spec.rb create mode 100644 spec/controllers/filters_controller_spec.rb create mode 100644 spec/controllers/health_controller_spec.rb create mode 100644 spec/controllers/privacy_controller_spec.rb create mode 100644 spec/lib/importer/base_importer_spec.rb create mode 100644 spec/lib/search_query_transformer_spec.rb create mode 100644 spec/models/admin/appeal_filter_spec.rb create mode 100644 spec/models/form/admin_settings_spec.rb create mode 100644 spec/models/form/status_filter_batch_action_spec.rb create mode 100644 spec/workers/verify_account_links_worker_spec.rb (limited to 'spec/helpers/statuses_helper_spec.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 90e644856..0035478c4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -661,6 +661,7 @@ RSpec/ExpectInHook: RSpec/FilePath: Exclude: - 'spec/config/initializers/rack_attack_spec.rb' + - 'spec/controllers/activitypub/claims_controller_spec.rb' - 'spec/controllers/activitypub/collections_controller_spec.rb' - 'spec/controllers/activitypub/followers_synchronizations_controller_spec.rb' - 'spec/controllers/activitypub/inboxes_controller_spec.rb' diff --git a/spec/controllers/activitypub/claims_controller_spec.rb b/spec/controllers/activitypub/claims_controller_spec.rb new file mode 100644 index 000000000..f00eeb732 --- /dev/null +++ b/spec/controllers/activitypub/claims_controller_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ActivityPub::ClaimsController do + let(:account) { Fabricate(:account) } + + describe 'POST #create' do + context 'without signature' do + before do + post :create, params: { account_username: account.username }, body: '{}' + end + + it 'returns http not authorized' do + expect(response).to have_http_status(401) + end + end + end +end diff --git a/spec/controllers/api/v2/instances_controller_spec.rb b/spec/controllers/api/v2/instances_controller_spec.rb new file mode 100644 index 000000000..b7206da0a --- /dev/null +++ b/spec/controllers/api/v2/instances_controller_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Api::V2::InstancesController do + render_views + + let(:user) { Fabricate(:user) } + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id) } + + before do + allow(controller).to receive(:doorkeeper_token) { token } + end + + describe 'GET #show' do + it 'returns http success' do + get :show + + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/controllers/api/v2/suggestions_controller_spec.rb b/spec/controllers/api/v2/suggestions_controller_spec.rb new file mode 100644 index 000000000..5e6508bfd --- /dev/null +++ b/spec/controllers/api/v2/suggestions_controller_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Api::V2::SuggestionsController do + render_views + + let(:user) { Fabricate(:user) } + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') } + + before do + allow(controller).to receive(:doorkeeper_token) { token } + end + + describe 'GET #index' do + it 'returns http success' do + get :index + + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/controllers/auth/setup_controller_spec.rb b/spec/controllers/auth/setup_controller_spec.rb new file mode 100644 index 000000000..75e42aaf9 --- /dev/null +++ b/spec/controllers/auth/setup_controller_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Auth::SetupController do + render_views + + describe 'GET #show' do + context 'with a signed out request' do + it 'returns http redirect' do + get :show + expect(response).to be_redirect + end + end + + context 'with an unconfirmed signed in user' do + before { sign_in Fabricate(:user, confirmed_at: nil) } + + it 'returns http success' do + get :show + expect(response).to have_http_status(200) + end + end + end +end diff --git a/spec/controllers/custom_css_controller_spec.rb b/spec/controllers/custom_css_controller_spec.rb new file mode 100644 index 000000000..47fe6031f --- /dev/null +++ b/spec/controllers/custom_css_controller_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe CustomCssController do + render_views + + describe 'GET #show' do + it 'returns http success' do + get :show + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/controllers/filters/statuses_controller_spec.rb b/spec/controllers/filters/statuses_controller_spec.rb new file mode 100644 index 000000000..492361188 --- /dev/null +++ b/spec/controllers/filters/statuses_controller_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Filters::StatusesController do + render_views + + describe 'GET #index' do + let(:filter) { Fabricate(:custom_filter) } + + context 'with signed out user' do + it 'redirects' do + get :index, params: { filter_id: filter } + + expect(response).to be_redirect + end + end + + context 'with a signed in user' do + context 'with the filter user signed in' do + before { sign_in(filter.account.user) } + + it 'returns http success' do + get :index, params: { filter_id: filter } + + expect(response).to have_http_status(200) + end + end + + context 'with another user signed in' do + before { sign_in(Fabricate(:user)) } + + it 'returns http not found' do + get :index, params: { filter_id: filter } + + expect(response).to have_http_status(404) + end + end + end + end +end diff --git a/spec/controllers/filters_controller_spec.rb b/spec/controllers/filters_controller_spec.rb new file mode 100644 index 000000000..f68f87ba7 --- /dev/null +++ b/spec/controllers/filters_controller_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe FiltersController do + render_views + + describe 'GET #index' do + context 'with signed out user' do + it 'redirects' do + get :index + + expect(response).to be_redirect + end + end + + context 'with a signed in user' do + before { sign_in(Fabricate(:user)) } + + it 'returns http success' do + get :index + + expect(response).to have_http_status(200) + end + end + end +end diff --git a/spec/controllers/health_controller_spec.rb b/spec/controllers/health_controller_spec.rb new file mode 100644 index 000000000..282b66419 --- /dev/null +++ b/spec/controllers/health_controller_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe HealthController do + render_views + + describe 'GET #show' do + it 'returns http success' do + get :show + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/controllers/privacy_controller_spec.rb b/spec/controllers/privacy_controller_spec.rb new file mode 100644 index 000000000..c92c71ea6 --- /dev/null +++ b/spec/controllers/privacy_controller_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe PrivacyController do + render_views + + describe 'GET #show' do + it 'returns http success' do + get :show + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/helpers/statuses_helper_spec.rb b/spec/helpers/statuses_helper_spec.rb index c8ca2ed32..105da7e1b 100644 --- a/spec/helpers/statuses_helper_spec.rb +++ b/spec/helpers/statuses_helper_spec.rb @@ -2,7 +2,33 @@ require 'rails_helper' -RSpec.describe StatusesHelper, type: :helper do +describe StatusesHelper do + describe 'status_text_summary' do + context 'with blank text' do + let(:status) { Status.new(spoiler_text: '') } + + it 'returns immediately with nil' do + result = helper.status_text_summary(status) + expect(result).to be_nil + end + end + + context 'with present text' do + let(:status) { Status.new(spoiler_text: 'SPOILERS!!!') } + + it 'returns the content warning' do + result = helper.status_text_summary(status) + expect(result).to eq(I18n.t('statuses.content_warning', warning: 'SPOILERS!!!')) + end + end + end + + def status_text_summary(status) + return if status.spoiler_text.blank? + + I18n.t('statuses.content_warning', warning: status.spoiler_text) + end + describe 'link_to_newer' do it 'returns a link to newer content' do url = 'https://example.com' diff --git a/spec/lib/importer/base_importer_spec.rb b/spec/lib/importer/base_importer_spec.rb new file mode 100644 index 000000000..78e9a869b --- /dev/null +++ b/spec/lib/importer/base_importer_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Importer::BaseImporter do + describe 'import!' do + let(:pool) { Concurrent::FixedThreadPool.new(5) } + let(:importer) { described_class.new(batch_size: 123, executor: pool) } + + it 'raises an error' do + expect { importer.import! }.to raise_error(NotImplementedError) + end + end +end diff --git a/spec/lib/search_query_transformer_spec.rb b/spec/lib/search_query_transformer_spec.rb new file mode 100644 index 000000000..109533469 --- /dev/null +++ b/spec/lib/search_query_transformer_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe SearchQueryTransformer do + describe 'initialization' do + let(:parser) { SearchQueryParser.new.parse('query') } + + it 'sets attributes' do + transformer = described_class.new.apply(parser) + + expect(transformer.should_clauses.first).to be_a(SearchQueryTransformer::TermClause) + expect(transformer.must_clauses.first).to be_nil + expect(transformer.must_not_clauses.first).to be_nil + expect(transformer.filter_clauses.first).to be_nil + end + end +end diff --git a/spec/models/admin/appeal_filter_spec.rb b/spec/models/admin/appeal_filter_spec.rb new file mode 100644 index 000000000..e840bc3bc --- /dev/null +++ b/spec/models/admin/appeal_filter_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Admin::AppealFilter do + describe '#results' do + let(:approved_appeal) { Fabricate(:appeal, approved_at: 10.days.ago) } + let(:not_approved_appeal) { Fabricate(:appeal, approved_at: nil) } + + it 'returns filtered appeals' do + filter = described_class.new(status: 'approved') + + expect(filter.results).to eq([approved_appeal]) + end + end +end diff --git a/spec/models/form/admin_settings_spec.rb b/spec/models/form/admin_settings_spec.rb new file mode 100644 index 000000000..0dc2d881a --- /dev/null +++ b/spec/models/form/admin_settings_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Form::AdminSettings do + describe 'validations' do + describe 'site_contact_username' do + context 'with no accounts' do + it 'is not valid' do + setting = described_class.new(site_contact_username: 'Test') + setting.valid? + + expect(setting).to model_have_error_on_field(:site_contact_username) + end + end + + context 'with an account' do + before { Fabricate(:account, username: 'Glorp') } + + it 'is not valid when account doesnt match' do + setting = described_class.new(site_contact_username: 'Test') + setting.valid? + + expect(setting).to model_have_error_on_field(:site_contact_username) + end + + it 'is valid when account matches' do + setting = described_class.new(site_contact_username: 'Glorp') + setting.valid? + + expect(setting).to_not model_have_error_on_field(:site_contact_username) + end + end + end + end +end diff --git a/spec/models/form/status_filter_batch_action_spec.rb b/spec/models/form/status_filter_batch_action_spec.rb new file mode 100644 index 000000000..f06a11cc8 --- /dev/null +++ b/spec/models/form/status_filter_batch_action_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Form::StatusFilterBatchAction do + describe '#save!' do + it 'does nothing if status_filter_ids is empty' do + batch_action = described_class.new(status_filter_ids: []) + + expect(batch_action.save!).to be_nil + end + end +end diff --git a/spec/workers/verify_account_links_worker_spec.rb b/spec/workers/verify_account_links_worker_spec.rb new file mode 100644 index 000000000..227591392 --- /dev/null +++ b/spec/workers/verify_account_links_worker_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe VerifyAccountLinksWorker do + let(:worker) { described_class.new } + + describe 'perform' do + it 'runs without error for missing record' do + expect { worker.perform(nil) }.to_not raise_error + end + end +end -- cgit