diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/resources/__init__.py b/test/resources/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/resources/entities/__init__.py b/test/resources/entities/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/resources/entities/test_datafile.py b/test/resources/entities/test_datafile.py new file mode 100644 index 00000000..3a163af0 --- /dev/null +++ b/test/resources/entities/test_datafile.py @@ -0,0 +1,111 @@ +import requests + +from common.database_helpers import insert_row_into_table, delete_row_by_id +from common.exceptions import MissingRecordError +from common.models.db_models import DATAFILE +from test.test_base.constants import GOOD_CREDENTIALS_HEADER, BAD_CREDENTIALS_HEADER +from test.test_base.base_rest_test import RestTestCase + +url_with_file_existing = "http://localhost:5000/datafiles/-50" +url_without_file_existing = "http://localhost:5000/datafiles/0" +good_data = {"NAME":"test"} +bad_data = '{"NAMEFf" : "test"}' + + +class TestDatafiles(RestTestCase): + + def setUp(self): + super().setUp() + insert_row_into_table(DATAFILE(ID=-50, MOD_ID="modID", CREATE_ID="create_id", NAME="test_name", + MOD_TIME="2019-05-05 11:11:11", CREATE_TIME="2019-04-06 12:12:12", + DATASET_ID=1)) + + def tearDown(self): + super().tearDown() + try: # This catches the exception when we attempt to delete the file, that was deleted in the test + delete_row_by_id(DATAFILE, -50) + except MissingRecordError: + pass + + def test_get_with_id_with_credentials_and_file_exist(self): + response = requests.get(url_with_file_existing, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(200, response) + self.expect_json_response(response) + # Add test to check the correct datafile is being returned + + def test_get_with_id_with_bad_credentials_and_file_exists(self): + response = requests.get(url_with_file_existing, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_get_with_id_with_no_credentials_and_file_exists(self): + response = requests.get(url_with_file_existing) + self.expect_status_code(403, response) + + def test_get_with_id_with_credentials_and_file_doesnt_exist(self): + response = requests.get(url_without_file_existing, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(404, response) + + def test_get_with_id_with_bad_credentials_and_file_doesnt_exist(self): + response = requests.get(url_without_file_existing, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_get_with_id_with_no_credentials_and_file_doesnt_exist(self): + response = requests.get(url_without_file_existing, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_delete_with_credentials_and_file_exists(self): + response = requests.delete(url_with_file_existing, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(204, response) + + def test_delete_with_bad_credentials_and_file_exists(self): + response = requests.delete(url_with_file_existing, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_delete_with_no_credentials_and_file_exists(self): + response = requests.delete(url_with_file_existing) + self.expect_status_code(403, response) + + def test_delete_with_credentials_and_file_doesnt_exists(self): + response = requests.delete(url_without_file_existing, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(404, response) + + def test_delete_with_bad_credentials_and_file_doesnt_exists(self): + response = requests.delete(url_without_file_existing, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_delete_with_no_credentials_and_file_doesnt_exists(self): + response = requests.delete(url_without_file_existing) + self.expect_status_code(403, response) + + def test_patch_with_credentials_and_file_exists_with_valid_data(self): + response = requests.patch(url_with_file_existing, headers=GOOD_CREDENTIALS_HEADER, json=good_data) + self.expect_status_code(200, response) + self.expect_json_response(response) + + def test_patch_with_credentials_and_file_exists_with_invalid_data(self): + response = requests.patch(url_with_file_existing, headers=GOOD_CREDENTIALS_HEADER, json=bad_data) + self.expect_status_code(400, response) + + def test_patch_with_credentials_and_file_exists_with_no_data(self): + response = requests.patch(url_with_file_existing, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(400, response) + + def test_patch_with_credentials_and_file_doesnt_exist_with_valid_data(self): + response = requests.patch(url_without_file_existing, headers=GOOD_CREDENTIALS_HEADER, json=good_data) + self.expect_status_code(404, response) + + def test_patch_with_credentials_and_file_doesnt_exist_with_invalid_data(self): + response = requests.patch(url_without_file_existing, headers=GOOD_CREDENTIALS_HEADER, json=bad_data) + self.expect_status_code(404, response) + + def test_patch_with_credentaisl_and_file_doesnt_exist_with_no_data(self): + response = requests.patch(url_without_file_existing, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(404, response) + + def test_patch_with_bad_credentials_and_file_exists_with_valid_data(self): + response = requests.patch(url_with_file_existing, headers=BAD_CREDENTIALS_HEADER, json=good_data ) + self.expect_status_code(403, response) + + def test_patch_with_no_credentials_and_file_exists_without_valid_data(self): + response = requests.patch(url_with_file_existing, json=bad_data) + self.expect_status_code(403, response) \ No newline at end of file diff --git a/test/resources/non_entities/__init__.py b/test/resources/non_entities/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/resources/non_entities/test_sessions.py b/test/resources/non_entities/test_sessions.py new file mode 100644 index 00000000..5c7ad635 --- /dev/null +++ b/test/resources/non_entities/test_sessions.py @@ -0,0 +1,69 @@ +import re + +import requests + +from test.test_base.constants import GOOD_CREDENTIALS_HEADER, BAD_CREDENTIALS_HEADER +from test.test_base.base_rest_test import RestTestCase + +uuid_pattern = re.compile("\\b[0-9a-f]{8}\\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\\b[0-9a-f]{12}\\b") +sessions_url = "http://localhost:5000/sessions" + + +def is_session_id_uuid(response): + if uuid_pattern.match(eval(response.text)["sessionID"]): + return True + return False + + +class TestSessions(RestTestCase): + + def test_post_generate_session_id_with_good_credentials(self): + response = requests.post(sessions_url, headers={"Authorization": "user:password"}) + self.assertTrue(is_session_id_uuid(response), "sessionID returned is not a uuid") + self.expect_status_code(201, response) + self.expect_json_response(response) + + def test_post_generate_session_id_with_bad_credentials(self): + response = requests.post(sessions_url, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_post_generate_session_id_with_no_credentials(self): + response = requests.post(sessions_url) + self.expect_status_code(401, response) + + def test_delete_remove_session_id_with_real_session_id(self): + response = requests.delete(sessions_url, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(200, response) + + def test_delete_remove_session_id_with_incorrect_session_id(self): + response = requests.delete(sessions_url, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_delete_remove_session_id_with_no_session_id(self): + response = requests.delete(sessions_url) + self.expect_status_code(403, response) + + def test_get_session_details_with_real_session_id(self): + response = requests.get(sessions_url, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(200, response) + self.expect_json_response(response) + + def test_get_session_details_with_incorrect_session_id(self): + response = requests.get(sessions_url, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_get_session_details_with_no_session_id(self): + response = requests.get(sessions_url) + self.expect_status_code(403, response) + + def test_put_refresh_session_with_real_session_id(self): # put is not currently not implemented properly + response = requests.put(sessions_url, headers=GOOD_CREDENTIALS_HEADER) + self.expect_status_code(200, response) + + def test_put_refresh_session_with_incorrect_session_id(self): # put is not currently implemented properly + response = requests.put(sessions_url, headers=BAD_CREDENTIALS_HEADER) + self.expect_status_code(403, response) + + def test_put_refresh_session_with_no_session_id(self): # put is not currently implemented properly + response = requests.put(sessions_url) + self.expect_status_code(403, response) diff --git a/test/test_base/__init__.py b/test/test_base/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/test_base/base_rest_test.py b/test/test_base/base_rest_test.py new file mode 100644 index 00000000..c839eff3 --- /dev/null +++ b/test/test_base/base_rest_test.py @@ -0,0 +1,43 @@ +from unittest import TestCase + +from common.database_helpers import insert_row_into_table, delete_row_by_id +from common.exceptions import MissingRecordError +from common.helpers import is_valid_json +from common.models.db_models import SESSION + + +class RestTestCase(TestCase): + """ + Parent class of endpoint test cases + """ + + def setUp(self): + """ + Inserts a session for testing into the user_sessions table + """ + insert_row_into_table(SESSION(ID="TestSession")) + + def tearDown(self): + """ + Removes the inserted session from the user_sessions table + """ + try: + delete_row_by_id(SESSION, "TestSession") + except MissingRecordError: + pass + + def expect_status_code(self, expected_status_code, response): + """ + Asserts whethere the returned status code is equal to the expected + :param expected_status_code: int: The status code that is expected + :param response: The response to be checked + """ + self.assertEqual(expected_status_code, response.status_code, "Incorrect status code, received: " + + str(response.status_code) + ". Expected " + str(expected_status_code)) + + def expect_json_response(self, response): + """ + Asserts whether the returned item is valid JSON + :param response: The response to be checked + """ + self.assertTrue(is_valid_json(response.text), "Response was not valid JSON") diff --git a/test/test_base/constants.py b/test/test_base/constants.py new file mode 100644 index 00000000..adee1741 --- /dev/null +++ b/test/test_base/constants.py @@ -0,0 +1,2 @@ +GOOD_CREDENTIALS_HEADER = {"Authorization": "TestSession"} +BAD_CREDENTIALS_HEADER = {"Authorization": "Santa Claus"} \ No newline at end of file