diff options
Diffstat (limited to 'spec/controllers/concerns')
-rw-r--r-- | spec/controllers/concerns/redis_tracking_spec.rb | 115 | ||||
-rw-r--r-- | spec/controllers/concerns/spammable_actions_spec.rb | 99 |
2 files changed, 49 insertions, 165 deletions
diff --git a/spec/controllers/concerns/redis_tracking_spec.rb b/spec/controllers/concerns/redis_tracking_spec.rb index ef59adf8c1d..53b49dd30a6 100644 --- a/spec/controllers/concerns/redis_tracking_spec.rb +++ b/spec/controllers/concerns/redis_tracking_spec.rb @@ -3,18 +3,13 @@ require "spec_helper" RSpec.describe RedisTracking do - let(:feature) { 'approval_rule' } let(:user) { create(:user) } - before do - skip_feature_flags_yaml_validation - end - controller(ApplicationController) do include RedisTracking skip_before_action :authenticate_user!, only: :show - track_redis_hll_event :index, :show, name: 'g_compliance_approval_rules', feature: :approval_rule, feature_default_enabled: true, + track_redis_hll_event :index, :show, name: 'g_compliance_approval_rules', if: [:custom_condition_one?, :custom_condition_two?] def index @@ -49,97 +44,75 @@ RSpec.describe RedisTracking do expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) end - context 'with feature disabled' do - it 'does not track the event' do - stub_feature_flags(feature => false) - - expect_no_tracking - - get :index - end - end - - context 'with feature enabled' do + context 'when user is logged in' do before do - stub_feature_flags(feature => true) + sign_in(user) end - context 'when user is logged in' do - before do - sign_in(user) - end - - it 'tracks the event' do - expect_tracking - - get :index - end - - it 'passes default_enabled flag' do - expect(controller).to receive(:metric_feature_enabled?).with(feature.to_sym, true) + it 'tracks the event' do + expect_tracking - get :index - end + get :index + end - it 'tracks the event if DNT is not enabled' do - request.headers['DNT'] = '0' + it 'tracks the event if DNT is not enabled' do + request.headers['DNT'] = '0' - expect_tracking + expect_tracking - get :index - end + get :index + end - it 'does not track the event if DNT is enabled' do - request.headers['DNT'] = '1' + it 'does not track the event if DNT is enabled' do + request.headers['DNT'] = '1' - expect_no_tracking + expect_no_tracking - get :index - end + get :index + end - it 'does not track the event if the format is not HTML' do - expect_no_tracking + it 'does not track the event if the format is not HTML' do + expect_no_tracking - get :index, format: :json - end + get :index, format: :json + end - it 'does not track the event if a custom condition returns false' do - expect(controller).to receive(:custom_condition_two?).and_return(false) + it 'does not track the event if a custom condition returns false' do + expect(controller).to receive(:custom_condition_two?).and_return(false) - expect_no_tracking + expect_no_tracking - get :index - end + get :index + end - it 'does not track the event for untracked actions' do - expect_no_tracking + it 'does not track the event for untracked actions' do + expect_no_tracking - get :new - end + get :new end + end - context 'when user is not logged in and there is a visitor_id' do - let(:visitor_id) { SecureRandom.uuid } + context 'when user is not logged in and there is a visitor_id' do + let(:visitor_id) { SecureRandom.uuid } - before do - routes.draw { get 'show' => 'anonymous#show' } - end + before do + routes.draw { get 'show' => 'anonymous#show' } + end - it 'tracks the event' do - cookies[:visitor_id] = { value: visitor_id, expires: 24.months } + it 'tracks the event' do + cookies[:visitor_id] = { value: visitor_id, expires: 24.months } - expect_tracking + expect_tracking - get :show - end + get :show end + end - context 'when user is not logged in and there is no visitor_id' do - it 'does not track the event' do - expect_no_tracking + context 'when user is not logged in and there is no visitor_id' do + it 'does not track the event' do + expect_no_tracking - get :index - end + get :index end end end diff --git a/spec/controllers/concerns/spammable_actions_spec.rb b/spec/controllers/concerns/spammable_actions_spec.rb index 3b5b4d11a9b..25d5398c9da 100644 --- a/spec/controllers/concerns/spammable_actions_spec.rb +++ b/spec/controllers/concerns/spammable_actions_spec.rb @@ -6,21 +6,8 @@ RSpec.describe SpammableActions do controller(ActionController::Base) do include SpammableActions - # #create is used to test spammable_params - # for testing purposes - def create - spam_params = spammable_params - - # replace the actual request with a string in the JSON response, all we care is that it got set - spam_params[:request] = 'this is the request' if spam_params[:request] - - # just return the params in the response so they can be verified in this fake controller spec. - # Normally, they are processed further by the controller action - render json: spam_params.to_json, status: :ok - end - - # #update is used to test recaptcha_check_with_fallback - # for testing purposes + # #update is used here to test #recaptcha_check_with_fallback, but it could be invoked + # from #create or any other action which mutates a spammable via a controller. def update should_redirect = params[:should_redirect] == 'true' @@ -35,80 +22,7 @@ RSpec.describe SpammableActions do end before do - # Ordinarily we would not stub a method on the class under test, but :ensure_spam_config_loaded! - # returns false in the test environment, and is also strong_memoized, so we need to stub it - allow(controller).to receive(:ensure_spam_config_loaded!) { true } - end - - describe '#spammable_params' do - subject { post :create, format: :json, params: params } - - shared_examples 'expects request param only' do - it do - subject - - expect(response).to be_successful - expect(json_response).to eq({ 'request' => 'this is the request' }) - end - end - - shared_examples 'expects all spammable params' do - it do - subject - - expect(response).to be_successful - expect(json_response['request']).to eq('this is the request') - expect(json_response['recaptcha_verified']).to eq(true) - expect(json_response['spam_log_id']).to eq('1') - end - end - - let(:recaptcha_response) { nil } - let(:spam_log_id) { nil } - - context 'when recaptcha response is not present' do - let(:params) do - { - spam_log_id: spam_log_id - } - end - - it_behaves_like 'expects request param only' - end - - context 'when recaptcha response is present' do - let(:recaptcha_response) { 'abd123' } - let(:params) do - { - 'g-recaptcha-response': recaptcha_response, - spam_log_id: spam_log_id - } - end - - context 'when verify_recaptcha returns falsey' do - before do - expect(controller).to receive(:verify_recaptcha).with( - { - response: recaptcha_response - }) { false } - end - - it_behaves_like 'expects request param only' - end - - context 'when verify_recaptcha returns truthy' do - let(:spam_log_id) { 1 } - - before do - expect(controller).to receive(:verify_recaptcha).with( - { - response: recaptcha_response - }) { true } - end - - it_behaves_like 'expects all spammable params' - end - end + allow(Gitlab::Recaptcha).to receive(:load_configurations!) { true } end describe '#recaptcha_check_with_fallback' do @@ -154,12 +68,9 @@ RSpec.describe SpammableActions do allow(spammable).to receive(:valid?) { false } end - # NOTE: Not adding coverage of details of render_recaptcha?, the plan is to refactor it out - # of this module anyway as part of adding support for the GraphQL reCAPTCHA flow. - - context 'when render_recaptcha? is true' do + context 'when spammable.render_recaptcha? is true' do before do - expect(controller).to receive(:render_recaptcha?) { true } + expect(spammable).to receive(:render_recaptcha?) { true } end context 'when format is :html' do |