Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/token auth #13

Merged
merged 3 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/objecttypes/conf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"DEFAULT_RENDERER_CLASSES": ["rest_framework.renderers.JSONRenderer"],
"DEFAULT_PARSER_CLASSES": ["rest_framework.parsers.JSONParser"],
"DEFAULT_FILTER_BACKENDS": ["vng_api_common.filters.Backend"],
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication"
],
"DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
# test
"TEST_REQUEST_DEFAULT_FORMAT": "json",
}
Expand All @@ -17,6 +21,9 @@
SWAGGER_SETTINGS.update(
{
"DEFAULT_INFO": "objecttypes.api.schema.info",
"SECURITY_DEFINITIONS": None,
# Use apiKey type since OAS2 doesn't support Bearer authentication
"SECURITY_DEFINITIONS": {
"Token": {"type": "apiKey", "name": "Authorization", "in": "header"}
},
}
)
1 change: 1 addition & 0 deletions src/objecttypes/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"compat", # Part of hijack
"hijack_admin",
"rest_framework",
"rest_framework.authtoken",
"drf_yasg",
"vng_api_common",
# Project applications.
Expand Down
21 changes: 21 additions & 0 deletions src/objecttypes/tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.urls import reverse

from rest_framework import status
from rest_framework.test import APITestCase

from objecttypes.core.tests.factories import ObjectTypeFactory


class AuthTests(APITestCase):
def test_non_auth(self):
object_type = ObjectTypeFactory.create()

urls = [
reverse("objecttype-list"),
reverse("objecttype-detail", args=[object_type.uuid]),
]

for url in urls:
with self.subTest(url=url):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
3 changes: 2 additions & 1 deletion src/objecttypes/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from rest_framework.test import APITestCase

from objecttypes.core.tests.factories import ObjectTypeFactory
from objecttypes.utils.test import TokenAuthMixin


class FilterTests(APITestCase):
class FilterTests(TokenAuthMixin, APITestCase):
url = reverse_lazy("objecttype-list")

def test_filter_public_data(self):
Expand Down
3 changes: 2 additions & 1 deletion src/objecttypes/tests/test_objecttype_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from rest_framework.test import APITestCase

from objecttypes.core.tests.factories import ObjectTypeFactory, ObjectVersionFactory
from objecttypes.utils.test import TokenAuthMixin


class ObjectTypeAPITests(APITestCase):
class ObjectTypeAPITests(TokenAuthMixin, APITestCase):
def test_get_objecttypes(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
Expand Down
17 changes: 17 additions & 0 deletions src/objecttypes/utils/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework.authtoken.models import Token

from objecttypes.accounts.models import User


class TokenAuthMixin:
@classmethod
def setUpTestData(cls):
super().setUpTestData()

test_user = User.objects.create(username="testsuite", password="letmein")
cls.token = Token.objects.create(user=test_user)

def setUp(self):
super().setUp()

self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token}")
7 changes: 7 additions & 0 deletions src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: objecttypes API
description: An API to access Object types
version: 0.1.0
security:
- Token: []
paths:
/objecttypes/:
get:
Expand Down Expand Up @@ -308,6 +310,11 @@ tags:
servers:
- url: https://example.com/api/v1
components:
securitySchemes:
Token:
type: apiKey
name: Authorization
in: header
schemas:
ObjectVersion:
type: object
Expand Down
12 changes: 12 additions & 0 deletions src/swagger2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
"produces": [
"application/json"
],
"securityDefinitions": {
"Token": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
},
"security": [
{
"Token": []
}
],
"paths": {
"/objecttypes/": {
"get": {
Expand Down