debian-mirror-gitlab/spec/lib/gitlab/identifier_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.1 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Identifier do
2016-11-03 12:29:30 +05:30
let(:identifier) do
Class.new { include Gitlab::Identifier }.new
end
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2016-11-03 12:29:30 +05:30
let(:user) { create(:user) }
let(:key) { create(:key, user: user) }
describe '#identify' do
context 'without an identifier' do
2018-12-13 13:39:08 +05:30
it 'returns nil' do
expect(identifier.identify('')).to be nil
2016-11-03 12:29:30 +05:30
end
end
context 'with a user identifier' do
it 'identifies the user using a user ID' do
2017-09-10 17:25:29 +05:30
expect(identifier).to receive(:identify_using_user)
.with("user-#{user.id}")
2016-11-03 12:29:30 +05:30
2018-12-13 13:39:08 +05:30
identifier.identify("user-#{user.id}")
2016-11-03 12:29:30 +05:30
end
end
context 'with an SSH key identifier' do
it 'identifies the user using an SSH key ID' do
2017-09-10 17:25:29 +05:30
expect(identifier).to receive(:identify_using_ssh_key)
.with("key-#{key.id}")
2016-11-03 12:29:30 +05:30
2018-12-13 13:39:08 +05:30
identifier.identify("key-#{key.id}")
2016-11-03 12:29:30 +05:30
end
end
end
describe '#identify_using_user' do
it 'returns the User for an existing ID in the identifier' do
found = identifier.identify_using_user("user-#{user.id}")
expect(found).to eq(user)
end
it 'returns nil for a non existing user ID' do
found = identifier.identify_using_user('user--1')
expect(found).to be_nil
end
it 'caches the found users per ID' do
expect(User).to receive(:find_by).once.and_call_original
2.times do
found = identifier.identify_using_user("user-#{user.id}")
expect(found).to eq(user)
end
end
end
describe '#identify_using_ssh_key' do
it 'returns the User for an existing SSH key' do
found = identifier.identify_using_ssh_key("key-#{key.id}")
expect(found).to eq(user)
end
it 'returns nil for an invalid SSH key' do
found = identifier.identify_using_ssh_key('key--1')
expect(found).to be_nil
end
it 'caches the found users per key' do
expect(User).to receive(:find_by_ssh_key_id).once.and_call_original
2.times do
found = identifier.identify_using_ssh_key("key-#{key.id}")
expect(found).to eq(user)
end
end
end
end