Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /FabianRutten_fastapi-permissions-alive /tests /test_example_app.py
| import pytest | |
| def get_with_user(url, username, client): | |
| response = client.post( | |
| "/token", data={"username": username, "password": "secret"} | |
| ) | |
| data = response.json() | |
| headers = {"Authorization": "Bearer " + data["access_token"]} | |
| return client.get(url, headers=headers) | |
| def test_app_get_token_valid_credentials(username, client): | |
| """ test the example login with valid credentials """ | |
| response = client.post( | |
| "/token", data={"username": username, "password": "secret"} | |
| ) | |
| print(response.text) | |
| assert response.status_code == 200 | |
| def test_app_get_token_bad_credentials(username, password, client): | |
| """ test the example login with invalid credentials """ | |
| response = client.post( | |
| "/token", data={"username": username, "password": password} | |
| ) | |
| assert response.status_code >= 400 | |
| def test_app_get_me(username, client): | |
| """ test if a logged in user can access a restricted url """ | |
| response = get_with_user("/me/", username, client) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["username"] == username | |
| def test_app_permissions(url, username, granted, client): | |
| """ test urls protected by principals, permissions and acls """ | |
| response = get_with_user(url, username, client) | |
| assert response.status_code == 200 if granted else 403 | |
| # the following tests are only here to get to a high coverage rate | |
| async def test_app_no_token_subject(): | |
| """ raise an error if no subject is specified in login token """ | |
| from datetime import timedelta | |
| from fastapi import HTTPException | |
| from fastapi_permissions_alive.example import ( | |
| get_current_user, | |
| create_access_token, | |
| ) | |
| token = create_access_token(data={}, expires_delta=timedelta(minutes=5)) | |
| with pytest.raises(HTTPException): | |
| await get_current_user(token) | |
| async def test_app_token_with_fake_user(): | |
| """ raise an error if an invalid subject is specified in login token """ | |
| from datetime import timedelta | |
| from fastapi import HTTPException | |
| from fastapi_permissions_alive.example import ( | |
| get_current_user, | |
| create_access_token, | |
| ) | |
| token = create_access_token( | |
| data={"sub": "unknown"}, expires_delta=timedelta(minutes=5) | |
| ) | |
| with pytest.raises(HTTPException): | |
| await get_current_user(token) | |
| async def test_app_modified_token(): | |
| """ raise an error if login token was modified """ | |
| from datetime import timedelta | |
| from fastapi import HTTPException | |
| from fastapi_permissions_alive.example import ( | |
| get_current_user, | |
| create_access_token, | |
| ) | |
| token = create_access_token(data={}, expires_delta=timedelta(minutes=5)) | |
| with pytest.raises(HTTPException): | |
| await get_current_user(token[:-1]) | |
| async def test_app_add_items_would_return_correct_value(): | |
| """ add_items will return the correct value if someone had permission """ | |
| from fastapi_permissions_alive.example import add_items | |
| result = await add_items([]) | |
| assert result == [{"items": "I can haz cheese?"}] | |
| def test_get_active_principals_for_not_logged_in_user(): | |
| """ return the correct principals for a non logged in user """ | |
| from fastapi_permissions_alive.example import Everyone, get_active_principals | |
| result = get_active_principals(None) | |
| assert result == [Everyone] | |
Xet Storage Details
- Size:
- 4.34 kB
- Xet hash:
- 6cd10a12fcac3c902d439c1b3bce798efe0a5280eee2ffa9584ee61039a0b6cd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.