Commit 7f0ceb87 authored by Z.J. van de Weg's avatar Z.J. van de Weg

Make tests green for ci_job_token authentication

parent bfa4395e
...@@ -328,7 +328,7 @@ Example requests: ...@@ -328,7 +328,7 @@ Example requests:
- Using the `job_token` parameter (only inside `.gitlab-ci.yml`): - Using the `job_token` parameter (only inside `.gitlab-ci.yml`):
``` ```
curl --header -form "job-token=$CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/master/download?job=test" curl --header --form "job-token=$CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/master/download?job=test"
``` ```
Response: Response:
...@@ -380,7 +380,7 @@ Example requests: ...@@ -380,7 +380,7 @@ Example requests:
- Using the `job_token` parameter (only inside `.gitlab-ci.yml`): - Using the `job_token` parameter (only inside `.gitlab-ci.yml`):
``` ```
curl --header -form "job-token=$CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/master/download?job=test" curl --header --form "job-token=$CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/master/download?job=test"
``` ```
Example response: Example response:
......
...@@ -377,6 +377,7 @@ module API ...@@ -377,6 +377,7 @@ module API
def initial_current_user def initial_current_user
return @initial_current_user if defined?(@initial_current_user) return @initial_current_user if defined?(@initial_current_user)
Gitlab::Auth::UniqueIpsLimiter.limit_user! do Gitlab::Auth::UniqueIpsLimiter.limit_user! do
@initial_current_user ||= find_user_by_private_token(scopes: scopes_registered_for_endpoint) @initial_current_user ||= find_user_by_private_token(scopes: scopes_registered_for_endpoint)
@initial_current_user ||= doorkeeper_guard(scopes: scopes_registered_for_endpoint) @initial_current_user ||= doorkeeper_guard(scopes: scopes_registered_for_endpoint)
......
...@@ -248,7 +248,7 @@ module API ...@@ -248,7 +248,7 @@ module API
end end
def check_cross_project_pipelines_feature! def check_cross_project_pipelines_feature!
not_found!('Project') if job_token_authentication? && project.feature_available?(:cross_project_pipelines) not_found!('Project') if job_token_authentication? && @project.feature_available?(:cross_project_pipelines)
end end
end end
end end
......
...@@ -13,9 +13,12 @@ describe API::Helpers do ...@@ -13,9 +13,12 @@ describe API::Helpers do
let(:env) { { 'REQUEST_METHOD' => 'GET' } } let(:env) { { 'REQUEST_METHOD' => 'GET' } }
let(:request) { Rack::Request.new(env) } let(:request) { Rack::Request.new(env) }
let(:header) { } let(:header) { }
let(:route_authentication_setting) { {} }
before do before do
allow_any_instance_of(self.class).to receive(:options).and_return({}) allow_any_instance_of(self.class).to receive(:options).and_return({})
allow_any_instance_of(self.class).to receive(:route_authentication_setting)
.and_return(route_authentication_setting)
end end
def set_env(user_or_token, identifier) def set_env(user_or_token, identifier)
...@@ -202,11 +205,15 @@ describe API::Helpers do ...@@ -202,11 +205,15 @@ describe API::Helpers do
end end
describe "when authenticating using a job token" do describe "when authenticating using a job token" do
let(:job) { create(:ci_build) } let(:job) { create(:ci_build, user: current_user) }
let(:route_authentication_setting) { { job_token_allowed: true } }
before do
allow_any_instance_of(API::Helpers).to receive(:doorkeeper_guard).and_return(nil)
end
it "returns nil for an invalid token" do it "returns nil for an invalid token" do
env[API::APIGuard::JOB_TOKEN_HEADER] = 'invalid token' env[API::APIGuard::JOB_TOKEN_HEADER] = 'invalid token'
allow_any_instance_of(self.class).to receive(:doorkeeper_guard){ false }
expect(current_user).to be_nil expect(current_user).to be_nil
end end
...@@ -224,13 +231,6 @@ describe API::Helpers do ...@@ -224,13 +231,6 @@ describe API::Helpers do
expect(current_user).to be_nil expect(current_user).to be_nil
end end
it "authenticates as user when route is allowed" do
env[API::APIGuard::JOB_TOKEN_HEADER] = job.token
route_setting(:authentication) = { job_token_allowed: true }
expect(current_user).to eq(user)
end
end end
context 'sudo usage' do context 'sudo usage' do
......
...@@ -207,7 +207,6 @@ describe API::Jobs do ...@@ -207,7 +207,6 @@ describe API::Jobs do
context 'normal authentication' do context 'normal authentication' do
before do before do
stub_artifacts_object_storage stub_artifacts_object_storage
job
get api("/projects/#{project.id}/jobs/#{job.id}/artifacts", api_user) get api("/projects/#{project.id}/jobs/#{job.id}/artifacts", api_user)
end end
...@@ -248,16 +247,16 @@ describe API::Jobs do ...@@ -248,16 +247,16 @@ describe API::Jobs do
before do before do
get api("/projects/#{project.id}/jobs/#{job.id}/artifacts"), job_token: job.token get api("/projects/#{project.id}/jobs/#{job.id}/artifacts"), job_token: job.token
end end
context 'user is developer' do context 'user is developer' do
let(:api_user) { user } let(:api_user) { user }
it_behaves_like 'downloads artifact' it_behaves_like 'downloads artifact'
end end
context 'user is admin, but not member' do context 'user is admin, but not member' do
let(:api_user) { create(:admin) } let(:api_user) { create(:admin) }
it 'does not allow to see that artfiact is present' do it 'does not allow to see that artfiact is present' do
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
...@@ -267,7 +266,7 @@ describe API::Jobs do ...@@ -267,7 +266,7 @@ describe API::Jobs do
describe 'GET /projects/:id/artifacts/:ref_name/download?job=name' do describe 'GET /projects/:id/artifacts/:ref_name/download?job=name' do
let(:api_user) { reporter } let(:api_user) { reporter }
let(:job) { create(:ci_build, :artifacts, pipeline: pipeline) } let(:job) { create(:ci_build, :artifacts, pipeline: pipeline, user: api_user) }
before do before do
stub_artifacts_object_storage stub_artifacts_object_storage
...@@ -338,7 +337,7 @@ describe API::Jobs do ...@@ -338,7 +337,7 @@ describe API::Jobs do
end end
context 'when artifacts are stored remotely' do context 'when artifacts are stored remotely' do
let(:job) { create(:ci_build, :artifacts, :remote_store, pipeline: pipeline) } let(:job) { create(:ci_build, :artifacts, :remote_store, pipeline: pipeline, user: api_user) }
it 'returns location redirect' do it 'returns location redirect' do
expect(response).to have_http_status(302) expect(response).to have_http_status(302)
...@@ -381,7 +380,7 @@ describe API::Jobs do ...@@ -381,7 +380,7 @@ describe API::Jobs do
get api("/projects/#{project.id}/jobs/artifacts/master/download"), job: job.name, job_token: job.token get api("/projects/#{project.id}/jobs/artifacts/master/download"), job: job.name, job_token: job.token
end end
context 'when user is reporter' do context 'when user is eporter' do
it_behaves_like 'a valid file' it_behaves_like 'a valid file'
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment