From 684a970b3c7ce19ec419c4db52b67a3b0903f493 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Wed, 8 Mar 2023 19:56:41 +0100 Subject: Unescape HTML entities (#24019) --- spec/lib/plain_text_formatter_spec.rb | 61 ++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/lib/plain_text_formatter_spec.rb b/spec/lib/plain_text_formatter_spec.rb index 4a0519aaf..80b3c331a 100644 --- a/spec/lib/plain_text_formatter_spec.rb +++ b/spec/lib/plain_text_formatter_spec.rb @@ -6,7 +6,7 @@ RSpec.describe PlainTextFormatter do describe '#to_s' do subject { described_class.new(status.text, status.local?).to_s } - context 'given a post with local status' do + context 'when status is local' do let(:status) { Fabricate(:status, text: '

a text by a nerd who uses an HTML tag in text

', uri: nil) } it 'returns the raw text' do @@ -14,12 +14,63 @@ RSpec.describe PlainTextFormatter do end end - context 'given a post with remote status' do + context 'when status is remote' do let(:remote_account) { Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/') } - let(:status) { Fabricate(:status, account: remote_account, text: '

Hello

') } - it 'returns tag-stripped text' do - expect(subject).to eq 'Hello' + context 'when text contains inline HTML tags' do + let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem ipsum') } + + it 'strips the tags' do + expect(subject).to eq 'Lorem ipsum' + end + end + + context 'when text contains

tags' do + let(:status) { Fabricate(:status, account: remote_account, text: '

Lorem

ipsum

') } + + it 'inserts a newline' do + expect(subject).to eq "Lorem\nipsum" + end + end + + context 'when text contains a single
tag' do + let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem
ipsum') } + + it 'inserts a newline' do + expect(subject).to eq "Lorem\nipsum" + end + end + + context 'when text contains consecutive
tag' do + let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem


