generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #840 from ita-social-projects/#822ViewUsersList
#822 view users list
- Loading branch information
Showing
5 changed files
with
286 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django_filters import filters | ||
from django_filters.rest_framework import FilterSet | ||
|
||
|
||
class UsersFilter(FilterSet): | ||
""" | ||
Filters | ||
/?id= , /?surname=, /?email= , /?is_active= , /?is_staff=, | ||
/?is_superuser=, /?is_deleted=True or False, /?company_name=, /?registration_date=, | ||
Ordering sample | ||
/?ordering=id asc or /?ordering=-id desc | ||
""" | ||
|
||
id = filters.CharFilter(lookup_expr="icontains") | ||
surname = filters.CharFilter(lookup_expr="icontains") | ||
email = filters.CharFilter(lookup_expr="icontains") | ||
is_active = filters.CharFilter(lookup_expr="icontains") | ||
is_staff = filters.CharFilter(lookup_expr="icontains") | ||
is_superuser = filters.CharFilter(lookup_expr="icontains") | ||
is_deleted = filters.BooleanFilter(method="filter_is_deleted") | ||
company_name = filters.CharFilter( | ||
field_name="profile__name", lookup_expr="icontains" | ||
) | ||
registration_date = filters.CharFilter( | ||
field_name="profile__created_at", lookup_expr="icontains" | ||
) | ||
|
||
def filter_is_deleted(self, queryset, name, value): | ||
if value: | ||
queryset = queryset.filter(email__startswith="is_deleted_") | ||
return queryset | ||
|
||
ordering = filters.OrderingFilter( | ||
fields=( | ||
("id", "id"), | ||
("name", "name"), | ||
("surname", "surname"), | ||
("email", "email"), | ||
("is_active", "is_active"), | ||
("is_staff", "is_staff"), | ||
("is_superuser", "is_superuser"), | ||
("is_deleted", "is_deleted"), | ||
("profile__name", "company_name"), | ||
("profile__created_at", "registration_date"), | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,165 @@ def test_get_user_id_not_staff(self): | |
self.assertEqual(status.HTTP_403_FORBIDDEN, response.status_code) | ||
|
||
|
||
class TestAdminUsersOrderingFilterAPITests(APITestCase): | ||
def setUp(self): | ||
self.users = AdminUserFactory.create_batch(2) | ||
AdminUserFactory.reset_sequence(1) | ||
self.user = self.users[0] | ||
|
||
def test_get_users_ordering_desc(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get(path="/api/admin/users/?ordering=-id") | ||
data = [ | ||
{ | ||
"id": 3, | ||
"email": "[email protected]", | ||
"name": "Test person 3", | ||
"surname": "Test person 3 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
}, | ||
{ | ||
"id": 2, | ||
"email": "[email protected]", | ||
"name": "Test person 2", | ||
"surname": "Test person 2 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
}, | ||
] | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(data, response.json()) | ||
|
||
def test_get_users_ordering_asc(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get(path="/api/admin/users/?ordering=id") | ||
data = [ | ||
{ | ||
"id": 2, | ||
"email": "[email protected]", | ||
"name": "Test person 2", | ||
"surname": "Test person 2 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
}, | ||
{ | ||
"id": 3, | ||
"email": "[email protected]", | ||
"name": "Test person 3", | ||
"surname": "Test person 3 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
}, | ||
] | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(data, response.json()) | ||
|
||
def test_get_users_filter_id_surname(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get(path="/api/admin/users/?id=5&surname=5") | ||
data = [ | ||
{ | ||
"id": 5, | ||
"email": "[email protected]", | ||
"name": "Test person 5", | ||
"surname": "Test person 5 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
}, | ||
] | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(data, response.json()) | ||
|
||
|
||
class TestAdminUsersStatusAPITests(APITestCase): | ||
def setUp(self): | ||
self.users = AdminUserFactory.create_batch(2) | ||
AdminUserFactory.reset_sequence(1) | ||
self.user = self.users[0] | ||
|
||
def test_get_users_filter_status_active_staff(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get( | ||
path="/api/admin/users/?is_active=True&is_staff=True" | ||
) | ||
data = [ | ||
{ | ||
"id": 2, | ||
"email": "[email protected]", | ||
"name": "Test person 2", | ||
"surname": "Test person 2 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
}, | ||
{ | ||
"id": 3, | ||
"email": "[email protected]", | ||
"name": "Test person 3", | ||
"surname": "Test person 3 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
}, | ||
] | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(data, response.json()) | ||
|
||
def test_get_users_filter_status_superuser_staff_active(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get( | ||
path="/api/admin/users/?is_superuser=True&is_deleted=True&is_active=False" | ||
) | ||
data = [] | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(data, response.json()) | ||
|
||
|
||
class TestAdminUsersAPITests(APITestCase): | ||
def setUp(self): | ||
self.user = AdminUserFactory() | ||
AdminUserFactory.reset_sequence(1) | ||
|
||
def test_get_users_not_authorized(self): | ||
response = self.client.get(path="/api/admin/users/?page=1&page_size=1") | ||
|
@@ -40,10 +196,40 @@ def test_get_users_authenticated(self): | |
response = self.client.get(path="/api/admin/users/") | ||
data = [ | ||
{ | ||
"id": 39, | ||
"email": "[email protected]", | ||
"name": "Test person 39", | ||
"surname": "Test person 39 surname", | ||
"id": 2, | ||
"email": "[email protected]", | ||
"name": "Test person 2", | ||
"surname": "Test person 2 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
} | ||
] | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(data, response.json()) | ||
|
||
def test_get_users_filter(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get(path="/api/admin/users/?ordering=id") | ||
data = [ | ||
{ | ||
"id": 2, | ||
"email": "[email protected]", | ||
"name": "Test person 2", | ||
"surname": "Test person 2 surname", | ||
"status": { | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
"is_deleted": False, | ||
}, | ||
"company_name": None, | ||
"registration_date": None, | ||
} | ||
] | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
@@ -52,11 +238,12 @@ def test_get_users_authenticated(self): | |
def test_get_user_id_authenticated(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get(path=f"/api/admin/users/{self.user.id}/") | ||
AdminUserFactory.reset_sequence(1) | ||
|
||
data = { | ||
"name": "Test person 37", | ||
"surname": "Test person 37 surname", | ||
"email": "test37@test.com", | ||
"name": f"Test person {self.user.id}", | ||
"surname": f"Test person {self.user.id} surname", | ||
"email": f"test{self.user.id}@test.com", | ||
"is_active": True, | ||
"is_staff": True, | ||
"is_superuser": False, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters