2021-02-22 17:27:13 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe DependencyProxy::AuthTokenService do
|
|
|
|
include DependencyProxyHelpers
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:deploy_token) { create(:deploy_token) }
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
describe '.user_or_deploy_token_from_jwt' do
|
|
|
|
subject { described_class.user_or_deploy_token_from_jwt(token.encoded) }
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
shared_examples 'handling token errors' do
|
|
|
|
context 'with a decoding error' do
|
|
|
|
before do
|
|
|
|
allow(JWT).to receive(:decode).and_raise(JWT::DecodeError)
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
it { is_expected.to eq(nil) }
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'with an immature signature error' do
|
|
|
|
before do
|
|
|
|
allow(JWT).to receive(:decode).and_raise(JWT::ImmatureSignature)
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
it { is_expected.to eq(nil) }
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'with an expired signature error' do
|
|
|
|
it 'returns nil' do
|
|
|
|
travel_to(Time.zone.now + Auth::DependencyProxyAuthenticationService.token_expire_at + 1.minute) do
|
|
|
|
expect(subject).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'with a user' do
|
|
|
|
let_it_be(:token) { build_jwt(user) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(user) }
|
|
|
|
|
|
|
|
context 'with an invalid user id' do
|
|
|
|
let_it_be(:token) { build_jwt { |jwt| jwt['user_id'] = 'this_is_not_a_user_id' } }
|
|
|
|
|
|
|
|
it 'raises an not found error' do
|
|
|
|
expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
it_behaves_like 'handling token errors'
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'with a deploy token' do
|
|
|
|
let_it_be(:token) { build_jwt(deploy_token) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(deploy_token) }
|
|
|
|
|
|
|
|
context 'with an invalid token' do
|
|
|
|
let_it_be(:token) { build_jwt { |jwt| jwt['deploy_token'] = 'this_is_not_a_token' } }
|
|
|
|
|
|
|
|
it { is_expected.to eq(nil) }
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
it_behaves_like 'handling token errors'
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'with an empty token payload' do
|
|
|
|
let_it_be(:token) { build_jwt(nil) }
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
it { is_expected.to eq(nil) }
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|