ipsum') } + + it 'inserts a single newline' do + expect(subject).to eq "Lorem\nipsum" + end + end + + context 'when text contains HTML entity' do + let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem & ipsum ❤') } + + it 'unescapes the entity' do + expect(subject).to eq 'Lorem & ipsum ❤' + end + end + + context 'when text contains ipsum') } + + it 'strips the tag and its contents' do + expect(subject).to eq 'Lorem ipsum' + end + end + + context 'when text contains an HTML comment tags' do + let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem ipsum') } + + it 'strips the comment' do + expect(subject).to eq 'Lorem ipsum' + end end end end -- cgit From 2f7a663b0bccd7b5de1fa47b89a7bdc7591e69ec Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 9 Mar 2023 08:27:48 -0500 Subject: Explicitly set github repo in instance presenter spec (#24036) --- Gemfile | 1 + Gemfile.lock | 1 + spec/presenters/instance_presenter_spec.rb | 24 ++++++++++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/Gemfile b/Gemfile index eaaf1bf1f..7a836b6ba 100644 --- a/Gemfile +++ b/Gemfile @@ -117,6 +117,7 @@ end group :test do gem 'capybara', '~> 3.38' + gem 'climate_control' gem 'faker', '~> 3.1' gem 'json-schema', '~> 3.0' gem 'rack-test', '~> 2.0' diff --git a/Gemfile.lock b/Gemfile.lock index bd1aece57..e5ad5bfe5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -784,6 +784,7 @@ DEPENDENCIES capybara (~> 3.38) charlock_holmes (~> 0.7.7) chewy (~> 7.2) + climate_control cocoon (~> 1.2) color_diff (~> 0.1) concurrent-ruby diff --git a/spec/presenters/instance_presenter_spec.rb b/spec/presenters/instance_presenter_spec.rb index 29170a79a..795abd8b4 100644 --- a/spec/presenters/instance_presenter_spec.rb +++ b/spec/presenters/instance_presenter_spec.rb @@ -89,8 +89,28 @@ describe InstancePresenter do end describe '#source_url' do - it 'returns "https://github.com/mastodon/mastodon"' do - expect(instance_presenter.source_url).to eq('https://github.com/mastodon/mastodon') + context 'with the GITHUB_REPOSITORY env variable set' do + around do |example| + ClimateControl.modify GITHUB_REPOSITORY: 'other/repo' do + example.run + end + end + + it 'uses the env variable to build a repo URL' do + expect(instance_presenter.source_url).to eq('https://github.com/other/repo') + end + end + + context 'without the GITHUB_REPOSITORY env variable set' do + around do |example| + ClimateControl.modify GITHUB_REPOSITORY: nil do + example.run + end + end + + it 'defaults to the core mastodon repo URL' do + expect(instance_presenter.source_url).to eq('https://github.com/mastodon/mastodon') + end end end -- cgit From 56bddfbfa39956ea3a8c52721eb364b1120634f6 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 10 Mar 2023 06:12:51 -0500 Subject: Serializers spec coverage (#24017) --- .rubocop_todo.yml | 4 ++++ spec/fabricators/encrypted_message_fabricator.rb | 7 ++++++ .../activitypub/device_serializer_spec.rb | 20 +++++++++++++++++ .../activitypub/one_time_key_serializer_spec.rb | 20 +++++++++++++++++ .../activitypub/undo_like_serializer_spec.rb | 20 +++++++++++++++++ .../activitypub/vote_serializer_spec.rb | 20 +++++++++++++++++ .../rest/encrypted_message_serializer_spec.rb | 20 +++++++++++++++++ spec/serializers/rest/instance_serializer_spec.rb | 20 +++++++++++++++++ .../rest/keys/claim_result_serializer_spec.rb | 20 +++++++++++++++++ .../rest/keys/device_serializer_spec.rb | 20 +++++++++++++++++ .../rest/keys/query_result_serializer_spec.rb | 20 +++++++++++++++++ .../serializers/rest/suggestion_serializer_spec.rb | 26 ++++++++++++++++++++++ 12 files changed, 217 insertions(+) create mode 100644 spec/fabricators/encrypted_message_fabricator.rb create mode 100644 spec/serializers/activitypub/device_serializer_spec.rb create mode 100644 spec/serializers/activitypub/one_time_key_serializer_spec.rb create mode 100644 spec/serializers/activitypub/undo_like_serializer_spec.rb create mode 100644 spec/serializers/activitypub/vote_serializer_spec.rb create mode 100644 spec/serializers/rest/encrypted_message_serializer_spec.rb create mode 100644 spec/serializers/rest/instance_serializer_spec.rb create mode 100644 spec/serializers/rest/keys/claim_result_serializer_spec.rb create mode 100644 spec/serializers/rest/keys/device_serializer_spec.rb create mode 100644 spec/serializers/rest/keys/query_result_serializer_spec.rb create mode 100644 spec/serializers/rest/suggestion_serializer_spec.rb (limited to 'spec') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b53f655bd..90e644856 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -698,7 +698,11 @@ RSpec/FilePath: - 'spec/lib/activitypub/tag_manager_spec.rb' - 'spec/lib/ostatus/tag_manager_spec.rb' - 'spec/lib/sanitize_config_spec.rb' + - 'spec/serializers/activitypub/device_serializer_spec.rb' - 'spec/serializers/activitypub/note_serializer_spec.rb' + - 'spec/serializers/activitypub/one_time_key_serializer_spec.rb' + - 'spec/serializers/activitypub/undo_like_serializer_spec.rb' + - 'spec/serializers/activitypub/vote_serializer_spec.rb' - 'spec/serializers/activitypub/update_poll_serializer_spec.rb' - 'spec/services/activitypub/fetch_featured_collection_service_spec.rb' - 'spec/services/activitypub/fetch_featured_tags_collection_service_spec.rb' diff --git a/spec/fabricators/encrypted_message_fabricator.rb b/spec/fabricators/encrypted_message_fabricator.rb new file mode 100644 index 000000000..43b310514 --- /dev/null +++ b/spec/fabricators/encrypted_message_fabricator.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +Fabricator(:encrypted_message) do + device + from_account { Fabricate(:account) } + from_device_id { Faker::Number.number(digits: 5) } +end diff --git a/spec/serializers/activitypub/device_serializer_spec.rb b/spec/serializers/activitypub/device_serializer_spec.rb new file mode 100644 index 000000000..2a3be8212 --- /dev/null +++ b/spec/serializers/activitypub/device_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ActivityPub::DeviceSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { Fabricate(:device) } + + describe 'type' do + it 'returns correct serialized type' do + expect(serialization['type']).to eq('Device') + end + end +end diff --git a/spec/serializers/activitypub/one_time_key_serializer_spec.rb b/spec/serializers/activitypub/one_time_key_serializer_spec.rb new file mode 100644 index 000000000..6fe1f0618 --- /dev/null +++ b/spec/serializers/activitypub/one_time_key_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ActivityPub::OneTimeKeySerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { Fabricate(:one_time_key) } + + describe 'type' do + it 'returns correct serialized type' do + expect(serialization['type']).to eq('Curve25519Key') + end + end +end diff --git a/spec/serializers/activitypub/undo_like_serializer_spec.rb b/spec/serializers/activitypub/undo_like_serializer_spec.rb new file mode 100644 index 000000000..43cf7192e --- /dev/null +++ b/spec/serializers/activitypub/undo_like_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ActivityPub::UndoLikeSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { Fabricate(:favourite) } + + describe 'type' do + it 'returns correct serialized type' do + expect(serialization['type']).to eq('Undo') + end + end +end diff --git a/spec/serializers/activitypub/vote_serializer_spec.rb b/spec/serializers/activitypub/vote_serializer_spec.rb new file mode 100644 index 000000000..c329542d7 --- /dev/null +++ b/spec/serializers/activitypub/vote_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ActivityPub::VoteSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { Fabricate(:poll_vote) } + + describe 'type' do + it 'returns correct serialized type' do + expect(serialization['type']).to eq('Create') + end + end +end diff --git a/spec/serializers/rest/encrypted_message_serializer_spec.rb b/spec/serializers/rest/encrypted_message_serializer_spec.rb new file mode 100644 index 000000000..e0e70a3b8 --- /dev/null +++ b/spec/serializers/rest/encrypted_message_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe REST::EncryptedMessageSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { Fabricate(:encrypted_message) } + + describe 'account' do + it 'returns the associated account' do + expect(serialization['account_id']).to eq(record.from_account.id.to_s) + end + end +end diff --git a/spec/serializers/rest/instance_serializer_spec.rb b/spec/serializers/rest/instance_serializer_spec.rb new file mode 100644 index 000000000..15a5de18d --- /dev/null +++ b/spec/serializers/rest/instance_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe REST::InstanceSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { InstancePresenter.new } + + describe 'usage' do + it 'returns recent usage data' do + expect(serialization['usage']).to eq({ 'users' => { 'active_month' => 0 } }) + end + end +end diff --git a/spec/serializers/rest/keys/claim_result_serializer_spec.rb b/spec/serializers/rest/keys/claim_result_serializer_spec.rb new file mode 100644 index 000000000..cf9416f03 --- /dev/null +++ b/spec/serializers/rest/keys/claim_result_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe REST::Keys::ClaimResultSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { Keys::ClaimService::Result.new(Account.new(id: 123), 456) } + + describe 'account' do + it 'returns the associated account' do + expect(serialization['account_id']).to eq('123') + end + end +end diff --git a/spec/serializers/rest/keys/device_serializer_spec.rb b/spec/serializers/rest/keys/device_serializer_spec.rb new file mode 100644 index 000000000..c15e197cb --- /dev/null +++ b/spec/serializers/rest/keys/device_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe REST::Keys::DeviceSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { Device.new(name: 'Device name') } + + describe 'name' do + it 'returns the name' do + expect(serialization['name']).to eq('Device name') + end + end +end diff --git a/spec/serializers/rest/keys/query_result_serializer_spec.rb b/spec/serializers/rest/keys/query_result_serializer_spec.rb new file mode 100644 index 000000000..983780ae9 --- /dev/null +++ b/spec/serializers/rest/keys/query_result_serializer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe REST::Keys::QueryResultSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) { Keys::QueryService::Result.new(Account.new(id: 123), []) } + + describe 'account' do + it 'returns the associated account id' do + expect(serialization['account_id']).to eq('123') + end + end +end diff --git a/spec/serializers/rest/suggestion_serializer_spec.rb b/spec/serializers/rest/suggestion_serializer_spec.rb new file mode 100644 index 000000000..b3c086208 --- /dev/null +++ b/spec/serializers/rest/suggestion_serializer_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe REST::SuggestionSerializer do + let(:serialization) do + JSON.parse( + ActiveModelSerializers::SerializableResource.new( + record, serializer: described_class + ).to_json + ) + end + let(:record) do + AccountSuggestions::Suggestion.new( + account: account, + source: 'SuggestionSource' + ) + end + let(:account) { Fabricate(:account) } + + describe 'account' do + it 'returns the associated account' do + expect(serialization['account']['id']).to eq(account.id.to_s) + end + end +end -- 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') 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 From 1ed12d5e2f6dc0f7d891916df84be6e06d37786d Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 10 Mar 2023 13:31:10 -0500 Subject: Add basic search specs for chewy indexes (#24065) --- spec/chewy/accounts_index_spec.rb | 31 +++++++++++++++++++++++++++++++ spec/chewy/statuses_index_spec.rb | 31 +++++++++++++++++++++++++++++++ spec/chewy/tags_index_spec.rb | 31 +++++++++++++++++++++++++++++++ spec/rails_helper.rb | 2 ++ 4 files changed, 95 insertions(+) create mode 100644 spec/chewy/accounts_index_spec.rb create mode 100644 spec/chewy/statuses_index_spec.rb create mode 100644 spec/chewy/tags_index_spec.rb (limited to 'spec') diff --git a/spec/chewy/accounts_index_spec.rb b/spec/chewy/accounts_index_spec.rb new file mode 100644 index 000000000..f9c5922c7 --- /dev/null +++ b/spec/chewy/accounts_index_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe AccountsIndex do + describe 'Searching the index' do + before do + mock_elasticsearch_response(described_class, raw_response) + end + + it 'returns results from a query' do + results = described_class.query(match: { name: 'account' }) + + expect(results).to eq [] + end + end + + def raw_response + { + took: 3, + hits: { + hits: [ + { + _id: '0', + _score: 1.6375021, + }, + ], + }, + } + end +end diff --git a/spec/chewy/statuses_index_spec.rb b/spec/chewy/statuses_index_spec.rb new file mode 100644 index 000000000..768e9415f --- /dev/null +++ b/spec/chewy/statuses_index_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe StatusesIndex do + describe 'Searching the index' do + before do + mock_elasticsearch_response(described_class, raw_response) + end + + it 'returns results from a query' do + results = described_class.query(match: { name: 'status' }) + + expect(results).to eq [] + end + end + + def raw_response + { + took: 3, + hits: { + hits: [ + { + _id: '0', + _score: 1.6375021, + }, + ], + }, + } + end +end diff --git a/spec/chewy/tags_index_spec.rb b/spec/chewy/tags_index_spec.rb new file mode 100644 index 000000000..054589bdf --- /dev/null +++ b/spec/chewy/tags_index_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe TagsIndex do + describe 'Searching the index' do + before do + mock_elasticsearch_response(described_class, raw_response) + end + + it 'returns results from a query' do + results = described_class.query(match: { name: 'tag' }) + + expect(results).to eq [] + end + end + + def raw_response + { + took: 3, + hits: { + hits: [ + { + _id: '0', + _score: 1.6375021, + }, + ], + }, + } + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 70d3a968c..c204fcdbd 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -10,6 +10,7 @@ require 'rspec/rails' require 'webmock/rspec' require 'paperclip/matchers' require 'capybara/rspec' +require 'chewy/rspec' Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } @@ -45,6 +46,7 @@ RSpec.configure do |config| config.include Devise::Test::ControllerHelpers, type: :view config.include Paperclip::Shoulda::Matchers config.include ActiveSupport::Testing::TimeHelpers + config.include Chewy::Rspec::Helpers config.include Redisable config.before :each, type: :feature do -- cgit From a471e1c48f4c9ec9e96832f51783b11407b67161 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 14 Mar 2023 22:56:09 -0400 Subject: Add spec for the CLI `version` task (#23978) --- spec/lib/mastodon/cli_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 spec/lib/mastodon/cli_spec.rb (limited to 'spec') diff --git a/spec/lib/mastodon/cli_spec.rb b/spec/lib/mastodon/cli_spec.rb new file mode 100644 index 000000000..419f8b864 --- /dev/null +++ b/spec/lib/mastodon/cli_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'cli' + +describe Mastodon::CLI do + describe 'version' do + it 'returns the Mastodon version' do + expect { described_class.new.invoke(:version) }.to output( + a_string_including(Mastodon::Version.to_s) + ).to_stdout + end + end +end -- cgit From a232a1feb804e22507ca104ca0aaf21a7c70cb26 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 15 Mar 2023 04:15:20 +0100 Subject: Fix misleading error code when receiving invalid WebAuthn credentials (#23568) --- .../two_factor_authentication/webauthn_credentials_controller.rb | 2 +- .../two_factor_authentication/webauthn_credentials_controller_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'spec') diff --git a/app/controllers/settings/two_factor_authentication/webauthn_credentials_controller.rb b/app/controllers/settings/two_factor_authentication/webauthn_credentials_controller.rb index e43818c94..d1ee7dc19 100644 --- a/app/controllers/settings/two_factor_authentication/webauthn_credentials_controller.rb +++ b/app/controllers/settings/two_factor_authentication/webauthn_credentials_controller.rb @@ -52,7 +52,7 @@ module Settings end else flash[:error] = I18n.t('webauthn_credentials.create.error') - status = :internal_server_error + status = :unprocessable_entity end else flash[:error] = t('webauthn_credentials.create.error') diff --git a/spec/controllers/settings/two_factor_authentication/webauthn_credentials_controller_spec.rb b/spec/controllers/settings/two_factor_authentication/webauthn_credentials_controller_spec.rb index f060c3a4b..a95521c94 100644 --- a/spec/controllers/settings/two_factor_authentication/webauthn_credentials_controller_spec.rb +++ b/spec/controllers/settings/two_factor_authentication/webauthn_credentials_controller_spec.rb @@ -248,7 +248,7 @@ describe Settings::TwoFactorAuthentication::WebauthnCredentialsController do post :create, params: { credential: new_webauthn_credential, nickname: 'USB Key' } - expect(response).to have_http_status(500) + expect(response).to have_http_status(422) expect(flash[:error]).to be_present end end @@ -268,7 +268,7 @@ describe Settings::TwoFactorAuthentication::WebauthnCredentialsController do post :create, params: { credential: new_webauthn_credential, nickname: nickname } - expect(response).to have_http_status(500) + expect(response).to have_http_status(422) expect(flash[:error]).to be_present end end -- cgit