Skip to content

Commit

Permalink
adding new simple tests for the route endpoints of the admin frontend…
Browse files Browse the repository at this point in the history
…, correction in the api tests
  • Loading branch information
ooemperor committed Dec 5, 2023
1 parent d716e77 commit 41e53f3
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 142 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
# PyCache Files
__pycache__
*.pyc

# Secret Values file for testing
*.sec.json
3 changes: 3 additions & 0 deletions codeGrader/backend/config/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def __init__(self):
# Configurations for Testing purposes
self.tests_ApiHost = self.config["Tests"]["ApiHost"]
self.tests_ApiPort = self.ApiPort
self.tests_frontendHost = self.config["Tests"]["FrontendHost"]
self.tests_adminPort = self.config["Tests"]["AdminPort"]
self.tests_userPort = self.config["Tests"]["UserPort"]

# Configuration for EvaluationService
self.evaluationHost = self.config["EvaluationService"]["Host"]
Expand Down
3 changes: 3 additions & 0 deletions codeGrader/backend/config/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Port = 8002

[Tests]
ApiHost = 127.0.0.1
FrontendHost = 127.0.0.1
AdminPort = 8101
UserPort = 8102


[Logging]
Expand Down
47 changes: 0 additions & 47 deletions codeGrader/backend/config/config.json

This file was deleted.

1 change: 0 additions & 1 deletion codeGrader/frontend/admin/static/css/styling.css
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ body {
font-size: 36px;
margin-left: 0;
margin-top: 10px;
visibility: hidden;
transition: visiblity 500ms;
}

Expand Down
117 changes: 117 additions & 0 deletions tests/backend/api_tests/test_ApiAdmin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import unittest
import requests, json
from codeGrader.backend.config import config


class ApiAdminUserTest(unittest.TestCase):
def test_createAndDeleteAdminUser(self):
"""
Test Case for creating and deleting the AdminUser.
Covers post, get and delete for the api/user
@return: No return
"""
create_url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admin/add"
adminUser_url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admin/"
adminUser_dict = {
"username": "admin_test",
"first_name": "admin",
"last_name": "user",
"email": "test.user@mail.com",
"password": "myPassword",
"tag": "usertag",
"admin_type": 1
}

# creating the user
r = requests.post(create_url, json=adminUser_dict)
self.assertIsNotNone(r)
self.assertEqual(201, r.status_code)
adminUser_id = json.loads(r.text)["response"]["id"]

# checks after creation
r = requests.get(f"{adminUser_url}{adminUser_id}")
self.assertEqual(200, r.status_code)
self.assertEqual("admin_test", json.loads(r.text)["username"])
self.assertEqual("admin", json.loads(r.text)["first_name"])
self.assertEqual("user", json.loads(r.text)["last_name"])
self.assertEqual("test.user@mail.com", json.loads(r.text)["email"])
self.assertEqual("usertag", json.loads(r.text)["tag"])

# deleting the user after the test
r = requests.delete(f"{adminUser_url}{adminUser_id}")
self.assertEqual(204, r.status_code)
self.assertIsNotNone(r)

def test_createUpdateAndDeleteAdminUser(self):
"""
Test Case for creating, updating and then deleting the user again via API
Covers post, get, put and delete for the api/user
@return: No return
"""

create_url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admin/add"
adminUser_url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admin/"
user_dict = {
"username": "admin_test",
"first_name": "admin",
"last_name": "user",
"email": "test.user@mail.com",
"password": "myPassword",
"tag": "usertag",
"admin_type": 1
}

# creating the user
r = requests.post(create_url, json=user_dict)
self.assertIsNotNone(r)
self.assertEqual(201, r.status_code)
adminUser_id = json.loads(r.text)["response"]["id"]

# checks after creation
r = requests.get(f"{adminUser_url}{adminUser_id}")
self.assertEqual(200, r.status_code)
self.assertEqual("admin_test", json.loads(r.text)["username"])
self.assertEqual("admin", json.loads(r.text)["first_name"])
self.assertEqual("user", json.loads(r.text)["last_name"])
self.assertEqual("test.user@mail.com", json.loads(r.text)["email"])
self.assertEqual("usertag", json.loads(r.text)["tag"])

