Skip to content

Commit ec546f3

Browse files
Add unit tests for token verification in security service
1 parent d88a01b commit ec546f3

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
from unittest.mock import patch
3+
import requests
4+
from fastapi import HTTPException
5+
from ..services.security import verify_token
6+
7+
8+
def test_verify_token_valid():
9+
token = "valid-token"
10+
user_data = {"user_id": 1, "username": "testuser"}
11+
12+
with patch("requests.get") as mock_get:
13+
mock_get.return_value.status_code = 200
14+
mock_get.return_value.json.return_value = user_data
15+
16+
result = verify_token(token)
17+
assert result == user_data
18+
19+
20+
def test_verify_token_invalid():
21+
token = "invalid-token"
22+
23+
with patch("requests.get") as mock_get:
24+
mock_get.return_value.status_code = 401
25+
26+
with pytest.raises(HTTPException) as exc_info:
27+
verify_token(token)
28+
assert exc_info.value.status_code == 401
29+
assert exc_info.value.detail == "Token inválido ou expirado"
30+
31+
32+
def test_verify_token_service_error():
33+
token = "any-token"
34+
35+
with patch("requests.get") as mock_get:
36+
mock_get.side_effect = requests.RequestException
37+
38+
with pytest.raises(HTTPException) as exc_info:
39+
verify_token(token)
40+
assert exc_info.value.status_code == 500
41+
assert exc_info.value.detail == "Erro ao conectar com o auth-service"

0 commit comments

Comments
 (0)