Skip to content

Commit

Permalink
Add test package
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Jun 11, 2019
1 parent 468e2d9 commit 83a7699
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 0 deletions.
Empty file added test/__init__.py
Empty file.
Empty file added test/resources/__init__.py
Empty file.
Empty file.
111 changes: 111 additions & 0 deletions test/resources/entities/test_datafile.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
69 changes: 69 additions & 0 deletions test/resources/non_entities/test_sessions.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file added test/test_base/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions test/test_base/base_rest_test.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 2 additions & 0 deletions test/test_base/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GOOD_CREDENTIALS_HEADER = {"Authorization": "TestSession"}
BAD_CREDENTIALS_HEADER = {"Authorization": "Santa Claus"}

0 comments on commit 83a7699

Please sign in to comment.