-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for DashboardApiView and ApiTokenRequiredMixin (#230)
* test(core): fix factories * test(api): Add tests for DashboardApiView and ApiTokenRequiredMixin * refactor(api): sort imports and ran black on api app
- Loading branch information
Showing
7 changed files
with
195 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from http import HTTPStatus | ||
|
||
from django.test import TestCase, RequestFactory | ||
from django.views import View | ||
|
||
from api.mixins import ApiTokenRequiredMixin | ||
from core.factories import UserFactory | ||
from core.models import _default_api_token, Service | ||
|
||
|
||
class TestApiTokenRequiredMixin(TestCase): | ||
class DummyView(ApiTokenRequiredMixin, View): | ||
model = Service | ||
template_name = "dashboard/pages/service.html" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.user = UserFactory() | ||
self.request = RequestFactory().get("/fake-path") | ||
|
||
# Setup request and view. | ||
self.factory = RequestFactory() | ||
self.view = self.DummyView() | ||
|
||
def test_get_user_by_token_without_authorization_token(self): | ||
""" | ||
GIVEN: A request without Authorization header | ||
WHEN: get_user_by_token is called | ||
THEN: It should return AnonymousUser | ||
""" | ||
user = self.view._get_user_by_token(self.request) | ||
|
||
self.assertEqual(user.is_anonymous, True) | ||
|
||
def test_get_user_by_token_with_invalid_authorization_token(self): | ||
""" | ||
GIVEN: A request with invalid Authorization header | ||
WHEN: get_user_by_token is called | ||
THEN: It should return AnonymousUser | ||
""" | ||
self.request.META["HTTP_AUTHORIZATION"] = "Bearer invalid-token" | ||
user = self.view._get_user_by_token(self.request) | ||
|
||
self.assertEqual(user.is_anonymous, True) | ||
|
||
def test_get_user_by_token_with_invalid_token(self): | ||
""" | ||
GIVEN: A request with invalid token | ||
WHEN: get_user_by_token is called | ||
THEN: It should return AnonymousUser | ||
""" | ||
self.request.META["HTTP_AUTHORIZATION"] = f"Token {_default_api_token()}" | ||
user = self.view._get_user_by_token(self.request) | ||
|
||
self.assertEqual(user.is_anonymous, True) | ||
|
||
def test_get_user_by_token_with_valid_token(self): | ||
""" | ||
GIVEN: A request with valid token | ||
WHEN: get_user_by_token is called | ||
THEN: It should return the user | ||
""" | ||
self.request.META["HTTP_AUTHORIZATION"] = f"Token {self.user.api_token}" | ||
user = self.view._get_user_by_token(self.request) | ||
|
||
self.assertEqual(user, self.user) | ||
|
||
def test_dispatch_with_unauthenticated_user(self): | ||
""" | ||
GIVEN: A request with unauthenticated user | ||
WHEN: dispatch is called | ||
THEN: It should return 403 | ||
""" | ||
self.request.META["HTTP_AUTHORIZATION"] = f"Token {_default_api_token()}" | ||
response = self.view.dispatch(self.request) | ||
|
||
self.assertEqual(response.status_code, HTTPStatus.FORBIDDEN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import json | ||
from http import HTTPStatus | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.test import TestCase, RequestFactory | ||
from django.urls import reverse | ||
|
||
from api.views import DashboardApiView | ||
from core.factories import UserFactory, ServiceFactory | ||
from core.models import Service | ||
|
||
User = get_user_model() | ||
|
||
|
||
class TestDashboardApiView(TestCase): | ||
def setUp(self) -> None: | ||
super().setUp() | ||
self.user: User = UserFactory() | ||
self.service_1: Service = ServiceFactory(owner=self.user) | ||
self.service_2: Service = ServiceFactory(owner=self.user) | ||
self.url = reverse("api:services") | ||
self.factory = RequestFactory() | ||
|
||
def test_get_with_unauthenticated_user(self): | ||
""" | ||
GIVEN: An unauthenticated user | ||
WHEN: The user makes a GET request to the dashboard API view | ||
THEN: It should return 403 | ||
""" | ||
response = self.client.get(self.url) | ||
self.assertEqual(response.status_code, HTTPStatus.FORBIDDEN) | ||
|
||
def test_get_returns_400(self): | ||
""" | ||
GIVEN: An authenticated user | ||
WHEN: The user makes a GET request to the dashboard API view with an invalid date format | ||
THEN: It should return 400 | ||
""" | ||
request = self.factory.get(self.url, {"startDate": "01/01/2000"}) | ||
request.META["HTTP_AUTHORIZATION"] = f"Token {self.user.api_token}" | ||
|
||
response = DashboardApiView.as_view()(request) | ||
self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST) | ||
|
||
data = json.loads(response.content) | ||
self.assertEqual(data["error"], "Invalid date format. Use YYYY-MM-DD.") | ||
|
||
def test_get_with_authenticated_user(self): | ||
""" | ||
GIVEN: An authenticated user | ||
WHEN: The user makes a GET request to the dashboard API view | ||
THEN: It should return 200 | ||
""" | ||
request = self.factory.get(self.url) | ||
request.META["HTTP_AUTHORIZATION"] = f"Token {self.user.api_token}" | ||
|
||
response = DashboardApiView.as_view()(request) | ||
self.assertEqual(response.status_code, HTTPStatus.OK) | ||
|
||
data = json.loads(response.content) | ||
self.assertEqual(len(data["services"]), 2) | ||
|
||
def test_get_with_service_uuid(self): | ||
""" | ||
GIVEN: An authenticated user | ||
WHEN: The user makes a GET request to the dashboard API view with a service UUID | ||
THEN: It should return 200 and a single service | ||
""" | ||
request = self.factory.get(self.url, {"uuid": str(self.service_1.uuid)}) | ||
request.META["HTTP_AUTHORIZATION"] = f"Token {self.user.api_token}" | ||
|
||
response = DashboardApiView.as_view()(request) | ||
self.assertEqual(response.status_code, HTTPStatus.OK) | ||
|
||
data = json.loads(response.content) | ||
self.assertEqual(len(data["services"]), 1) | ||
self.assertEqual(data["services"][0]["uuid"], str(self.service_1.uuid)) | ||
self.assertEqual(data["services"][0]["name"], str(self.service_1.name)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import uuid | ||
|
||
|
||
def is_valid_uuid(value: str) -> bool: | ||
"""Check if a string is a valid UUID.""" | ||
try: | ||
uuid.UUID(value) | ||
return True | ||
except ValueError: | ||
return False |