Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DataCatalog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
eFlows4HPC WP2
DataCatalog
Commits
67afb0ac
Commit
67afb0ac
authored
3 years ago
by
Jedrzej Rybicki
Browse files
Options
Downloads
Patches
Plain Diff
authenticte and current user tests
parent
6e6e78eb
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#69464
passed
3 years ago
Stage: test
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
apiserver/security/user.py
+6
-13
6 additions, 13 deletions
apiserver/security/user.py
tests/apiserver_tests/test_responsiveness.py
+8
-0
8 additions, 0 deletions
tests/apiserver_tests/test_responsiveness.py
tests/user_tests/test_user.py
+22
-5
22 additions, 5 deletions
tests/user_tests/test_user.py
with
36 additions
and
18 deletions
apiserver/security/user.py
+
6
−
13
View file @
67afb0ac
...
...
@@ -114,25 +114,18 @@ def get_password_hash(password):
def
authenticate_user
(
userdb
:
AbstractDBInterface
,
username
:
str
,
password
:
str
):
user
:
UserInDB
=
get_user
(
userdb
,
username
)
user
:
UserInDB
=
userdb
.
get
(
username
)
if
user
and
verify_password
(
password
,
user
.
hashed_password
):
return
user
return
None
def
create_access_token
(
data
:
dict
,
expires_delta
:
Optional
[
timedelta
]
=
None
):
def
create_access_token
(
data
:
dict
,
expires_delta
:
Optional
[
timedelta
]
=
timedelta
(
minutes
=
15
)
):
to_encode
=
data
.
copy
()
if
expires_delta
:
expire
=
datetime
.
utcnow
()
+
expires_delta
else
:
expire
=
datetime
.
utcnow
()
+
timedelta
(
minutes
=
15
)
expire
=
datetime
.
utcnow
()
+
expires_delta
to_encode
.
update
({
"
exp
"
:
expire
})
encoded_jwt
=
jwt
.
encode
(
to_encode
,
SECRET_KEY
,
algorithm
=
ALGORITHM
)
return
encoded_jwt
def
get_user
(
db
:
AbstractDBInterface
,
username
:
str
):
return
db
.
get
(
username
)
return
jwt
.
encode
(
to_encode
,
SECRET_KEY
,
algorithm
=
ALGORITHM
)
credentials_exception
=
HTTPException
(
status_code
=
status
.
HTTP_401_UNAUTHORIZED
,
...
...
@@ -144,7 +137,7 @@ def get_current_user(token: str, userdb: AbstractDBInterface):
try
:
payload
=
jwt
.
decode
(
token
,
SECRET_KEY
,
algorithms
=
[
ALGORITHM
])
username
:
str
=
payload
.
get
(
"
sub
"
)
if
(
username
is
None
)
or
((
user
:
=
get_user
(
userdb
,
username
))
is
None
):
if
(
username
is
None
)
or
((
user
:
=
userdb
.
get
(
username
))
is
None
):
raise
credentials_exception
return
user
...
...
This diff is collapsed.
Click to expand it.
tests/apiserver_tests/test_responsiveness.py
+
8
−
0
View file @
67afb0ac
...
...
@@ -36,3 +36,11 @@ class NonAuthTests(unittest.TestCase):
def
test_token
(
self
):
rsp
=
self
.
client
.
post
(
'
/token
'
,
data
=
{
'
username
'
:
'
foo
'
,
'
password
'
:
'
bar
'
})
self
.
assertEqual
(
rsp
.
status_code
,
401
,
'
Ath
'
)
def
test_get_non_existing
(
self
):
rsp
=
self
.
client
.
get
(
'
/dataset/foo
'
)
self
.
assertEqual
(
404
,
rsp
.
status_code
)
j
=
rsp
.
json
()
self
.
assertTrue
(
'
message
'
in
j
,
f
"
{
j
}
should contain message
"
)
self
.
assertTrue
(
'
foo
'
in
j
[
'
message
'
],
f
"
{
j
}
should contain object id (foo)
"
)
This diff is collapsed.
Click to expand it.
tests/user_tests/test_user.py
+
22
−
5
View file @
67afb0ac
import
unittest
from
apiserver.security
import
User
,
JsonDBInterface
,
UserInDB
from
apiserver.security
import
User
,
JsonDBInterface
,
UserInDB
,
authenticate_user
,
get_current_user
from
apiserver.config
import
ApiserverSettings
from
fastapi
import
HTTPException
from
collections
import
namedtuple
import
os
import
pathlib
import
shutil
import
random
from
unittest.mock
import
Mock
,
patch
class
UserTests
(
unittest
.
TestCase
):
...
...
@@ -77,7 +79,22 @@ class UserTests(unittest.TestCase):
self
.
userdb
.
add
(
UserInDB
(
username
=
f
"
user_
{
n
}
"
,
email
=
'
jo@go.com
'
,
hashed_password
=
f
"
{
random
.
randint
(
0
,
200
)
}
"
))
self
.
assertEqual
(
len
(
self
.
userdb
.
list
()),
25
)
def
test_not_authenticate_user
(
self
):
mock
=
Mock
(
spec
=
JsonDBInterface
)
mock
.
get
.
return_value
=
None
user
=
authenticate_user
(
userdb
=
mock
,
username
=
'
foo
'
,
password
=
'
pass
'
)
self
.
assertIsNone
(
user
)
mock
.
get
.
assert_called_with
(
'
foo
'
)
def
test_authenticate_user
(
self
):
mock
=
Mock
(
spec
=
JsonDBInterface
)
mock
.
get
.
return_value
(
UserInDB
(
username
=
'
foo
'
,
email
=
'
bar@o.w
'
,
hashed_password
=
'
passed
'
))
with
patch
(
'
apiserver.security.user.verify_password
'
)
as
vp
:
user
=
authenticate_user
(
userdb
=
mock
,
username
=
'
foo
'
,
password
=
'
passed
'
)
self
.
assertIsNotNone
(
user
)
vp
.
assert_called_once
()
mock
.
get
.
assert_called_once
()
mock
.
get
.
assert_called_with
(
'
foo
'
)
def
test_current_user
(
self
):
self
.
assertRaises
(
HTTPException
,
get_current_user
,
'
falsetoken
'
,
Mock
(
spec
=
JsonDBInterface
))
\ No newline at end of file
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