Commit a07a65b5 authored by Alexander Turinske's avatar Alexander Turinske

Redirect threat_monitoring policies endpoints

- redirect threat monitoring policies endpoints to the
  policies controller
- update tests

Changelog: changed
EE: true
parent ec74935e
......@@ -6,10 +6,6 @@ module Projects
before_action :authorize_read_threat_monitoring!
before_action do
push_frontend_feature_flag(:security_orchestration_policies_configuration, @project, default_enabled: :yaml)
end
feature_category :not_owned
# rubocop: disable CodeReuse/ActiveRecord
......@@ -19,19 +15,17 @@ module Projects
# rubocop: enable CodeReuse/ActiveRecord
def edit
@environment = project.environments.find(params[:environment_id])
@policy_name = params[:id]
response = NetworkPolicies::FindResourceService.new(
resource_name: @policy_name,
environment: @environment,
kind: params[:kind].presence || Gitlab::Kubernetes::CiliumNetworkPolicy::KIND
).execute
redirect_to edit_project_security_policy_path(
project,
environment_id: params[:environment_id],
id: params[:id],
type: params[:type],
kind: params[:kind]
)
end
if response.success?
@policy = response.payload
else
render_404
end
def new
redirect_to new_project_security_policy_path(project)
end
end
end
......@@ -75,33 +75,15 @@ RSpec.describe Projects::ThreatMonitoringController do
context 'with authorized user' do
before do
stub_licensed_features(threat_monitoring: true)
project.add_developer(user)
sign_in(user)
end
context 'when feature is available' do
before do
stub_licensed_features(threat_monitoring: true)
end
it 'renders the new template' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:new)
end
end
context 'when feature is not available' do
before do
stub_licensed_features(threat_monitoring: false)
end
it 'returns 404' do
subject
it 'redirects to policies#new page' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
expect(response).to redirect_to(new_project_security_policy_path(project))
end
end
......@@ -110,16 +92,10 @@ RSpec.describe Projects::ThreatMonitoringController do
sign_in(user)
end
context 'when feature is available' do
before do
stub_licensed_features(threat_monitoring: true)
end
it 'returns 404' do
subject
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
expect(response).to have_gitlab_http_status(:not_found)
end
end
......@@ -144,96 +120,40 @@ RSpec.describe Projects::ThreatMonitoringController do
let(:kind) { 'CiliumNetworkPolicy' }
context 'with authorized user' do
before do
project.add_developer(user)
sign_in(user)
let(:service) { instance_double('NetworkPolicies::FindResourceService', execute: ServiceResponse.success(payload: policy)) }
let(:policy) do
Gitlab::Kubernetes::CiliumNetworkPolicy.new(
name: 'policy',
namespace: 'another',
selector: { matchLabels: { role: 'db' } },
ingress: [{ from: [{ namespaceSelector: { matchLabels: { project: 'myproject' } } }] }]
)
end
context 'when feature is available' do
let(:service) { instance_double('NetworkPolicies::FindResourceService', execute: ServiceResponse.success(payload: policy)) }
let(:policy) do
Gitlab::Kubernetes::CiliumNetworkPolicy.new(
name: 'policy',
namespace: 'another',
selector: { matchLabels: { role: 'db' } },
ingress: [{ from: [{ namespaceSelector: { matchLabels: { project: 'myproject' } } }] }]
)
end
before do
stub_licensed_features(threat_monitoring: true)
allow(NetworkPolicies::FindResourceService).to(
receive(:new)
.with(resource_name: 'policy', environment: environment, kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND)
.and_return(service)
)
end
it 'renders the new template' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:edit)
end
context 'when different policy kind is requested' do
let(:policy) do
Gitlab::Kubernetes::NetworkPolicy.new(
name: 'not-cilium-policy',
namespace: 'another',
selector: { matchLabels: { role: 'db' } },
ingress: [{ from: [{ namespaceSelector: { matchLabels: { project: 'myproject' } } }] }]
)
end
before do
allow(NetworkPolicies::FindResourceService).to(
receive(:new)
.with(resource_name: 'policy', environment: environment, kind: Gitlab::Kubernetes::NetworkPolicy::KIND)
.and_return(service)
)
end
it 'renders the new template' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:edit)
end
end
context 'when environment is missing' do
let(:environment_id) { 'missing' }
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when service failed' do
let(:service) { instance_double('NetworkPolicies::FindResourceService', execute: ServiceResponse.error(message: 'error')) }
before do
stub_licensed_features(threat_monitoring: true)
it 'returns 404' do
subject
allow(NetworkPolicies::FindResourceService).to(
receive(:new)
.with(resource_name: 'policy', environment: environment, kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND)
.and_return(service)
)
expect(response).to have_gitlab_http_status(:not_found)
end
end
project.add_developer(user)
sign_in(user)
end
context 'when feature is not available' do
before do
stub_licensed_features(threat_monitoring: false)
end
it 'returns 404' do
subject
it 'redirects to policies#edit page' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
expect(response).to redirect_to(
edit_project_security_policy_path(
project,
environment_id: environment_id,
id: 'policy',
kind: kind
)
)
end
end
......@@ -242,16 +162,10 @@ RSpec.describe Projects::ThreatMonitoringController do
sign_in(user)
end
context 'when feature is available' do
before do
stub_licensed_features(threat_monitoring: true)
end
it 'returns 404' do
subject
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
expect(response).to have_gitlab_http_status(:not_found)
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