|
9 | 9 | ClassifiedProductFactory, |
10 | 10 | CountryFactory, |
11 | 11 | CurrencyFactory, |
| 12 | + HeaProfileFactory, |
12 | 13 | UnitOfMeasureFactory, |
13 | 14 | UserFactory, |
14 | 15 | ) |
@@ -190,3 +191,59 @@ def test_search_fields(self): |
190 | 191 | self.assertEqual(response.status_code, 200) |
191 | 192 | result = json.loads(response.content.decode("utf-8")) |
192 | 193 | self.assertEqual(len(result), 1) |
| 194 | + |
| 195 | + |
| 196 | +class UserViewSetTestCase(APITestCase): |
| 197 | + def setUp(self): |
| 198 | + self.user = UserFactory(username="testuser", password="password123", first_name="Test", last_name="User") |
| 199 | + self.client.force_authenticate(user=self.user) |
| 200 | + self.url = reverse("user-list") |
| 201 | + |
| 202 | + def test_get_current_user(self): |
| 203 | + response = self.client.get(f"{self.url}current/") |
| 204 | + self.assertEqual(response.status_code, 200) |
| 205 | + self.assertEqual(response.data["username"], self.user.username) |
| 206 | + |
| 207 | + def test_search_users(self): |
| 208 | + UserFactory(username="searchuser", password="password123", first_name="Search", last_name="User") |
| 209 | + response = self.client.get(self.url, {"search": "Search"}) |
| 210 | + self.assertEqual(response.status_code, 200) |
| 211 | + self.assertEqual(len(response.data), 1) |
| 212 | + self.assertEqual(response.data[0]["first_name"], "Search") |
| 213 | + |
| 214 | + |
| 215 | +class HeaProfileViewSetTestCase(APITestCase): |
| 216 | + def setUp(self): |
| 217 | + self.user = UserFactory(username="testuser", password="password123") |
| 218 | + self.profile = HeaProfileFactory(user=self.user) |
| 219 | + self.client.force_authenticate(user=self.user) |
| 220 | + self.url = reverse("heaprofile-list") |
| 221 | + |
| 222 | + def test_get_current_profile(self): |
| 223 | + response = self.client.get(f"{self.url}current/") |
| 224 | + self.assertEqual(response.status_code, 200) |
| 225 | + self.assertEqual(response.data["user"], self.user.id) |
| 226 | + |
| 227 | + def test_superuser_access_profiles(self): |
| 228 | + superuser = UserFactory(username="admin", password="password123", is_superuser=True) |
| 229 | + self.client.force_authenticate(user=superuser) |
| 230 | + response = self.client.get(f"{self.url}{self.profile.user.id}/") |
| 231 | + self.assertEqual(response.status_code, 200) |
| 232 | + self.assertEqual(response.data["user"], self.user.id) |
| 233 | + |
| 234 | + def test_queryset_filters(self): |
| 235 | + other_user = UserFactory(username="otheruser", password="password123") |
| 236 | + HeaProfileFactory(user=other_user) |
| 237 | + |
| 238 | + # Current user profile only |
| 239 | + response = self.client.get(f"{self.url}?pk=current") |
| 240 | + self.assertEqual(response.status_code, 200) |
| 241 | + self.assertEqual(len(response.data), 1) |
| 242 | + self.assertEqual(response.data[0]["user"], self.user.id) |
| 243 | + |
| 244 | + # Superuser access to all profiles |
| 245 | + superuser = UserFactory(username="admin", password="password123", is_superuser=True) |
| 246 | + self.client.force_authenticate(user=superuser) |
| 247 | + response = self.client.get(self.url) |
| 248 | + self.assertEqual(response.status_code, 200) |
| 249 | + self.assertGreaterEqual(len(response.data), 2) |
0 commit comments