FAQ
| This is a
LIVE
service |
Changelog
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
API Gateway Auth
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Information Services
DevOps
Django Libraries
API Gateway Auth
Commits
09eabe35
Commit
09eabe35
authored
3 years ago
by
Monty Dawson
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for permissions spec methods
parent
cfd454ee
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1
Initial implementation
Pipeline
#172529
passed
3 years ago
Stage: build
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
apigatewayauth/tests/mocks/permissions_spec.py
+23
-0
23 additions, 0 deletions
apigatewayauth/tests/mocks/permissions_spec.py
apigatewayauth/tests/test_permissions.py
+49
-1
49 additions, 1 deletion
apigatewayauth/tests/test_permissions.py
with
72 additions
and
1 deletion
apigatewayauth/tests/mocks/permissions_spec.py
0 → 100644
+
23
−
0
View file @
09eabe35
from
typing
import
Dict
from
functools
import
wraps
from
tempfile
import
NamedTemporaryFile
import
yaml
from
django.test
import
override_settings
def
override_permission_spec
(
permissions_spec
:
Dict
[
str
,
Dict
[
str
,
str
]]):
"""
A decorator which allows the permissions specification to be mocked, allowing a
permission to only be enabled for the given identities.
"""
def
decorator
(
func
):
@wraps
(
func
)
def
wrapped_function
(
*
args
,
**
kwargs
):
with
NamedTemporaryFile
(
'
w+
'
)
as
temp_file
:
yaml
.
dump
(
permissions_spec
,
temp_file
.
file
)
with
override_settings
(
PERMISSIONS_SPECIFICATION_URL
=
temp_file
.
name
):
func
(
*
args
,
**
kwargs
)
return
wrapped_function
return
decorator
This diff is collapsed.
Click to expand it.
apigatewayauth/tests/test_permissions.py
+
49
−
1
View file @
09eabe35
...
...
@@ -4,6 +4,7 @@ from identitylib.identifiers import Identifier, IdentifierSchemes
from
rest_framework.test
import
APIRequestFactory
from
ucamlookup.ibisclient
import
IbisException
from
.mocks.permissions_spec
import
override_permission_spec
from
.mocks.mock_ibis
import
MockIbisGroup
from
.mocks.models
import
TestModel
...
...
@@ -13,7 +14,8 @@ from apigatewayauth.permissions import (
Disallowed
,
HasAnyScope
,
SpecifiedPermission
,
IsResourceOwningPrincipal
IsResourceOwningPrincipal
,
get_permissions_for_request
)
...
...
@@ -337,3 +339,49 @@ class PermissionsTestCase(TestCase):
get_groups_mock
.
assert_called_with
(
scheme
=
'
crsid
'
,
identifier
=
self
.
client_auth_details
.
principal_identifier
.
value
)
@override_permission_spec
({
'
DATA_READER
'
:
{
'
principals
'
:
{
str
(
Identifier
(
'
abc44
'
,
IdentifierSchemes
.
CRSID
)),
str
(
Identifier
(
'
abc55
'
,
IdentifierSchemes
.
CRSID
))
}
},
'
DATA_OWNER
'
:
{
'
principals
'
:
{
str
(
Identifier
(
'
abc44
'
,
IdentifierSchemes
.
CRSID
))
}
}
})
def
test_get_permissions_for_request
(
self
):
"""
Test that `get_permissions_for_request` returns correctly based on the user authenticated
on the request.
"""
mock_request
=
APIRequestFactory
().
get
(
'
/
'
)
setattr
(
mock_request
,
'
auth
'
,
None
)
# expect an empty list to be returned if no auth details provided
self
.
assertListEqual
(
get_permissions_for_request
(
mock_request
),
[])
# if we have a principal not in our permissions spec we should get an empty list
mock_request
.
auth
=
APIGatewayAuthenticationDetails
(
Identifier
(
'
abc123
'
,
IdentifierSchemes
.
CRSID
),
[]
)
self
.
assertListEqual
(
get_permissions_for_request
(
mock_request
),
[])
# abc55 should be shown as a `DATA_READER`
mock_request
.
auth
=
APIGatewayAuthenticationDetails
(
Identifier
(
'
abc55
'
,
IdentifierSchemes
.
CRSID
),
[]
)
self
.
assertListEqual
(
get_permissions_for_request
(
mock_request
),
[
'
DATA_READER
'
])
# abc44 should be shown as a `DATA_READER` and `DATA_WRITER`
mock_request
.
auth
=
APIGatewayAuthenticationDetails
(
Identifier
(
'
abc44
'
,
IdentifierSchemes
.
CRSID
),
[]
)
self
.
assertListEqual
(
get_permissions_for_request
(
mock_request
),
[
'
DATA_OWNER
'
,
'
DATA_READER
'
]
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment