summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatija Čupić <matteeyah@gmail.com>2017-12-01 17:22:41 +0100
committerMatija Čupić <matteeyah@gmail.com>2017-12-01 17:22:41 +0100
commit9199d30a81e221ebe5d76fef58071d991e02ef9f (patch)
tree89d5cda9a3ae60adb9a40646e0967dfec92a5e12
parentce704a9533f8026edbfd535f5477caf09604a8a8 (diff)
downloadgitlab-ce-9199d30a81e221ebe5d76fef58071d991e02ef9f.tar.gz
Add can_toggle_cluster? helper
-rw-r--r--app/helpers/clusters_helper.rb5
-rw-r--r--app/views/projects/clusters/_cluster.html.haml2
-rw-r--r--spec/helpers/clusters_helper_spec.rb45
3 files changed, 51 insertions, 1 deletions
diff --git a/app/helpers/clusters_helper.rb b/app/helpers/clusters_helper.rb
new file mode 100644
index 00000000000..f8281c893fa
--- /dev/null
+++ b/app/helpers/clusters_helper.rb
@@ -0,0 +1,5 @@
+module ClustersHelper
+ def can_toggle_cluster?(cluster)
+ can?(current_user, :update_cluster, cluster) && cluster.created?
+ end
+end
diff --git a/app/views/projects/clusters/_cluster.html.haml b/app/views/projects/clusters/_cluster.html.haml
index ac1e7d0ac19..c9c494688f1 100644
--- a/app/views/projects/clusters/_cluster.html.haml
+++ b/app/views/projects/clusters/_cluster.html.haml
@@ -13,7 +13,7 @@
.table-mobile-header{ role: "rowheader" }
.table-mobile-content
%button{ type: "button",
- class: "js-toggle-cluster-list project-feature-toggle #{'is-checked' unless !cluster.enabled?} #{'is-disabled' unless can?(current_user, :update_cluster, cluster) && cluster.provider_gcp&.created?}",
+ class: "js-toggle-cluster-list project-feature-toggle #{'is-checked' if cluster.enabled?} #{'is-disabled' if can_toggle_cluster?(cluster)}",
"aria-label": s_("ClusterIntegration|Toggle Cluster"),
disabled: !can?(current_user, :update_cluster, cluster),
data: { "enabled-text": s_("ClusterIntegration|Active"),
diff --git a/spec/helpers/clusters_helper_spec.rb b/spec/helpers/clusters_helper_spec.rb
new file mode 100644
index 00000000000..459fdfdc5a4
--- /dev/null
+++ b/spec/helpers/clusters_helper_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+describe ClustersHelper do
+ let(:cluster) { create(:cluster) }
+
+ describe '.can_toggle_cluster' do
+ let(:user) { create(:user) }
+
+ before do
+ allow(helper).to receive(:current_user).and_return(user)
+ end
+
+ subject { helper.can_toggle_cluster?(cluster) }
+
+ context 'when user can update' do
+ before do
+ allow(helper).to receive(:can?).with(any_args).and_return(true)
+ end
+
+ context 'when cluster is created' do
+ before do
+ allow(cluster).to receive(:created?).and_return(true)
+ end
+
+ it { is_expected.to eq(true) }
+ end
+
+ context 'when cluster is not created' do
+ before do
+ allow(cluster).to receive(:created?).and_return(false)
+ end
+
+ it { is_expected.to eq(false) }
+ end
+ end
+
+ context 'when user can not update' do
+ before do
+ allow(helper).to receive(:can?).with(any_args).and_return(false)
+ end
+
+ it { is_expected.to eq(false) }
+ end
+ end
+end