1
1
import uuid
2
2
from unittest .mock import patch
3
3
4
+ import pytest
4
5
from fastapi .testclient import TestClient
5
6
from sqlmodel import Session , select
6
7
7
8
from app import crud
8
9
from app .core .config import settings
9
10
from app .core .security import verify_password
10
11
from app .models import User , UserCreate
12
+ from app .tests .utils .user import create_random_user , user_authentication_headers
11
13
from app .tests .utils .utils import random_email , random_lower_string
12
14
13
15
@@ -56,7 +58,7 @@ def test_create_user_new_email(
56
58
assert user .email == created_user ["email" ]
57
59
58
60
59
- def test_get_existing_user (
61
+ def test_get_existing_user_as_superuser (
60
62
client : TestClient , superuser_token_headers : dict [str , str ], db : Session
61
63
) -> None :
62
64
username = random_email ()
@@ -75,21 +77,32 @@ def test_get_existing_user(
75
77
assert existing_user .email == api_user ["email" ]
76
78
77
79
78
- def test_get_existing_user_current_user (client : TestClient , db : Session ) -> None :
80
+ def test_get_non_existing_user_as_superuser (
81
+ client : TestClient , superuser_token_headers : dict [str , str ]
82
+ ):
83
+ r = client .get (
84
+ f"{ settings .API_V1_STR } /users/{ uuid .uuid4 ()} " ,
85
+ headers = superuser_token_headers ,
86
+ )
87
+ assert r .status_code == 404
88
+ assert r .json () == {"detail" : "User not found" }
89
+
90
+
91
+ @pytest .mark .parametrize (
92
+ "is_superuser" , (True , False ), ids = lambda x : "superuser" if x else "normal user"
93
+ )
94
+ def test_get_existing_user_current_user (
95
+ client : TestClient , db : Session , is_superuser : bool
96
+ ) -> None :
79
97
username = random_email ()
80
98
password = random_lower_string ()
81
- user_in = UserCreate (email = username , password = password )
99
+ user_in = UserCreate (email = username , password = password , is_superuser = is_superuser )
82
100
user = crud .create_user (session = db , user_create = user_in )
83
101
user_id = user .id
84
102
85
- login_data = {
86
- "username" : username ,
87
- "password" : password ,
88
- }
89
- r = client .post (f"{ settings .API_V1_STR } /login/access-token" , data = login_data )
90
- tokens = r .json ()
91
- a_token = tokens ["access_token" ]
92
- headers = {"Authorization" : f"Bearer { a_token } " }
103
+ headers = user_authentication_headers (
104
+ client = client , email = username , password = password
105
+ )
93
106
94
107
r = client .get (
95
108
f"{ settings .API_V1_STR } /users/{ user_id } " ,
@@ -102,11 +115,22 @@ def test_get_existing_user_current_user(client: TestClient, db: Session) -> None
102
115
assert existing_user .email == api_user ["email" ]
103
116
104
117
118
+ @pytest .mark .parametrize (
119
+ "exists" , (True , False ), ids = lambda x : "Existing user" if x else "No user"
120
+ )
105
121
def test_get_existing_user_permissions_error (
106
- client : TestClient , normal_user_token_headers : dict [str , str ]
122
+ db : Session ,
123
+ client : TestClient ,
124
+ normal_user_token_headers : dict [str , str ],
125
+ exists : bool ,
107
126
) -> None :
127
+ if exists :
128
+ user = create_random_user (db )
129
+ user_id = user .id
130
+ else :
131
+ user_id = uuid .uuid4 ()
108
132
r = client .get (
109
- f"{ settings .API_V1_STR } /users/{ uuid . uuid4 () } " ,
133
+ f"{ settings .API_V1_STR } /users/{ user_id } " ,
110
134
headers = normal_user_token_headers ,
111
135
)
112
136
assert r .status_code == 403
0 commit comments