1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe DependencyProxy::AuthTokenService, feature_category: :virtual_registry do
include DependencyProxyHelpers
let_it_be(:user) { create(:user) }
let_it_be(:personal_access_token) { create(:personal_access_token, user: user) }
let_it_be(:group_access_token) { create(:personal_access_token, user: user) }
let_it_be(:deploy_token) { create(:deploy_token) }
shared_examples 'handling token errors' do
context 'with a decoding error' do
before do
allow(JWT).to receive(:decode).and_raise(JWT::DecodeError)
end
it { is_expected.to eq(nil) }
end
context 'with an immature signature error' do
before do
allow(JWT).to receive(:decode).and_raise(JWT::ImmatureSignature)
end
it { is_expected.to eq(nil) }
end
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
end
end
describe '.user_or_deploy_token_from_jwt' do
subject { described_class.user_or_deploy_token_from_jwt(token.encoded) }
shared_examples 'handling token errors' do
context 'with a decoding error' do
before do
allow(JWT).to receive(:decode).and_raise(JWT::DecodeError)
end
it { is_expected.to eq(nil) }
end
context 'with an immature signature error' do
before do
allow(JWT).to receive(:decode).and_raise(JWT::ImmatureSignature)
end
it { is_expected.to eq(nil) }
end
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
end
end
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
end
it_behaves_like 'handling token errors'
end
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
it_behaves_like 'handling token errors'
end
context 'with an empty token payload' do
let_it_be(:token) { build_jwt(nil) }
it { is_expected.to eq(nil) }
end
end
describe '.user_or_token_from_jwt' do
subject { described_class.user_or_token_from_jwt(token.encoded) }
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
end
it_behaves_like 'handling token errors'
end
context 'with a personal access token' do
let_it_be(:token) { build_jwt(personal_access_token) }
it { is_expected.to eq(personal_access_token) }
context 'with an inactive token' do
before do
personal_access_token.revoke!
end
it { is_expected.to eq(nil) }
end
context 'with an invalid token' do
let_it_be(:token) { build_jwt { |jwt| jwt['personal_access_token'] = 'this_is_not_a_token' } }
it { is_expected.to eq(nil) }
end
end
context 'with a group access token' do
let_it_be(:token) { build_jwt(group_access_token) }
it { is_expected.to eq(group_access_token) }
context 'with an inactive token' do
before do
group_access_token.revoke!
end
it { is_expected.to eq(nil) }
end
context 'with an invalid token' do
let_it_be(:token) { build_jwt { |jwt| jwt['group_access_token'] = 'this_is_not_a_token' } }
it { is_expected.to eq(nil) }
end
end
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
it_behaves_like 'handling token errors'
end
context 'with an empty token payload' do
let_it_be(:token) { build_jwt(nil) }
it { is_expected.to eq(nil) }
end
end
end
|