Skip to content

Commit

Permalink
Merge pull request #15 from NOWUM/feature/test-auth
Browse files Browse the repository at this point in the history
Added tests for auth endpoints
  • Loading branch information
einfachMel authored Dec 2, 2021
2 parents 7f5fb9e + b2557dd commit f9a530c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
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

0 comments on commit f9a530c

Please sign in to comment.