2018-07-31 12:35:02 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::Submodules do
|
2018-07-31 12:35:02 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let!(:project) { create(:project, :repository, namespace: user.namespace ) }
|
|
|
|
let(:guest) { create(:user) { |u| project.add_guest(u) } }
|
|
|
|
let(:submodule) { 'six' }
|
|
|
|
let(:commit_sha) { 'e25eda1fece24ac7a03624ed1320f82396f35bd8' }
|
|
|
|
let(:branch) { 'master' }
|
|
|
|
let(:commit_message) { 'whatever' }
|
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
submodule: submodule,
|
|
|
|
commit_sha: commit_sha,
|
|
|
|
branch: branch,
|
|
|
|
commit_message: commit_message
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def route(submodule = nil)
|
|
|
|
"/projects/#{project.id}/repository/submodules/#{submodule}"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "PUT /projects/:id/repository/submodule/:submodule" do
|
|
|
|
context 'when unauthenticated' do
|
|
|
|
it 'returns 401' do
|
2018-12-17 17:52:17 -05:00
|
|
|
put api(route(submodule)), params: params
|
2018-07-31 12:35:02 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-07-31 12:35:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated', 'as a guest' do
|
|
|
|
it 'returns 403' do
|
2018-12-17 17:52:17 -05:00
|
|
|
put api(route(submodule), guest), params: params
|
2018-07-31 12:35:02 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-07-31 12:35:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated', 'as a developer' do
|
|
|
|
it 'returns 400 if params is missing' do
|
|
|
|
put api(route(submodule), user)
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-07-31 12:35:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 if branch is missing' do
|
2018-12-17 17:52:17 -05:00
|
|
|
put api(route(submodule), user), params: params.except(:branch)
|
2018-07-31 12:35:02 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-07-31 12:35:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 if commit_sha is missing' do
|
2018-12-17 17:52:17 -05:00
|
|
|
put api(route(submodule), user), params: params.except(:commit_sha)
|
2018-07-31 12:35:02 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-07-31 12:35:02 -04:00
|
|
|
end
|
|
|
|
|
2019-01-21 13:06:16 -05:00
|
|
|
it 'returns the commit' do
|
2018-07-31 12:35:02 -04:00
|
|
|
head_commit = project.repository.commit.id
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put api(route(submodule), user), params: params
|
2018-07-31 12:35:02 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-07-31 12:35:02 -04:00
|
|
|
expect(json_response['message']).to eq commit_message
|
|
|
|
expect(json_response['author_name']).to eq user.name
|
|
|
|
expect(json_response['committer_name']).to eq user.name
|
|
|
|
expect(json_response['parent_ids'].first).to eq head_commit
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the submodule name is urlencoded' do
|
|
|
|
let(:submodule) { 'test_inside_folder/another_folder/six' }
|
|
|
|
let(:branch) { 'submodule_inside_folder' }
|
|
|
|
let(:encoded_submodule) { CGI.escape(submodule) }
|
|
|
|
|
2019-01-21 13:06:16 -05:00
|
|
|
it 'returns the commit' do
|
2018-07-31 12:35:02 -04:00
|
|
|
expect(Submodules::UpdateService)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(any_args, hash_including(submodule: submodule))
|
|
|
|
.and_call_original
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put api(route(encoded_submodule), user), params: params
|
2018-07-31 12:35:02 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-07-31 12:35:02 -04:00
|
|
|
expect(json_response['id']).to eq project.repository.commit(branch).id
|
|
|
|
expect(project.repository.blob_at(branch, submodule).id).to eq commit_sha
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|