]> cat aescling's git repositories - mastodon.git/blob - spec/controllers/auth/sessions_controller_spec.rb
Go to root after login in single user mode (#3289)
[mastodon.git] / spec / controllers / auth / sessions_controller_spec.rb
1 # frozen_string_literal: true
2
3 require 'rails_helper'
4
5 RSpec.describe Auth::SessionsController, type: :controller do
6 render_views
7
8 describe 'GET #new' do
9 before do
10 request.env['devise.mapping'] = Devise.mappings[:user]
11 end
12
13 it 'returns http success' do
14 get :new
15 expect(response).to have_http_status(:success)
16 end
17 end
18
19 describe 'DELETE #destroy' do
20 let(:user) { Fabricate(:user) }
21
22 before do
23 request.env['devise.mapping'] = Devise.mappings[:user]
24 end
25
26 context 'with a regular user' do
27 it 'redirects to home after sign out' do
28 sign_in(user, scope: :user)
29 delete :destroy
30
31 expect(response).to redirect_to(root_path)
32 end
33 end
34
35 context 'with a suspended user' do
36 it 'redirects to home after sign out' do
37 Fabricate(:account, user: user, suspended: true)
38 sign_in(user, scope: :user)
39 delete :destroy
40
41 expect(response).to redirect_to(root_path)
42 end
43 end
44 end
45
46 describe 'POST #create' do
47 before do
48 request.env['devise.mapping'] = Devise.mappings[:user]
49 end
50
51 context 'using password authentication' do
52 let(:user) { Fabricate(:user, email: 'foo@bar.com', password: 'abcdefgh') }
53
54 context 'using a valid password' do
55 before do
56 post :create, params: { user: { email: user.email, password: user.password } }
57 end
58
59 it 'redirects to home' do
60 expect(response).to redirect_to(root_path)
61 end
62
63 it 'logs the user in' do
64 expect(controller.current_user).to eq user
65 end
66 end
67
68 context 'using an invalid password' do
69 before do
70 post :create, params: { user: { email: user.email, password: 'wrongpw' } }
71 end
72
73 it 'shows a login error' do
74 expect(flash[:alert]).to match I18n.t('devise.failure.invalid', authentication_keys: 'Email')
75 end
76
77 it "doesn't log the user in" do
78 expect(controller.current_user).to be_nil
79 end
80 end
81
82 context 'using an unconfirmed password' do
83 before do
84 request.headers['Accept-Language'] = accept_language
85 post :create, params: { user: { email: unconfirmed_user.email, password: unconfirmed_user.password } }
86 end
87
88 let(:unconfirmed_user) { user.tap { |u| u.update!(confirmed_at: nil) } }
89 let(:accept_language) { 'fr' }
90
91 it 'shows a translated login error' do
92 expect(flash[:alert]).to eq(I18n.t('devise.failure.unconfirmed', locale: accept_language))
93 end
94 end
95
96 context "logging in from the user's page" do
97 before do
98 allow(controller).to receive(:single_user_mode?).and_return(single_user_mode)
99 allow(controller).to receive(:stored_location_for).with(:user).and_return("/@#{user.account.username}")
100 post :create, params: { user: { email: user.email, password: user.password } }
101 end
102
103 context "in single user mode" do
104 let(:single_user_mode) { true }
105
106 it 'redirects to home' do
107 expect(response).to redirect_to(root_path)
108 end
109 end
110
111 context "in non-single user mode" do
112 let(:single_user_mode) { false }
113
114 it "redirects back to the user's page" do
115 expect(response).to redirect_to(short_account_path(username: user.account))
116 end
117 end
118 end
119 end
120
121 context 'using two-factor authentication' do
122 let(:user) do
123 Fabricate(:user, email: 'x@y.com', password: 'abcdefgh',
124 otp_required_for_login: true, otp_secret: User.generate_otp_secret(32))
125 end
126 let(:recovery_codes) do
127 codes = user.generate_otp_backup_codes!
128 user.save
129 return codes
130 end
131
132 context 'using a valid OTP' do
133 before do
134 post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
135 end
136
137 it 'redirects to home' do
138 expect(response).to redirect_to(root_path)
139 end
140
141 it 'logs the user in' do
142 expect(controller.current_user).to eq user
143 end
144 end
145
146 context 'when the server has an decryption error' do
147 before do
148 allow_any_instance_of(User).to receive(:validate_and_consume_otp!).and_raise(OpenSSL::Cipher::CipherError)
149 post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
150 end
151
152 it 'shows a login error' do
153 expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
154 end
155
156 it "doesn't log the user in" do
157 expect(controller.current_user).to be_nil
158 end
159 end
160
161 context 'using a valid recovery code' do
162 before do
163 post :create, params: { user: { otp_attempt: recovery_codes.first } }, session: { otp_user_id: user.id }
164 end
165
166 it 'redirects to home' do
167 expect(response).to redirect_to(root_path)
168 end
169
170 it 'logs the user in' do
171 expect(controller.current_user).to eq user
172 end
173 end
174
175 context 'using an invalid OTP' do
176 before do
177 post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { otp_user_id: user.id }
178 end
179
180 it 'shows a login error' do
181 expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
182 end
183
184 it "doesn't log the user in" do
185 expect(controller.current_user).to be_nil
186 end
187 end
188 end
189 end
190 end
This page took 0.206347 seconds and 5 git commands to generate.