Skip to content

Commit

Permalink
Add fetching and updating CurrentUser (#48)
Browse files Browse the repository at this point in the history
* Specify required Python version for all packages
* Enable Ruff unsafe-fixes, ignore bool args rules
* Move GET /me response examples to separate file
* Add authed_current_user fixture
* Add initial exceptions
  • Loading branch information
nifadyev authored Jul 26, 2024
1 parent b0122fc commit d4d9aba
Show file tree
Hide file tree
Showing 11 changed files with 673 additions and 12 deletions.
203 changes: 202 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]


[project]
name = "toggl_python"
requires-python = ">=3.8"

[tool.poetry.dependencies]
python = "^3.8.18"
httpx = {extras = ["http2"], version = "^0.27.0"}
# Necessary for listing available timezones
# use zoneinfo.available_timezones()
backports-zoneinfo = {version = "^0.2.1", python = "3.8"}
pydantic = {extras = ["email"], version = "^2.8.2"}

[tool.poetry.group.dev.dependencies]
pytest = "^8.3.1"
Expand All @@ -39,6 +48,7 @@ build-backend = "poetry.core.masonry.api"
[tool.ruff]
src = ["toggl_python"]
line-length = 99
unsafe-fixes = true
extend-exclude = [
"docs/*",
".venv",
Expand All @@ -48,6 +58,7 @@ extend-exclude = [
[tool.ruff.lint]
select = ["ALL"]
ignore = [
"COM812", # Trailing comma missing
"D100", # Missing docstring in public module
"D101", # Missing docstring in public class
"D102", # Missing docstring in public method
Expand All @@ -56,10 +67,22 @@ ignore = [
"D105", # Missing docstring in magic method
"D106", # Missing docstring in public nested class
"D107", # Missing docstring in __init__
"UP006", # Use `list` instead of `List` for type annotation
"UP007", # Use `X | Y` for type annotations (not supported on current Python version - 3.8)
# Ignored because function args emulates query args which could be boolean
"FBT001", # Boolean-typed positional argument in function definition
"FBT002", # Boolean default positional argument in function definition
"N818", # Exception name should be named with Error suffix
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = ["S101"]
"tests/*" = [
"S101",
"S106", # Possible hardcoded password assigned to argument
]
# Move standard/third-party library import into a type-checking block
# Necessary for valid Pydantic schemas
"toggl_python/schemas/*" = ["TCH002", "TCH003"]

[tool.ruff.lint.isort]
lines-after-imports = 2
Expand Down
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@
from respx import MockRouter
from respx import mock as respx_mock
from toggl_python.api import ROOT_URL
from toggl_python.auth import TokenAuth
from toggl_python.entities.user import CurrentUser

from tests.responses.me_get import FAKE_TOKEN


@pytest.fixture()
def response_mock() -> Generator[MockRouter, None, None]:
with respx_mock(base_url=ROOT_URL) as mock_with_base_url:
yield mock_with_base_url


@pytest.fixture()
def authed_current_user() -> CurrentUser:
auth = TokenAuth(token=FAKE_TOKEN)

return CurrentUser(auth=auth)
Empty file added tests/responses/__init__.py
Empty file.
77 changes: 77 additions & 0 deletions tests/responses/me_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from __future__ import annotations

from typing import Dict, Final, List, Union


FLAT_RESPONSE_TYPE = Dict[str, Union[str, int, bool, None]]
NESTED_RESPONSE_TYPE = Dict[str, Union[str, int, bool, List, None]]

FAKE_TOKEN: str = "flstsapa42cdwiueii2tjg2t08f91kdr"

ME_RESPONSE_SHORT: Final[FLAT_RESPONSE_TYPE] = {
"at": "2024-07-24T09:42:55.391879Z",
"authorization_updated_at": "2024-07-02T16:13:32.109174Z",
"beginning_of_week": 1,
"country_id": None,
"created_at": "2024-05-16T12:01:04.834085Z",
"default_workspace_id": 43644207,
"email": "[email protected]",
"fullname": "Test User",
"has_password": True,
"id": 30809356,
"image_url": "https://assets.track.toggl.com/images/profile.png",
"openid_email": None,
"openid_enabled": False,
"timezone": "Europe/London",
"toggl_accounts_id": "uWGsHAeXZGhJvQ3XjdY63h",
"updated_at": "2024-05-16T12:01:24.447981Z",
}

ME_RESPONSE: NESTED_RESPONSE_TYPE = {
"api_token": FAKE_TOKEN,
"at": "2024-07-24T09:42:55.391879Z",
"authorization_updated_at": "2024-07-02T16:13:32.109174Z",
"beginning_of_week": 1,
"country_id": None,
"created_at": "2024-05-16T12:01:04.834085Z",
"default_workspace_id": 43644207,
"email": "[email protected]",
"fullname": "Test User",
"has_password": True,
"id": 30809356,
"image_url": "https://assets.track.toggl.com/images/profile.png",
"intercom_hash": "78hcsq59lsca33ivsd5iwy42yu3gdf0sctutuku5gvjfk1qbj71puu7r1z74dzdp",
"openid_email": None,
"openid_enabled": False,
"options": [],
"timezone": "Europe/London",
"toggl_accounts_id": "uWGsHAeXZGhJvQ3XjdY63h",
"updated_at": "2024-05-16T12:01:24.447981Z",
}

ME_RESPONSE_WITH_RELATED_DATA: NESTED_RESPONSE_TYPE = {
"api_token": FAKE_TOKEN,
"at": "2024-07-24T09:42:55.391879Z",
"authorization_updated_at": "2024-07-02T16:13:32.109174Z",
"beginning_of_week": 1,
"country_id": None,
"created_at": "2024-05-16T12:01:04.834085Z",
"default_workspace_id": 43644207,
"email": "[email protected]",
"fullname": "Test User",
"has_password": True,
"id": 30809356,
"image_url": "https://assets.track.toggl.com/images/profile.png",
"intercom_hash": "78hcsq59lsca33ivsd5iwy42yu3gdf0sctutuku5gvjfk1qbj71puu7r1z74dzdp",
"openid_email": None,
"openid_enabled": False,
"options": [],
"timezone": "Europe/London",
"toggl_accounts_id": "uWGsHAeXZGhJvQ3XjdY63h",
"updated_at": "2024-05-16T12:01:24.447981Z",
"clients": [],
"projects": [],
"time_entries": [],
"tags": [],
"workspaces": [],
}
27 changes: 27 additions & 0 deletions tests/responses/me_put.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Dict, List, Union

from tests.responses.me_get import FAKE_TOKEN


NESTED_RESPONSE_TYPE = Dict[str, Union[str, int, bool, List, None]]

UPDATE_ME_RESPONSE: NESTED_RESPONSE_TYPE = {
"api_token": FAKE_TOKEN,
"at": "2024-07-24T09:42:55.391879Z",
"beginning_of_week": 1,
"country_id": None,
"created_at": "2024-05-16T12:01:04.834085Z",
"default_workspace_id": 43644207,
"email": "[email protected]",
"fullname": "Test User",
"has_password": True,
"id": 30809356,
"image_url": "https://assets.track.toggl.com/images/profile.png",
"openid_email": None,
"openid_enabled": False,
"options": [],
"timezone": "Europe/London",
"toggl_accounts_id": "uWGsHAeXZGhJvQ3XjdY63h",
"toggl_accounts_updated_at": "2024-05-16T12:01:24.447981Z",
"updated_at": "2024-05-16T12:01:24.447981Z",
}
Loading

0 comments on commit d4d9aba

Please sign in to comment.