Skip to content

Commit

Permalink
test: added tests for pat data, also fixed type hinting to be correct
Browse files Browse the repository at this point in the history
  • Loading branch information
pbullhove committed Sep 28, 2023
1 parent 9ee5a19 commit d1192b9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, List, Optional
from uuid import uuid4

from pydantic import UUID4, BaseModel
from pydantic import BaseModel

from config import config

Expand Down Expand Up @@ -100,7 +100,7 @@ def default_with_owner(cls, user: User):

class PATData(BaseModel):
pat_hash: str | None = None
uuid: UUID4 = str(uuid4())
uuid: str = str(uuid4())
user_id: str
# TODO: Roles should be checked on every request, as they mey be updated after the PAT has been created
roles: List[str] = []
Expand Down
47 changes: 47 additions & 0 deletions src/tests/unit/authentication/test_pat_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import unittest
from datetime import datetime
from uuid import UUID

from authentication.models import AccessLevel, PATData


class PatDataTestCase(unittest.TestCase):
def test_init(self):
expire = datetime.utcnow()

pat_data = PATData(
pat_hash="hash123",
user_id="user1",
roles=["role1", "role2"],
scope=AccessLevel.READ,
expire=expire,
)
self.assertEqual(pat_data.pat_hash, "hash123")
self.assertEqual(pat_data.user_id, "user1")
self.assertEqual(pat_data.roles, ["role1", "role2"])
self.assertEqual(pat_data.scope, AccessLevel.READ)
self.assertEqual(pat_data.expire, expire)
self.assertIsNotNone(pat_data.uuid)

def test_dict(self):
pat_data = PATData(
pat_hash="hash123",
user_id="user1",
roles=["role1", "role2"],
scope=AccessLevel.READ,
expire=datetime.utcnow(),
)
as_dict = pat_data.dict()
self.assertEqual(as_dict["_id"], "hash123")
self.assertEqual(as_dict["user_id"], "user1")
self.assertEqual(as_dict["roles"], ["role1", "role2"])
self.assertEqual(as_dict["scope"], AccessLevel.READ)

def test_uuid_default(self):
pat_data = PATData(user_id="user1", scope=AccessLevel.WRITE, expire=datetime.utcnow())

self.assertIsInstance(pat_data.uuid, str)
try:
UUID(pat_data.uuid, version=4) # see if we can cast it to uuid.
except ValueError:
self.fail(f"Was not able to cast {pat_data.uuid} to UUID, and therefore it is not a valid UUID string. ")

0 comments on commit d1192b9

Please sign in to comment.