debian-mirror-gitlab/spec/controllers/users/terms_controller_spec.rb

158 lines
3.7 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-10-15 14:42:47 +05:30
require 'spec_helper'
describe Users::TermsController do
2018-11-08 19:23:39 +05:30
include TermsHelper
2020-04-08 14:13:33 +05:30
let_it_be(:user) { create(:user) }
2018-11-08 19:23:39 +05:30
let(:term) { create(:term) }
2018-10-15 14:42:47 +05:30
before do
sign_in user
end
describe 'GET #index' do
2020-04-08 14:13:33 +05:30
context 'when a user is signed in' do
it 'redirects when no terms exist' do
get :index
expect(response).to redirect_to(root_path)
end
context 'when terms exist' do
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
term
end
it 'shows terms when they exist' do
get :index
expect(response).to have_gitlab_http_status(:success)
end
it 'shows a message when the user already accepted the terms' do
accept_terms(user)
get :index
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
expect(controller).to set_flash.now[:notice].to(/already accepted/)
end
end
2018-10-15 14:42:47 +05:30
end
2020-04-08 14:13:33 +05:30
context 'when a user is not signed in' do
2018-11-08 19:23:39 +05:30
before do
2020-04-08 14:13:33 +05:30
sign_out user
2018-11-08 19:23:39 +05:30
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
context 'when terms exist' do
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
term
end
2018-11-08 19:23:39 +05:30
2020-04-08 14:13:33 +05:30
it 'returns success response' do
get :index
2018-11-08 19:23:39 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:success)
end
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
context 'when no terms exist' do
it 'redirects' do
get :index
2018-11-08 19:23:39 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(root_path)
end
2018-11-08 19:23:39 +05:30
end
2018-10-15 14:42:47 +05:30
end
end
describe 'POST #accept' do
2020-04-08 14:13:33 +05:30
context 'when a user is signed in' do
it 'saves that the user accepted the terms' do
post :accept, params: { id: term.id }
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
agreement = user.term_agreements.find_by(term: term)
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
expect(agreement.accepted).to eq(true)
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
it 'redirects to a path when specified' do
post :accept, params: { id: term.id, redirect: groups_path }
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(groups_path)
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
it 'redirects to the referer when no redirect specified' do
request.env["HTTP_REFERER"] = groups_url
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
post :accept, params: { id: term.id }
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(groups_path)
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
context 'redirecting to another domain' do
it 'is prevented when passing a redirect param' do
post :accept, params: { id: term.id, redirect: '//example.com/random/path' }
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(root_path)
end
it 'is prevented when redirecting to the referer' do
request.env["HTTP_REFERER"] = 'http://example.com/and/a/path'
post :accept, params: { id: term.id }
expect(response).to redirect_to(root_path)
end
2018-10-15 14:42:47 +05:30
end
2020-04-08 14:13:33 +05:30
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
context 'when a user is not signed in' do
before do
sign_out user
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
it 'redirects to login page' do
2019-02-15 15:39:39 +05:30
post :accept, params: { id: term.id }
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(new_user_session_path)
2018-10-15 14:42:47 +05:30
end
end
end
describe 'POST #decline' do
2020-04-08 14:13:33 +05:30
context 'when a user is signed in' do
it 'stores that the user declined the terms' do
post :decline, params: { id: term.id }
agreement = user.term_agreements.find_by(term: term)
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
expect(agreement.accepted).to eq(false)
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
it 'signs out the user' do
post :decline, params: { id: term.id }
expect(response).to redirect_to(root_path)
expect(assigns(:current_user)).to be_nil
end
2018-10-15 14:42:47 +05:30
end
2020-04-08 14:13:33 +05:30
context 'when a user is not signed in' do
before do
sign_out user
end
2018-10-15 14:42:47 +05:30
2020-04-08 14:13:33 +05:30
it 'redirects to login page' do
post :decline, params: { id: term.id }
expect(response).to redirect_to(new_user_session_path)
end
2018-10-15 14:42:47 +05:30
end
end
end