Unit test api

* adds unit test for APIUserToken
* enhances handling of token fetching for API
This commit is contained in:
2023-08-17 10:44:58 +02:00
parent 1726eb38ad
commit b854695399
4 changed files with 87 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ class APIUserToken(models.Model):
valid_until = models.DateField(
blank=True,
null=True,
help_text="Token is only valid until this date",
help_text="Token is only valid until this date. Forever if null/blank.",
)
is_active = models.BooleanField(
default=False,
@@ -25,12 +25,11 @@ class APIUserToken(models.Model):
return self.token
@staticmethod
def get_user_from_token(token: str, username: str):
def get_user_from_token(token: str):
""" Getter for the related user object
Args:
token (str): The used token
username (str): The username
Returns:
user (User): Otherwise None
@@ -39,7 +38,6 @@ class APIUserToken(models.Model):
try:
token_obj = APIUserToken.objects.get(
token=token,
user__username=username
)
if not token_obj.is_active:
raise PermissionError("Token unverified")