Skip to content

Commit

Permalink
Merge branch 'feature/EGCETSII#43-backend-test' of github.com:Full-To…
Browse files Browse the repository at this point in the history
…rtuga/decide-full-tortuga-admin into feature/EGCETSII#43-backend-test
  • Loading branch information
pedalopon committed Jan 8, 2022
2 parents 707815a + 7b4d08e commit 5420b7e
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 43 deletions.
193 changes: 158 additions & 35 deletions decide/administration/tests.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
from django.contrib.auth.models import User

from base.models import Auth
from voting.models import Voting, QuestionOption
from census.models import Census

from .serializers import AdminVotingGetSerializer
from voting.models import Voting, QuestionOption

from rest_framework.test import APITestCase, APIClient

# Create your tests here.

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": "[email protected]",
Expand Down Expand Up @@ -75,24 +76,37 @@
}


class AdministrationTestCase(APITestCase):
def create_voting(self):
url = base_url + "/votings"
response = self.client.post(url,
voting_json_mock, format='json')
return response
def create_voting(self):
response = self.client.post(base_url + "/votings",
voting_json_mock, format='json')
self.assertEqual(response.status_code, 201)

def create_auth(self, data=auth_json_mock):
url = base_url + "/base/auth"
response = self.client.post(url,
data, format='json')
return response
return response


def create_auth(self, data=auth_json_mock):
response = self.client.post(base_url + "/base/auth",
data, format='json')
self.assertEqual(response.status_code, 201)

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


class AdministrationTestCase(APITestCase):
def setUp(self):
super().setUp()
self.client = APIClient()

admin_mock = User(username="admin", is_superuser=True)
admin_mock = User(username="admin",
email="[email protected]", is_superuser=True)
admin_mock.set_password("qwerty")
admin_mock.save()

Expand All @@ -115,6 +129,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")
Expand All @@ -125,8 +140,113 @@ 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")

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):
create_user(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)
self.assertEqual(User.objects.count(), 2)
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, 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 = self.create_voting()
response = create_voting(self)
db_voting = Voting.objects.last()

self.assertEqual(response.status_code, 201)
Expand All @@ -135,9 +255,11 @@ def test_post_voting_api(self):
self.assertEqual(db_voting.desc, voting_json_mock.get("desc"))
self.assertEqual(db_voting.question.desc,
voting_json_mock.get("question").get("desc"))
options = QuestionOption.objects.all().filter(question__pk=db_voting.question.id)
options = QuestionOption.objects.all().filter(
question__pk=db_voting.question.id)
self.assertEqual(options.count(), 3)
self.assertEqual(db_voting.auths.all().first().url, voting_json_mock.get("auth"))
self.assertEqual(db_voting.auths.all().first().url,
voting_json_mock.get("auth"))
censuss = Census.objects.filter(voting_id=db_voting.id)
self.assertEqual([census.voter_id for census in censuss], [1])

Expand All @@ -147,7 +269,7 @@ def test_post_voting_api(self):
self.assertEqual(response.status_code, 400)

def test_get_voting_api(self):
self.create_voting()
create_voting(self)

url = base_url + "/votings"
response = self.client.get(url, format="json")
Expand All @@ -158,7 +280,7 @@ def test_get_voting_api(self):
self.assertEqual(response.data[0]['desc'], "Test description")

def test_update_voting_api(self):
self.create_voting()
create_voting(self)
db_voting = Voting.objects.last()
url = base_url + "/votings/" + str(db_voting.id) + "/"
data = voting_json_mock_updated
Expand All @@ -170,7 +292,8 @@ def test_update_voting_api(self):
self.assertEqual(db_voting.desc, data.get("desc"))
self.assertEqual(db_voting.question.desc,
data.get("question").get("desc"))
options = QuestionOption.objects.all().filter(question__pk=db_voting.question.id)
options = QuestionOption.objects.all().filter(
question__pk=db_voting.question.id)
self.assertEqual(options.count(), 2)
self.assertEqual(db_voting.auths.all().first().url, data.get("auth"))
censuss = Census.objects.filter(voting_id=db_voting.id)
Expand All @@ -181,16 +304,16 @@ def test_update_voting_api(self):
self.assertEqual(response.status_code, 400)

def test_delete_voting_api(self):
self.create_voting()
create_voting(self)
db_voting = Voting.objects.last()
url = base_url + "/votings/" + str(db_voting.id) + "/"
response = self.client.delete(url, format="json")

self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, 204)
self.assertEqual(Voting.objects.filter(id=db_voting.id).count(), 0)

def test_get_list_auth_api(self):
self.create_auth()
create_auth(self)
url = base_url + "/base/auth"
response = self.client.get(url, format="json")
self.assertEqual(len(response.data), Auth.objects.count())
Expand All @@ -199,7 +322,7 @@ def test_get_list_auth_api(self):
self.assertEqual(response.data[len(response.data) - 1]['me'], True)

def test_get_auth_api(self):
self.create_auth()
create_auth(self)
db_auth = Auth.objects.last()
url = base_url + "/base/auth/" + str(db_auth.id)
response = self.client.get(url, format="json")
Expand All @@ -208,7 +331,7 @@ def test_get_auth_api(self):
self.assertEqual(response.data['me'], True)

def test_post_auth_api(self):
self.create_auth()
create_auth(self)
db_auth = Auth.objects.last()
self.assertEqual(auth_json_mock.get('name'), db_auth.name)
self.assertEqual(auth_json_mock.get('url'), db_auth.url)
Expand All @@ -220,7 +343,7 @@ def test_post_auth_api(self):
self.assertEqual(response.status_code, 400)

def test_put_auth_api(self):
self.create_auth()
create_auth(self)
db_auth = Auth.objects.last()
url = base_url + "/base/auth/" + str(db_auth.id)
response = self.client.put(
Expand All @@ -237,7 +360,7 @@ def test_put_auth_api(self):
self.assertEqual(response.status_code, 400)

def test_delete_auth_api(self):
self.create_auth()
create_auth(self)
db_auth = Auth.objects.last()

url = base_url + "/base/auth/" + str(db_auth.id)
Expand All @@ -247,8 +370,8 @@ def test_delete_auth_api(self):
self.assertEqual(Voting.objects.filter(id=db_auth.id).count(), 0)

def test_bulk_delete_auth_api(self):
self.create_auth()
self.create_auth(auth_json_mock_delete)
create_auth(self)
create_auth(self, auth_json_mock_delete)
db_auth_id = Auth.objects.last().id

url = base_url + "/base/auth"
Expand All @@ -258,8 +381,8 @@ def test_bulk_delete_auth_api(self):
self.assertEqual(response.status_code, 204)
self.assertEqual(Voting.objects.filter(id__in=id_list).count(), 0)

self.create_auth()
self.create_auth(auth_json_mock_delete)
create_auth(self)
create_auth(self, auth_json_mock_delete)

response = self.client.delete(url, format="json")
self.assertEqual(response.status_code, 204)
Expand Down
21 changes: 13 additions & 8 deletions decide/administration/views.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down

0 comments on commit 5420b7e

Please sign in to comment.