Commit 83648f24 authored by Matija Čupić's avatar Matija Čupić

Move can_toggle_cluster? from helper to presenter

parent 2eb41b5a
module ClustersHelper
def can_toggle_cluster?(cluster)
can?(current_user, :update_cluster, cluster) && cluster.created?
end
end
...@@ -5,5 +5,9 @@ module Clusters ...@@ -5,5 +5,9 @@ module Clusters
def gke_cluster_url def gke_cluster_url
"https://console.cloud.google.com/kubernetes/clusters/details/#{provider.zone}/#{name}" if gcp? "https://console.cloud.google.com/kubernetes/clusters/details/#{provider.zone}/#{name}" if gcp?
end end
def can_toggle_cluster?
can?(current_user, :update_cluster, cluster) && created?
end
end end
end end
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
.table-mobile-header{ role: "rowheader" } .table-mobile-header{ role: "rowheader" }
.table-mobile-content .table-mobile-content
%button{ type: "button", %button{ type: "button",
class: "js-toggle-cluster-list project-feature-toggle #{'is-checked' if cluster.enabled?} #{'is-disabled' if !can_toggle_cluster?(cluster)}", class: "js-toggle-cluster-list project-feature-toggle #{'is-checked' if cluster.enabled?} #{'is-disabled' if !cluster.can_toggle_cluster?}",
"aria-label": s_("ClusterIntegration|Toggle Cluster"), "aria-label": s_("ClusterIntegration|Toggle Cluster"),
disabled: !can_toggle_cluster?(cluster), disabled: !cluster.can_toggle_cluster?,
data: { "enabled-text": s_("ClusterIntegration|Active"), data: { "enabled-text": s_("ClusterIntegration|Active"),
"disabled-text": s_("ClusterIntegration|Inactive"), "disabled-text": s_("ClusterIntegration|Inactive"),
endpoint: namespace_project_cluster_path(@project.namespace, @project, cluster, format: :json) } } endpoint: namespace_project_cluster_path(@project.namespace, @project, cluster, format: :json) } }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
= s_("ClusterIntegration|Project namespace") = s_("ClusterIntegration|Project namespace")
.table-section.section-10{ role: "rowheader" } .table-section.section-10{ role: "rowheader" }
- @clusters.each do |cluster| - @clusters.each do |cluster|
= render "cluster", cluster: cluster = render "cluster", cluster: cluster.present(current_user: current_user)
= paginate @clusters, theme: "gitlab" = paginate @clusters, theme: "gitlab"
- elsif @scope == 'all' - elsif @scope == 'all'
= render "empty_state" = render "empty_state"
......
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
...@@ -31,4 +31,44 @@ describe Clusters::ClusterPresenter do ...@@ -31,4 +31,44 @@ describe Clusters::ClusterPresenter do
it { is_expected.to include(cluster.provider.zone) } it { is_expected.to include(cluster.provider.zone) }
it { is_expected.to include(cluster.name) } it { is_expected.to include(cluster.name) }
end end
describe '#can_toggle_cluster' do
let(:user) { create(:user) }
before do
allow(cluster).to receive(:current_user).and_return(user)
end
subject { described_class.new(cluster).can_toggle_cluster? }
context 'when user can update' do
before do
allow_any_instance_of(described_class).to receive(:can?).with(user, :update_cluster, cluster).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_any_instance_of(described_class).to receive(:can?).with(user, :update_cluster, cluster).and_return(false)
end
it { is_expected.to eq(false) }
end
end
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