2018-06-06 03:47:53 -04:00
|
|
|
# coding: utf-8
|
2013-09-17 16:37:36 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ApplicationController do
|
2018-04-27 10:50:33 -04:00
|
|
|
include TermsHelper
|
|
|
|
|
2016-11-14 09:55:31 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2013-09-17 16:37:36 -04:00
|
|
|
describe '#check_password_expiration' do
|
2017-05-01 11:13:33 -04:00
|
|
|
let(:controller) { described_class.new }
|
2013-09-17 16:37:36 -04:00
|
|
|
|
2017-09-13 07:18:15 -04:00
|
|
|
before do
|
|
|
|
allow(controller).to receive(:session).and_return({})
|
|
|
|
end
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'redirects if the user is over their password expiry' do
|
2013-09-17 16:37:36 -04:00
|
|
|
user.password_expires_at = Time.new(2002)
|
2017-08-31 06:21:40 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(user.ldap_user?).to be_falsey
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
expect(controller).to receive(:redirect_to)
|
|
|
|
expect(controller).to receive(:new_profile_password_path)
|
2017-08-31 06:21:40 -04:00
|
|
|
|
2013-09-17 16:37:36 -04:00
|
|
|
controller.send(:check_password_expiration)
|
|
|
|
end
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'does not redirect if the user is under their password expiry' do
|
2013-09-17 16:37:36 -04:00
|
|
|
user.password_expires_at = Time.now + 20010101
|
2017-08-31 06:21:40 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(user.ldap_user?).to be_falsey
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
2017-08-31 06:21:40 -04:00
|
|
|
|
2013-09-17 16:37:36 -04:00
|
|
|
controller.send(:check_password_expiration)
|
|
|
|
end
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'does not redirect if the user is over their password expiry but they are an ldap user' do
|
2013-09-17 16:37:36 -04:00
|
|
|
user.password_expires_at = Time.new(2002)
|
2017-08-31 06:21:40 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
allow(user).to receive(:ldap_user?).and_return(true)
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
2017-08-31 06:21:40 -04:00
|
|
|
|
2013-09-17 16:37:36 -04:00
|
|
|
controller.send(:check_password_expiration)
|
|
|
|
end
|
2017-06-27 14:02:09 -04:00
|
|
|
|
2017-11-23 08:16:14 -05:00
|
|
|
it 'does not redirect if the user is over their password expiry but password authentication is disabled for the web interface' do
|
|
|
|
stub_application_setting(password_authentication_enabled_for_web: false)
|
|
|
|
stub_application_setting(password_authentication_enabled_for_git: false)
|
2017-06-27 14:02:09 -04:00
|
|
|
user.password_expires_at = Time.new(2002)
|
2017-08-31 06:21:40 -04:00
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
2017-11-23 08:16:14 -05:00
|
|
|
expect(controller).not_to receive(:redirect_to)
|
2017-06-27 14:02:09 -04:00
|
|
|
|
|
|
|
controller.send(:check_password_expiration)
|
|
|
|
end
|
2013-09-17 16:37:36 -04:00
|
|
|
end
|
2016-04-19 06:52:15 -04:00
|
|
|
|
2018-08-07 17:28:57 -04:00
|
|
|
describe '#add_gon_variables' do
|
|
|
|
before do
|
|
|
|
Gon.clear
|
|
|
|
sign_in user
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:json_response) { JSON.parse(response.body) }
|
|
|
|
|
|
|
|
controller(described_class) do
|
|
|
|
def index
|
|
|
|
render json: Gon.all_variables
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'setting gon variables' do
|
|
|
|
it 'sets gon variables' do
|
|
|
|
get :index, format: format
|
|
|
|
|
|
|
|
expect(json_response.size).not_to be_zero
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'not setting gon variables' do
|
|
|
|
it 'does not set gon variables' do
|
|
|
|
get :index, format: format
|
|
|
|
|
|
|
|
expect(json_response.size).to be_zero
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with html format' do
|
|
|
|
let(:format) { :html }
|
|
|
|
|
|
|
|
it_behaves_like 'setting gon variables'
|
|
|
|
|
|
|
|
context 'for peek requests' do
|
|
|
|
before do
|
|
|
|
request.path = '/-/peek'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not setting gon variables'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with json format' do
|
|
|
|
let(:format) { :json }
|
|
|
|
|
|
|
|
it_behaves_like 'not setting gon variables'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
describe "#authenticate_user_from_personal_access_token!" do
|
2018-07-27 06:56:34 -04:00
|
|
|
before do
|
|
|
|
stub_authentication_activity_metrics(debug: false)
|
|
|
|
end
|
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
controller(described_class) do
|
|
|
|
def index
|
|
|
|
render text: 'authenticated'
|
2016-06-01 04:34:38 -04:00
|
|
|
end
|
2016-04-19 06:52:15 -04:00
|
|
|
end
|
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
let(:personal_access_token) { create(:personal_access_token, user: user) }
|
2016-04-19 06:52:15 -04:00
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
context "when the 'personal_access_token' param is populated with the personal access token" do
|
|
|
|
it "logs the user in" do
|
2018-07-27 06:56:34 -04:00
|
|
|
expect(authentication_metrics)
|
|
|
|
.to increment(:user_authenticated_counter)
|
|
|
|
.and increment(:user_session_override_counter)
|
|
|
|
.and increment(:user_sessionless_authentication_counter)
|
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
get :index, private_token: personal_access_token.token
|
2018-07-27 06:56:34 -04:00
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response.body).to eq('authenticated')
|
2016-06-01 04:34:38 -04:00
|
|
|
end
|
2017-10-12 05:01:12 -04:00
|
|
|
end
|
2016-04-19 06:52:15 -04:00
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
context "when the 'PERSONAL_ACCESS_TOKEN' header is populated with the personal access token" do
|
|
|
|
it "logs the user in" do
|
2018-07-27 06:56:34 -04:00
|
|
|
expect(authentication_metrics)
|
|
|
|
.to increment(:user_authenticated_counter)
|
|
|
|
.and increment(:user_session_override_counter)
|
|
|
|
.and increment(:user_sessionless_authentication_counter)
|
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
@request.headers["PRIVATE-TOKEN"] = personal_access_token.token
|
|
|
|
get :index
|
2018-07-27 06:56:34 -04:00
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response.body).to eq('authenticated')
|
2016-06-01 04:34:38 -04:00
|
|
|
end
|
2017-10-12 05:01:12 -04:00
|
|
|
end
|
2016-04-19 06:52:15 -04:00
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
it "doesn't log the user in otherwise" do
|
2018-07-27 06:56:34 -04:00
|
|
|
expect(authentication_metrics)
|
|
|
|
.to increment(:user_unauthenticated_counter)
|
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
get :index, private_token: "token"
|
2018-07-27 06:56:34 -04:00
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
expect(response.status).not_to eq(200)
|
|
|
|
expect(response.body).not_to eq('authenticated')
|
2016-04-19 06:52:15 -04:00
|
|
|
end
|
|
|
|
end
|
2016-11-14 09:55:31 -05:00
|
|
|
|
2018-07-18 14:18:14 -04:00
|
|
|
describe 'session expiration' do
|
|
|
|
controller(described_class) do
|
2018-08-10 15:15:06 -04:00
|
|
|
# The anonymous controller will report 401 and fail to run any actions.
|
|
|
|
# Normally, GitLab will just redirect you to sign in.
|
|
|
|
skip_before_action :authenticate_user!, only: :index
|
|
|
|
|
2018-07-18 14:18:14 -04:00
|
|
|
def index
|
|
|
|
render text: 'authenticated'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authenticated user' do
|
|
|
|
it 'does not set the expire_after option' do
|
|
|
|
sign_in(create(:user))
|
|
|
|
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(request.env['rack.session.options'][:expire_after]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthenticated user' do
|
|
|
|
it 'sets the expire_after option' do
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(request.env['rack.session.options'][:expire_after]).to eq(Settings.gitlab['unauthenticated_session_expire_delay'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-17 12:17:15 -04:00
|
|
|
describe 'rescue from Gitlab::Git::Storage::Inaccessible' do
|
|
|
|
controller(described_class) do
|
|
|
|
def index
|
|
|
|
raise Gitlab::Git::Storage::Inaccessible.new('broken', 100)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders a 503 when storage is not available' do
|
|
|
|
sign_in(create(:user))
|
|
|
|
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(response.status).to eq(503)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders includes a Retry-After header' do
|
|
|
|
sign_in(create(:user))
|
|
|
|
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(response.headers['Retry-After']).to eq(100)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-20 09:53:05 -04:00
|
|
|
describe 'response format' do
|
|
|
|
controller(described_class) do
|
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-12 05:01:12 -04:00
|
|
|
before do
|
|
|
|
sign_in user
|
|
|
|
end
|
|
|
|
|
2017-06-20 09:53:05 -04:00
|
|
|
context 'when format is handled' do
|
|
|
|
let(:requested_format) { :json }
|
|
|
|
|
|
|
|
it 'returns 200 response' do
|
2017-10-12 05:01:12 -04:00
|
|
|
get :index, format: requested_format
|
2017-06-20 09:53:05 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status 200
|
2017-06-20 09:53:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when format is not handled' do
|
|
|
|
it 'returns 404 response' do
|
2017-10-12 05:01:12 -04:00
|
|
|
get :index
|
2017-06-20 09:53:05 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status 404
|
2017-06-20 09:53:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
describe '#authenticate_sessionless_user!' do
|
2018-07-27 06:56:34 -04:00
|
|
|
before do
|
|
|
|
stub_authentication_activity_metrics(debug: false)
|
|
|
|
end
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
describe 'authenticating a user from a feed token' do
|
2017-05-23 09:59:33 -04:00
|
|
|
controller(described_class) do
|
|
|
|
def index
|
|
|
|
render text: 'authenticated'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
context "when the 'feed_token' param is populated with the feed token" do
|
2017-05-23 09:59:33 -04:00
|
|
|
context 'when the request format is atom' do
|
|
|
|
it "logs the user in" do
|
2018-07-27 06:56:34 -04:00
|
|
|
expect(authentication_metrics)
|
|
|
|
.to increment(:user_authenticated_counter)
|
|
|
|
.and increment(:user_session_override_counter)
|
|
|
|
.and increment(:user_sessionless_authentication_counter)
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
get :index, feed_token: user.feed_token, format: :atom
|
2018-07-27 06:56:34 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status 200
|
2017-05-23 09:59:33 -04:00
|
|
|
expect(response.body).to eq 'authenticated'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
context 'when the request format is ics' do
|
|
|
|
it "logs the user in" do
|
2018-07-27 06:56:34 -04:00
|
|
|
expect(authentication_metrics)
|
|
|
|
.to increment(:user_authenticated_counter)
|
|
|
|
.and increment(:user_session_override_counter)
|
|
|
|
.and increment(:user_sessionless_authentication_counter)
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
get :index, feed_token: user.feed_token, format: :ics
|
2018-07-27 06:56:34 -04:00
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
expect(response).to have_gitlab_http_status 200
|
|
|
|
expect(response.body).to eq 'authenticated'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the request format is neither atom nor ics' do
|
2017-05-23 09:59:33 -04:00
|
|
|
it "doesn't log the user in" do
|
2018-07-27 06:56:34 -04:00
|
|
|
expect(authentication_metrics)
|
|
|
|
.to increment(:user_unauthenticated_counter)
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
get :index, feed_token: user.feed_token
|
2018-07-27 06:56:34 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response.status).not_to have_gitlab_http_status 200
|
2017-05-23 09:59:33 -04:00
|
|
|
expect(response.body).not_to eq 'authenticated'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
context "when the 'feed_token' param is populated with an invalid feed token" do
|
2017-05-23 09:59:33 -04:00
|
|
|
it "doesn't log the user" do
|
2018-07-27 06:56:34 -04:00
|
|
|
expect(authentication_metrics)
|
|
|
|
.to increment(:user_unauthenticated_counter)
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
get :index, feed_token: 'token', format: :atom
|
2018-07-27 06:56:34 -04:00
|
|
|
|
2017-05-23 09:59:33 -04:00
|
|
|
expect(response.status).not_to eq 200
|
|
|
|
expect(response.body).not_to eq 'authenticated'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-14 09:55:31 -05:00
|
|
|
describe '#route_not_found' do
|
|
|
|
it 'renders 404 if authenticated' do
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
expect(controller).to receive(:not_found)
|
|
|
|
controller.send(:route_not_found)
|
|
|
|
end
|
|
|
|
|
2017-05-01 16:46:30 -04:00
|
|
|
it 'does redirect to login page via authenticate_user! if not authenticated' do
|
2016-11-14 09:55:31 -05:00
|
|
|
allow(controller).to receive(:current_user).and_return(nil)
|
2017-05-01 16:46:30 -04:00
|
|
|
expect(controller).to receive(:authenticate_user!)
|
2016-11-14 09:55:31 -05:00
|
|
|
controller.send(:route_not_found)
|
|
|
|
end
|
|
|
|
end
|
2017-01-24 16:09:58 -05:00
|
|
|
|
2017-10-20 12:44:29 -04:00
|
|
|
describe '#set_page_title_header' do
|
|
|
|
let(:controller) { described_class.new }
|
|
|
|
|
|
|
|
it 'URI encodes UTF-8 characters in the title' do
|
|
|
|
response = double(headers: {})
|
|
|
|
allow_any_instance_of(PageLayoutHelper).to receive(:page_title).and_return('€100 · GitLab')
|
|
|
|
allow(controller).to receive(:response).and_return(response)
|
|
|
|
|
|
|
|
controller.send(:set_page_title_header)
|
|
|
|
|
|
|
|
expect(response.headers['Page-Title']).to eq('%E2%82%AC100%20%C2%B7%20GitLab')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-24 16:09:58 -05:00
|
|
|
context 'two-factor authentication' do
|
2017-05-01 11:13:33 -04:00
|
|
|
let(:controller) { described_class.new }
|
2017-01-24 16:09:58 -05:00
|
|
|
|
2017-03-07 13:48:57 -05:00
|
|
|
describe '#check_two_factor_requirement' do
|
|
|
|
subject { controller.send :check_two_factor_requirement }
|
2017-01-24 16:09:58 -05:00
|
|
|
|
|
|
|
it 'does not redirect if 2FA is not required' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(false)
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not redirect if user is not logged in' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
|
|
|
|
allow(controller).to receive(:current_user).and_return(nil)
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not redirect if user has 2FA enabled' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
|
|
|
|
allow(controller).to receive(:current_user).twice.and_return(user)
|
|
|
|
allow(user).to receive(:two_factor_enabled?).and_return(true)
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not redirect if 2FA setup can be skipped' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
|
|
|
|
allow(controller).to receive(:current_user).twice.and_return(user)
|
|
|
|
allow(user).to receive(:two_factor_enabled?).and_return(false)
|
|
|
|
allow(controller).to receive(:skip_two_factor?).and_return(true)
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to 2FA setup otherwise' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
|
|
|
|
allow(controller).to receive(:current_user).twice.and_return(user)
|
|
|
|
allow(user).to receive(:two_factor_enabled?).and_return(false)
|
|
|
|
allow(controller).to receive(:skip_two_factor?).and_return(false)
|
|
|
|
allow(controller).to receive(:profile_two_factor_auth_path)
|
|
|
|
expect(controller).to receive(:redirect_to)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#two_factor_authentication_required?' do
|
|
|
|
subject { controller.send :two_factor_authentication_required? }
|
|
|
|
|
|
|
|
it 'returns false if no 2FA requirement is present' do
|
|
|
|
allow(controller).to receive(:current_user).and_return(nil)
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if a 2FA requirement is set in the application settings' do
|
|
|
|
stub_application_setting require_two_factor_authentication: true
|
|
|
|
allow(controller).to receive(:current_user).and_return(nil)
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if a 2FA requirement is set on the user' do
|
2017-03-14 09:34:21 -04:00
|
|
|
user.require_two_factor_authentication_from_group = true
|
2017-01-24 16:09:58 -05:00
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#two_factor_grace_period' do
|
|
|
|
subject { controller.send :two_factor_grace_period }
|
|
|
|
|
|
|
|
it 'returns the grace period from the application settings' do
|
|
|
|
stub_application_setting two_factor_grace_period: 23
|
|
|
|
allow(controller).to receive(:current_user).and_return(nil)
|
|
|
|
|
|
|
|
expect(subject).to eq 23
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a 2FA requirement set on the user' do
|
2017-03-14 09:34:21 -04:00
|
|
|
let(:user) { create :user, require_two_factor_authentication_from_group: true, two_factor_grace_period: 23 }
|
2017-01-24 16:09:58 -05:00
|
|
|
|
|
|
|
it 'returns the user grace period if lower than the application grace period' do
|
|
|
|
stub_application_setting two_factor_grace_period: 24
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
|
|
|
|
expect(subject).to eq 23
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the application grace period if lower than the user grace period' do
|
|
|
|
stub_application_setting two_factor_grace_period: 22
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
|
|
|
|
expect(subject).to eq 22
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#two_factor_grace_period_expired?' do
|
|
|
|
subject { controller.send :two_factor_grace_period_expired? }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the user has not started their grace period yet' do
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with grace period started' do
|
|
|
|
let(:user) { create :user, otp_grace_period_started_at: 2.hours.ago }
|
|
|
|
|
|
|
|
it 'returns true if the grace period has expired' do
|
|
|
|
allow(controller).to receive(:two_factor_grace_period).and_return(1)
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the grace period is still active' do
|
|
|
|
allow(controller).to receive(:two_factor_grace_period).and_return(3)
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#two_factor_skippable' do
|
|
|
|
subject { controller.send :two_factor_skippable? }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if 2FA is not required' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(false)
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the user has already enabled 2FA' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
|
|
|
|
allow(user).to receive(:two_factor_enabled?).and_return(true)
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the 2FA grace period has expired' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
|
|
|
|
allow(user).to receive(:two_factor_enabled?).and_return(false)
|
|
|
|
allow(controller).to receive(:two_factor_grace_period_expired?).and_return(true)
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true otherwise' do
|
|
|
|
allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
|
|
|
|
allow(user).to receive(:two_factor_enabled?).and_return(false)
|
|
|
|
allow(controller).to receive(:two_factor_grace_period_expired?).and_return(false)
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#skip_two_factor?' do
|
|
|
|
subject { controller.send :skip_two_factor? }
|
|
|
|
|
|
|
|
it 'returns false if 2FA setup was not skipped' do
|
|
|
|
allow(controller).to receive(:session).and_return({})
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 2FA setup skipped' do
|
|
|
|
before do
|
2017-03-07 13:51:22 -05:00
|
|
|
allow(controller).to receive(:session).and_return({ skip_two_factor: 2.hours.from_now })
|
2017-01-24 16:09:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the grace period has expired' do
|
|
|
|
Timecop.freeze(3.hours.from_now) do
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if the grace period is still active' do
|
|
|
|
Timecop.freeze(1.hour.from_now) do
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-27 10:50:33 -04:00
|
|
|
|
|
|
|
context 'terms' do
|
|
|
|
controller(described_class) do
|
|
|
|
def index
|
|
|
|
render text: 'authenticated'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
|
|
|
|
sign_in user
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not query more when terms are enforced' do
|
|
|
|
control = ActiveRecord::QueryRecorder.new { get :index }
|
|
|
|
|
|
|
|
enforce_terms
|
|
|
|
|
|
|
|
expect { get :index }.not_to exceed_query_limit(control)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when terms are enforced' do
|
|
|
|
before do
|
|
|
|
enforce_terms
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects if the user did not accept the terms' do
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(302)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not redirect when the user accepted terms' do
|
|
|
|
accept_terms(user)
|
|
|
|
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for sessionless users' do
|
2018-06-13 10:05:55 -04:00
|
|
|
render_views
|
|
|
|
|
2018-04-27 10:50:33 -04:00
|
|
|
before do
|
|
|
|
sign_out user
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders a 403 when the sessionless user did not accept the terms' do
|
2018-05-31 10:01:04 -04:00
|
|
|
get :index, feed_token: user.feed_token, format: :atom
|
2018-04-27 10:50:33 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(403)
|
|
|
|
end
|
|
|
|
|
2018-06-13 10:05:55 -04:00
|
|
|
it 'renders the error message when the format was html' do
|
|
|
|
get :index,
|
|
|
|
private_token: create(:personal_access_token, user: user).token,
|
|
|
|
format: :html
|
|
|
|
|
|
|
|
expect(response.body).to have_content /accept the terms of service/i
|
|
|
|
end
|
|
|
|
|
2018-04-27 10:50:33 -04:00
|
|
|
it 'renders a 200 when the sessionless user accepted the terms' do
|
|
|
|
accept_terms(user)
|
|
|
|
|
2018-05-31 10:01:04 -04:00
|
|
|
get :index, feed_token: user.feed_token, format: :atom
|
2018-04-27 10:50:33 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-04 11:04:04 -04:00
|
|
|
|
2018-06-06 03:47:53 -04:00
|
|
|
describe '#append_info_to_payload' do
|
|
|
|
controller(described_class) do
|
|
|
|
attr_reader :last_payload
|
|
|
|
|
|
|
|
def index
|
|
|
|
render text: 'authenticated'
|
|
|
|
end
|
|
|
|
|
|
|
|
def append_info_to_payload(payload)
|
|
|
|
super
|
|
|
|
|
|
|
|
@last_payload = payload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not log errors with a 200 response' do
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(controller.last_payload.has_key?(:response)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context '422 errors' do
|
|
|
|
it 'logs a response with a string' do
|
2018-06-13 11:35:39 -04:00
|
|
|
response = spy(ActionDispatch::Response, status: 422, body: 'Hello world', content_type: 'application/json', cookies: {})
|
2018-06-06 03:47:53 -04:00
|
|
|
allow(controller).to receive(:response).and_return(response)
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(controller.last_payload[:response]).to eq('Hello world')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs a response with an array' do
|
|
|
|
body = ['I want', 'my hat back']
|
2018-06-13 11:35:39 -04:00
|
|
|
response = spy(ActionDispatch::Response, status: 422, body: body, content_type: 'application/json', cookies: {})
|
2018-06-06 03:47:53 -04:00
|
|
|
allow(controller).to receive(:response).and_return(response)
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(controller.last_payload[:response]).to eq(body)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not log a string with an empty body' do
|
2018-06-13 11:35:39 -04:00
|
|
|
response = spy(ActionDispatch::Response, status: 422, body: nil, content_type: 'application/json', cookies: {})
|
2018-06-06 03:47:53 -04:00
|
|
|
allow(controller).to receive(:response).and_return(response)
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(controller.last_payload.has_key?(:response)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not log an HTML body' do
|
2018-06-13 11:35:39 -04:00
|
|
|
response = spy(ActionDispatch::Response, status: 422, body: 'This is a test', content_type: 'application/html', cookies: {})
|
2018-06-06 03:47:53 -04:00
|
|
|
allow(controller).to receive(:response).and_return(response)
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(controller.last_payload.has_key?(:response)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-04 11:04:04 -04:00
|
|
|
describe '#access_denied' do
|
|
|
|
controller(described_class) do
|
|
|
|
def index
|
|
|
|
access_denied!(params[:message])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in user
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders a 404 without a message' do
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders a 403 when a message is passed to access denied' do
|
|
|
|
get :index, message: 'None shall pass'
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(403)
|
|
|
|
end
|
|
|
|
end
|
2014-06-21 05:18:57 -04:00
|
|
|
end
|