Skip to content

Commit 81cd583

Browse files
refactor: reorganize imports and clean up whitespace in auth_utils tests
1 parent 68c5251 commit 81cd583

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

src/tests/backend/api/auth/auth_utils_test.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import json
55
from unittest.mock import MagicMock, patch
66

7-
import pytest
8-
from fastapi import HTTPException
9-
107
from backend.api.auth.auth_utils import (
118
UserDetails,
12-
get_tenant_id,
139
get_authenticated_user,
10+
get_tenant_id,
1411
)
1512

13+
from fastapi import HTTPException
14+
15+
import pytest
16+
1617

1718
class TestUserDetails:
1819
"""Tests for UserDetails class."""
@@ -25,9 +26,9 @@ def test_user_details_initialization(self):
2526
"auth_provider": "azure",
2627
"auth_token": "test-token",
2728
}
28-
29+
2930
user = UserDetails(user_data)
30-
31+
3132
assert user.user_principal_id == "test-user-id"
3233
assert user.user_name == "Test User"
3334
assert user.auth_provider == "azure"
@@ -40,14 +41,14 @@ def test_user_details_with_client_principal(self):
4041
client_principal = base64.b64encode(
4142
json.dumps({"tid": tenant_id}).encode()
4243
).decode()
43-
44+
4445
user_data = {
4546
"user_principal_id": "test-user-id",
4647
"client_principal_b64": client_principal,
4748
}
48-
49+
4950
user = UserDetails(user_data)
50-
51+
5152
assert user.user_principal_id == "test-user-id"
5253
assert user.tenant_id == tenant_id
5354

@@ -57,17 +58,17 @@ def test_user_details_with_placeholder_principal(self):
5758
"user_principal_id": "test-user-id",
5859
"client_principal_b64": "your_base_64_encoded_token",
5960
}
60-
61+
6162
user = UserDetails(user_data)
62-
63+
6364
assert user.tenant_id is None
6465

6566
def test_user_details_missing_fields(self):
6667
"""Test UserDetails with missing fields."""
6768
user_data = {}
68-
69+
6970
user = UserDetails(user_data)
70-
71+
7172
assert user.user_principal_id is None
7273
assert user.user_name is None
7374
assert user.auth_provider is None
@@ -80,31 +81,31 @@ def test_get_tenant_id_success(self):
8081
"""Test successful tenant ID extraction."""
8182
tenant_id = "my-tenant-id"
8283
encoded = base64.b64encode(json.dumps({"tid": tenant_id}).encode()).decode()
83-
84+
8485
result = get_tenant_id(encoded)
85-
86+
8687
assert result == tenant_id
8788

8889
def test_get_tenant_id_missing_tid(self):
8990
"""Test extraction when tid is missing."""
9091
encoded = base64.b64encode(json.dumps({"other": "data"}).encode()).decode()
91-
92+
9293
result = get_tenant_id(encoded)
93-
94+
9495
assert result == ""
9596

9697
def test_get_tenant_id_invalid_base64(self):
9798
"""Test extraction with invalid base64."""
9899
result = get_tenant_id("not-valid-base64!!!")
99-
100+
100101
assert result == ""
101102

102103
def test_get_tenant_id_invalid_json(self):
103104
"""Test extraction with invalid JSON."""
104105
encoded = base64.b64encode(b"not valid json").decode()
105-
106+
106107
result = get_tenant_id(encoded)
107-
108+
108109
assert result == ""
109110

110111

@@ -118,19 +119,19 @@ def test_get_authenticated_user_from_headers(self):
118119
"x-ms-client-principal-id": "test-user-id",
119120
"x-ms-client-principal-name": "Test User",
120121
}
121-
122+
122123
user = get_authenticated_user(request)
123-
124+
124125
assert user.user_principal_id == "test-user-id"
125126

126127
def test_get_authenticated_user_development_mode(self):
127128
"""Test getting user in development mode (no headers)."""
128129
request = MagicMock()
129130
request.headers = {}
130-
131+
131132
with patch("backend.api.auth.auth_utils.sample_user", {"x-ms-client-principal-id": "dev-user"}):
132133
user = get_authenticated_user(request)
133-
134+
134135
assert user.user_principal_id == "dev-user"
135136

136137
def test_get_authenticated_user_no_principal_id(self):
@@ -139,21 +140,21 @@ def test_get_authenticated_user_no_principal_id(self):
139140
request.headers = {
140141
"x-ms-client-principal-id": None,
141142
}
142-
143+
143144
# When sample_user also doesn't have ID
144145
with patch("backend.api.auth.auth_utils.sample_user", {}):
145146
with pytest.raises(HTTPException) as exc_info:
146147
get_authenticated_user(request)
147148
assert exc_info.value.status_code == 401
148149

149150
def test_get_authenticated_user_mixed_case_headers(self):
150-
"""Test getting user with mixed case headers - falls back to sample_user since header check is case-sensitive."""
151+
"""Test mixed case headers - falls back to sample_user."""
151152
request = MagicMock()
152153
# The function checks for lowercase header, so uppercase won't match
153154
request.headers = {
154155
"X-MS-CLIENT-PRINCIPAL-ID": "test-user-id",
155156
}
156-
157+
157158
# Since x-ms-client-principal-id (lowercase) is not in headers, it uses sample_user
158159
with patch("backend.api.auth.auth_utils.sample_user", {"x-ms-client-principal-id": "sample-user-id"}):
159160
user = get_authenticated_user(request)

0 commit comments

Comments
 (0)