1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'VerifiesWithEmail', :clean_gitlab_redis_sessions, :clean_gitlab_redis_rate_limiting,
feature_category: :instance_resiliency do
include SessionHelpers
include EmailHelpers
let(:user) { create(:user) }
shared_examples_for 'does not send verification instructions' do
let(:recipient_email) { nil }
specify do
mail = find_email_for(recipient_email || user)
expect(mail&.subject).not_to eq(s_('IdentityVerification|Verify your identity'))
end
end
shared_examples_for 'locks the user and sends verification instructions' do
let(:recipient_email) { nil }
it 'locks the user' do
user.reload
expect(user.unlock_token).not_to be_nil
expect(user.locked_at).not_to be_nil
end
it 'sends an email', :aggregate_failures do
mail = find_email_for(recipient_email || user)
expect(mail.to).to match_array([recipient_email || user.email])
expect(mail.subject).to eq(s_('IdentityVerification|Verify your identity'))
end
end
shared_examples_for 'send verification instructions' do
it_behaves_like 'locks the user and sends verification instructions'
context 'when an unconfirmed verification email exists' do
let(:new_email) { 'new@email' }
let(:user) { create(:user, unconfirmed_email: new_email, confirmation_sent_at: 1.minute.ago) }
it 'sends a verification instructions email to the unconfirmed email address' do
mail = ActionMailer::Base.deliveries.find { |d| d.to.include?(new_email) }
expect(mail.subject).to eq(s_('IdentityVerification|Verify your identity'))
end
end
end
shared_examples_for 'prompt for email verification' do
it 'sets the verification_user_id session variable and renders the email verification template',
:aggregate_failures do
expect(request.session[:verification_user_id]).to eq(user.id)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('devise/sessions/email_verification')
end
end
shared_examples_for 'rate limited' do
it 'redirects to the login form and shows an alert message' do
expect(response).to redirect_to(new_user_session_path)
expect(flash[:alert])
.to eq(s_('IdentityVerification|Maximum login attempts exceeded. Wait 10 minutes and try again.'))
end
end
shared_examples_for 'two factor prompt or successful login' do
it 'shows the 2FA prompt when enabled or redirects to the root path' do
if user.two_factor_enabled?
expect(response.body).to include('Enter verification code')
else
expect(response).to redirect_to(root_path)
end
end
end
shared_examples_for 'verifying with email' do
context 'when rate limited' do
before do
allow(Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(true)
sign_in
end
it_behaves_like 'rate limited'
end
context 'when the user already has an unlock_token set' do
before do
user.update!(unlock_token: 'token')
sign_in
end
it_behaves_like 'prompt for email verification'
end
context 'when the user is already locked' do
before do
user.update!(locked_at: Time.current)
perform_enqueued_jobs { sign_in }
end
it_behaves_like 'send verification instructions'
it_behaves_like 'prompt for email verification'
end
context 'when the user is signing in from an unknown ip address' do
let(:ip_check_enabled) { true }
before do
stub_feature_flags(check_ip_address_for_email_verification: ip_check_enabled)
allow(AuthenticationEvent)
.to receive(:initial_login_or_known_ip_address?)
.and_return(false)
perform_enqueued_jobs { sign_in }
end
it_behaves_like 'send verification instructions'
it_behaves_like 'prompt for email verification'
context 'when the check_ip_address_for_email_verification feature flag is disabled' do
let(:ip_check_enabled) { false }
it_behaves_like 'not verifying with email'
end
end
end
shared_examples_for 'not verifying with email' do
context 'when rate limited' do
before do
allow(Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(true)
sign_in
end
it_behaves_like 'two factor prompt or successful login'
end
context 'when the user already has an unlock_token set' do
before do
user.update!(unlock_token: 'token')
sign_in
end
it_behaves_like 'two factor prompt or successful login'
end
context 'when the user is signing in from an unknown ip address' do
before do
allow(AuthenticationEvent)
.to receive(:initial_login_or_known_ip_address?)
.and_return(false)
sign_in
end
it_behaves_like 'two factor prompt or successful login'
end
end
describe 'verify_with_email' do
context 'when user is locked and a verification_user_id session variable exists' do
before do
encrypted_token = Devise.token_generator.digest(User, user.email, 'token')
user.update!(locked_at: Time.current, unlock_token: encrypted_token)
stub_session(session_data: { verification_user_id: user.id })
end
context 'when rate limited and a verification_token param exists' do
before do
allow(Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(true)
post(user_session_path(user: { verification_token: 'token' }))
end
it 'adds a verification error message' do
expect(json_response)
.to include('message' => "You've reached the maximum amount of tries. "\
'Wait 10 minutes or send a new code and try again.')
end
end
context 'when an invalid verification_token param exists' do
before do
post(user_session_path(user: { verification_token: 'invalid_token' }))
end
it 'adds a verification error message' do
expect(json_response)
.to include('message' => s_('IdentityVerification|The code is incorrect. '\
'Enter it again, or send a new code.'))
end
end
context 'when an expired verification_token param exists' do
before do
user.update!(locked_at: 1.hour.ago)
post(user_session_path(user: { verification_token: 'token' }))
end
it 'adds a verification error message' do
expect(json_response)
.to include('message' => s_('IdentityVerification|The code has expired. Send a new code and try again.'))
end
end
context 'when a valid verification_token param exists' do
subject(:submit_token) { post(user_session_path(user: { verification_token: 'token' })) }
it 'unlocks the user, create logs and records the activity', :freeze_time do
expect { submit_token }.to change { user.reload.unlock_token }.to(nil)
.and change { user.locked_at }.to(nil)
.and change { AuditEvent.count }.by(1)
.and change { AuthenticationEvent.count }.by(1)
.and change { user.last_activity_on }.to(Date.today)
.and change { user.email_reset_offered_at }.to(Time.current)
end
it 'returns the success status and a redirect path' do
submit_token
expect(json_response).to eq('status' => 'success', 'redirect_path' => users_successful_verification_path)
end
context 'when an unconfirmed verification email exists' do
before do
user.update!(email: new_email)
end
let(:new_email) { 'new@email' }
it 'confirms the email' do
expect { submit_token }
.to change { user.reload.email }.to(new_email)
.and change { user.confirmed_at }
.and change { user.unconfirmed_email }.from(new_email).to(nil)
end
end
context 'when email reset has already been offered' do
before do
user.update!(email_reset_offered_at: 1.hour.ago, email: 'new@email')
end
it 'does not change the email_reset_offered_at field' do
expect { submit_token }.not_to change { user.reload.email_reset_offered_at }
end
it 'does not confirm the email' do
expect { submit_token }.not_to change { user.reload.email }
end
end
end
context 'when not completing identity verification and logging in with another account' do
let(:another_user) { create(:user) }
before do
post user_session_path, params: { user: { login: another_user.username, password: another_user.password } }
end
it 'redirects to the root path' do
expect(response).to redirect_to(root_path)
end
end
end
context 'when signing in with a valid password' do
let(:headers) { {} }
let(:sign_in) do
post user_session_path, params: { user: { login: user.username, password: user.password } }, headers: headers
end
it_behaves_like 'not verifying with email'
context 'when the feature flag is toggled on' do
before do
stub_feature_flags(require_email_verification: user)
stub_feature_flags(skip_require_email_verification: false)
end
it_behaves_like 'verifying with email'
context 'when 2FA is enabled' do
before do
user.update!(otp_required_for_login: true)
end
it_behaves_like 'not verifying with email'
end
context 'when request is not from a QA user' do
before do
allow(Gitlab::Qa).to receive(:request?).and_return(false)
end
it_behaves_like 'verifying with email'
end
context 'when the skip_require_email_verification feature flag is turned on' do
before do
stub_feature_flags(skip_require_email_verification: user)
end
it_behaves_like 'not verifying with email'
end
end
end
end
describe 'resend_verification_code' do
let(:params) { { user: { email: '' } } }
context 'when no verification_user_id session variable exists' do
before do
post(users_resend_verification_code_path, params: params)
end
it 'returns 204 No Content' do
expect(response).to have_gitlab_http_status(:no_content)
expect(response.body).to be_empty
end
end
context 'when a verification_user_id session variable exists' do
before do
stub_session(session_data: { verification_user_id: user.id })
perform_enqueued_jobs do
post(users_resend_verification_code_path, params: params)
end
end
it_behaves_like 'send verification instructions'
context 'when user => email param is present' do
context 'when email param matches the user\'s verified primary email' do
let(:params) { { user: { email: user.email } } }
it_behaves_like 'locks the user and sends verification instructions'
end
context 'when email param matches one of the user\'s verified secondary emails' do
let(:secondary_email) { create(:email, :confirmed, user: user) }
let(:params) { { user: { email: secondary_email.email } } }
it_behaves_like 'locks the user and sends verification instructions' do
let(:recipient_email) { secondary_email.email }
end
end
context 'when email param matches one of the user\'s unverified secondary emails' do
let(:secondary_email) { create(:email, user: user) }
let(:params) { { user: { email: secondary_email.email } } }
it_behaves_like 'does not send verification instructions' do
let(:recipient_email) { secondary_email.email }
end
end
context 'when email param does not match any of the user\'s verified emails' do
let(:bad_actor) { create(:user) }
let(:params) { { user: { email: bad_actor.email } } }
it_behaves_like 'does not send verification instructions' do
let(:recipient_email) { bad_actor.email }
end
end
end
end
context 'when exceeding the rate limit' do
before do
allow(Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(true)
stub_session(session_data: { verification_user_id: user.id })
perform_enqueued_jobs do
post(users_resend_verification_code_path, params: params)
end
end
it 'does not lock the user' do
user.reload
expect(user.unlock_token).to be_nil
expect(user.locked_at).to be_nil
end
it 'does not send an email' do
mail = find_email_for(user)
expect(mail).to be_nil
end
end
end
describe 'update_email' do
let(:new_email) { 'new@email' }
subject(:do_request) { patch(users_update_email_path(user: { email: new_email })) }
context 'when no verification_user_id session variable exists' do
it 'returns 204 No Content' do
do_request
expect(response).to have_gitlab_http_status(:no_content)
expect(response.body).to be_empty
end
end
context 'when a verification_user_id session variable exists' do
before do
stub_session(session_data: { verification_user_id: user.id })
end
it 'locks the user' do
do_request
expect(user.reload.unlock_token).not_to be_nil
expect(user.locked_at).not_to be_nil
end
it 'sends a changed notification to the primary email and verification instructions to the unconfirmed email' do
perform_enqueued_jobs { do_request }
sent_mails = ActionMailer::Base.deliveries.map { |mail| { mail.to[0] => mail.subject } }
expect(sent_mails).to match_array([
{ user.reload.unconfirmed_email => s_('IdentityVerification|Verify your identity') },
{ user.email => 'Email Changed' }
])
end
it 'calls the UpdateEmailService and returns a success response' do
expect_next_instance_of(Users::EmailVerification::UpdateEmailService, user: user) do |instance|
expect(instance).to receive(:execute).with(email: new_email).and_call_original
end
do_request
expect(json_response).to eq('status' => 'success')
end
end
context 'when failing to update the email address' do
let(:service_response) do
{
status: 'failure',
reason: 'the reason',
message: 'the message'
}
end
before do
stub_session(session_data: { verification_user_id: user.id })
end
it 'calls the UpdateEmailService and returns an error response' do
expect_next_instance_of(Users::EmailVerification::UpdateEmailService, user: user) do |instance|
expect(instance).to receive(:execute).with(email: new_email).and_return(service_response)
end
do_request
expect(json_response).to eq(service_response.with_indifferent_access)
end
end
end
describe 'successful_verification' do
before do
allow(user).to receive(:role_required?).and_return(true) # It skips the required signup info before_action
sign_in(user)
end
it 'renders the template and removes the verification_user_id session variable' do
stub_session(session_data: { verification_user_id: user.id })
get(users_successful_verification_path)
expect(request.session.has_key?(:verification_user_id)).to eq(false)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('successful_verification', layout: 'minimal')
expect(response.body).to include(root_path)
end
end
end
|