new_user_dict = {
"username": "new",
"first_name": "first",
"last_name": "last",
"email": "test.user@mail.com",
"password": "myNewPassword",
"tag": "newUserTag"
}

# updating the user
r = requests.put(f"{adminUser_url}{adminUser_id}", json=new_user_dict, headers={'content-type': 'application/json'})
self.assertEqual(200, r.status_code)

# checking again
r = requests.get(f"{adminUser_url}{adminUser_id}")
self.assertEqual(200, r.status_code)
self.assertEqual("new", json.loads(r.text)["username"])
self.assertEqual("first", json.loads(r.text)["first_name"])
self.assertEqual("last", json.loads(r.text)["last_name"])
self.assertEqual("test.user@mail.com", json.loads(r.text)["email"])
self.assertEqual("newUserTag", json.loads(r.text)["tag"])

# deleting the user after the test
r = requests.delete(f"{adminUser_url}{adminUser_id}")
self.assertIsNotNone(r)
self.assertEqual(204, r.status_code)

def test_GETAdminsEndpoint(self):
"""
Test Case for the testing if the /admins endpoint is working
@return: No return
"""
url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admins"

r = requests.get(url)
self.assertIsNotNone(r)
self.assertEqual(200, r.status_code)
self.assertIsNotNone(json.loads(r.text)["adminuser"])

12 changes: 12 additions & 0 deletions tests/backend/api_tests/test_ApiExercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ def test_createUpdateAndDeleteExercise(self):
r = requests.delete(f"{exercise_url}{exercise_id}")
self.assertIsNotNone(r)
self.assertEqual(204, r.status_code)

def test_GETExercisesEndpoint(self):
"""
Test Case for the testing if the /exercises endpoint is working
@return: No return
"""
url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/exercises"

r = requests.get(url)
self.assertIsNotNone(r)
self.assertEqual(200, r.status_code)
self.assertIsNotNone(json.loads(r.text)["exercise"])
12 changes: 12 additions & 0 deletions tests/backend/api_tests/test_ApiProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ def test_createUpdateAndDeleteProfile(self):
r = requests.delete(f"{profile_url}{profile_id}")
self.assertIsNotNone(r)
self.assertEqual(204, r.status_code)

def test_GETProfilesEndpoint(self):
"""
Test Case for the testing if the /profiles endpoint is working
@return: No return
"""
url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/profiles"

r = requests.get(url)
self.assertIsNotNone(r)
self.assertEqual(200, r.status_code)
self.assertIsNotNone(json.loads(r.text)["profile"])
12 changes: 12 additions & 0 deletions tests/backend/api_tests/test_ApiSubject.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ def test_createUpdateAndDeleteSubject(self):
r = requests.delete(f"{subject_url}{subject_id}")
self.assertIsNotNone(r)
self.assertEqual(204, r.status_code)

def test_GETSubjectsEndpoint(self):
"""
Test Case for the testing if the /subjects endpoint is working
@return: No return
"""
url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/subjects"

r = requests.get(url)
self.assertIsNotNone(r)
self.assertEqual(200, r.status_code)
self.assertIsNotNone(json.loads(r.text)["subject"])
12 changes: 12 additions & 0 deletions tests/backend/api_tests/test_ApiTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ def test_createUpdateAndDeleteTask(self):
r = requests.delete(f"{task_url}{task_id}")
self.assertIsNotNone(r)
self.assertEqual(204, r.status_code)

def test_GETTasksEndpoint(self):
"""
Test Case for the testing if the /tasks endpoint is working
@return: No return
"""
url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/tasks"

r = requests.get(url)
self.assertIsNotNone(r)
self.assertEqual(200, r.status_code)
self.assertIsNotNone(json.loads(r.text)["task"])
99 changes: 5 additions & 94 deletions tests/backend/api_tests/test_ApiUser.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,103 +101,14 @@ def test_createUpdateAndDeleteUser(self):
self.assertIsNotNone(r)
self.assertEqual(204, r.status_code)


class ApiAdminUserTest(unittest.TestCase):
def test_createAndDeleteAdminUser(self):
"""
Test Case for creating and deleting the AdminUser.
Covers post, get and delete for the api/user
@return: No return
"""
create_url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admin/add"
adminUser_url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admin/"
adminUser_dict = {
"username": "admin_test",
"first_name": "admin",
"last_name": "user",
"email": "test.user@mail.com",
"password": "myPassword",
"tag": "usertag",
"admin_type": 1
}

# creating the user
r = requests.post(create_url, json=adminUser_dict)
self.assertIsNotNone(r)
self.assertEqual(201, r.status_code)
adminUser_id = json.loads(r.text)["response"]["id"]

# checks after creation
r = requests.get(f"{adminUser_url}{adminUser_id}")
self.assertEqual(200, r.status_code)
self.assertEqual("admin_test", json.loads(r.text)["username"])
self.assertEqual("admin", json.loads(r.text)["first_name"])
self.assertEqual("user", json.loads(r.text)["last_name"])
self.assertEqual("test.user@mail.com", json.loads(r.text)["email"])
self.assertEqual("usertag", json.loads(r.text)["tag"])

# deleting the user after the test
r = requests.delete(f"{adminUser_url}{adminUser_id}")
self.assertEqual(204, r.status_code)
self.assertIsNotNone(r)

def test_createUpdateAndDeleteAdminUser(self):
def test_GETUsersEndpoint(self):
"""
Test Case for creating, updating and then deleting the user again via API
Covers post, get, put and delete for the api/user
Test Case for the testing if the /users endpoint is working
@return: No return
"""
url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/users"

create_url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admin/add"
adminUser_url = f"http://{config.tests_ApiHost}:{config.tests_ApiPort}/admin/"
user_dict = {
"username": "admin_test",
"first_name": "admin",
"last_name": "user",
"email": "test.user@mail.com",
"password": "myPassword",
"tag": "usertag",
"admin_type": 1
}

# creating the user
r = requests.post(create_url, json=user_dict)
r = requests.get(url)
self.assertIsNotNone(r)
self.assertEqual(201, r.status_code)
adminUser_id = json.loads(r.text)["response"]["id"]

# checks after creation
r = requests.get(f"{adminUser_url}{adminUser_id}")
self.assertEqual(200, r.status_code)
self.assertEqual("admin_test", json.loads(r.text)["username"])
self.assertEqual("admin", json.loads(r.text)["first_name"])
self.assertEqual("user", json.loads(r.text)["last_name"])
self.assertEqual("test.user@mail.com", json.loads(r.text)["email"])
self.assertEqual("usertag", json.loads(r.text)["tag"])

new_user_dict = {
"username": "new",
"first_name": "first",
"last_name": "last",
"email": "test.user@mail.com",
"password": "myNewPassword",
"tag": "newUserTag"
}

# updating the user
r = requests.put(f"{adminUser_url}{adminUser_id}", json=new_user_dict, headers={'content-type': 'application/json'})
self.assertEqual(200, r.status_code)

# checking again
r = requests.get(f"{adminUser_url}{adminUser_id}")
self.assertEqual(200, r.status_code)
self.assertEqual("new", json.loads(r.text)["username"])
self.assertEqual("first", json.loads(r.text)["first_name"])
self.assertEqual("last", json.loads(r.text)["last_name"])
self.assertEqual("test.user@mail.com", json.loads(r.text)["email"])
self.assertEqual("newUserTag", json.loads(r.text)["tag"])

# deleting the user after the test
r = requests.delete(f"{adminUser_url}{adminUser_id}")
self.assertIsNotNone(r)
self.assertEqual(204, r.status_code)
self.assertIsNotNone(json.loads(r.text)["user"])
Empty file added tests/frontend/__init__.py
Empty file.
Empty file.
Loading

0 comments on commit 41e53f3

Please sign in to comment.