Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Metagenomics
metagenedb
Commits
c16a9567
Commit
c16a9567
authored
Jun 19, 2019
by
Kenzo-Hugo Hillion
♻
Browse files
add JWT auth
parent
5c0eff4a
Pipeline
#12644
failed with stage
in 52 seconds
Changes
15
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
.gitlab-ci.yml
View file @
c16a9567
...
...
@@ -30,5 +30,5 @@ test-backend:
DJANGO_SETTINGS_MODULE
:
"
metagenedb.settings-gitlab-ci"
script
:
-
flake8 --max-line-length
120
-
pytest --cov . 2>&1
pytest
_tmp.out
-
pytest --cov scripts/ --cov-append
-
coverage run --source='.' scripts/manage.
py
test
metagenedb/tests
-
coverage report
backend/metagenedb/apps/accounts/__init__.py
0 → 100644
View file @
c16a9567
backend/metagenedb/apps/accounts/apps.py
0 → 100644
View file @
c16a9567
from
django.apps
import
AppConfig
class
AccountsConfig
(
AppConfig
):
name
=
'accounts'
backend/metagenedb/apps/accounts/migrations/__init__.py
0 → 100644
View file @
c16a9567
backend/metagenedb/apps/accounts/urls.py
0 → 100644
View file @
c16a9567
from
django.urls
import
re_path
from
rest_framework_jwt.views
import
(
obtain_jwt_token
,
refresh_jwt_token
,
verify_jwt_token
,
)
urlpatterns
=
[
re_path
(
r
'^auth/obtain_token/'
,
obtain_jwt_token
,
name
=
'api-jwt-auth'
),
re_path
(
r
'^auth/refresh_token/'
,
refresh_jwt_token
,
name
=
'api-jwt-refresh'
),
re_path
(
r
'^auth/verify_token/'
,
verify_jwt_token
,
name
=
'api-jwt-verify'
),
]
backend/metagenedb/settings/django.py
View file @
c16a9567
...
...
@@ -10,6 +10,7 @@ environ.Env.read_env(root('.env')) # reading .env file
INSTALLED_APPS
=
[
'metagenedb.apps.catalog'
,
'metagenedb.apps.accounts'
,
'django.contrib.admin'
,
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
...
...
@@ -93,6 +94,20 @@ CORS_ORIGIN_WHITELIST = (
)
# Rest framework
REST_FRAMEWORK
=
{
'DEFAULT_PERMISSION_CLASSES'
:
(
'rest_framework.permissions.IsAuthenticated'
,
),
'DEFAULT_AUTHENTICATION_CLASSES'
:
(
'rest_framework_jwt.authentication.JSONWebTokenAuthentication'
,
'rest_framework.authentication.SessionAuthentication'
,
'rest_framework.authentication.BasicAuthentication'
,
),
}
# Config by .env file
DEBUG
=
env
.
bool
(
'DEBUG'
,
default
=
False
)
...
...
backend/metagenedb/tests/__init__.py
0 → 100644
View file @
c16a9567
backend/metagenedb/tests/apps/__init__.py
0 → 100644
View file @
c16a9567
backend/metagenedb/tests/apps/accounts/__init__.py
0 → 100644
View file @
c16a9567
backend/metagenedb/tests/apps/accounts/tests.py
0 → 100644
View file @
c16a9567
from
django.urls
import
reverse
from
rest_framework.test
import
APITestCase
from
rest_framework
import
status
from
django.contrib.auth.models
import
User
class
TestAccounts
(
APITestCase
):
def
test_obtain_jwt
(
self
):
# create an inactive user
url
=
reverse
(
'api-jwt-auth'
)
u
=
User
.
objects
.
create_user
(
username
=
'user'
,
email
=
'user@foo.com'
,
password
=
'pass'
)
u
.
is_active
=
False
u
.
save
()
# authenticate with username and password
resp
=
self
.
client
.
post
(
url
,
{
'email'
:
'user@foo.com'
,
'password'
:
'pass'
},
format
=
'json'
)
self
.
assertEqual
(
resp
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
# set the user to activate and attempt to get a token from login
u
.
is_active
=
True
u
.
save
()
resp
=
self
.
client
.
post
(
url
,
{
'username'
:
'user'
,
'password'
:
'pass'
},
format
=
'json'
)
self
.
assertEqual
(
resp
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertTrue
(
'token'
in
resp
.
data
)
backend/metagenedb/tests/test_user.py
deleted
100644 → 0
View file @
5c0eff4a
from
django.contrib.auth.models
import
User
from
django.test
import
TestCase
class
TestDatabase
(
TestCase
):
def
test_create_user
(
self
):
user
=
User
.
objects
.
create_user
(
username
=
'user'
,
email
=
'user@foo.com'
,
password
=
'pass'
)
user
.
save
()
user_count
=
User
.
objects
.
all
().
count
()
self
.
assertEqual
(
user_count
,
1
)
backend/metagenedb/urls.py
View file @
c16a9567
...
...
@@ -18,6 +18,7 @@ from django.urls import include, path
urlpatterns
=
[
path
(
''
,
include
(
'metagenedb.apps.accounts.urls'
)),
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
'catalog/'
,
include
(
'metagenedb.apps.catalog.urls'
))
]
backend/pytest.ini
View file @
c16a9567
[pytest]
DJANGO_SETTINGS_MODULE
=
backend
.settings
DJANGO_SETTINGS_MODULE
=
metagenedb
.settings
python_files
=
tests.py test_*.py *_tests.py
\ No newline at end of file
backend/requirements.txt
View file @
c16a9567
...
...
@@ -2,8 +2,11 @@ Django==2.2.1
django-cors-headers==3.0.2
django-environ==0.4.5
django-extensions==2.1.7
django-filter==2.1.0
djangorestframework==3.9.4
djangorestframework-jwt==1.11.0
psycopg2==2.8.2
PyJWT==1.7.1
pytz==2019.1
six==1.12.0
sqlparse==0.3.0
backend/requirements_dev.txt
View file @
c16a9567
...
...
@@ -20,7 +20,6 @@ pyflakes==2.1.1
pyparsing==2.4.0
pytest==4.6.3
pytest-cov==2.7.1
pytest-django==3.5.0
pytz==2019.1
six==1.12.0
sqlparse==0.3.0
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment