Commit 28ff5940 authored by Ash McKenzie's avatar Ash McKenzie

New User class

parent 5ef06ac7
require_relative 'gitlab_net'
class User
ANONYMOUS_USER = 'Anonymous'.freeze
def initialize(key_id, audit_usernames: false)
@key_id = key_id
@audit_usernames = audit_usernames
end
def username
@username ||= begin
user = GitlabNet.new.discover(key_id)
user ? "@#{user['username']}" : ANONYMOUS_USER
end
end
def log_username
audit_usernames ? username : "user with key #{key_id}"
end
private
attr_reader :key_id, :audit_usernames
end
require_relative 'spec_helper'
require_relative '../lib/user'
describe User, vcr: true do
let(:key_id) { 'key-1' }
let(:username) { 'testuser' }
let(:api) { double(GitlabNet) }
let(:discover_payload) { { 'username' => username } }
let(:audit_usernames) { nil }
before do
allow(GitlabNet).to receive(:new).and_return(api)
allow(api).to receive(:discover).with(key_id).and_return(discover_payload)
end
subject { described_class.new(key_id, audit_usernames: audit_usernames) }
describe '#username' do
context 'with a valid user' do
it "returns '@testuser'" do
expect(subject.username).to eql '@testuser'
end
end
context 'without a valid user' do
let(:discover_payload) { nil }
it "returns 'Anonymous'" do
expect(subject.username).to eql 'Anonymous'
end
end
end
describe '#log_username' do
context 'when audit_usernames is true' do
let(:audit_usernames) { true }
it "returns 'testuser'" do
expect(subject.log_username).to eql '@testuser'
end
end
context 'when audit_usernames is false' do
let(:audit_usernames) { false }
it "returns 'user with key key-1'" do
expect(subject.log_username).to eql 'user with key key-1'
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