Commit 33e11345 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add custom expectations for authentication activity metrics

parent ac4b954c
...@@ -47,8 +47,14 @@ module Gitlab ...@@ -47,8 +47,14 @@ module Gitlab
self.class.user_signed_out_counter.increment self.class.user_signed_out_counter.increment
end end
COUNTERS.each_pair do |metric, description| def self.each_counter
define_singleton_method("#{metric}_counter") do 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 strong_memoize(metric) do
Gitlab::Metrics.counter("gitlab_auth_#{metric}_total".to_sym, description) Gitlab::Metrics.counter("gitlab_auth_#{metric}_total".to_sym, description)
end end
......
...@@ -3,6 +3,10 @@ require 'spec_helper' ...@@ -3,6 +3,10 @@ require 'spec_helper'
describe 'Login' do describe 'Login' do
include TermsHelper include TermsHelper
before do
stub_authentication_activity_metrics
end
it 'Successful user signin invalidates password reset token' do it 'Successful user signin invalidates password reset token' do
user = create(:user) user = create(:user)
...@@ -29,7 +33,6 @@ describe 'Login' do ...@@ -29,7 +33,6 @@ describe 'Login' do
User.delete_all User.delete_all
user = create(:admin, password_automatically_set: true) user = create(:admin, password_automatically_set: true)
expect(Gitlab::Auth::Activity).to increment(:user_authenticated_counter)
visit root_path visit root_path
expect(current_path).to eq edit_user_password_path expect(current_path).to eq edit_user_password_path
...@@ -47,6 +50,7 @@ describe 'Login' do ...@@ -47,6 +50,7 @@ describe 'Login' do
click_button 'Sign in' click_button 'Sign in'
expect(current_path).to eq root_path expect(current_path).to eq root_path
expect(authentication_metrics).to have_incremented(:user_authenticated_counter)
end end
it 'does not show flash messages when login page' do it 'does not show flash messages when login page' do
......
...@@ -3,8 +3,8 @@ require 'spec_helper' ...@@ -3,8 +3,8 @@ require 'spec_helper'
describe Gitlab::Auth::Activity do describe Gitlab::Auth::Activity do
describe 'counters' do describe 'counters' do
it 'has all static counters defined' do it 'has all static counters defined' do
described_class::COUNTERS.each_key do |metric| described_class.each_counter do |counter|
expect(described_class).to respond_to("#{metric}_counter") expect(described_class).to respond_to(counter)
end end
end end
end end
......
...@@ -68,6 +68,17 @@ module StubConfiguration ...@@ -68,6 +68,17 @@ module StubConfiguration
allow(Gitlab.config.repositories).to receive(:storages).and_return(Settingslogic.new(messages)) allow(Gitlab.config.repositories).to receive(:storages).and_return(Settingslogic.new(messages))
end 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 private
# Modifies stubbed messages to also stub possible predicate versions # Modifies stubbed messages to also stub possible predicate versions
......
RSpec::Matchers.define :increment do |counter| RSpec::Matchers.define :have_incremented do |counter|
match do |metric| match do |adapter|
expect(metric.send(counter)).to receive(:increment) matcher = RSpec::Mocks::Matchers::HaveReceived.new(:increment)
matcher.matches?(adapter.send(counter))
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment