Combine shared-setup examples across spec/controllers/auth/* specs (#32906)

This commit is contained in:
Matt Jankowski 2024-11-15 11:07:26 -05:00 committed by GitHub
parent 54a7c1898e
commit 7bd7705f59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 121 additions and 124 deletions

View file

@ -8,9 +8,7 @@ RSpec.describe Auth::ChallengesController do
let(:password) { 'foobar12345' } let(:password) { 'foobar12345' }
let(:user) { Fabricate(:user, password: password) } let(:user) { Fabricate(:user, password: password) }
before do before { sign_in user }
sign_in user
end
describe 'POST #create' do describe 'POST #create' do
let(:return_to) { edit_user_registration_path } let(:return_to) { edit_user_registration_path }
@ -18,28 +16,24 @@ RSpec.describe Auth::ChallengesController do
context 'with correct password' do context 'with correct password' do
before { post :create, params: { form_challenge: { return_to: return_to, current_password: password } } } before { post :create, params: { form_challenge: { return_to: return_to, current_password: password } } }
it 'redirects back' do it 'redirects back and sets challenge passed at in session' do
expect(response).to redirect_to(return_to) expect(response)
end .to redirect_to(return_to)
expect(session[:challenge_passed_at])
it 'sets session' do .to_not be_nil
expect(session[:challenge_passed_at]).to_not be_nil
end end
end end
context 'with incorrect password' do context 'with incorrect password' do
before { post :create, params: { form_challenge: { return_to: return_to, current_password: 'hhfggjjd562' } } } before { post :create, params: { form_challenge: { return_to: return_to, current_password: 'hhfggjjd562' } } }
it 'renders challenge' do it 'renders challenge, displays error, does not set session' do
expect(response).to render_template('auth/challenges/new') expect(response)
end .to render_template('auth/challenges/new')
expect(response.body)
it 'displays error' do .to include 'Invalid password'
expect(response.body).to include 'Invalid password' expect(session[:challenge_passed_at])
end .to be_nil
it 'does not set session' do
expect(session[:challenge_passed_at]).to be_nil
end end
end end
end end

View file

@ -23,12 +23,11 @@ RSpec.describe Auth::ConfirmationsController do
get :show, params: { confirmation_token: 'foobar' } get :show, params: { confirmation_token: 'foobar' }
end end
it 'redirects to login' do it 'redirects to login and queues worker' do
expect(response).to redirect_to(new_user_session_path) expect(response)
end .to redirect_to(new_user_session_path)
expect(BootstrapTimelineWorker)
it 'queues up bootstrapping of home timeline' do .to have_received(:perform_async).with(user.account_id)
expect(BootstrapTimelineWorker).to have_received(:perform_async).with(user.account_id)
end end
end end
@ -88,13 +87,13 @@ RSpec.describe Auth::ConfirmationsController do
get :show, params: { confirmation_token: 'foobar' } get :show, params: { confirmation_token: 'foobar' }
end end
it 'redirects to login and confirms email' do it 'redirects to login, confirms email, does not queue worker' do
expect(response).to redirect_to(new_user_session_path) expect(response)
expect(user.reload.unconfirmed_email).to be_nil .to redirect_to(new_user_session_path)
end expect(user.reload.unconfirmed_email)
.to be_nil
it 'does not queue up bootstrapping of home timeline' do expect(BootstrapTimelineWorker)
expect(BootstrapTimelineWorker).to_not have_received(:perform_async) .to_not have_received(:perform_async)
end end
end end
end end

View file

@ -57,29 +57,30 @@ RSpec.describe Auth::PasswordsController do
post :update, params: { user: { password: password, password_confirmation: password, reset_password_token: token } } post :update, params: { user: { password: password, password_confirmation: password, reset_password_token: token } }
end end
it 'redirect to sign in' do it 'resets the password' do
expect(response).to redirect_to '/auth/sign_in' expect(response)
end .to redirect_to '/auth/sign_in'
it 'changes password' do # Change password
this_user = User.find(user.id) expect(User.find(user.id))
.to be_present
.and be_valid_password(password)
expect(this_user).to_not be_nil # Deactivate session
expect(this_user.valid_password?(password)).to be true expect(user.session_activations.count)
end .to eq 0
expect { session_activation.reload }
.to raise_error(ActiveRecord::RecordNotFound)
it 'deactivates all sessions' do # Revoke tokens
expect(user.session_activations.count).to eq 0 expect(Doorkeeper::AccessToken.active_for(user).count)
expect { session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound) .to eq 0
end
it 'revokes all access tokens' do # Remove push subs
expect(Doorkeeper::AccessToken.active_for(user).count).to eq 0 expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count)
end .to eq 0
expect { web_push_subscription.reload }
it 'removes push subscriptions' do .to raise_error(ActiveRecord::RecordNotFound)
expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count).to eq 0
expect { web_push_subscription.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
end end
@ -88,15 +89,13 @@ RSpec.describe Auth::PasswordsController do
post :update, params: { user: { password: password, password_confirmation: password, reset_password_token: 'some_invalid_value' } } post :update, params: { user: { password: password, password_confirmation: password, reset_password_token: 'some_invalid_value' } }
end end
it 'renders reset password' do it 'renders reset password and retains password' do
expect(response).to render_template(:new) expect(response)
end .to render_template(:new)
it 'retains password' do expect(User.find(user.id))
this_user = User.find(user.id) .to be_present
.and be_external_or_valid_password(user.password)
expect(this_user).to_not be_nil
expect(this_user.external_or_valid_password?(user.password)).to be true
end end
end end
end end

View file

@ -6,27 +6,35 @@ RSpec.describe Auth::RegistrationsController do
render_views render_views
shared_examples 'checks for enabled registrations' do |path| shared_examples 'checks for enabled registrations' do |path|
it 'redirects if it is in single user mode while it is open for registration' do context 'when in single user mode and open for registration' do
Fabricate(:account) before do
Setting.registrations_mode = 'open' Setting.registrations_mode = 'open'
allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true) allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
end
it 'redirects to root' do
Fabricate(:account)
get path get path
expect(response).to redirect_to '/' expect(response).to redirect_to '/'
expect(Rails.configuration.x).to have_received(:single_user_mode) expect(Rails.configuration.x).to have_received(:single_user_mode)
end end
end
it 'redirects if it is not open for registration while it is not in single user mode' do context 'when registrations closed and not in single user mode' do
before do
Setting.registrations_mode = 'none' Setting.registrations_mode = 'none'
allow(Rails.configuration.x).to receive(:single_user_mode).and_return(false) allow(Rails.configuration.x).to receive(:single_user_mode).and_return(false)
end
it 'redirects to root' do
get path get path
expect(response).to redirect_to '/' expect(response).to redirect_to '/'
expect(Rails.configuration.x).to have_received(:single_user_mode) expect(Rails.configuration.x).to have_received(:single_user_mode)
end end
end end
end
describe 'GET #edit' do describe 'GET #edit' do
before do before do
@ -35,12 +43,12 @@ RSpec.describe Auth::RegistrationsController do
get :edit get :edit
end end
it 'returns http success' do it 'returns http success and cache headers' do
expect(response).to have_http_status(200) expect(response)
end .to have_http_status(200)
it 'returns private cache control header' do expect(response.headers['Cache-Control'])
expect(response.headers['Cache-Control']).to include('private, no-store') .to include('private, no-store')
end end
end end
@ -53,14 +61,13 @@ RSpec.describe Auth::RegistrationsController do
sign_in(user, scope: :user) sign_in(user, scope: :user)
end end
it 'returns http success' do it 'returns http success and cache headers' do
put :update put :update
expect(response).to have_http_status(200)
end
it 'returns private cache control headers' do expect(response)
put :update .to have_http_status(200)
expect(response.headers['Cache-Control']).to include('private, no-store') expect(response.headers['Cache-Control'])
.to include('private, no-store')
end end
it 'can update the user email' do it 'can update the user email' do
@ -174,16 +181,14 @@ RSpec.describe Auth::RegistrationsController do
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } } post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
end end
it 'redirects to setup' do it 'redirects to setup and creates user' do
subject subject
expect(response).to redirect_to auth_setup_path
end
it 'creates user' do expect(response)
subject .to redirect_to auth_setup_path
user = User.find_by(email: 'test@example.com') expect(User.find_by(email: 'test@example.com'))
expect(user).to_not be_nil .to be_present
expect(user.locale).to eq(accept_language) .and have_attributes(locale: eq(accept_language))
end end
end end
@ -254,17 +259,18 @@ RSpec.describe Auth::RegistrationsController do
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } } post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
end end
it 'redirects to setup' do it 'redirects to setup and creates user' do
subject subject
expect(response).to redirect_to auth_setup_path
end
it 'creates user' do expect(response)
subject .to redirect_to auth_setup_path
user = User.find_by(email: 'test@example.com')
expect(user).to_not be_nil expect(User.find_by(email: 'test@example.com'))
expect(user.locale).to eq(accept_language) .to be_present
expect(user.approved).to be(false) .and have_attributes(
locale: eq(accept_language),
approved: be(false)
)
end end
end end
@ -276,17 +282,17 @@ RSpec.describe Auth::RegistrationsController do
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } } post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } }
end end
it 'redirects to setup' do it 'redirects to setup and creates user' do
subject subject
expect(response).to redirect_to auth_setup_path
end
it 'creates user' do expect(response).to redirect_to auth_setup_path
subject
user = User.find_by(email: 'test@example.com') expect(User.find_by(email: 'test@example.com'))
expect(user).to_not be_nil .to be_present
expect(user.locale).to eq(accept_language) .and have_attributes(
expect(user.approved).to be(false) locale: eq(accept_language),
approved: be(false)
)
end end
end end
@ -300,17 +306,17 @@ RSpec.describe Auth::RegistrationsController do
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } } post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } }
end end
it 'redirects to setup' do it 'redirects to setup and creates user' do
subject subject
expect(response).to redirect_to auth_setup_path
end
it 'creates user' do expect(response).to redirect_to auth_setup_path
subject
user = User.find_by(email: 'test@example.com') expect(User.find_by(email: 'test@example.com'))
expect(user).to_not be_nil .to be_present
expect(user.locale).to eq(accept_language) .and have_attributes(
expect(user.approved).to be(true) locale: eq(accept_language),
approved: be(true)
)
end end
end end
@ -348,12 +354,11 @@ RSpec.describe Auth::RegistrationsController do
delete :destroy delete :destroy
end end
it 'returns http not found' do it 'returns http not found and keeps user' do
expect(response).to have_http_status(404) expect(response)
end .to have_http_status(404)
expect(User.find(user.id))
it 'does not delete user' do .to_not be_nil
expect(User.find(user.id)).to_not be_nil
end end
end end
end end