|
| 1 | +from unittest.mock import patch |
| 2 | + |
1 | 3 | from fastapi.testclient import TestClient
|
2 |
| -from pytest_mock import MockerFixture |
3 | 4 | from sqlmodel import Session
|
4 | 5 |
|
5 | 6 | from app import crud
|
@@ -31,27 +32,24 @@ def test_get_users_normal_user_me(
|
31 | 32 |
|
32 | 33 |
|
33 | 34 | def test_create_user_new_email(
|
34 |
| - client: TestClient, |
35 |
| - superuser_token_headers: dict[str, str], |
36 |
| - db: Session, |
37 |
| - mocker: MockerFixture, |
| 35 | + client: TestClient, superuser_token_headers: dict[str, str], db: Session |
38 | 36 | ) -> None:
|
39 |
| - mocker.patch("app.utils.send_email") |
40 |
| - mocker.patch("app.core.config.settings.SMTP_HOST", "smtp.example.com") |
41 |
| - mocker.patch( "app.core.config.settings.SMTP_USER", "[email protected]") |
42 |
| - username = random_email() |
43 |
| - password = random_lower_string() |
44 |
| - data = {"email": username, "password": password} |
45 |
| - r = client.post( |
46 |
| - f"{settings.API_V1_STR}/users/", |
47 |
| - headers=superuser_token_headers, |
48 |
| - json=data, |
49 |
| - ) |
50 |
| - assert 200 <= r.status_code < 300 |
51 |
| - created_user = r.json() |
52 |
| - user = crud.get_user_by_email(session=db, email=username) |
53 |
| - assert user |
54 |
| - assert user.email == created_user["email"] |
| 37 | + with patch("app.utils.send_email", return_value=None), patch( |
| 38 | + "app.core.config.settings.SMTP_HOST", "smtp.example.com" |
| 39 | + ), patch( "app.core.config.settings.SMTP_USER", "[email protected]") : |
| 40 | + username = random_email() |
| 41 | + password = random_lower_string() |
| 42 | + data = {"email": username, "password": password} |
| 43 | + r = client.post( |
| 44 | + f"{settings.API_V1_STR}/users/", |
| 45 | + headers=superuser_token_headers, |
| 46 | + json=data, |
| 47 | + ) |
| 48 | + assert 200 <= r.status_code < 300 |
| 49 | + created_user = r.json() |
| 50 | + user = crud.get_user_by_email(session=db, email=username) |
| 51 | + assert user |
| 52 | + assert user.email == created_user["email"] |
55 | 53 |
|
56 | 54 |
|
57 | 55 | def test_get_existing_user(
|
@@ -265,55 +263,56 @@ def test_update_password_me_same_password_error(
|
265 | 263 | )
|
266 | 264 |
|
267 | 265 |
|
268 |
| -def test_create_user_open(client: TestClient, mocker: MockerFixture) -> None: |
269 |
| - mocker.patch("app.core.config.settings.USERS_OPEN_REGISTRATION", True) |
270 |
| - username = random_email() |
271 |
| - password = random_lower_string() |
272 |
| - full_name = random_lower_string() |
273 |
| - data = {"email": username, "password": password, "full_name": full_name} |
274 |
| - r = client.post( |
275 |
| - f"{settings.API_V1_STR}/users/open", |
276 |
| - json=data, |
277 |
| - ) |
278 |
| - assert r.status_code == 200 |
279 |
| - created_user = r.json() |
280 |
| - assert created_user["email"] == username |
281 |
| - assert created_user["full_name"] == full_name |
282 |
| - |
283 |
| - |
284 |
| -def test_create_user_open_forbidden_error( |
285 |
| - client: TestClient, mocker: MockerFixture |
286 |
| -) -> None: |
287 |
| - mocker.patch("app.core.config.settings.USERS_OPEN_REGISTRATION", False) |
288 |
| - username = random_email() |
289 |
| - password = random_lower_string() |
290 |
| - full_name = random_lower_string() |
291 |
| - data = {"email": username, "password": password, "full_name": full_name} |
292 |
| - r = client.post( |
293 |
| - f"{settings.API_V1_STR}/users/open", |
294 |
| - json=data, |
295 |
| - ) |
296 |
| - assert r.status_code == 403 |
297 |
| - assert r.json()["detail"] == "Open user registration is forbidden on this server" |
298 |
| - |
299 |
| - |
300 |
| -def test_create_user_open_already_exists_error( |
301 |
| - client: TestClient, mocker: MockerFixture |
302 |
| -) -> None: |
303 |
| - mocker.patch("app.core.config.settings.USERS_OPEN_REGISTRATION", True) |
304 |
| - password = random_lower_string() |
305 |
| - full_name = random_lower_string() |
306 |
| - data = { |
307 |
| - "email": settings.FIRST_SUPERUSER, |
308 |
| - "password": password, |
309 |
| - "full_name": full_name, |
310 |
| - } |
311 |
| - r = client.post( |
312 |
| - f"{settings.API_V1_STR}/users/open", |
313 |
| - json=data, |
314 |
| - ) |
315 |
| - assert r.status_code == 400 |
316 |
| - assert r.json()["detail"] == "The user with this email already exists in the system" |
| 266 | +def test_create_user_open(client: TestClient) -> None: |
| 267 | + with patch("app.core.config.settings.USERS_OPEN_REGISTRATION", True): |
| 268 | + username = random_email() |
| 269 | + password = random_lower_string() |
| 270 | + full_name = random_lower_string() |
| 271 | + data = {"email": username, "password": password, "full_name": full_name} |
| 272 | + r = client.post( |
| 273 | + f"{settings.API_V1_STR}/users/open", |
| 274 | + json=data, |
| 275 | + ) |
| 276 | + assert r.status_code == 200 |
| 277 | + created_user = r.json() |
| 278 | + assert created_user["email"] == username |
| 279 | + assert created_user["full_name"] == full_name |
| 280 | + |
| 281 | + |
| 282 | +def test_create_user_open_forbidden_error(client: TestClient) -> None: |
| 283 | + with patch("app.core.config.settings.USERS_OPEN_REGISTRATION", False): |
| 284 | + username = random_email() |
| 285 | + password = random_lower_string() |
| 286 | + full_name = random_lower_string() |
| 287 | + data = {"email": username, "password": password, "full_name": full_name} |
| 288 | + r = client.post( |
| 289 | + f"{settings.API_V1_STR}/users/open", |
| 290 | + json=data, |
| 291 | + ) |
| 292 | + assert r.status_code == 403 |
| 293 | + assert ( |
| 294 | + r.json()["detail"] == "Open user registration is forbidden on this server" |
| 295 | + ) |
| 296 | + |
| 297 | + |
| 298 | +def test_create_user_open_already_exists_error(client: TestClient) -> None: |
| 299 | + with patch("app.core.config.settings.USERS_OPEN_REGISTRATION", True): |
| 300 | + password = random_lower_string() |
| 301 | + full_name = random_lower_string() |
| 302 | + data = { |
| 303 | + "email": settings.FIRST_SUPERUSER, |
| 304 | + "password": password, |
| 305 | + "full_name": full_name, |
| 306 | + } |
| 307 | + r = client.post( |
| 308 | + f"{settings.API_V1_STR}/users/open", |
| 309 | + json=data, |
| 310 | + ) |
| 311 | + assert r.status_code == 400 |
| 312 | + assert ( |
| 313 | + r.json()["detail"] |
| 314 | + == "The user with this email already exists in the system" |
| 315 | + ) |
317 | 316 |
|
318 | 317 |
|
319 | 318 | def test_update_user(
|
|
0 commit comments