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
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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
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
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