-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
188 lines (150 loc) · 5.98 KB
/
tests.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import unittest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from main import app, get_db
from database import Base
import schemas
import security
# Test Database Configuration
SQLALCHEMY_DATABASE_URL = "sqlite:///./test_database.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
class UserManagementTestCase(unittest.TestCase):
def setUp(self):
# Create tables
Base.metadata.create_all(bind=engine)
# Create test client
self.client = TestClient(app)
# Prepare test data
self.test_user = {
"username": "testuser",
"email": "testuser@example.com",
"password": "strongpassword123",
"full_name": "Test User",
}
def tearDown(self):
# Drop all tables after each test
Base.metadata.drop_all(bind=engine)
def test_register_user_success(self):
# Test successful user registration
response = self.client.post("/register", json=self.test_user)
self.assertEqual(response.status_code, 200)
user_data = response.json()
self.assertEqual(user_data["username"], self.test_user["username"])
self.assertEqual(user_data["email"], self.test_user["email"])
self.assertEqual(user_data["full_name"], self.test_user["full_name"])
self.assertEqual(user_data["address"], self.test_user["address"])
def test_register_duplicate_user(self):
# Register first user
self.client.post("/register", json=self.test_user)
# Try registering same user again
response = self.client.post("/register", json=self.test_user)
self.assertEqual(response.status_code, 400)
self.assertIn("already registered", response.json()["detail"])
def test_login_success(self):
# First register a user
self.client.post("/register", json=self.test_user)
# Then attempt login
login_data = {
"username": self.test_user["username"],
"password": self.test_user["password"],
}
response = self.client.post("/login", json=login_data)
self.assertEqual(response.status_code, 200)
self.assertIn("access_token", response.json())
self.assertEqual(response.json()["token_type"], "bearer")
def test_login_invalid_credentials(self):
# Register a user first
self.client.post("/register", json=self.test_user)
# Try login with wrong password
login_data = {
"username": self.test_user["username"],
"password": "wrongpassword",
}
response = self.client.post("/login", json=login_data)
self.assertEqual(response.status_code, 401)
self.assertIn("Incorrect username", response.json()["detail"])
def test_update_user_profile(self):
# Register and login to get token
self.client.post("/register", json=self.test_user)
login_response = self.client.post(
"/login",
json={
"username": self.test_user["username"],
"password": self.test_user["password"],
},
)
access_token = login_response.json()["access_token"]
# Update profile
update_data = {
"full_name": "Updated Test User",
"email": "updated_testuser@example.com",
}
response = self.client.put(
"/update",
json=update_data,
headers={"Authorization": f"Bearer {access_token}"},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["full_name"], "Updated Test User")
self.assertEqual(response.json()["email"], "updated_testuser@example.com")
def test_update_user_unauthorized(self):
# Try to update without authentication
update_data = {"full_name": "Updated Test User"}
response = self.client.put("/update", json=update_data)
self.assertEqual(response.status_code, 401)
def test_password_hashing(self):
# Verify that passwords are hashed and not stored in plain text
password = "test_password"
hashed_password = security.get_password_hash(password)
self.assertNotEqual(password, hashed_password)
self.assertTrue(security.verify_password(password, hashed_password))
def test_update_duplicate_email(self):
# Register two users
first_user = {
"username": "firstuser",
"email": "first@example.com",
"password": "password123",
}
second_user = {
"username": "seconduser",
"email": "second@example.com",
"password": "password456",
}
# Register both users
self.client.post("/register", json=first_user)
self.client.post("/register", json=second_user)
# Login as first user
first_login = self.client.post(
"/login",
json={
"username": first_user["username"],
"password": first_user["password"],
},
)
first_token = first_login.json()["access_token"]
# Try to update first user's email to second user's email
update_data = {"email": second_user["email"]}
response = self.client.put(
"/update",
json=update_data,
headers={"Authorization": f"Bearer {first_token}"},
)
self.assertEqual(response.status_code, 400)
self.assertIn("Email already in use", response.json()["detail"])
# Optional: Add this if you want to run tests directly
if __name__ == "__main__":
unittest.main()