From 98b8acff20adc60bf784262f8eb2b00eb5ef3b9b Mon Sep 17 00:00:00 2001 From: Jose Nieves Date: Sat, 8 Jan 2022 12:51:56 +0100 Subject: [PATCH 1/3] #43-test: user test cases --- decide/administration/tests.py | 110 ++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 7 deletions(-) diff --git a/decide/administration/tests.py b/decide/administration/tests.py index c67779f15e..083d004f2f 100644 --- a/decide/administration/tests.py +++ b/decide/administration/tests.py @@ -10,15 +10,14 @@ base_url = "/administration/api" user_json_mock = { - "username": "admin", + "username": "mock", "password": "qwerty", - "email": "ad@admin.com", - "first_name": "admin", - "last_name": "admin", + "email": "mock@mock.com", + "first_name": "mock", + "last_name": "mock", } user_json_mock_updated = { - "id": 1, "username": "updated", "password": "updated", "email": "updated@admin.com", @@ -58,8 +57,18 @@ def create_voting(self): - response = self.client.post(base_url + "/voting/", + response = self.client.post(base_url + "/voting", voting_json_mock, format='json') + self.assertEqual(response.status_code, 200) + + return response + + +def create_user(self): + response = self.client.post(base_url + "/users", + user_json_mock, format='json') + self.assertEqual(response.status_code, 201) + return response @@ -68,7 +77,8 @@ def setUp(self): super().setUp() self.client = APIClient() - admin_mock = User(username="admin", is_superuser=True) + admin_mock = User(username="admin", + email="a@admin.com", is_superuser=True) admin_mock.set_password("qwerty") admin_mock.save() @@ -101,6 +111,92 @@ def test_get_dashboard_api(self): self.assertEqual(response.data['users'], { "active": 1, "admins": 1, "employees": 0, "total": 1}) + def test_create_user_api(self): + response = create_user(self) + db_user = User.objects.get(username="mock") + + self.assertTrue(db_user) + self.assertEqual(response.status_code, 201) + self.assertEqual(db_user.username, user_json_mock['username']) + self.assertEqual(db_user.email, user_json_mock['email']) + self.assertEqual(db_user.first_name, user_json_mock['first_name']) + self.assertEqual(db_user.last_name, user_json_mock['last_name']) + self.assertEqual(db_user.is_active, True) + self.assertEqual(db_user.is_superuser, False) + self.assertEqual(db_user.is_staff, False) + + def test_get_users_api(self): + url = base_url + '/users' + response = self.client.get(url, format="json") + user_count = User.objects.count() + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.data), user_count) + + def test_update_user_api(self): + create_user(self) + db_user_id = User.objects.get(username="mock").id + + url = base_url + '/users/' + str(db_user_id) + response = self.client.put(url, user_json_mock_updated, format="json") + self.assertEqual(response.status_code, 200) + + db_user = User.objects.get(username="updated") + self.assertEquals(db_user.id, db_user_id) + self.assertEqual(db_user.username, user_json_mock_updated['username']) + self.assertEqual(db_user.email, user_json_mock_updated['email']) + self.assertEqual(db_user.first_name, + user_json_mock_updated['first_name']) + self.assertEqual(db_user.last_name, + user_json_mock_updated['last_name']) + self.assertEqual(db_user.is_active, True) + self.assertEqual(db_user.is_superuser, False) + self.assertEqual(db_user.is_staff, False) + + def test_update_user_state_api(self): + create_user(self) + db_user_id = User.objects.get(username="mock").id + + data = { + "idList": [db_user_id], + "state": "Active", + "value": "False" + } + url = base_url + "/users/state" + response = self.client.post(url, data, format="json") + self.assertEqual(response.status_code, 200) + + data = { + "idList": [db_user_id], + "state": "Staff", + "value": "True" + } + response = self.client.post(url, data, format="json") + self.assertEqual(response.status_code, 200) + + data = { + "idList": [db_user_id], + "state": "Superuser", + "value": "True" + } + response = self.client.post(url, data, format="json") + self.assertEqual(response.status_code, 200) + + db_user = User.objects.get(username="mock") + self.assertTrue(db_user.is_superuser) + self.assertTrue(db_user.is_staff) + self.assertFalse(db_user.is_active) + + def test_delete_user_api(self): + create_user(self) + db_user_id = User.objects.get(username="mock").id + + url = base_url + '/users/' + str(db_user_id) + response = self.client.delete(url, format="json") + + self.assertEqual(response.status_code, 200) + self.assertEqual(User.objects.count(), 1) + ''' def test_post_voting_api(self): From a10e296fa4fcafb3952d6a931d0ca0cd822b86a5 Mon Sep 17 00:00:00 2001 From: Jose Nieves Date: Sat, 8 Jan 2022 13:14:33 +0100 Subject: [PATCH 2/3] #43-refactor: views.py imports --- decide/administration/views.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/decide/administration/views.py b/decide/administration/views.py index fa1c03632c..80937f6ea3 100644 --- a/decide/administration/views.py +++ b/decide/administration/views.py @@ -1,19 +1,24 @@ +from django.contrib.auth.models import User from django.utils import timezone from django.shortcuts import render, get_object_or_404 -from rest_framework.status import * + +from rest_framework.status import HTTP_200_OK, HTTP_400_BAD_REQUEST, HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_403_FORBIDDEN from rest_framework import parsers, renderers, status from rest_framework.authtoken.models import Token from rest_framework.authtoken.serializers import AuthTokenSerializer from rest_framework.response import Response from rest_framework.views import APIView -from base.models import Auth, Key -from authentication.serializers import UserSerializer -from administration.serializers import * -from base.serializers import AuthSerializer, KeySerializer -from voting.serializers import VotingSerializer -from .serializers import CensusSerializer + from base.perms import IsAdminAPI -from voting.models import Question + +from base.serializers import KeySerializer, AuthSerializer +from voting.serializers import VotingSerializer +from .serializers import AdminQuestionSerializer, AdminVotingGetSerializer, AdminVotingSerializer, CensusSerializer, UserAdminSerializer, UserSerializer, UserUpdateSerializer + +from base.models import Auth, Key +from voting.models import Question, Voting, QuestionOption +from census.models import Census + from utils.utils import is_valid From 7b4d08e45dd1ecbd42f797ae4f9ed54a8107f55d Mon Sep 17 00:00:00 2001 From: Jose Nieves Date: Sat, 8 Jan 2022 13:19:07 +0100 Subject: [PATCH 3/3] #43-test: bulk delete users --- decide/administration/tests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/decide/administration/tests.py b/decide/administration/tests.py index 29b331990b..7ad8d3bd00 100644 --- a/decide/administration/tests.py +++ b/decide/administration/tests.py @@ -103,6 +103,7 @@ def tearDown(self): self.assertEqual(self.token["expires"], "Thu, 01 Jan 1970 00:00:00 GMT") + #! DASHBOARD TESTS def test_get_dashboard_api(self): url = base_url + '/dashboard' response = self.client.get(url, format="json") @@ -113,6 +114,7 @@ def test_get_dashboard_api(self): self.assertEqual(response.data['users'], { "active": 1, "admins": 1, "employees": 0, "total": 1}) + #! USER TESTS def test_create_user_api(self): response = create_user(self) db_user = User.objects.get(username="mock") @@ -193,6 +195,7 @@ def test_update_user_state_api(self): def test_delete_user_api(self): create_user(self) + self.assertEqual(User.objects.count(), 2) db_user_id = User.objects.get(username="mock").id url = base_url + '/users/' + str(db_user_id) @@ -201,6 +204,21 @@ def test_delete_user_api(self): self.assertEqual(response.status_code, 204) self.assertEqual(User.objects.count(), 1) + def test_bulk_delete_users_api(self): + create_user(self) + self.assertEqual(User.objects.count(), 2) + db_user_id = User.objects.get(username="mock").id + + data = { + "idList": [db_user_id] + } + url = base_url + '/users' + response = self.client.delete(url, data, format="json") + + self.assertEqual(response.status_code, 204) + self.assertEqual(User.objects.count(), 1) + + #! VOTING TESTS def test_post_voting_api(self): response = create_voting(self) db_voting = Voting.objects.last()