-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.py
145 lines (115 loc) · 4.51 KB
/
main_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import json
from datetime import datetime
from typing import List
from unittest.mock import MagicMock, patch
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import parse_obj_as, BaseModel
from requests import Response
from models.user import User as DBUser
from schemas.auth import Token
from schemas.user import User, UserWithPassword
from utils.security_utils.route_dep import get_current_user
EXAMPLE_ACCESS_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2V4YW1wbGUuYXV0aDAuY29tLyIsImF1ZCI6Imh0dHBzOi8vYXBpLmV4YW1wbGUuY29tL2NhbGFuZGFyL3YxLyIsInN1YiI6InVzcl8xMjMiLCJpYXQiOjE0NTg3ODU3OTYsImV4cCI6MTQ1ODg3MjE5Nn0.CA7eaHjIHz5NxeIJoFK9krqaeZrPLwmMmgI_XiQiIkQ"
def create_test_db_user(*, is_active=True, roles=None):
return DBUser(
user_id=1,
username="admin",
password="password",
roles=roles,
is_active=is_active,
created_datetime=datetime(year=2020, month=1, day=1),
)
def get_current_user_ovveride(**kwargs):
return lambda: create_test_db_user(**kwargs)
def test_read_main(test_client: TestClient):
response: Response = test_client.get("/")
assert response.status_code == 200
assert response.json() == "Hello World!"
class TestLoginController:
@patch("main.create_access_token")
@patch("main.user_service.authenticate")
def test_successful_login(
self, user_service_auth_stub, create_access_token_stub, test_client: TestClient
):
# Set up Stubs
user_service_auth_stub.return_value = create_test_db_user()
create_access_token_stub.return_value = EXAMPLE_ACCESS_TOKEN
# Make Call to Controller\
response: Response = test_client.post(
"/login", {"username": "admin", "password": "password"}
)
# Assertions
assert response.status_code == 200
assert (
response.json()
== Token(access_token=EXAMPLE_ACCESS_TOKEN, token_type="bearer").dict()
)
@patch("main.user_service.authenticate")
def test_user_not_found_for_login(
self, user_service_auth_stub, test_client: TestClient
):
# Set up Stubs
user_service_auth_stub.return_value = None
# Make Call to Controller
response: Response = test_client.post(
"/login", {"username": "admin", "password": "password"}
)
# Assertions
assert response.status_code == 400
assert response.json() == {"detail": "Incorrect email or password"}
@patch("main.user_service.authenticate")
def test_user_is_inactive_for_login(
self, user_service_auth_stub, test_client: TestClient
):
# Set up Stubs
user_service_auth_stub.return_value = create_test_db_user(is_active=False)
# Make Call to Controller
response: Response = test_client.post(
"/login", {"username": "admin", "password": "password"}
)
# Assertions
assert response.status_code == 400
assert response.json() == {"detail": "Inactive user"}
@patch("main.user_service.get_all")
class TestUserController:
def test_get_all_users_with_passwords(
self,
user_service_get_all_stub: MagicMock,
test_client: TestClient,
test_app: FastAPI,
):
# Set up Stubs
test_app.dependency_overrides[get_current_user] = get_current_user_ovveride(
roles="admin"
)
list_of_users = [
create_test_db_user(),
create_test_db_user(),
]
class UsersWithPasswordList(BaseModel):
__root__: List[UserWithPassword]
user_service_get_all_stub.return_value = list_of_users
# Make Call to Controller
response: Response = test_client.get("/users-with-passwords")
# Assertions
assert response.status_code == 200
assert (
str(json.dumps(response.json()))
== UsersWithPasswordList.parse_obj(list_of_users).json()
)
def test_get_all_users_admin_only(
self,
user_service_get_all_stub: MagicMock,
test_client: TestClient,
test_app: FastAPI,
):
# Set up Stubs
test_app.dependency_overrides[get_current_user] = get_current_user_ovveride(
roles=None
)
# Make Call to Controller
response: Response = test_client.get("/users-with-passwords")
# Assertions
assert response.status_code == 400
assert response.json() == {"detail": "The user doesn't have enough privilege"}