Skip to content

Commit

Permalink
correct test v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
romanmyko committed Oct 21, 2024
1 parent 2d27e9e commit 8426692
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
2 changes: 2 additions & 0 deletions BackEnd/administration/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
class AdminUserFactory(factory.django.DjangoModelFactory):
class Meta:
model = CustomUser
django_get_or_create = ("email",)

id = factory.Sequence(lambda n: n + 1)
email = factory.Sequence(lambda n: f"test{n + 1}@test.com")
name = factory.Sequence(lambda n: f"Test person {n + 1}")
surname = factory.Sequence(lambda n: f"Test person {n + 1} surname")
Expand Down
84 changes: 44 additions & 40 deletions BackEnd/administration/tests/test_admin_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ def test_get_user_id_not_staff(self):
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": 49,
"email": "test49@test.com",
"name": "Test person 49",
"surname": "Test person 49 surname",
"id": 3,
"email": "test3@test.com",
"name": "Test person 3",
"surname": "Test person 3 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -47,10 +48,10 @@ def test_get_users_ordering_desc(self):
"registration_date": None,
},
{
"id": 48,
"email": "test48@test.com",
"name": "Test person 48",
"surname": "Test person 48 surname",
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -69,10 +70,10 @@ def test_get_users_ordering_asc(self):
response = self.client.get(path="/api/admin/users/?ordering=id")
data = [
{
"id": 46,
"email": "test46@test.com",
"name": "Test person 46",
"surname": "Test person 46 surname",
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -83,10 +84,10 @@ def test_get_users_ordering_asc(self):
"registration_date": None,
},
{
"id": 47,
"email": "test47@test.com",
"name": "Test person 47",
"surname": "Test person 47 surname",
"id": 3,
"email": "test3@test.com",
"name": "Test person 3",
"surname": "Test person 3 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -102,13 +103,13 @@ def test_get_users_ordering_asc(self):

def test_get_users_filter_id_surname(self):
self.client.force_authenticate(self.user)
response = self.client.get(path="/api/admin/users/?id=44&surname=44")
response = self.client.get(path="/api/admin/users/?id=5&surname=5")
data = [
{
"id": 44,
"email": "test44@test.com",
"name": "Test person 44",
"surname": "Test person 44 surname",
"id": 5,
"email": "test5@test.com",
"name": "Test person 5",
"surname": "Test person 5 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -126,6 +127,7 @@ def test_get_users_filter_id_surname(self):
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):
Expand All @@ -135,10 +137,10 @@ def test_get_users_filter_status_active_staff(self):
)
data = [
{
"id": 50,
"email": "test50@test.com",
"name": "Test person 50",
"surname": "Test person 50 surname",
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -149,10 +151,10 @@ def test_get_users_filter_status_active_staff(self):
"registration_date": None,
},
{
"id": 51,
"email": "test51@test.com",
"name": "Test person 51",
"surname": "Test person 51 surname",
"id": 3,
"email": "test3@test.com",
"name": "Test person 3",
"surname": "Test person 3 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -179,6 +181,7 @@ def test_get_users_filter_status_superuser_staff_active(self):
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")
Expand All @@ -193,10 +196,10 @@ def test_get_users_authenticated(self):
response = self.client.get(path="/api/admin/users/")
data = [
{
"id": 39,
"email": "test39@test.com",
"name": "Test person 39",
"surname": "Test person 39 surname",
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -215,10 +218,10 @@ def test_get_users_filter(self):
response = self.client.get(path="/api/admin/users/?ordering=id")
data = [
{
"id": 40,
"email": "test40@test.com",
"name": "Test person 40",
"surname": "Test person 40 surname",
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
Expand All @@ -235,11 +238,12 @@ def test_get_users_filter(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,
Expand Down
4 changes: 3 additions & 1 deletion BackEnd/administration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ class UsersListView(ListAPIView):
permission_classes = [IsStaffUser]
pagination_class = ListPagination
serializer_class = AdminUserListSerializer
queryset = CustomUser.objects.all()
filter_backends = [DjangoFilterBackend]
filterset_class = UsersFilter

def get_queryset(self):
return CustomUser.objects.select_related("profile")


class UserDetailView(RetrieveUpdateDestroyAPIView):
"""
Expand Down

0 comments on commit 8426692

Please sign in to comment.