Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for auth endpoints #15

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ensysmod/api/endpoints/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def register(
return user


@router.post("/test-token", response_model=schemas.User)
@router.get("/test-token", response_model=schemas.User)
def test_token(
current_user: model.User = Depends(deps.get_current_user)
) -> schemas.User:
Expand Down
1 change: 1 addition & 0 deletions ensysmod/model/energy_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ class EnergySource(Base):
id = Column(Integer, primary_key=True, index=True)
name = Column(String, unique=True, index=True, nullable=False)
description = Column(String, nullable=True)
# Wirkungsgrad in %
51 changes: 51 additions & 0 deletions tests/api/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,54 @@ def test_register_twice_endpoint(client: TestClient):

r2 = client.post("/auth/register", json=payload)
assert r2.status_code == status.HTTP_400_BAD_REQUEST


def test_login_endpoint(client: TestClient, db: Session):
payload = get_register_payload()
r = client.post("/auth/register", json=payload)
assert r.status_code == status.HTTP_200_OK

r2 = client.post("/auth/login", data=payload, headers={"content-type": "application/x-www-form-urlencoded"})
assert r2.status_code == status.HTTP_200_OK
assert r2.json()['access_token']
assert r2.json()['token_type'] == 'bearer'


def test_login_unknown_user_endpoint(client: TestClient, db: Session):
payload = get_register_payload()
r = client.post("/auth/login", data=payload, headers={"content-type": "application/x-www-form-urlencoded"})
assert r.status_code == status.HTTP_401_UNAUTHORIZED


def test_test_token_endpoint(client: TestClient):
payload = get_register_payload()
r = client.post("/auth/register", json=payload)
assert r.status_code == status.HTTP_200_OK

r2 = client.post("/auth/login", data=payload, headers={"content-type": "application/x-www-form-urlencoded"})
assert r2.status_code == status.HTTP_200_OK

r3 = client.get("/auth/test-token", headers={"Authorization": f"Bearer {r2.json()['access_token']}"})
user = r3.json()
assert r3.status_code == status.HTTP_200_OK
assert user['username'] == payload['username']


def test_test_token_unknown_access_token_endpoint(client: TestClient):
r = client.get("/auth/test-token", headers={"Authorization": "Bearer unknown"})
assert r.status_code == status.HTTP_403_FORBIDDEN


def test_test_token_user_deleted(client: TestClient, db: Session):
payload = get_register_payload()
r = client.post("/auth/register", json=payload)
assert r.status_code == status.HTTP_200_OK

r2 = client.post("/auth/login", data=payload, headers={"content-type": "application/x-www-form-urlencoded"})
assert r2.status_code == status.HTTP_200_OK

user_id = crud.user.get_by_username(db=db, username=payload['username']).id
crud.user.remove(db=db, id=user_id)

r3 = client.get("/auth/test-token", headers={"Authorization": f"Bearer {r2.json()['access_token']}"})
assert r3.status_code == status.HTTP_401_UNAUTHORIZED