2016-07-26 07:37:02 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-21 20:32:02 +00:00
|
|
|
describe API::Environments do
|
2016-07-26 07:37:02 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:non_member) { create(:user) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :private, namespace: user.namespace) }
|
2016-07-26 07:37:02 +00:00
|
|
|
let!(:environment) { create(:environment, project: project) }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 14:36:08 +00:00
|
|
|
project.add_maintainer(user)
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/environments' do
|
|
|
|
context 'as member of the project' do
|
2016-07-29 10:14:36 +00:00
|
|
|
it 'returns project environments' do
|
2017-08-03 07:43:24 +00:00
|
|
|
project_data_keys = %w(
|
|
|
|
id description default_branch tag_list
|
2018-05-22 10:53:37 +00:00
|
|
|
ssh_url_to_repo http_url_to_repo web_url readme_url
|
2017-08-03 07:43:24 +00:00
|
|
|
name name_with_namespace
|
|
|
|
path path_with_namespace
|
|
|
|
star_count forks_count
|
|
|
|
created_at last_activity_at
|
2018-07-23 07:52:15 +00:00
|
|
|
avatar_url namespace
|
2017-08-03 07:43:24 +00:00
|
|
|
)
|
2017-03-02 20:36:21 +00:00
|
|
|
|
2016-07-26 07:37:02 +00:00
|
|
|
get api("/projects/#{project.id}/environments", user)
|
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-01-24 20:49:10 +00:00
|
|
|
expect(response).to include_pagination_headers
|
2016-07-26 07:37:02 +00:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.size).to eq(1)
|
|
|
|
expect(json_response.first['name']).to eq(environment.name)
|
|
|
|
expect(json_response.first['external_url']).to eq(environment.external_url)
|
2017-03-02 20:36:21 +00:00
|
|
|
expect(json_response.first['project'].keys).to contain_exactly(*project_data_keys)
|
2019-04-09 09:16:57 +00:00
|
|
|
expect(json_response.first).not_to have_key("last_deployment")
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
2019-06-24 12:18:40 +00:00
|
|
|
|
|
|
|
context 'when filtering' do
|
|
|
|
let!(:environment2) { create(:environment, project: project) }
|
|
|
|
|
|
|
|
it 'returns environment by name' do
|
|
|
|
get api("/projects/#{project.id}/environments?name=#{environment.name}", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.size).to eq(1)
|
|
|
|
expect(json_response.first['name']).to eq(environment.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no environment by non-existent name' do
|
|
|
|
get api("/projects/#{project.id}/environments?name=test", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns environments by name_like' do
|
|
|
|
get api("/projects/#{project.id}/environments?search=envir", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.size).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no environment by non-existent name_like' do
|
|
|
|
get api("/projects/#{project.id}/environments?search=test", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'as non member' do
|
2016-07-29 10:14:36 +00:00
|
|
|
it 'returns a 404 status code' do
|
2016-07-26 07:37:02 +00:00
|
|
|
get api("/projects/#{project.id}/environments", non_member)
|
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-26 12:19:37 +00:00
|
|
|
describe 'POST /projects/:id/environments' do
|
2016-07-26 07:37:02 +00:00
|
|
|
context 'as a member' do
|
|
|
|
it 'creates a environment with valid params' do
|
2018-12-17 22:52:17 +00:00
|
|
|
post api("/projects/#{project.id}/environments", user), params: { name: "mepmep" }
|
2016-07-26 07:37:02 +00:00
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2016-07-26 07:37:02 +00:00
|
|
|
expect(json_response['name']).to eq('mepmep')
|
2016-12-08 01:09:18 +00:00
|
|
|
expect(json_response['slug']).to eq('mepmep')
|
2016-07-26 07:37:02 +00:00
|
|
|
expect(json_response['external']).to be nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'requires name to be passed' do
|
2018-12-17 22:52:17 +00:00
|
|
|
post api("/projects/#{project.id}/environments", user), params: { external_url: 'test.gitlab.com' }
|
2016-07-26 07:37:02 +00:00
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
|
2016-07-29 10:14:36 +00:00
|
|
|
it 'returns a 400 if environment already exists' do
|
2018-12-17 22:52:17 +00:00
|
|
|
post api("/projects/#{project.id}/environments", user), params: { name: environment.name }
|
2016-07-26 07:37:02 +00:00
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
2016-12-08 01:09:18 +00:00
|
|
|
|
|
|
|
it 'returns a 400 if slug is specified' do
|
2018-12-17 22:52:17 +00:00
|
|
|
post api("/projects/#{project.id}/environments", user), params: { name: "foo", slug: "foo" }
|
2016-12-08 01:09:18 +00:00
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2016-12-08 01:09:18 +00:00
|
|
|
expect(json_response["error"]).to eq("slug is automatically generated and cannot be changed")
|
|
|
|
end
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'a non member' do
|
|
|
|
it 'rejects the request' do
|
2018-12-17 22:52:17 +00:00
|
|
|
post api("/projects/#{project.id}/environments", non_member), params: { name: 'gitlab.com' }
|
2016-07-26 07:37:02 +00:00
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-08-01 06:42:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 400 when the required params are missing' do
|
2018-12-17 22:52:17 +00:00
|
|
|
post api("/projects/12345/environments", non_member), params: { external_url: 'http://env.git.com' }
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-29 10:14:36 +00:00
|
|
|
describe 'PUT /projects/:id/environments/:environment_id' do
|
|
|
|
it 'returns a 200 if name and external_url are changed' do
|
|
|
|
url = 'https://mepmep.whatever.ninja'
|
|
|
|
put api("/projects/#{project.id}/environments/#{environment.id}", user),
|
2018-12-17 22:52:17 +00:00
|
|
|
params: { name: 'Mepmep', external_url: url }
|
2016-07-26 07:37:02 +00:00
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2016-07-29 10:14:36 +00:00
|
|
|
expect(json_response['name']).to eq('Mepmep')
|
|
|
|
expect(json_response['external_url']).to eq(url)
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
|
2016-12-08 01:09:18 +00:00
|
|
|
it "won't allow slug to be changed" do
|
|
|
|
slug = environment.slug
|
|
|
|
api_url = api("/projects/#{project.id}/environments/#{environment.id}", user)
|
2018-12-17 22:52:17 +00:00
|
|
|
put api_url, params: { slug: slug + "-foo" }
|
2016-12-08 01:09:18 +00:00
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2016-12-08 01:09:18 +00:00
|
|
|
expect(json_response["error"]).to eq("slug is automatically generated and cannot be changed")
|
|
|
|
end
|
|
|
|
|
2016-07-29 10:14:36 +00:00
|
|
|
it "won't update the external_url if only the name is passed" do
|
|
|
|
url = environment.external_url
|
2016-07-26 07:37:02 +00:00
|
|
|
put api("/projects/#{project.id}/environments/#{environment.id}", user),
|
2018-12-17 22:52:17 +00:00
|
|
|
params: { name: 'Mepmep' }
|
2016-07-26 07:37:02 +00:00
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2016-07-26 07:37:02 +00:00
|
|
|
expect(json_response['name']).to eq('Mepmep')
|
2016-07-29 10:14:36 +00:00
|
|
|
expect(json_response['external_url']).to eq(url)
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
|
2016-07-29 10:14:36 +00:00
|
|
|
it 'returns a 404 if the environment does not exist' do
|
2016-07-26 07:37:02 +00:00
|
|
|
put api("/projects/#{project.id}/environments/12345", user)
|
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
end
|
2016-07-29 10:14:36 +00:00
|
|
|
|
|
|
|
describe 'DELETE /projects/:id/environments/:environment_id' do
|
2018-07-11 14:36:08 +00:00
|
|
|
context 'as a maintainer' do
|
2016-07-29 10:14:36 +00:00
|
|
|
it 'returns a 200 for an existing environment' do
|
|
|
|
delete api("/projects/#{project.id}/environments/#{environment.id}", user)
|
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(204)
|
2016-07-29 10:14:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 404 for non existing id' do
|
|
|
|
delete api("/projects/#{project.id}/environments/12345", user)
|
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-07-29 10:14:36 +00:00
|
|
|
expect(json_response['message']).to eq('404 Not found')
|
|
|
|
end
|
2017-08-24 16:03:39 +00:00
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
|
|
|
let(:request) { api("/projects/#{project.id}/environments/#{environment.id}", user) }
|
|
|
|
end
|
2016-07-29 10:14:36 +00:00
|
|
|
end
|
2016-08-01 06:42:09 +00:00
|
|
|
|
|
|
|
context 'a non member' do
|
|
|
|
it 'rejects the request' do
|
|
|
|
delete api("/projects/#{project.id}/environments/#{environment.id}", non_member)
|
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-08-01 06:42:09 +00:00
|
|
|
end
|
|
|
|
end
|
2016-07-29 10:14:36 +00:00
|
|
|
end
|
2017-02-28 13:55:21 +00:00
|
|
|
|
|
|
|
describe 'POST /projects/:id/environments/:environment_id/stop' do
|
2018-07-11 14:36:08 +00:00
|
|
|
context 'as a maintainer' do
|
2017-02-28 13:55:21 +00:00
|
|
|
context 'with a stoppable environment' do
|
|
|
|
before do
|
|
|
|
environment.update(state: :available)
|
|
|
|
|
|
|
|
post api("/projects/#{project.id}/environments/#{environment.id}/stop", user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 200' do
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-02-28 13:55:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'actually stops the environment' do
|
|
|
|
expect(environment.reload).to be_stopped
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 404 for non existing id' do
|
|
|
|
post api("/projects/#{project.id}/environments/12345/stop", user)
|
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-02-28 13:55:21 +00:00
|
|
|
expect(json_response['message']).to eq('404 Not found')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'a non member' do
|
|
|
|
it 'rejects the request' do
|
|
|
|
post api("/projects/#{project.id}/environments/#{environment.id}/stop", non_member)
|
|
|
|
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-02-28 13:55:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-04-09 09:16:57 +00:00
|
|
|
|
|
|
|
describe 'GET /projects/:id/environments/:environment_id' do
|
|
|
|
context 'as member of the project' do
|
|
|
|
it 'returns project environments' do
|
|
|
|
create(:deployment, :success, project: project, environment: environment)
|
|
|
|
|
|
|
|
get api("/projects/#{project.id}/environments/#{environment.id}", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response).to match_response_schema('public_api/v4/environment')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'as non member' do
|
|
|
|
it 'returns a 404 status code' do
|
|
|
|
get api("/projects/#{project.id}/environments/#{environment.id}", non_member)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|