Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
1f6f8e2e
Commit
1f6f8e2e
authored
Mar 01, 2021
by
Can Eldem
Committed by
Igor Drozdov
Mar 01, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create service for assigning policy project
parent
e3bc67fa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
112 additions
and
0 deletions
+112
-0
ee/app/services/security/orchestration/assign_service.rb
ee/app/services/security/orchestration/assign_service.rb
+50
-0
ee/spec/services/security/orchestration/assign_service_spec.rb
...ec/services/security/orchestration/assign_service_spec.rb
+62
-0
No files found.
ee/app/services/security/orchestration/assign_service.rb
0 → 100644
View file @
1f6f8e2e
# frozen_string_literal: true
module
Security
module
Orchestration
class
AssignService
<
::
BaseService
def
execute
res
=
create_or_update_security_policy_configuration
return
success
if
res
rescue
ActiveRecord
::
RecordNotFound
=>
_
error
(
'Policy project doesn\'t exists'
)
rescue
ActiveRecord
::
RecordInvalid
=>
_
error
(
'Couldn\'t assign policy to project'
)
end
private
def
create_or_update_security_policy_configuration
policy_project
=
Project
.
find
(
policy_project_id
)
if
has_existing_policy?
project
.
security_orchestration_policy_configuration
.
update!
(
security_policy_management_project_id:
policy_project
.
id
)
else
project
.
create_security_orchestration_policy_configuration!
do
|
p
|
p
.
security_policy_management_project_id
=
policy_project
.
id
end
end
end
def
success
ServiceResponse
.
success
(
payload:
{
policy_project:
policy_project_id
})
end
def
error
(
message
)
ServiceResponse
.
error
(
payload:
{
policy_project:
policy_project_id
},
message:
message
)
end
def
has_existing_policy?
project
.
security_orchestration_policy_configuration
.
present?
end
def
policy_project_id
params
[
:policy_project_id
]
end
end
end
end
ee/spec/services/security/orchestration/assign_service_spec.rb
0 → 100644
View file @
1f6f8e2e
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Security
::
Orchestration
::
AssignService
do
let_it_be
(
:project
,
reload:
true
)
{
create
(
:project
)
}
let_it_be
(
:another_project
)
{
create
(
:project
)
}
let_it_be
(
:policy_project
)
{
create
(
:project
)
}
let_it_be
(
:new_policy_project
)
{
create
(
:project
)
}
describe
'#execute'
do
subject
(
:service
)
{
described_class
.
new
(
project
,
nil
,
policy_project_id:
policy_project
.
id
).
execute
}
it
'assigns policy project to project'
do
expect
(
service
).
to
be_success
expect
(
project
.
security_orchestration_policy_configuration
.
security_policy_management_project_id
).
to
eq
(
policy_project
.
id
)
end
it
'updates project with new policy project'
do
service
repeated_service
=
described_class
.
new
(
project
,
nil
,
policy_project_id:
new_policy_project
.
id
).
execute
expect
(
repeated_service
).
to
be_success
expect
(
project
.
security_orchestration_policy_configuration
.
security_policy_management_project_id
).
to
eq
(
new_policy_project
.
id
)
end
it
'returns error when same policy is assigned to different projects'
do
service
repeated_service
=
described_class
.
new
(
another_project
,
nil
,
policy_project_id:
policy_project
.
id
).
execute
expect
(
repeated_service
).
to
be_error
end
it
'returns error when db has problem'
do
dbl_error
=
double
(
'ActiveRecord'
)
dbl
=
double
(
'Security::OrchestrationPolicyConfiguration'
,
security_orchestration_policy_configuration:
dbl_error
)
allow
(
dbl_error
).
to
receive
(
:update!
).
and_raise
(
ActiveRecord
::
RecordInvalid
)
allow_next_instance_of
(
described_class
)
do
|
instance
|
allow
(
instance
).
to
receive
(
:has_existing_policy?
).
and_return
(
true
)
allow
(
instance
).
to
receive
(
:project
).
and_return
(
dbl
)
end
repeated_service
=
described_class
.
new
(
project
,
nil
,
policy_project_id:
new_policy_project
.
id
).
execute
expect
(
repeated_service
).
to
be_error
end
describe
'with invalid project id'
do
subject
(
:service
)
{
described_class
.
new
(
project
,
nil
,
policy_project_id:
345
).
execute
}
it
'assigns policy project to project'
do
expect
(
service
).
to
be_error
expect
{
service
}.
not_to
change
{
project
.
security_orchestration_policy_configuration
}
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment