From 36eeb70d5315be045a638d77f8ff0a71dce61076 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 11 Apr 2023 11:35:39 +0200 Subject: [PATCH] Spec coverage on Settings/ controllers specs (#24221) --- .../settings/aliases_controller_spec.rb | 46 +++++++++++++++++ .../settings/featured_tags_controller_spec.rb | 39 +++++++++++---- .../migration/redirects_controller_spec.rb | 45 ++++++++++++++++- .../settings/pictures_controller_spec.rb | 30 ++++++++++++ .../preferences/appearance_controller_spec.rb | 9 ++++ ..._authentication_methods_controller_spec.rb | 49 +++++++++++++++---- spec/fabricators/featured_tag_fabricator.rb | 7 +++ 7 files changed, 205 insertions(+), 20 deletions(-) create mode 100644 spec/fabricators/featured_tag_fabricator.rb diff --git a/spec/controllers/settings/aliases_controller_spec.rb b/spec/controllers/settings/aliases_controller_spec.rb index 805f659886..ef8724faf4 100644 --- a/spec/controllers/settings/aliases_controller_spec.rb +++ b/spec/controllers/settings/aliases_controller_spec.rb @@ -18,4 +18,50 @@ describe Settings::AliasesController do expect(response).to have_http_status(200) end end + + describe 'POST #create' do + context 'with valid alias' do + before { stub_resolver } + + it 'creates an alias for the user' do + expect do + post :create, params: { account_alias: { acct: 'new@example.com' } } + end.to change(AccountAlias, :count).by(1) + + expect(response).to redirect_to(settings_aliases_path) + end + end + + context 'with invalid alias' do + it 'does not create an alias for the user' do + expect do + post :create, params: { account_alias: { acct: 'format-wrong' } } + end.to_not change(AccountAlias, :count) + + expect(response).to have_http_status(200) + end + end + end + + describe 'DELETE #destroy' do + let(:account_alias) do + AccountAlias.new(account: user.account, acct: 'new@example.com').tap do |account_alias| + account_alias.save(validate: false) + end + end + + it 'removes an alias' do + delete :destroy, params: { id: account_alias.id } + + expect(response).to redirect_to(settings_aliases_path) + expect { account_alias.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + end + + private + + def stub_resolver + resolver = instance_double(ResolveAccountService, call: Fabricate(:account)) + allow(ResolveAccountService).to receive(:new).and_return(resolver) + end end diff --git a/spec/controllers/settings/featured_tags_controller_spec.rb b/spec/controllers/settings/featured_tags_controller_spec.rb index 5c61351af3..fc25e7aa88 100644 --- a/spec/controllers/settings/featured_tags_controller_spec.rb +++ b/spec/controllers/settings/featured_tags_controller_spec.rb @@ -11,20 +11,20 @@ describe Settings::FeaturedTagsController do end end - describe 'POST #create' do - context 'when user is not sign in' do - subject { post :create } + context 'when user is not signed in' do + subject { post :create } - it_behaves_like 'authenticate user' - end + it_behaves_like 'authenticate user' + end - context 'when user is sign in' do + context 'when user is signed in' do + let(:user) { Fabricate(:user, password: '12345678') } + + before { sign_in user, scope: :user } + + describe 'POST #create' do subject { post :create, params: { featured_tag: params } } - let(:user) { Fabricate(:user, password: '12345678') } - - before { sign_in user, scope: :user } - context 'when parameter is valid' do let(:params) { { name: 'test' } } @@ -41,5 +41,24 @@ describe Settings::FeaturedTagsController do end end end + + describe 'GET to #index' do + it 'responds with success' do + get :index + + expect(response).to have_http_status(200) + end + end + + describe 'DELETE to #destroy' do + let(:featured_tag) { Fabricate(:featured_tag, account: user.account) } + + it 'removes the featured tag' do + delete :destroy, params: { id: featured_tag.id } + + expect(response).to redirect_to(settings_featured_tags_path) + expect { featured_tag.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + end end end diff --git a/spec/controllers/settings/migration/redirects_controller_spec.rb b/spec/controllers/settings/migration/redirects_controller_spec.rb index 50d9e1927b..54897bb7fd 100644 --- a/spec/controllers/settings/migration/redirects_controller_spec.rb +++ b/spec/controllers/settings/migration/redirects_controller_spec.rb @@ -5,7 +5,7 @@ require 'rails_helper' describe Settings::Migration::RedirectsController do render_views - let!(:user) { Fabricate(:user) } + let!(:user) { Fabricate(:user, password: 'testtest') } before do sign_in user, scope: :user @@ -17,4 +17,47 @@ describe Settings::Migration::RedirectsController do expect(response).to have_http_status(200) end end + + describe 'POST #create' do + context 'with valid params' do + before { stub_resolver } + + it 'redirects to the settings migration path' do + post :create, params: { form_redirect: { acct: 'new@host.com', current_password: 'testtest' } } + + expect(response).to redirect_to(settings_migration_path) + end + end + + context 'with non valid params' do + it 'returns success and renders the new page' do + post :create, params: { form_redirect: { acct: '' } } + + expect(response).to have_http_status(200) + expect(response).to render_template(:new) + end + end + end + + describe 'DELETE #destroy' do + let(:account) { Fabricate(:account) } + + before do + user.account.update(moved_to_account_id: account.id) + end + + it 'resets the account and sends an update' do + delete :destroy + + expect(response).to redirect_to(settings_migration_path) + expect(user.account.reload.moved_to_account).to be_nil + end + end + + private + + def stub_resolver + resolver = instance_double(ResolveAccountService, call: Fabricate(:account)) + allow(ResolveAccountService).to receive(:new).and_return(resolver) + end end diff --git a/spec/controllers/settings/pictures_controller_spec.rb b/spec/controllers/settings/pictures_controller_spec.rb index 2368dc55dd..705878f03d 100644 --- a/spec/controllers/settings/pictures_controller_spec.rb +++ b/spec/controllers/settings/pictures_controller_spec.rb @@ -18,5 +18,35 @@ describe Settings::PicturesController do expect(response).to have_http_status(400) end end + + context 'with valid picture id' do + context 'when account updates correctly' do + let(:service) { instance_double(UpdateAccountService, call: true) } + + before do + allow(UpdateAccountService).to receive(:new).and_return(service) + end + + it 'updates the account' do + delete :destroy, params: { id: 'avatar' } + expect(response).to redirect_to(settings_profile_path) + expect(response).to have_http_status(303) + expect(service).to have_received(:call).with(user.account, { 'avatar' => nil, 'avatar_remote_url' => '' }) + end + end + + context 'when account cannot update' do + let(:service) { instance_double(UpdateAccountService, call: false) } + + before do + allow(UpdateAccountService).to receive(:new).and_return(service) + end + + it 'redirects to profile' do + delete :destroy, params: { id: 'avatar' } + expect(response).to redirect_to(settings_profile_path) + end + end + end end end diff --git a/spec/controllers/settings/preferences/appearance_controller_spec.rb b/spec/controllers/settings/preferences/appearance_controller_spec.rb index 7c7f716b71..df0237a6b3 100644 --- a/spec/controllers/settings/preferences/appearance_controller_spec.rb +++ b/spec/controllers/settings/preferences/appearance_controller_spec.rb @@ -14,7 +14,16 @@ describe Settings::Preferences::AppearanceController do describe 'GET #show' do it 'returns http success' do get :show + expect(response).to have_http_status(200) end end + + describe 'PUT #update' do + it 'redirects correctly' do + put :update, params: { user: { setting_theme: 'contrast' } } + + expect(response).to redirect_to(settings_preferences_appearance_path) + end + end end diff --git a/spec/controllers/settings/two_factor_authentication_methods_controller_spec.rb b/spec/controllers/settings/two_factor_authentication_methods_controller_spec.rb index 66ffe89f3d..153eca1a5a 100644 --- a/spec/controllers/settings/two_factor_authentication_methods_controller_spec.rb +++ b/spec/controllers/settings/two_factor_authentication_methods_controller_spec.rb @@ -5,14 +5,24 @@ require 'rails_helper' describe Settings::TwoFactorAuthenticationMethodsController do render_views - let(:user) { Fabricate(:user) } + context 'when not signed in' do + describe 'GET to #index' do + it 'redirects' do + get :index - describe 'GET #index' do - context 'when signed in' do - before do - sign_in user, scope: :user + expect(response).to redirect_to '/auth/sign_in' end + end + end + context 'when signed in' do + let(:user) { Fabricate(:user) } + + before do + sign_in user, scope: :user + end + + describe 'GET #index' do describe 'when user has enabled otp' do before do user.update(otp_required_for_login: true) @@ -38,11 +48,32 @@ describe Settings::TwoFactorAuthenticationMethodsController do end end - context 'when not signed in' do - it 'redirects' do - get :index + describe 'POST to #disable' do + before do + user.update(otp_required_for_login: true) + end - expect(response).to redirect_to '/auth/sign_in' + context 'when user has not passed challenge' do + it 'renders challenge page' do + post :disable + + expect(response).to have_http_status(200) + expect(response).to render_template('auth/challenges/new') + end + end + + context 'when user has passed challenge' do + before do + mailer = instance_double(ApplicationMailer::MessageDelivery, deliver_later!: true) + allow(UserMailer).to receive(:two_factor_disabled).with(user).and_return(mailer) + end + + it 'redirects to settings page' do + post :disable, session: { challenge_passed_at: 10.minutes.ago } + + expect(UserMailer).to have_received(:two_factor_disabled).with(user) + expect(response).to redirect_to(settings_otp_authentication_path) + end end end end diff --git a/spec/fabricators/featured_tag_fabricator.rb b/spec/fabricators/featured_tag_fabricator.rb new file mode 100644 index 0000000000..747d8e36a5 --- /dev/null +++ b/spec/fabricators/featured_tag_fabricator.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +Fabricator(:featured_tag) do + account + tag + name 'Tag' +end