Commit e9880722 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Fix environments specs

parent 4a369185
class Projects::EnvironmentsController < Projects::ApplicationController
layout 'project'
before_action :authorize_read_environment!
before_action :authorize_create_environment!, only: [:new, :create]
before_action :authorize_update_environment!, only: [:edit, :update, :stop]
before_action :authorize_create_environment!, only: [:new, :create, :stop]
before_action :authorize_create_deployment!, only: [:stop]
before_action :authorize_update_environment!, only: [:edit, :update]
before_action :environment, only: [:show, :edit, :update, :stop]
def index
......
......@@ -21,6 +21,9 @@ class Environment < ActiveRecord::Base
delegate :stop_action, to: :last_deployment, allow_nil: true
scope :available, -> { with_state(:available) }
scope :stopped, -> { with_state(:stopped) }
state_machine :state, initial: :available do
event :start do
transition stopped: :available
......
......@@ -10,8 +10,8 @@ class CreateDeploymentService < BaseService
@environment = environment
@environment.external_url = expanded_url if expanded_url
@environment.fire_state_event(action)
@environment.save!
return unless @environment.save
return if @environment.stopped?
deploy.tap do |deployment|
......
- if environment.external_url
- if environment.external_url && can?(current_user, :read_environment, environment)
= link_to environment.external_url, target: '_blank', class: 'btn external-url' do
= icon('external-link')
- if environment.stoppable?
- if can?(current_user, :create_deployment, environment) && environment.stoppable?
.inline
= link_to stop_namespace_project_environment_path(@project.namespace, @project, environment), method: :post,
class: 'btn close-env-link', rel: 'nofollow', data: { confirm: 'Are you sure you want to stop this environment?' } do
......
......@@ -8,12 +8,10 @@
%h3.page-title= @environment.name.capitalize
.col-md-3
.nav-controls
- if can?(current_user, :read_environmnet, @environment)
= render 'projects/environments/external_url', environment: @environment
- if can?(current_user, :update_environment, @environment)
= link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn'
- if @environment.stoppable?
- if can?(current_user, :create_deployment, @environment) && @environment.stoppable?
= link_to 'Stop', stop_namespace_project_environment_path(@project.namespace, @project, @environment), data: { confirm: 'Are you sure you want to stop this environment?' }, class: 'btn btn-danger', method: :post
.deployments-container
......
......@@ -35,7 +35,7 @@ feature 'Environments', feature: true do
end
scenario 'does show 0 as counter for environments in both tabs' do
expect(page.find('.js-avaibale-environments-count').text).to eq('0')
expect(page.find('.js-available-environments-count').text).to eq('0')
expect(page.find('.js-stopped-environments-count').text).to eq('0')
end
end
......@@ -48,7 +48,7 @@ feature 'Environments', feature: true do
end
scenario 'does show number of opened environments in Availabe tab' do
expect(page.find('.js-avaibale-environments-count').text).to eq('1')
expect(page.find('.js-available-environments-count').text).to eq('1')
end
scenario 'does show number of closed environments in Stopped tab' do
......@@ -93,6 +93,14 @@ feature 'Environments', feature: true do
expect(page).to have_link("#{build.name} (##{build.id})")
end
scenario 'does not show stop button' do
expect(page).not_to have_selector('.close-env-link')
end
scenario 'does not show external link button' do
expect(page).not_to have_css('external-url')
end
context 'with external_url' do
given(:environment) { create(:environment, project: project, external_url: 'https://git.gitlab.com') }
given(:build) { create(:ci_build, pipeline: pipeline) }
......@@ -103,14 +111,27 @@ feature 'Environments', feature: true do
end
end
scenario 'does show close button' do
# TODO: Add test to verify if close button is visible
# This needs to be true: if local_assigns.fetch(:allow_close, false) && deployment.closeable?
context 'with stop action' do
given(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'close_app') }
given(:deployment) { create(:deployment, environment: environment, deployable: build, on_stop: 'close_app') }
scenario 'does show stop button' do
expect(page).to have_selector('.close-env-link')
end
scenario 'does allow to stop environment' do
first('.close-env-link').click
expect(page).to have_content('close_app')
end
scenario 'does allow to close environment' do
# TODO: Add test to verify if close environment works
# This needs to be true: if local_assigns.fetch(:allow_close, false) && deployment.closeable?
context 'for reporter' do
let(:role) { :reporter }
scenario 'does not show stop button' do
expect(page).not_to have_selector('.close-env-link')
end
end
end
end
end
......@@ -160,6 +181,10 @@ feature 'Environments', feature: true do
expect(page).to have_link('Re-deploy')
end
scenario 'does not show stop button' do
expect(page).not_to have_link('Stop')
end
context 'with manual action' do
given(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'deploy to production') }
......@@ -184,14 +209,27 @@ feature 'Environments', feature: true do
end
end
scenario 'does show close button' do
# TODO: Add test to verify if close button is visible
# This needs to be true: if local_assigns.fetch(:allow_close, false) && deployment.closeable?
context 'with stop action' do
given(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'close_app') }
given(:deployment) { create(:deployment, environment: environment, deployable: build, on_stop: 'close_app') }
scenario 'does show stop button' do
expect(page).to have_link('Stop')
end
scenario 'does allow to stop environment' do
click_link('Stop')
expect(page).to have_content('close_app')
end
scenario 'does allow to close environment' do
# TODO: Add test to verify if close environment works
# This needs to be true: if local_assigns.fetch(:allow_close, false) && deployment.closeable?
context 'for reporter' do
let(:role) { :reporter }
scenario 'does not show stop button' do
expect(page).not_to have_link('Stop')
end
end
end
end
end
......
......@@ -54,8 +54,8 @@ describe CreateDeploymentService, services: true do
expect(environment.reload).to be_available
end
it 'does not create a deployment' do
expect(subject).not_to be_persisted
it 'does create a deployment' do
expect(subject).to be_persisted
end
end
end
......@@ -95,7 +95,7 @@ describe CreateDeploymentService, services: true do
end
it 'does not create a deployment' do
expect(subject).not_to be_persisted
expect(subject).to be_nil
end
end
......@@ -127,6 +127,8 @@ describe CreateDeploymentService, services: true do
end
context 'and environment exist' do
let!(:environment) { create(:environment, project: project, name: 'review-apps/feature-review-apps') }
it 'does not create a new environment' do
expect { subject }.not_to change { Environment.count }
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