debian-mirror-gitlab/spec/requests/api/remote_mirrors_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

163 lines
5 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe API::RemoteMirrors do
2020-01-01 13:55:28 +05:30
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :repository, :remote_mirror) }
2020-03-13 15:44:24 +05:30
let_it_be(:developer) { create(:user) { |u| project.add_developer(u) } }
2020-01-01 13:55:28 +05:30
describe 'GET /projects/:id/remote_mirrors' do
let(:route) { "/projects/#{project.id}/remote_mirrors" }
it 'requires `admin_remote_mirror` permission' do
2020-03-13 15:44:24 +05:30
get api(route, developer)
2020-01-01 13:55:28 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'returns a list of remote mirrors' do
project.add_maintainer(user)
get api(route, user)
expect(response).to have_gitlab_http_status(:success)
expect(response).to match_response_schema('remote_mirrors')
end
2020-04-08 14:13:33 +05:30
end
2020-01-01 13:55:28 +05:30
2022-06-21 17:19:12 +05:30
describe 'GET /projects/:id/remote_mirrors/:mirror_id' do
let(:route) { "/projects/#{project.id}/remote_mirrors/#{mirror.id}" }
let(:mirror) { project.remote_mirrors.first }
it 'requires `admin_remote_mirror` permission' do
get api(route, developer)
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'returns at remote mirror' do
project.add_maintainer(user)
get api(route, user)
expect(response).to have_gitlab_http_status(:success)
expect(response).to match_response_schema('remote_mirror')
end
end
2020-04-08 14:13:33 +05:30
describe 'POST /projects/:id/remote_mirrors' do
let(:route) { "/projects/#{project.id}/remote_mirrors" }
shared_examples 'creates a remote mirror' do
it 'creates a remote mirror and returns reponse' do
project.add_maintainer(user)
post api(route, user), params: params
enabled = params.fetch(:enabled, false)
expect(response).to have_gitlab_http_status(:success)
expect(response).to match_response_schema('remote_mirror')
expect(json_response['enabled']).to eq(enabled)
2020-01-01 13:55:28 +05:30
end
2020-04-08 14:13:33 +05:30
end
2020-01-01 13:55:28 +05:30
2020-04-08 14:13:33 +05:30
it 'requires `admin_remote_mirror` permission' do
post api(route, developer)
expect(response).to have_gitlab_http_status(:unauthorized)
end
context 'creates a remote mirror' do
context 'disabled by default' do
let(:params) { { url: 'https://foo:bar@test.com' } }
2020-01-01 13:55:28 +05:30
2020-04-08 14:13:33 +05:30
it_behaves_like 'creates a remote mirror'
2020-01-01 13:55:28 +05:30
end
2020-04-08 14:13:33 +05:30
context 'enabled' do
let(:params) { { url: 'https://foo:bar@test.com', enabled: true } }
it_behaves_like 'creates a remote mirror'
end
end
it 'returns error if url is invalid' do
project.add_maintainer(user)
post api(route, user), params: {
url: 'ftp://foo:bar@test.com'
}
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']['url']).to eq(["is blocked: Only allowed schemes are ssh, git, http, https"])
2020-01-01 13:55:28 +05:30
end
end
2020-03-13 15:44:24 +05:30
describe 'PUT /projects/:id/remote_mirrors/:mirror_id' do
2022-06-21 17:19:12 +05:30
let(:route) { "/projects/#{project.id}/remote_mirrors/#{mirror.id}" }
2020-03-13 15:44:24 +05:30
let(:mirror) { project.remote_mirrors.first }
it 'requires `admin_remote_mirror` permission' do
2022-06-21 17:19:12 +05:30
put api(route, developer)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'updates a remote mirror' do
project.add_maintainer(user)
2022-06-21 17:19:12 +05:30
put api(route, user), params: {
2020-03-13 15:44:24 +05:30
enabled: '0',
2020-04-08 14:13:33 +05:30
only_protected_branches: 'true',
keep_divergent_refs: 'true'
2020-03-13 15:44:24 +05:30
}
expect(response).to have_gitlab_http_status(:success)
expect(json_response['enabled']).to eq(false)
expect(json_response['only_protected_branches']).to eq(true)
2020-05-24 23:13:21 +05:30
expect(json_response['keep_divergent_refs']).to eq(true)
2020-03-13 15:44:24 +05:30
end
end
2022-06-21 17:19:12 +05:30
describe 'DELETE /projects/:id/remote_mirrors/:mirror_id' do
let(:route) { ->(id) { "/projects/#{project.id}/remote_mirrors/#{id}" } }
let(:mirror) { project.remote_mirrors.first }
it 'requires `admin_remote_mirror` permission' do
expect { delete api(route[mirror.id], developer) }.not_to change { project.remote_mirrors.count }
expect(response).to have_gitlab_http_status(:unauthorized)
end
context 'when the user is a maintainer' do
before do
project.add_maintainer(user)
end
it 'returns 404 for non existing id' do
delete api(route[non_existing_record_id], user)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns bad request if the update service fails' do
expect_next_instance_of(Projects::UpdateService) do |service|
expect(service).to receive(:execute).and_return(status: :error, message: 'message')
end
expect { delete api(route[mirror.id], user) }.not_to change { project.remote_mirrors.count }
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response).to eq({ 'message' => 'message' })
end
it 'deletes a remote mirror' do
expect { delete api(route[mirror.id], user) }.to change { project.remote_mirrors.count }.from(1).to(0)
expect(response).to have_gitlab_http_status(:no_content)
end
end
end
2020-01-01 13:55:28 +05:30
end