Commit e9880722 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Fix environments specs

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