Skip to content

Commit

Permalink
tests(test_views_token): add test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Camalon <[email protected]>
  • Loading branch information
thbcmlowk committed Aug 29, 2024
1 parent ad4ef44 commit 716c064
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions backend/api/tests/views/test_views_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@

import pytest
from django.contrib.auth.models import User
from django.db.utils import IntegrityError
from django.utils import timezone
from rest_framework import status

from users.models.token import BearerToken


@pytest.mark.django_db
def test_cannot_create_non_expiring_token(authenticated_client):
authenticated_client.create_user()
user = authenticated_client.user
# create a token without expiration date
with pytest.raises(IntegrityError):
BearerToken.objects.create(user=user)


@pytest.mark.django_db
def test_delete_token(authenticated_client):
authenticated_client.create_user()
user = authenticated_client.user
token = BearerToken.objects.create(user=user)
token = BearerToken.objects.create(user=user, expires_at=timezone.now() + timedelta(days=1))

tokens_count = BearerToken.objects.count()
assert tokens_count == 1
Expand Down Expand Up @@ -83,36 +93,12 @@ def test_expired_token(authenticated_client, api_client):
assert tokens_count == 1


@pytest.mark.django_db
def test_token_instant_expires(authenticated_client, api_client):
authenticated_client.create_user()
user = authenticated_client.user
# create a token that expired a day ago
token = BearerToken.objects.create(user=user)

tokens_count = BearerToken.objects.count()
assert tokens_count == 1

valid_auth_token_header = f"Token {token}"
api_client.credentials(HTTP_AUTHORIZATION=valid_auth_token_header)

response = api_client.get("/active-api-tokens/")
assert response.status_code == status.HTTP_401_UNAUTHORIZED

url = f"/active-api-tokens/?id={token.id}"
response = api_client.delete(url)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

tokens_count = BearerToken.objects.count()
assert tokens_count == 1


@pytest.mark.django_db
def test_delete_token_other_user(authenticated_client):
other_user = User.objects.create(username="user-2")
other_user.set_password("p@sswr0d44")
other_user.save()
token = BearerToken.objects.create(user=other_user)
token = BearerToken.objects.create(user=other_user, expires_at=timezone.now() + timedelta(days=1))

tokens_count = BearerToken.objects.count()
assert tokens_count == 1
Expand All @@ -135,3 +121,17 @@ def test_token_creation_post(authenticated_client):

tokens_count = BearerToken.objects.count()
assert tokens_count == 1


@pytest.mark.django_db
def test_cannot_post_token_wo_expires_at(authenticated_client):
authenticated_client.create_user()
payload = {}
url = "/api-token/"
response = authenticated_client.post(url, payload)

assert response.json() == {"expires_at": ["This field is required."]}
assert response.status_code == status.HTTP_400_BAD_REQUEST

tokens_count = BearerToken.objects.count()
assert tokens_count == 0

0 comments on commit 716c064

Please sign in to comment.