1
1
from fastapi .testclient import TestClient
2
2
from httpx import Response
3
3
from sqlmodel import Session
4
+ from unittest .mock import patch
4
5
5
6
from main import app
6
7
from utils .models import User
8
+ from utils .images import InvalidImageError
9
+
10
+ # Mock data for consistent testing
11
+ MOCK_IMAGE_DATA = b"processed fake image data"
12
+ MOCK_CONTENT_TYPE = "image/png"
7
13
8
14
9
15
def test_update_profile_unauthorized (unauth_client : TestClient ):
@@ -23,11 +29,12 @@ def test_update_profile_unauthorized(unauth_client: TestClient):
23
29
assert response .headers ["location" ] == app .url_path_for ("read_login" )
24
30
25
31
26
- def test_update_profile_authorized (auth_client : TestClient , test_user : User , session : Session ):
32
+ @patch ('routers.user.validate_and_process_image' )
33
+ def test_update_profile_authorized (mock_validate , auth_client : TestClient , test_user : User , session : Session ):
27
34
"""Test that authorized users can edit their profile"""
28
35
29
- # Create test image data
30
- test_image_data = b"fake image data"
36
+ # Configure mock to return processed image data
37
+ mock_validate . return_value = ( MOCK_IMAGE_DATA , MOCK_CONTENT_TYPE )
31
38
32
39
# Update profile
33
40
response : Response = auth_client .post (
@@ -37,7 +44,7 @@ def test_update_profile_authorized(auth_client: TestClient, test_user: User, ses
37
44
38
45
},
39
46
files = {
40
- "avatar_file" : ("test_avatar.jpg" , test_image_data , "image/jpeg" )
47
+ "avatar_file" : ("test_avatar.jpg" , b"fake image data" , "image/jpeg" )
41
48
},
42
49
follow_redirects = False
43
50
)
@@ -48,8 +55,11 @@ def test_update_profile_authorized(auth_client: TestClient, test_user: User, ses
48
55
session .refresh (test_user )
49
56
assert test_user .name == "Updated Name"
50
57
assert test_user .
email == "[email protected] "
51
- assert test_user .avatar_data == test_image_data
52
- assert test_user .avatar_content_type == "image/jpeg"
58
+ assert test_user .avatar_data == MOCK_IMAGE_DATA
59
+ assert test_user .avatar_content_type == MOCK_CONTENT_TYPE
60
+
61
+ # Verify mock was called correctly
62
+ mock_validate .assert_called_once ()
53
63
54
64
55
65
def test_update_profile_without_avatar (auth_client : TestClient , test_user : User , session : Session ):
@@ -110,18 +120,21 @@ def test_delete_account_success(auth_client: TestClient, test_user: User, sessio
110
120
assert user is None
111
121
112
122
113
- def test_get_avatar_authorized (auth_client : TestClient , test_user : User ):
123
+ @patch ('routers.user.validate_and_process_image' )
124
+ def test_get_avatar_authorized (mock_validate , auth_client : TestClient , test_user : User ):
114
125
"""Test getting user avatar"""
126
+ # Configure mock to return processed image data
127
+ mock_validate .return_value = (MOCK_IMAGE_DATA , MOCK_CONTENT_TYPE )
128
+
115
129
# First upload an avatar
116
- test_image_data = b"fake image data"
117
130
auth_client .post (
118
131
app .url_path_for ("update_profile" ),
119
132
data = {
120
133
"name" : test_user .name ,
121
134
"email" : test_user .email ,
122
135
},
123
136
files = {
124
- "avatar_file" : ("test_avatar.jpg" , test_image_data , "image/jpeg" )
137
+ "avatar_file" : ("test_avatar.jpg" , b"fake image data" , "image/jpeg" )
125
138
},
126
139
)
127
140
@@ -130,8 +143,8 @@ def test_get_avatar_authorized(auth_client: TestClient, test_user: User):
130
143
app .url_path_for ("get_avatar" )
131
144
)
132
145
assert response .status_code == 200
133
- assert response .content == test_image_data
134
- assert response .headers ["content-type" ] == "image/jpeg"
146
+ assert response .content == MOCK_IMAGE_DATA
147
+ assert response .headers ["content-type" ] == MOCK_CONTENT_TYPE
135
148
136
149
137
150
def test_get_avatar_unauthorized (unauth_client : TestClient ):
@@ -142,3 +155,24 @@ def test_get_avatar_unauthorized(unauth_client: TestClient):
142
155
)
143
156
assert response .status_code == 303
144
157
assert response .headers ["location" ] == app .url_path_for ("read_login" )
158
+
159
+
160
+ # Add new test for invalid image
161
+ @patch ('routers.user.validate_and_process_image' )
162
+ def test_update_profile_invalid_image (mock_validate , auth_client : TestClient ):
163
+ """Test that invalid images are rejected"""
164
+ # Configure mock to raise InvalidImageError
165
+ mock_validate .side_effect = InvalidImageError ("Invalid test image" )
166
+
167
+ response : Response = auth_client .post (
168
+ app .url_path_for ("update_profile" ),
169
+ data = {
170
+ "name" : "Updated Name" ,
171
+
172
+ },
173
+ files = {
174
+ "avatar_file" : ("test_avatar.jpg" , b"invalid image data" , "image/jpeg" )
175
+ },
176
+ )
177
+ assert response .status_code == 400
178
+ assert "Invalid test image" in response .text
0 commit comments