Commit 27ab6b77 authored by Mayra Cabrera's avatar Mayra Cabrera

Adds feature specs for Protected Environments sections

Includes feature specs for the section under CI/CD settings and for
interacting with the environments page once they are protected

Closes #7200
parent 9cda7958
# frozen_string_literal: true
require 'spec_helper'
describe 'Environments page', :js do
let(:project) { create(:project, :repository) }
let!(:environment) { create(:environment, name: 'production', project: project) }
let(:user) { create(:user) }
before do
stub_feature_flags(protected_environments: true)
allow(License).to receive(:feature_available?).and_call_original
allow(License).to receive(:feature_available?).with(:protected_environments).and_return(true)
project.add_maintainer(user)
sign_in(user)
end
context 'when an environment is protected and user has access to it' do
before do
create(:protected_environment,
project: project, name: 'production',
authorize_user_to_deploy: user)
end
context 'when environment has manual actions' do
let!(:pipeline) { create(:ci_pipeline, project: project) }
let!(:build) { create(:ci_build, pipeline: pipeline) }
let!(:deployment) do
create(:deployment,
environment: environment, deployable: build,
sha: project.commit.id)
end
let!(:action) do
create(:ci_build, :manual,
pipeline: pipeline, name: 'deploy to production',
environment: 'production')
end
before do
visit project_environments_path(project)
wait_for_requests
end
it 'shows an enabled play button' do
find('.js-dropdown-play-icon-container').click
play_button = %q{button[class="js-manual-action-link no-btn btn"]}
expect(page).to have_selector(play_button)
end
it 'shows a stop button' do
stop_button_selector = %q{button[data-original-title="Stop environment"]}
expect(page).to have_selector(stop_button_selector)
end
context 'with external_url' do
let(:environment) { create(:environment, project: project, external_url: 'https://git.gitlab.com') }
it 'shows an external link button' do
expect(page).to have_link(nil, href: environment.external_url)
end
end
context 'when terminal is available' do
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [create(:project, :repository)]) }
let(:project) { cluster.project }
it 'shows a terminal button' do
expect(page).to have_link(nil, href: terminal_project_environment_path(project, environment))
end
end
end
context 'when environment can be rollback' do
let!(:pipeline) { create(:ci_pipeline, :success, project: project) }
let!(:build) { create(:ci_build, :success, pipeline: pipeline, environment: 'production') }
let!(:deployment) do
create(:deployment,
environment: environment, deployable: build,
sha: project.commit.id)
end
before do
visit project_environments_path(project)
wait_for_requests
end
it 'shows re deploy button' do
redeploy_button_selector = %q{button[data-original-title="Re-deploy to environment"]}
expect(page).to have_selector(redeploy_button_selector)
end
end
end
context 'when environment is protected and user does not have access to it' do
before do
create(:protected_environment,
project: project, name: 'production',
authorize_user_to_deploy: create(:user))
end
context 'when environment has manual actions' do
let!(:pipeline) { create(:ci_pipeline, project: project) }
let!(:build) { create(:ci_build, pipeline: pipeline, environment: 'production') }
let!(:deployment) do
create(:deployment,
environment: environment, deployable: build,
sha: project.commit.id)
end
let!(:action) do
create(:ci_build, :manual,
pipeline: pipeline, name: 'deploy to production',
environment: 'production')
end
before do
visit project_environments_path(project)
wait_for_requests
end
it 'show a disabled play button' do
find('.js-dropdown-play-icon-container').click
disabled_play_button = %q{button[class="js-manual-action-link no-btn btn disabled"]}
expect(page).to have_selector(disabled_play_button)
end
it 'does not show a stop button' do
stop_button_selector = %q{button[data-original-title="Stop environment"]}
expect(page).not_to have_selector(stop_button_selector)
end
context 'with external_url' do
let(:environment) { create(:environment, project: project, external_url: 'https://git.gitlab.com') }
it 'shows an external link button' do
expect(page).to have_link(nil, href: environment.external_url)
end
end
context 'when terminal is available' do
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [create(:project, :repository)]) }
let(:project) { cluster.project }
it 'does not shows a terminal button' do
expect(page).not_to have_link(nil, href: terminal_project_environment_path(project, environment))
end
end
end
context 'when environment can be rollback' do
let!(:pipeline) { create(:ci_pipeline, :success, project: project) }
let!(:build) { create(:ci_build, :success, pipeline: pipeline, environment: 'production') }
let!(:deployment) do
create(:deployment,
environment: environment, deployable: build,
sha: project.commit.id)
end
before do
visit project_environments_path(project)
wait_for_requests
end
it 'does not show a re deploy button' do
redeploy_button_selector = %q{button[data-original-title="Re-deploy to environment"]}
expect(page).not_to have_selector(redeploy_button_selector)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe 'Protected Environments' do
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
let(:environments) { %w(production development staging test) }
before do
stub_feature_flags(protected_environments: true)
environments.each do |environment_name|
create(:environment, name: environment_name, project: project)
end
create(:protected_environment, project: project, name: 'production')
sign_in(user)
end
context 'logged in as developer' do
before do
project.add_developer(user)
visit project_settings_ci_cd_path(project)
end
it 'does not have access to Protected Environments settings' do
expect(page).to have_gitlab_http_status(404)
end
end
context 'logged in as a maintainer' do
before do
project.add_maintainer(user)
visit project_settings_ci_cd_path(project)
end
it 'has access to Protected Environments settings' do
expect(page).to have_gitlab_http_status(200)
end
it 'allows seeing a list of protected environments' do
within('.protected-environments-list') do
expect(page).to have_content('production')
end
end
it 'allows creating explicit protected environments', :js do
set_protected_environment('staging')
within('.js-new-protected-environment') do
set_allowed_to_deploy('Developers + Maintainers')
click_on('Protect')
end
wait_for_requests
within('.protected-environments-list') do
expect(page).to have_content('staging')
end
end
it 'allows updating access to a protected environment', :js do
within('.protected-environments-list') do
set_allowed_to_deploy('Developers + Maintainers')
end
visit project_settings_ci_cd_path(project)
within('.protected-environments-list') do
expect(page).to have_content('1 role, 1 user')
end
end
it 'allows unprotecting an environment', :js do
within('.protected-environments-list') do
accept_alert { click_on('Unprotect') }
end
wait_for_requests
within('.protected-environments-list') do
expect(page).not_to have_content('production')
end
end
end
def set_protected_environment(environment_name)
within('.js-new-protected-environment') do
find('.js-protected-environment-select').click
find('.dropdown-input-field').set(environment_name)
find('.is-focused').click
end
end
def set_allowed_to_deploy(option)
find('.js-allowed-to-deploy').click
within('.dropdown-content') do
Array(option).each { |opt| click_on(opt) }
end
find('.js-allowed-to-deploy').click
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