Commit 265fbfea authored by Alexander Turinske's avatar Alexander Turinske

Abstract out details of policy_helper

- move details into a private method to make
  #policy_details more readable
- abstract out reusable test data
parent 1f7e1907
...@@ -4,15 +4,7 @@ module PolicyHelper ...@@ -4,15 +4,7 @@ module PolicyHelper
def policy_details(project, policy = nil, environment = nil) def policy_details(project, policy = nil, environment = nil)
return unless project return unless project
details = { details = details(project)
network_policies_endpoint: project_security_network_policies_path(project),
configure_agent_help_path: help_page_url('user/clusters/agent/repository.html'),
create_agent_help_path: help_page_url('user/clusters/agent/index.md', anchor: 'create-an-agent-record-in-gitlab'),
environments_endpoint: project_environments_path(project),
project_path: project.full_path,
threat_monitoring_path: project_threat_monitoring_path(project)
}
return details unless policy && environment return details unless policy && environment
edit_details = { edit_details = {
...@@ -21,4 +13,17 @@ module PolicyHelper ...@@ -21,4 +13,17 @@ module PolicyHelper
} }
details.merge(edit_details) details.merge(edit_details)
end end
private
def details(project)
{
network_policies_endpoint: project_security_network_policies_path(project),
configure_agent_help_path: help_page_url('user/clusters/agent/repository.html'),
create_agent_help_path: help_page_url('user/clusters/agent/index.md', anchor: 'create-an-agent-record-in-gitlab'),
environments_endpoint: project_environments_path(project),
project_path: project.full_path,
threat_monitoring_path: project_threat_monitoring_path(project)
}
end
end end
---
title: Abstract out details of policy_helper
merge_request: 52714
author:
type: other
...@@ -4,6 +4,8 @@ require 'spec_helper' ...@@ -4,6 +4,8 @@ require 'spec_helper'
RSpec.describe PolicyHelper do RSpec.describe PolicyHelper do
let(:project) { create(:project, :repository, :public) } let(:project) { create(:project, :repository, :public) }
let(:environment) { create(:environment, project: project) }
let(:policy) do let(:policy) do
Gitlab::Kubernetes::CiliumNetworkPolicy.new( Gitlab::Kubernetes::CiliumNetworkPolicy.new(
name: 'policy', name: 'policy',
...@@ -13,21 +15,23 @@ RSpec.describe PolicyHelper do ...@@ -13,21 +15,23 @@ RSpec.describe PolicyHelper do
) )
end end
let(:environment) { create(:environment, project: project) } let(:base_data) do
{
network_policies_endpoint: kind_of(String),
configure_agent_help_path: kind_of(String),
create_agent_help_path: kind_of(String),
environments_endpoint: kind_of(String),
project_path: project.full_path,
threat_monitoring_path: kind_of(String)
}
end
describe '#policy_details' do describe '#policy_details' do
context 'when a new policy is being created' do context 'when a new policy is being created' do
subject { helper.policy_details(project) } subject { helper.policy_details(project) }
it 'returns expected policy data' do it 'returns expected policy data' do
expect(subject).to match( expect(subject).to match(base_data)
network_policies_endpoint: kind_of(String),
configure_agent_help_path: kind_of(String),
create_agent_help_path: kind_of(String),
environments_endpoint: kind_of(String),
project_path: project.full_path,
threat_monitoring_path: kind_of(String)
)
end end
end end
...@@ -36,16 +40,20 @@ RSpec.describe PolicyHelper do ...@@ -36,16 +40,20 @@ RSpec.describe PolicyHelper do
it 'returns expected policy data' do it 'returns expected policy data' do
expect(subject).to match( expect(subject).to match(
network_policies_endpoint: kind_of(String), base_data.merge(
configure_agent_help_path: kind_of(String), policy: policy.to_json,
create_agent_help_path: kind_of(String), environment_id: environment.id
environments_endpoint: kind_of(String), )
project_path: project.full_path,
threat_monitoring_path: kind_of(String),
policy: policy.to_json,
environment_id: environment.id
) )
end end
end end
context 'when no environment is passed in' do
subject { helper.policy_details(project, policy) }
it 'returns expected policy data' do
expect(subject).to match(base_data)
end
end
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