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

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

421 lines
16 KiB
Ruby
Raw Permalink Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe API::Environments, feature_category: :continuous_delivery do
2021-06-08 01:23:25 +05:30
let_it_be(:user) { create(:user) }
2023-03-17 16:20:25 +05:30
let_it_be(:developer) { create(:user) }
2021-06-08 01:23:25 +05:30
let_it_be(:non_member) { create(:user) }
let_it_be(:project) { create(:project, :private, :repository, namespace: user.namespace) }
let_it_be_with_reload(:environment) { create(:environment, project: project) }
2016-09-13 17:45:13 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2023-03-17 16:20:25 +05:30
project.add_developer(developer)
2016-09-13 17:45:13 +05:30
end
2021-06-08 01:23:25 +05:30
describe 'GET /projects/:id/environments', :aggregate_failures do
2016-09-13 17:45:13 +05:30
context 'as member of the project' do
it 'returns project environments' do
get api("/projects/#{project.id}/environments", user)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-11-18 22:05:49 +05:30
expect(response).to match_response_schema('public_api/v4/environments')
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2016-09-13 17:45:13 +05:30
expect(json_response).to be_an Array
expect(json_response.size).to eq(1)
expect(json_response.first['name']).to eq(environment.name)
2022-07-16 23:28:13 +05:30
expect(json_response.first['tier']).to eq(environment.tier)
2016-09-13 17:45:13 +05:30
expect(json_response.first['external_url']).to eq(environment.external_url)
2021-06-08 01:23:25 +05:30
expect(json_response.first['project']).to match_schema('public_api/v4/project')
expect(json_response.first).not_to have_key('last_deployment')
2016-09-13 17:45:13 +05:30
end
2019-09-30 21:07:59 +05:30
context 'when filtering' do
2022-07-23 23:45:48 +05:30
let_it_be(:stopped_environment) { create(:environment, :stopped, project: project) }
2019-09-30 21:07:59 +05:30
it 'returns environment by name' do
get api("/projects/#{project.id}/environments?name=#{environment.name}", user)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-09-30 21:07:59 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-09-30 21:07:59 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-09-30 21:07:59 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-09-30 21:07:59 +05:30
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.size).to eq(0)
end
2022-07-16 23:28:13 +05:30
2023-03-17 16:20:25 +05:30
context "when params[:search] is less than #{described_class::MIN_SEARCH_LENGTH} characters" do
2023-07-09 08:55:56 +05:30
it 'returns with status 400' do
2023-03-17 16:20:25 +05:30
get api("/projects/#{project.id}/environments?search=ab", user)
2023-07-09 08:55:56 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to include("Search query is less than #{described_class::MIN_SEARCH_LENGTH} characters")
2023-03-17 16:20:25 +05:30
end
end
2022-07-23 23:45:48 +05:30
it 'returns environment by valid state' do
get api("/projects/#{project.id}/environments?states=available", user)
expect(response).to have_gitlab_http_status(:ok)
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 all environments when state is not specified' do
get api("/projects/#{project.id}/environments", user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.size).to eq(2)
expect(json_response.first['name']).to eq(environment.name)
expect(json_response.last['name']).to eq(stopped_environment.name)
end
it 'returns a 400 when filtering by invalid state' do
2022-07-16 23:28:13 +05:30
get api("/projects/#{project.id}/environments?states=test", user)
expect(response).to have_gitlab_http_status(:bad_request)
2022-07-23 23:45:48 +05:30
expect(json_response['error']).to eq('states does not have a valid value')
2022-07-16 23:28:13 +05:30
end
2019-09-30 21:07:59 +05:30
end
2016-09-13 17:45:13 +05:30
end
context 'as non member' do
it 'returns a 404 status code' do
get api("/projects/#{project.id}/environments", non_member)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-09-13 17:45:13 +05:30
end
end
end
describe 'POST /projects/:id/environments' do
context 'as a member' do
it 'creates a environment with valid params' do
2022-07-16 23:28:13 +05:30
post api("/projects/#{project.id}/environments", user), params: { name: "mepmep", tier: 'staging' }
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2021-11-18 22:05:49 +05:30
expect(response).to match_response_schema('public_api/v4/environment')
2016-09-13 17:45:13 +05:30
expect(json_response['name']).to eq('mepmep')
2017-08-17 22:00:37 +05:30
expect(json_response['slug']).to eq('mepmep')
2022-07-16 23:28:13 +05:30
expect(json_response['tier']).to eq('staging')
2016-09-13 17:45:13 +05:30
expect(json_response['external']).to be nil
end
it 'requires name to be passed' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/environments", user), params: { external_url: 'test.gitlab.com' }
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2016-09-13 17:45:13 +05:30
end
it 'returns a 400 if environment already exists' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/environments", user), params: { name: environment.name }
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
it 'returns a 400 if slug is specified' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/environments", user), params: { name: "foo", slug: "foo" }
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2017-08-17 22:00:37 +05:30
expect(json_response["error"]).to eq("slug is automatically generated and cannot be changed")
end
2016-09-13 17:45:13 +05:30
end
context 'a non member' do
it 'rejects the request' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/environments", non_member), params: { name: 'gitlab.com' }
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-09-13 17:45:13 +05:30
end
it 'returns a 400 when the required params are missing' do
2020-04-22 19:07:51 +05:30
post api("/projects/#{non_existing_record_id}/environments", non_member), params: { external_url: 'http://env.git.com' }
2016-09-13 17:45:13 +05:30
end
end
end
2023-03-17 16:20:25 +05:30
describe 'POST /projects/:id/environments/stop_stale' do
context 'as a maintainer' do
it 'returns a 200' do
post api("/projects/#{project.id}/environments/stop_stale", user), params: { before: 1.week.ago.to_date.to_s }
expect(response).to have_gitlab_http_status(:ok)
end
it 'returns a 400 for bad input date' do
post api("/projects/#{project.id}/environments/stop_stale", user), params: { before: 1.day.ago.to_date.to_s }
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('400 Bad request - Invalid Date')
end
it 'returns a 400 for service error' do
expect_next_instance_of(::Environments::StopStaleService) do |service|
expect(service).to receive(:execute).and_return(ServiceResponse.error(message: 'Test Error'))
end
post api("/projects/#{project.id}/environments/stop_stale", user), params: { before: 1.week.ago.to_date.to_s }
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('Test Error')
end
end
context 'a non member' do
it 'rejects the request' do
post api("/projects/#{project.id}/environments/stop_stale", non_member), params: { before: 1.week.ago.to_date.to_s }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'a developer' do
it 'rejects the request' do
post api("/projects/#{project.id}/environments/stop_stale", developer), params: { before: 1.week.ago.to_date.to_s }
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
2016-09-13 17:45:13 +05:30
describe 'PUT /projects/:id/environments/:environment_id' do
2023-07-09 08:55:56 +05:30
it 'returns a 200 if external_url is changed' do
2016-09-13 17:45:13 +05:30
url = 'https://mepmep.whatever.ninja'
put api("/projects/#{project.id}/environments/#{environment.id}", user),
2023-07-09 08:55:56 +05:30
params: { external_url: url }
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-11-18 22:05:49 +05:30
expect(response).to match_response_schema('public_api/v4/environment')
2016-09-13 17:45:13 +05:30
expect(json_response['external_url']).to eq(url)
end
2022-07-16 23:28:13 +05:30
it 'returns a 200 if tier is changed' do
put api("/projects/#{project.id}/environments/#{environment.id}", user),
params: { tier: 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('public_api/v4/environment')
expect(json_response['tier']).to eq('production')
end
2017-08-17 22:00:37 +05:30
it "won't allow slug to be changed" do
slug = environment.slug
api_url = api("/projects/#{project.id}/environments/#{environment.id}", user)
2019-02-15 15:39:39 +05:30
put api_url, params: { slug: slug + "-foo" }
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2017-08-17 22:00:37 +05:30
expect(json_response["error"]).to eq("slug is automatically generated and cannot be changed")
end
2016-09-13 17:45:13 +05:30
it 'returns a 404 if the environment does not exist' do
2020-04-22 19:07:51 +05:30
put api("/projects/#{project.id}/environments/#{non_existing_record_id}", user)
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-09-13 17:45:13 +05:30
end
end
describe 'DELETE /projects/:id/environments/:environment_id' do
2018-11-18 11:00:15 +05:30
context 'as a maintainer' do
2020-04-22 19:07:51 +05:30
it "rejects the requests in environment isn't stopped" do
delete api("/projects/#{project.id}/environments/#{environment.id}", user)
expect(response).to have_gitlab_http_status(:forbidden)
end
2021-11-18 22:05:49 +05:30
it 'returns a 204 for stopped environment' do
2020-04-22 19:07:51 +05:30
environment.stop
2016-09-13 17:45:13 +05:30
delete api("/projects/#{project.id}/environments/#{environment.id}", user)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2016-09-13 17:45:13 +05:30
end
it 'returns a 404 for non existing id' do
2020-04-22 19:07:51 +05:30
delete api("/projects/#{project.id}/environments/#{non_existing_record_id}", user)
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-09-13 17:45:13 +05:30
expect(json_response['message']).to eq('404 Not found')
end
2018-03-17 18:26:18 +05:30
it_behaves_like '412 response' do
2020-04-22 19:07:51 +05:30
before do
environment.stop
end
2018-03-17 18:26:18 +05:30
let(:request) { api("/projects/#{project.id}/environments/#{environment.id}", user) }
end
2016-09-13 17:45:13 +05:30
end
context 'a non member' do
it 'rejects the request' do
delete api("/projects/#{project.id}/environments/#{environment.id}", non_member)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-09-13 17:45:13 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
describe 'POST /projects/:id/environments/:environment_id/stop' do
2018-11-18 11:00:15 +05:30
context 'as a maintainer' do
2017-08-17 22:00:37 +05:30
context 'with a stoppable environment' do
before do
2021-04-29 21:17:54 +05:30
environment.update!(state: :available)
2017-08-17 22:00:37 +05:30
post api("/projects/#{project.id}/environments/#{environment.id}/stop", user)
end
it 'returns a 200' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-11-18 22:05:49 +05:30
expect(response).to match_response_schema('public_api/v4/environment')
2017-08-17 22:00:37 +05:30
end
it 'actually stops the environment' do
expect(environment.reload).to be_stopped
end
end
it 'returns a 404 for non existing id' do
2020-04-22 19:07:51 +05:30
post api("/projects/#{project.id}/environments/#{non_existing_record_id}/stop", user)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
end
end
2019-07-31 22:56:46 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-07-31 22:56:46 +05:30
expect(response).to match_response_schema('public_api/v4/environment')
2021-11-18 22:05:49 +05:30
expect(json_response['last_deployment']).to be_present
2019-07-31 22:56:46 +05:30
end
end
context 'as non member' do
it 'returns a 404 status code' do
get api("/projects/#{project.id}/environments/#{environment.id}", non_member)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-07-31 22:56:46 +05:30
end
end
end
2021-04-17 20:07:23 +05:30
describe "DELETE /projects/:id/environments/review_apps" do
shared_examples "delete stopped review environments" do
around do |example|
freeze_time { example.run }
end
it "deletes the old stopped review apps" do
old_stopped_review_env = create(:environment, :with_review_app, :stopped, created_at: 31.days.ago, project: project)
new_stopped_review_env = create(:environment, :with_review_app, :stopped, project: project)
old_active_review_env = create(:environment, :with_review_app, :available, created_at: 31.days.ago, project: project)
old_stopped_other_env = create(:environment, :stopped, created_at: 31.days.ago, project: project)
new_stopped_other_env = create(:environment, :stopped, project: project)
old_active_other_env = create(:environment, :available, created_at: 31.days.ago, project: project)
delete api("/projects/#{project.id}/environments/review_apps", current_user), params: { dry_run: false }
project.environments.reload
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["scheduled_entries"].size).to eq(1)
expect(json_response["scheduled_entries"].first["id"]).to eq(old_stopped_review_env.id)
expect(json_response["unprocessable_entries"].size).to eq(0)
2023-03-04 22:38:38 +05:30
expect(json_response["scheduled_entries"]).to match_schema('public_api/v4/basic_environments')
expect(json_response["unprocessable_entries"]).to match_schema('public_api/v4/basic_environments')
2021-04-17 20:07:23 +05:30
expect(old_stopped_review_env.reload.auto_delete_at).to eq(1.week.from_now)
expect(new_stopped_review_env.reload.auto_delete_at).to be_nil
expect(old_active_review_env.reload.auto_delete_at).to be_nil
expect(old_stopped_other_env.reload.auto_delete_at).to be_nil
expect(new_stopped_other_env.reload.auto_delete_at).to be_nil
expect(old_active_other_env.reload.auto_delete_at).to be_nil
end
end
context "as a maintainer" do
it_behaves_like "delete stopped review environments" do
let(:current_user) { user }
end
end
context "as a developer" do
let(:developer) { create(:user) }
before do
project.add_developer(developer)
end
it_behaves_like "delete stopped review environments" do
let(:current_user) { developer }
end
end
context "as a reporter" do
let(:reporter) { create(:user) }
before do
project.add_reporter(reporter)
end
it "rejects the request" do
delete api("/projects/#{project.id}/environments/review_apps", reporter)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context "as a non member" do
it "rejects the request" do
delete api("/projects/#{project.id}/environments/review_apps", non_member)
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2016-09-13 17:45:13 +05:30
end