Skip to content

Commit

Permalink
feat: add token support (#24)
Browse files Browse the repository at this point in the history
* Add token related fields to melding model

* Add abstract token generator

* Let melding create action generate token

* Set token expiry moment

* Pass token generator mock and token duration to action in test
  • Loading branch information
4c0n authored Apr 10, 2024
1 parent 81ed0ce commit 5d2ac6a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
11 changes: 11 additions & 0 deletions meldingen_core/actions/melding.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABCMeta, abstractmethod
from datetime import datetime, timedelta
from typing import Generic, TypeVar, override

from meldingen_core.actions.base import BaseCreateAction, BaseListAction, BaseRetrieveAction
Expand All @@ -7,6 +8,7 @@
from meldingen_core.models import Melding
from meldingen_core.repositories import BaseMeldingRepository, BaseRepository
from meldingen_core.statemachine import BaseMeldingStateMachine, MeldingTransitions
from meldingen_core.token import BaseTokenGenerator

T = TypeVar("T", bound=Melding)
T_co = TypeVar("T_co", covariant=True, bound=Melding)
Expand All @@ -17,21 +19,30 @@ class MeldingCreateAction(BaseCreateAction[T, T_co]):

_classify: Classifier
_state_machine: BaseMeldingStateMachine[T]
_generate_token: BaseTokenGenerator
_token_duration: timedelta

def __init__(
self,
repository: BaseRepository[T, T_co],
classifier: Classifier,
state_machine: BaseMeldingStateMachine[T],
token_generator: BaseTokenGenerator,
token_duration: timedelta,
):
super().__init__(repository)
self._classify = classifier
self._state_machine = state_machine
self._generate_token = token_generator
self._token_duration = token_duration

@override
async def __call__(self, obj: T) -> None:
await super().__call__(obj)

obj.token = await self._generate_token()
obj.token_expires = datetime.now() + self._token_duration

classification = await self._classify(obj.text)
obj.classification = classification

Expand Down
3 changes: 3 additions & 0 deletions meldingen_core/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from datetime import datetime


@dataclass
Expand All @@ -14,6 +15,8 @@ class Melding:

text: str
classification: Classification | None = None
token: str | None = None
token_expires: datetime | None = None


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion meldingen_core/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections.abc import Collection
from typing import Generic, TypeVar

from meldingen_core.models import Answer, Classification, Melding, Question, User, Form
from meldingen_core.models import Answer, Classification, Form, Melding, Question, User

T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
Expand Down
7 changes: 7 additions & 0 deletions meldingen_core/token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from abc import ABCMeta, abstractmethod


class BaseTokenGenerator(metaclass=ABCMeta):
@abstractmethod
async def __call__(self) -> str:
"""Generates and returns token"""
6 changes: 5 additions & 1 deletion tests/test_actions/test_melding_actions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
from unittest.mock import AsyncMock, Mock

import pytest
Expand All @@ -14,6 +15,7 @@
from meldingen_core.models import Classification, Melding
from meldingen_core.repositories import BaseMeldingRepository
from meldingen_core.statemachine import BaseMeldingStateMachine, MeldingTransitions
from meldingen_core.token import BaseTokenGenerator


@pytest.mark.asyncio
Expand All @@ -22,7 +24,9 @@ async def test_melding_create_action() -> None:
classifier = AsyncMock(Classifier, return_value=classification)
state_machine = Mock(BaseMeldingStateMachine)
repository = Mock(BaseMeldingRepository)
action: MeldingCreateAction[Melding, Melding] = MeldingCreateAction(repository, classifier, state_machine)
action: MeldingCreateAction[Melding, Melding] = MeldingCreateAction(
repository, classifier, state_machine, AsyncMock(BaseTokenGenerator), timedelta(days=3)
)
melding = Melding("text")

await action(melding)
Expand Down

0 comments on commit 5d2ac6a

Please sign in to comment.