From 41e53f3e48250b393530766498ea1ded14b08ab4 Mon Sep 17 00:00:00 2001 From: Michael Kaiser Date: Tue, 5 Dec 2023 21:55:28 +0100 Subject: [PATCH] adding new simple tests for the route endpoints of the admin frontend, correction in the api tests --- .gitignore | 3 + codeGrader/backend/config/Config.py | 3 + codeGrader/backend/config/config.conf | 3 + codeGrader/backend/config/config.json | 47 ----- .../frontend/admin/static/css/styling.css | 1 - tests/backend/api_tests/test_ApiAdmin.py | 117 ++++++++++++ tests/backend/api_tests/test_ApiExercise.py | 12 ++ tests/backend/api_tests/test_ApiProfile.py | 12 ++ tests/backend/api_tests/test_ApiSubject.py | 12 ++ tests/backend/api_tests/test_ApiTask.py | 12 ++ tests/backend/api_tests/test_ApiUser.py | 99 +--------- tests/frontend/__init__.py | 0 tests/frontend/admin/__init__.py | 0 tests/frontend/admin/test_Routes.py | 177 ++++++++++++++++++ tests/frontend/user/__init__.py | 0 15 files changed, 356 insertions(+), 142 deletions(-) delete mode 100644 codeGrader/backend/config/config.json create mode 100644 tests/backend/api_tests/test_ApiAdmin.py create mode 100644 tests/frontend/__init__.py create mode 100644 tests/frontend/admin/__init__.py create mode 100644 tests/frontend/admin/test_Routes.py create mode 100644 tests/frontend/user/__init__.py diff --git a/.gitignore b/.gitignore index 86e5885..0f18a5d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ # PyCache Files __pycache__ *.pyc + +# Secret Values file for testing +*.sec.json diff --git a/codeGrader/backend/config/Config.py b/codeGrader/backend/config/Config.py index 9aeddfc..aad4c6b 100644 --- a/codeGrader/backend/config/Config.py +++ b/codeGrader/backend/config/Config.py @@ -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"] diff --git a/codeGrader/backend/config/config.conf b/codeGrader/backend/config/config.conf index 159e43b..79ed075 100644 --- a/codeGrader/backend/config/config.conf +++ b/codeGrader/backend/config/config.conf @@ -31,6 +31,9 @@ Port = 8002 [Tests] ApiHost = 127.0.0.1 +FrontendHost = 127.0.0.1 +AdminPort = 8101 +UserPort = 8102 [Logging] diff --git a/codeGrader/backend/config/config.json b/codeGrader/backend/config/config.json deleted file mode 100644 index 5f1414a..0000000 --- a/codeGrader/backend/config/config.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "api": { - "applicationName": "CodeGrader", - "host": "127.0.0.1", - "port": 8001, - "TokenAuthorization": 1, - "tokenLength": 30 - }, - - "evaluationService": { - "host": "127.0.0.1", - "port": 8002 - }, - - "executionService": { - "host": "127.0.0.1", - "port": 8003, - "PathToExecutionFiles": "/opt" - }, - - "database": { - "dialect": "postgresql", - "DBdriver": "psycopg2", - "username": "codeGrader", - "password": "codeGrader", - "host": "10.101.10.80", - "port": "5432", - "database": "codeGraderDB", - "datamodel": { - "metadataColumnsCount": 3, - "columnIgnoreList": [ - "id", - "creation_dts", - "updated_dts" - ] - } - }, - - "tests": { - "apiHost": "127.0.0.1" - }, - - "logging": { - "debug": 0, - "useIntegratedLogging": 1 - } -} diff --git a/codeGrader/frontend/admin/static/css/styling.css b/codeGrader/frontend/admin/static/css/styling.css index 511b006..c7d0468 100644 --- a/codeGrader/frontend/admin/static/css/styling.css +++ b/codeGrader/frontend/admin/static/css/styling.css @@ -137,7 +137,6 @@ body { font-size: 36px; margin-left: 0; margin-top: 10px; - visibility: hidden; transition: visiblity 500ms; } diff --git a/tests/backend/api_tests/test_ApiAdmin.py b/tests/backend/api_tests/test_ApiAdmin.py new file mode 100644 index 0000000..ce37c61 --- /dev/null +++ b/tests/backend/api_tests/test_ApiAdmin.py @@ -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"]) + diff --git a/tests/backend/api_tests/test_ApiExercise.py b/tests/backend/api_tests/test_ApiExercise.py index f5dc55d..929a484 100644 --- a/tests/backend/api_tests/test_ApiExercise.py +++ b/tests/backend/api_tests/test_ApiExercise.py @@ -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"]) diff --git a/tests/backend/api_tests/test_ApiProfile.py b/tests/backend/api_tests/test_ApiProfile.py index e36ae75..a6563fd 100644 --- a/tests/backend/api_tests/test_ApiProfile.py +++ b/tests/backend/api_tests/test_ApiProfile.py @@ -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"]) diff --git a/tests/backend/api_tests/test_ApiSubject.py b/tests/backend/api_tests/test_ApiSubject.py index 5b044d0..72c73b4 100644 --- a/tests/backend/api_tests/test_ApiSubject.py +++ b/tests/backend/api_tests/test_ApiSubject.py @@ -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"]) diff --git a/tests/backend/api_tests/test_ApiTask.py b/tests/backend/api_tests/test_ApiTask.py index 5fd770f..7a0e42d 100644 --- a/tests/backend/api_tests/test_ApiTask.py +++ b/tests/backend/api_tests/test_ApiTask.py @@ -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"]) diff --git a/tests/backend/api_tests/test_ApiUser.py b/tests/backend/api_tests/test_ApiUser.py index 8bfd6cf..c210ced 100644 --- a/tests/backend/api_tests/test_ApiUser.py +++ b/tests/backend/api_tests/test_ApiUser.py @@ -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"]) diff --git a/tests/frontend/__init__.py b/tests/frontend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/frontend/admin/__init__.py b/tests/frontend/admin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/frontend/admin/test_Routes.py b/tests/frontend/admin/test_Routes.py new file mode 100644 index 0000000..1e8964e --- /dev/null +++ b/tests/frontend/admin/test_Routes.py @@ -0,0 +1,177 @@ +import unittest +import requests, json +import browser_cookie3 +import urllib.request +from codeGrader.backend.config import config + + +class RouteTest(unittest.TestCase): + + def setUp(self): + f = open("./frontend/admin/login.sec.json", 'rb') + content = f.read() + f.close() + + data = json.loads(content) + self.username = data["username"] + self.password = data["password"] + + self.url = f"http://{config.tests_frontendHost}:{config.tests_adminPort}/" + login_data = {"username": self.username, "password": self.password} + + r = requests.post(self.url + "login", data=login_data) + self.cookies = r.request._cookies + + def test_GET_home(self): + """ + Test Case for the /home route + @return: No return + """ + + r = requests.get(self.url, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_admins(self): + """ + Test Case for the /admins route + @return: No return + """ + route = "admins" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_admin_add(self): + """ + Test Case for the /admin/add route + @return: No return + """ + route = "admin/add" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_users(self): + """ + Test Case for the /users route + @return: No return + """ + route = "users" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_user_add(self): + """ + Test Case for the /user/add route + @return: No return + """ + route = "user/add" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_profiles(self): + """ + Test Case for the /profiles route + @return: No return + """ + route = "profiles" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_profile_add(self): + """ + Test Case for the /profile/add route + @return: No return + """ + route = "profile/add" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_subjects(self): + """ + Test Case for the /subjects route + @return: No return + """ + route = "subjects" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_subject_add(self): + """ + Test Case for the /subject/add route + @return: No return + """ + route = "subject/add" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_exercises(self): + """ + Test Case for the /exercises route + @return: No return + """ + route = "exercises" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_exercise_add(self): + """ + Test Case for the /exercise/add route + @return: No return + """ + route = "exercise/add" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_tasks(self): + """ + Test Case for the /tasks route + @return: No return + """ + route = "tasks" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_task_add(self): + """ + Test Case for the /task/add route + @return: No return + """ + route = "task/add" + r = requests.get(self.url+route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertFalse("input input_login" in r.text) + + def test_GET_logout(self): + """ + Test Case for the /task/add route + @return: No return + """ + route = "logout" + r = requests.get(self.url + route, cookies=self.cookies) + self.assertIsNotNone(r) + self.assertEqual(200, r.status_code) + self.assertTrue("input input_login" in r.text) diff --git a/tests/frontend/user/__init__.py b/tests/frontend/user/__init__.py new file mode 100644 index 0000000..e69de29