2021-02-04 16:09:06 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe U2fRegistration do
|
|
|
|
let_it_be(:user) { create(:user) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2021-11-16 07:10:23 -05:00
|
|
|
let(:u2f_registration_name) { 'u2f_device' }
|
|
|
|
|
2021-02-04 16:09:06 -05:00
|
|
|
let(:u2f_registration) do
|
|
|
|
device = U2F::FakeU2F.new(FFaker::BaconIpsum.characters(5))
|
2021-11-16 07:10:23 -05:00
|
|
|
create(:u2f_registration, name: u2f_registration_name,
|
2021-02-04 16:09:06 -05:00
|
|
|
user: user,
|
|
|
|
certificate: Base64.strict_encode64(device.cert_raw),
|
|
|
|
key_handle: U2F.urlsafe_encode64(device.key_handle_raw),
|
|
|
|
public_key: Base64.strict_encode64(device.origin_public_key_raw))
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'callbacks' do
|
|
|
|
describe '#create_webauthn_registration' do
|
2021-11-16 07:10:23 -05:00
|
|
|
shared_examples_for 'creates webauthn registration' do
|
|
|
|
it 'creates webauthn registration' do
|
2021-11-18 10:10:19 -05:00
|
|
|
created_record = u2f_registration
|
2021-11-16 07:10:23 -05:00
|
|
|
|
2021-11-18 10:10:19 -05:00
|
|
|
webauthn_registration = WebauthnRegistration.where(u2f_registration_id: created_record.id)
|
2021-11-16 07:10:23 -05:00
|
|
|
expect(webauthn_registration).to exist
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'creates webauthn registration'
|
|
|
|
|
|
|
|
context 'when the u2f_registration has a blank name' do
|
|
|
|
let(:u2f_registration_name) { '' }
|
|
|
|
|
|
|
|
it_behaves_like 'creates webauthn registration'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the u2f_registration has the name as `nil`' do
|
|
|
|
let(:u2f_registration_name) { nil }
|
2021-02-04 16:09:06 -05:00
|
|
|
|
2021-11-16 07:10:23 -05:00
|
|
|
it_behaves_like 'creates webauthn registration'
|
2021-02-04 16:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs error' do
|
|
|
|
allow(Gitlab::Auth::U2fWebauthnConverter).to receive(:new).and_raise('boom!')
|
|
|
|
|
2021-11-18 10:10:19 -05:00
|
|
|
allow_next_instance_of(U2fRegistration) do |u2f_registration|
|
|
|
|
allow(u2f_registration).to receive(:id).and_return(123)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(Gitlab::ErrorTracking).to(
|
|
|
|
receive(:track_exception).with(kind_of(StandardError),
|
|
|
|
u2f_registration_id: 123))
|
|
|
|
|
|
|
|
u2f_registration
|
2021-02-04 16:09:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|