Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Spec coverage on Settings/ controllers specs (mastodon#24221)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored Apr 11, 2023
1 parent a2a6630 commit 36eeb70
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 19 deletions.
46 changes: 46 additions & 0 deletions spec/controllers/settings/aliases_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,50 @@
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: '[email protected]' } }
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: '[email protected]').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
37 changes: 28 additions & 9 deletions spec/controllers/settings/featured_tags_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
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
subject { post :create, params: { featured_tag: params } }
context 'when user is signed in' do
let(:user) { Fabricate(:user, password: '12345678') }

let(:user) { Fabricate(:user, password: '12345678') }
before { sign_in user, scope: :user }

before { sign_in user, scope: :user }
describe 'POST #create' do
subject { post :create, params: { featured_tag: params } }

context 'when parameter is valid' do
let(:params) { { name: 'test' } }
Expand All @@ -41,5 +41,24 @@
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
45 changes: 44 additions & 1 deletion spec/controllers/settings/migration/redirects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe Settings::Migration::RedirectsController do
render_views

let!(:user) { Fabricate(:user) }
let!(:user) { Fabricate(:user, password: 'testtest') }

before do
sign_in user, scope: :user
Expand All @@ -17,4 +17,47 @@
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: '[email protected]', 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
30 changes: 30 additions & 0 deletions spec/controllers/settings/pictures_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,35 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
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)
Expand All @@ -38,11 +48,32 @@
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
Expand Down
7 changes: 7 additions & 0 deletions spec/fabricators/featured_tag_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

Fabricator(:featured_tag) do
account
tag
name 'Tag'
end

0 comments on commit 36eeb70

Please sign in to comment.