This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: file structure documentation (#62)
- Loading branch information
Showing
36 changed files
with
290 additions
and
194 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 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
File renamed without changes.
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,11 @@ | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
from ultron8.api.models.rwmodel import convert_datetime_to_realworld | ||
|
||
|
||
@pytest.mark.unittest | ||
def test_api_datetime_is_in_realworld_format() -> None: | ||
dt = datetime.fromisoformat("2019-10-27T02:21:42.844640") | ||
assert convert_datetime_to_realworld(dt) == "2019-10-27T02:21:42.844640Z" |
File renamed without changes.
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,62 @@ | ||
from datetime import timedelta | ||
|
||
import jwt | ||
import pytest | ||
|
||
# from app.models.domain.users import UserInDB | ||
from ultron8.api.models.user import UserInDB | ||
from ultron8.api.services.jwt import ( | ||
ALGORITHM, | ||
create_access_token_for_user, | ||
create_jwt_token, | ||
get_email_from_token, | ||
) | ||
|
||
|
||
@pytest.mark.unittest | ||
@pytest.mark.jwtonly | ||
def test_creating_jwt_token() -> None: | ||
token = create_jwt_token( | ||
jwt_content={"content": "payload"}, | ||
secret_key="secret", | ||
expires_delta=timedelta(minutes=1), | ||
) | ||
parsed_payload = jwt.decode(token, "secret", algorithms=[ALGORITHM]) | ||
|
||
assert parsed_payload["content"] == "payload" | ||
|
||
|
||
@pytest.mark.unittest | ||
@pytest.mark.jwtonly | ||
def test_creating_token_for_user(test_user: UserInDB) -> None: | ||
token = create_access_token_for_user(user=test_user, secret_key="secret") | ||
parsed_payload = jwt.decode(token, "secret", algorithms=[ALGORITHM]) | ||
|
||
assert parsed_payload["email"] == test_user.email | ||
|
||
|
||
@pytest.mark.unittest | ||
@pytest.mark.jwtonly | ||
def test_retrieving_token_from_user(test_user: UserInDB) -> None: | ||
token = create_access_token_for_user(user=test_user, secret_key="secret") | ||
email = get_email_from_token(token, "secret") | ||
assert email == test_user.email | ||
|
||
|
||
@pytest.mark.unittest | ||
@pytest.mark.jwtonly | ||
def test_error_when_wrong_token() -> None: | ||
with pytest.raises(ValueError): | ||
get_email_from_token("asdf", "asdf") | ||
|
||
|
||
@pytest.mark.unittest | ||
@pytest.mark.jwtonly | ||
def test_error_when_wrong_token_shape() -> None: | ||
token = create_jwt_token( | ||
jwt_content={"content": "payload"}, | ||
secret_key="secret", | ||
expires_delta=timedelta(minutes=1), | ||
) | ||
with pytest.raises(ValueError): | ||
get_email_from_token(token, "secret") |
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
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,16 @@ | ||
# pylint: disable=E0611 | ||
# NOTE: fixes [pylint] No name 'BaseModel' in module 'pydantic' | ||
# SOURCE: https://github.com/nokia-wroclaw/innovativeproject-sudoku/issues/39 | ||
|
||
from datetime import datetime | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class JWTMeta(BaseModel): | ||
exp: datetime | ||
sub: str | ||
|
||
|
||
class JWTUser(BaseModel): | ||
email: str |
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,28 @@ | ||
# pylint: disable=E0611 | ||
# NOTE: fixes [pylint] No name 'BaseModel' in module 'pydantic' | ||
# SOURCE: https://github.com/nokia-wroclaw/innovativeproject-sudoku/issues/39 | ||
|
||
import datetime | ||
|
||
from pydantic import BaseConfig, BaseModel | ||
|
||
|
||
def convert_datetime_to_realworld(dt: datetime.datetime) -> str: | ||
return dt.replace(tzinfo=datetime.timezone.utc).isoformat().replace("+00:00", "Z") | ||
|
||
|
||
def convert_field_to_camel_case(string: str) -> str: | ||
return "".join( | ||
word if index == 0 else word.capitalize() | ||
for index, word in enumerate(string.split("_")) | ||
) | ||
|
||
|
||
class RWModel(BaseModel): | ||
class Config(BaseConfig): | ||
# whether an aliased field may be populated by its name as given by the model attribute, as well as the alias (default: False) | ||
allow_population_by_field_name = True | ||
# a dict used to customise the way types are encoded to JSON; see https://pydantic-docs.helpmanual.io/usage/exporting_models/#modeljson | ||
json_encoders = {datetime.datetime: convert_datetime_to_realworld} | ||
# If data source field names do not match your code style (e. g. CamelCase fields), you can automatically generate aliases using alias_generator | ||
alias_generator = convert_field_to_camel_case |
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,11 @@ | ||
# from app.models.domain.rwmodel import RWModel | ||
from ultron8.api.models.rwmodel import RWModel | ||
|
||
|
||
class RWSchema(RWModel): | ||
class Config(RWModel.Config): | ||
# NOTE: https://pydantic-docs.helpmanual.io/usage/model_config/ | ||
# whether to allow usage of ORM mode | ||
# https://pydantic-docs.helpmanual.io/usage/models/#orm-mode-aka-arbitrary-class-instances | ||
# Pydantic models can be created from arbitrary class instances to support models that map to ORM objects. | ||
orm_mode = True |
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.
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
Empty file.
Oops, something went wrong.