debian-mirror-gitlab/spec/support/shared_examples/controllers/known_sign_in_shared_examples.rb

116 lines
2.8 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
RSpec.shared_examples 'known sign in' do
def stub_remote_ip(ip)
request.remote_ip = ip
end
def stub_user_ip(ip)
user.update!(current_sign_in_ip: ip)
end
2020-07-28 23:09:34 +05:30
def stub_cookie(value = user.id)
cookies.encrypted[KnownSignIn::KNOWN_SIGN_IN_COOKIE] = {
value: value, expires: KnownSignIn::KNOWN_SIGN_IN_COOKIE_EXPIRY
}
end
context 'when the remote IP and the last sign in IP match' do
before do
stub_user_ip('169.0.0.1')
stub_remote_ip('169.0.0.1')
end
it 'does not notify the user' do
expect(NotificationService).not_to receive(:new)
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
post_action
end
it 'sets/updates the encrypted cookie' do
post_action
expect(cookies.encrypted[KnownSignIn::KNOWN_SIGN_IN_COOKIE]).to eq(user.id)
end
end
context 'when the remote IP and the last sign in IP do not match' do
before do
stub_user_ip('127.0.0.1')
stub_remote_ip('169.0.0.1')
end
context 'when the cookie is not previously set' do
2020-05-24 23:13:21 +05:30
it 'notifies the user' do
expect_next_instance_of(NotificationService) do |instance|
expect(instance).to receive(:unknown_sign_in)
end
post_action
end
2020-07-28 23:09:34 +05:30
it 'sets the encrypted cookie' do
post_action
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
expect(cookies.encrypted[KnownSignIn::KNOWN_SIGN_IN_COOKIE]).to eq(user.id)
2020-05-24 23:13:21 +05:30
end
2020-07-28 23:09:34 +05:30
end
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
it 'notifies the user when the cookie is expired' do
stub_cookie
Timecop.freeze((KnownSignIn::KNOWN_SIGN_IN_COOKIE_EXPIRY + 1.day).from_now) do
expect_next_instance_of(NotificationService) do |instance|
expect(instance).to receive(:unknown_sign_in)
end
2020-05-24 23:13:21 +05:30
post_action
end
end
2020-07-28 23:09:34 +05:30
context 'when notify_on_unknown_sign_in global setting is false' do
2020-05-24 23:13:21 +05:30
before do
2020-07-28 23:09:34 +05:30
stub_application_setting(notify_on_unknown_sign_in: false)
2020-05-24 23:13:21 +05:30
end
it 'does not notify the user' do
2020-07-28 23:09:34 +05:30
expect(NotificationService).not_to receive(:new)
post_action
end
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
it 'does not set a cookie' do
2020-05-24 23:13:21 +05:30
post_action
2020-07-28 23:09:34 +05:30
expect(cookies.encrypted[KnownSignIn::KNOWN_SIGN_IN_COOKIE]).to be_nil
end
end
it 'notifies the user when the cookie is for another user' do
stub_cookie(create(:user).id)
expect_next_instance_of(NotificationService) do |instance|
expect(instance).to receive(:unknown_sign_in)
2020-05-24 23:13:21 +05:30
end
2020-07-28 23:09:34 +05:30
post_action
end
it 'does not notify the user when remote IP matches an active session' do
ActiveSession.set(user, request)
expect(NotificationService).not_to receive(:new)
post_action
end
it 'does not notify the user when the cookie is present and not expired' do
stub_cookie
expect(NotificationService).not_to receive(:new)
post_action
2020-05-24 23:13:21 +05:30
end
end
end