|
1 | 1 | """Integration tests for user routes.""" |
2 | 2 |
|
| 3 | +import datetime |
| 4 | + |
| 5 | +import jwt |
3 | 6 | import pytest |
4 | 7 | from fastapi import status |
5 | 8 |
|
| 9 | +from app.config.settings import get_settings |
6 | 10 | from app.database.helpers import hash_password |
| 11 | +from app.managers.auth import AuthManager |
| 12 | +from app.models.user import User |
7 | 13 |
|
8 | 14 |
|
9 | 15 | @pytest.mark.integration |
@@ -64,3 +70,57 @@ async def test_routes_bad_auth(self, client, route) -> None: |
64 | 70 |
|
65 | 71 | assert response.status_code == status.HTTP_401_UNAUTHORIZED |
66 | 72 | assert response.json() == {"detail": "That token is Invalid"} |
| 73 | + |
| 74 | + @pytest.mark.asyncio |
| 75 | + @pytest.mark.parametrize( |
| 76 | + "route", |
| 77 | + test_routes, |
| 78 | + ) |
| 79 | + async def test_routes_refresh_token_rejected( |
| 80 | + self, client, test_db, route |
| 81 | + ) -> None: |
| 82 | + """Test that refresh tokens are rejected on protected routes.""" |
| 83 | + test_user = User(**self.test_user) |
| 84 | + test_db.add(test_user) |
| 85 | + await test_db.commit() |
| 86 | + refresh_token = AuthManager.encode_refresh_token(test_user) |
| 87 | + |
| 88 | + route_name, method = route |
| 89 | + fn = getattr(client, method) |
| 90 | + response = await fn( |
| 91 | + route_name, headers={"Authorization": f"Bearer {refresh_token}"} |
| 92 | + ) |
| 93 | + |
| 94 | + assert response.status_code == status.HTTP_401_UNAUTHORIZED |
| 95 | + assert response.json() == {"detail": "That token is Invalid"} |
| 96 | + |
| 97 | + @pytest.mark.asyncio |
| 98 | + @pytest.mark.parametrize( |
| 99 | + "route", |
| 100 | + test_routes, |
| 101 | + ) |
| 102 | + async def test_routes_missing_typ_rejected( |
| 103 | + self, client, test_db, route |
| 104 | + ) -> None: |
| 105 | + """Test that tokens without typ are rejected on protected routes.""" |
| 106 | + test_user = User(**self.test_user) |
| 107 | + test_db.add(test_user) |
| 108 | + await test_db.commit() |
| 109 | + token = jwt.encode( |
| 110 | + { |
| 111 | + "sub": test_user.id, |
| 112 | + "exp": datetime.datetime.now(tz=datetime.timezone.utc) |
| 113 | + + datetime.timedelta(minutes=10), |
| 114 | + }, |
| 115 | + get_settings().secret_key, |
| 116 | + algorithm="HS256", |
| 117 | + ) |
| 118 | + |
| 119 | + route_name, method = route |
| 120 | + fn = getattr(client, method) |
| 121 | + response = await fn( |
| 122 | + route_name, headers={"Authorization": f"Bearer {token}"} |
| 123 | + ) |
| 124 | + |
| 125 | + assert response.status_code == status.HTTP_401_UNAUTHORIZED |
| 126 | + assert response.json() == {"detail": "That token is Invalid"} |
0 commit comments