Skip to content

Commit

Permalink
GH-5: Add User representation class
Browse files Browse the repository at this point in the history
  • Loading branch information
markhobson committed Oct 26, 2023
1 parent a0cde22 commit 594c59d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
12 changes: 10 additions & 2 deletions schemes/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def get_all(self) -> List[User]:
@api_key_auth
@inject.autoparams()
def add(users: UserRepository) -> Response:
json = request.get_json()
users.add(*[User(element["email"]) for element in json])
users_repr = [UserRepr(**element) for element in request.get_json()]
users.add(*[user_repr.to_domain() for user_repr in users_repr])
return Response(status=201)


Expand All @@ -80,3 +80,11 @@ def add(users: UserRepository) -> Response:
def clear(users: UserRepository) -> Response:
users.clear()
return Response(status=204)


@dataclass
class UserRepr:
email: str

def to_domain(self) -> User:
return User(email=self.email)
14 changes: 11 additions & 3 deletions tests/e2e/app_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass

import requests


Expand All @@ -8,12 +12,11 @@ def __init__(self, url: str, api_key: str):
self._url = url
self._api_key = api_key

def add_user(self, email: str) -> None:
users = [{"email": email}]
def add_user(self, user: UserRepr) -> None:
response = requests.post(
f"{self._url}/users",
headers={"Authorization": f"API-Key {self._api_key}"},
json=users,
json=[user.__dict__],
timeout=self.DEFAULT_TIMEOUT,
)
assert response.status_code == 201
Expand All @@ -23,3 +26,8 @@ def clear_users(self) -> None:
f"{self._url}/users", headers={"Authorization": f"API-Key {self._api_key}"}, timeout=self.DEFAULT_TIMEOUT
)
assert response.status_code == 204


@dataclass
class UserRepr:
email: str
6 changes: 3 additions & 3 deletions tests/e2e/test_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask import Flask
from playwright.sync_api import Page

from tests.e2e.app_client import AppClient
from tests.e2e.app_client import AppClient, UserRepr
from tests.e2e.oidc_server.users import StubUser
from tests.e2e.oidc_server.web_client import OidcClient
from tests.e2e.pages import SchemesPage
Expand All @@ -15,7 +15,7 @@ def oidc_user(self, oidc_client: OidcClient) -> None:
oidc_client.add_user(StubUser("boardman", "boardman@example.com"))

def test_schemes_when_authorized(self, app_client: AppClient, app: Flask, page: Page) -> None:
app_client.add_user("boardman@example.com")
app_client.add_user(UserRepr(email="boardman@example.com"))

schemes_page = SchemesPage(app, page).open()

Expand All @@ -27,7 +27,7 @@ def test_schemes_when_unauthorized(self, app: Flask, page: Page) -> None:
assert unauthorized_page.visible()

def test_header_sign_out(self, app_client: AppClient, app: Flask, page: Page) -> None:
app_client.add_user("boardman@example.com")
app_client.add_user(UserRepr(email="boardman@example.com"))
schemes_page = SchemesPage(app, page).open()

start_page = schemes_page.header.sign_out()
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask import Flask
from playwright.sync_api import Page

from tests.e2e.app_client import AppClient
from tests.e2e.app_client import AppClient, UserRepr
from tests.e2e.oidc_server.users import StubUser
from tests.e2e.oidc_server.web_client import OidcClient
from tests.e2e.pages import StartPage
Expand All @@ -28,7 +28,7 @@ def test_start_shows_login(self, app: Flask, page: Page) -> None:
class TestAuthenticated:
def test_start_shows_schemes(self, oidc_client: OidcClient, app_client: AppClient, app: Flask, page: Page) -> None:
oidc_client.add_user(StubUser("boardman", "boardman@example.com"))
app_client.add_user("boardman@example.com")
app_client.add_user(UserRepr(email="boardman@example.com"))
start_page = StartPage(app, page).open()
start_page.start()

Expand Down

0 comments on commit 594c59d

Please sign in to comment.