diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-07-20 15:06:11 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-07-20 15:06:11 +0200 |
commit | 33e11345e086678fce7591bcd1d465f879d838e7 (patch) | |
tree | d3faea3c4bead6c5080876ccb6c39e42683ef84d | |
parent | ac4b954c5fabbfa98fb72d88526a30713a20af5d (diff) | |
download | gitlab-ce-33e11345e086678fce7591bcd1d465f879d838e7.tar.gz |
Add custom expectations for authentication activity metrics
-rw-r--r-- | lib/gitlab/auth/activity.rb | 10 | ||||
-rw-r--r-- | spec/features/users/login_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/auth/activity_spec.rb | 4 | ||||
-rw-r--r-- | spec/support/helpers/stub_configuration.rb | 11 | ||||
-rw-r--r-- | spec/support/prometheus/custom_matchers.rb | 8 |
5 files changed, 31 insertions, 8 deletions
diff --git a/lib/gitlab/auth/activity.rb b/lib/gitlab/auth/activity.rb index b8a0979ac35..375583c1aec 100644 --- a/lib/gitlab/auth/activity.rb +++ b/lib/gitlab/auth/activity.rb @@ -47,8 +47,14 @@ module Gitlab self.class.user_signed_out_counter.increment end - COUNTERS.each_pair do |metric, description| - define_singleton_method("#{metric}_counter") do + def self.each_counter + COUNTERS.each_pair do |metric, description| + yield "#{metric}_counter", metric, description + end + end + + each_counter do |counter, metric, description| + define_singleton_method(counter) do strong_memoize(metric) do Gitlab::Metrics.counter("gitlab_auth_#{metric}_total".to_sym, description) end diff --git a/spec/features/users/login_spec.rb b/spec/features/users/login_spec.rb index 7a268c54de5..950385bc15c 100644 --- a/spec/features/users/login_spec.rb +++ b/spec/features/users/login_spec.rb @@ -3,6 +3,10 @@ require 'spec_helper' describe 'Login' do include TermsHelper + before do + stub_authentication_activity_metrics + end + it 'Successful user signin invalidates password reset token' do user = create(:user) @@ -29,7 +33,6 @@ describe 'Login' do User.delete_all user = create(:admin, password_automatically_set: true) - expect(Gitlab::Auth::Activity).to increment(:user_authenticated_counter) visit root_path expect(current_path).to eq edit_user_password_path @@ -47,6 +50,7 @@ describe 'Login' do click_button 'Sign in' expect(current_path).to eq root_path + expect(authentication_metrics).to have_incremented(:user_authenticated_counter) end it 'does not show flash messages when login page' do diff --git a/spec/lib/gitlab/auth/activity_spec.rb b/spec/lib/gitlab/auth/activity_spec.rb index 311c29010b3..7d22b1f3c4f 100644 --- a/spec/lib/gitlab/auth/activity_spec.rb +++ b/spec/lib/gitlab/auth/activity_spec.rb @@ -3,8 +3,8 @@ require 'spec_helper' describe Gitlab::Auth::Activity do describe 'counters' do it 'has all static counters defined' do - described_class::COUNTERS.each_key do |metric| - expect(described_class).to respond_to("#{metric}_counter") + described_class.each_counter do |counter| + expect(described_class).to respond_to(counter) end end end diff --git a/spec/support/helpers/stub_configuration.rb b/spec/support/helpers/stub_configuration.rb index 1823099dd9c..eb9a884e350 100644 --- a/spec/support/helpers/stub_configuration.rb +++ b/spec/support/helpers/stub_configuration.rb @@ -68,6 +68,17 @@ module StubConfiguration allow(Gitlab.config.repositories).to receive(:storages).and_return(Settingslogic.new(messages)) end + def authentication_metrics + Gitlab::Auth::Activity + end + + def stub_authentication_activity_metrics + authentication_metrics.each_counter do |counter, metric, description| + allow(authentication_metrics).to receive(counter) + .and_return(spy("#{metric} - #{description}")) + end + end + private # Modifies stubbed messages to also stub possible predicate versions diff --git a/spec/support/prometheus/custom_matchers.rb b/spec/support/prometheus/custom_matchers.rb index 2ad3b8a7fce..21d5fd3e6ff 100644 --- a/spec/support/prometheus/custom_matchers.rb +++ b/spec/support/prometheus/custom_matchers.rb @@ -1,5 +1,7 @@ -RSpec::Matchers.define :increment do |counter| - match do |metric| - expect(metric.send(counter)).to receive(:increment) +RSpec::Matchers.define :have_incremented do |counter| + match do |adapter| + matcher = RSpec::Mocks::Matchers::HaveReceived.new(:increment) + + matcher.matches?(adapter.send(counter)) end end |