2020-07-28 23:09:34 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe API::Helpers::PackagesManagerClientsHelpers do
|
2020-11-24 15:15:51 +05:30
|
|
|
include HttpBasicAuthHelpers
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
let_it_be(:personal_access_token) { create(:personal_access_token) }
|
|
|
|
let_it_be(:username) { personal_access_token.user.username }
|
|
|
|
let_it_be(:helper) { Class.new.include(described_class).new }
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
let(:password) { personal_access_token.token }
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:env) do
|
|
|
|
{
|
|
|
|
'rack.input' => ''
|
|
|
|
}
|
|
|
|
end
|
2020-09-03 11:15:55 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:request) { ActionDispatch::Request.new(env) }
|
2020-09-03 11:15:55 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
before do
|
|
|
|
allow(helper).to receive(:request).and_return(request)
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
shared_examples 'invalid auth header' do
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'with an invalid Authorization header' do
|
2020-11-24 15:15:51 +05:30
|
|
|
before do
|
|
|
|
env.merge!(build_auth_headers('Invalid'))
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
shared_examples 'valid auth header' do
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'with a valid Authorization header' do
|
2020-11-24 15:15:51 +05:30
|
|
|
before do
|
|
|
|
env.merge!(basic_auth_header(username, password))
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'with an unknown password' do
|
|
|
|
let(:password) { 'Unknown' }
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it { is_expected.to be nil }
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it { is_expected.to eq expected_result }
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
describe '#find_job_from_http_basic_auth' do
|
|
|
|
let_it_be(:user) { personal_access_token.user }
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:job) { create(:ci_build, user: user, status: :running) }
|
|
|
|
let(:password) { job.token }
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
subject { helper.find_job_from_http_basic_auth }
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it_behaves_like 'valid auth header' do
|
|
|
|
let(:expected_result) { job }
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it_behaves_like 'invalid auth header'
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'when the job is not running' do
|
2020-07-28 23:09:34 +05:30
|
|
|
before do
|
2020-11-24 15:15:51 +05:30
|
|
|
job.update!(status: :failed)
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it_behaves_like 'valid auth header' do
|
|
|
|
let(:expected_result) { nil }
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
describe '#find_deploy_token_from_http_basic_auth' do
|
|
|
|
let_it_be(:deploy_token) { create(:deploy_token) }
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:token) { deploy_token.token }
|
|
|
|
let(:username) { deploy_token.username }
|
|
|
|
let(:password) { token }
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
subject { helper.find_deploy_token_from_http_basic_auth }
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it_behaves_like 'valid auth header' do
|
|
|
|
let(:expected_result) { deploy_token }
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it_behaves_like 'invalid auth header'
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|