diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7bfa762..a69110b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -26,18 +26,25 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt + - name: Delete Existing SQLite Test DB + run: | + if [ -f db.sqlite3 ]; then rm db.sqlite3; fi + - name: Run migrations run: | python manage.py makemigrations python manage.py migrate + - name: Insert fixture data run: | chmod +x scripts/create_fixture_data.sh ./scripts/create_fixture_data.sh + - name: Insert dummy data if DB is empty run: | chmod +x scripts/create_dummy_data.sh ./scripts/create_dummy_data.sh + - name: Start Django server in the background run: | python manage.py runserver 0.0.0.0:8000 & @@ -47,6 +54,7 @@ jobs: - name: Run tests run: | python manage.py test + - name: Stop Django server run: | kill $(lsof -t -i:8000) || echo "Server stopped" diff --git a/EndUserManagement/dummyData/cases.py b/EndUserManagement/dummyData/cases.py index f95f343..4863deb 100755 --- a/EndUserManagement/dummyData/cases.py +++ b/EndUserManagement/dummyData/cases.py @@ -57,6 +57,13 @@ ] } ] + }, + { + "Coverage": "INDIVIDUAL", + "Description": "I need psychological help about an issue. I would like to speak with a case worker in private.", + "Status": "REQUEST RECEIVED", + "CaseTypes": ["Health"], + "PsnTypes": [] } ], "Dilshad": [ diff --git a/EndUserManagement/management/commands/createDummyData.py b/EndUserManagement/management/commands/createDummyData.py index 8d3e5be..89cd677 100755 --- a/EndUserManagement/management/commands/createDummyData.py +++ b/EndUserManagement/management/commands/createDummyData.py @@ -59,6 +59,7 @@ def handle(self, *args, **options): dummyUser = User(**dummyUserInfo) dummyUser.set_password(dummyPwd) dummyUser.save() + print("User inserted") dummyCasesOfUser = dummyCases[dummyUserInfo["Name"]] if dummyUserInfo["Name"] in dummyCases else None if dummyCasesOfUser: for dummyCaseInfo in dummyCasesOfUser: @@ -70,14 +71,19 @@ def handle(self, *args, **options): dummyCase = Case(User = dummyUser, **caseCreateDict) # We first need to save the model for attaching many to many fields dummyCase.save() + print("Case inserted") # Attaching the case types - chosenCaseTypes = CaseType.objects.filter(name__in = dummyCaseInfo["CaseTypes"]) - for chosenCaseType in chosenCaseTypes: - dummyCase.CaseTypes.add(chosenCaseType) + caseTypes = dummyCaseInfo["CaseTypes"] if "CaseTypes" in dummyCaseInfo else None + if caseTypes: + chosenCaseTypes = CaseType.objects.filter(name__in = caseTypes) + for chosenCaseType in chosenCaseTypes: + dummyCase.CaseTypes.add(chosenCaseType) # Attaching the psn types - chosenPsnTypes = PsnType.objects.filter(name__in = dummyCaseInfo["PsnTypes"]) - for chosenPsnType in chosenPsnTypes: - dummyCase.PsnTypes.add(chosenPsnType) + psnTypes = dummyCaseInfo["PsnTypes"] if "PsnTypes" in dummyCaseInfo else None + if psnTypes: + chosenPsnTypes = PsnType.objects.filter(name__in = dummyCaseInfo["PsnTypes"]) + for chosenPsnType in chosenPsnTypes: + dummyCase.PsnTypes.add(chosenPsnType) # Calculating and setting the vulnerability score for household cases if dummyCase.Coverage == "HOUSEHOLD": vulnerabilityScore, vulnerabilityCategory = caseService.calcCaseVulnerabilityScore(dummyCase) diff --git a/tests/AdminManagement/__init__.py b/tests/AdminManagement/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/EndUserManagement/__init__.py b/tests/EndUserManagement/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/EndUserManagement/testCaseEndpoints.py b/tests/EndUserManagement/testCaseEndpoints.py new file mode 100644 index 0000000..5d1dac6 --- /dev/null +++ b/tests/EndUserManagement/testCaseEndpoints.py @@ -0,0 +1,188 @@ +import requests +from django.test import TestCase +from EndUserManagement.models import Case +from EndUserManagement.dummyData import getDummyCases + +class CasesTestCase(TestCase): + def setUp(self): + # The host for the docker + self.baseServerUrl = "http://localhost:8000/api" + self.dummyUserEmail = "eduard@gmail.com" + self.dummyUserPwd = "1234567" + payload = { + "EmailAddress": self.dummyUserEmail, + "Password": self.dummyUserPwd + } + loginUrl = self.baseServerUrl + "/login" + # Send a POST request to the login endpoint + response = requests.post(loginUrl, json=payload) + + # Check if the request was successful (status code 200) + if response.status_code == 200: + # Parse the JSON response + responseData = response.json() + # Extract the accessToken from the response data + accessToken = responseData.get('data', {}).get('access_token') + if accessToken: + self.dummyUserAccessToken = accessToken + else: + raise Exception("No access token in the login response. Check out the test set up method.") + else: + raise Exception("Login request failed. Check out the test set up method.") + + def tearDown(self): + # Clean up: Delete all test cases created by the tests + caseListUrl = self.baseServerUrl + "/cases" + caseListHeaders = { + 'Authorization': f'Bearer {self.dummyUserAccessToken}' + } + response = requests.get(caseListUrl, headers=caseListHeaders) + if response.status_code == 200: + responseData = response.json() + casesList = responseData.get('data', []) + for case in casesList: + case_id = case.get('ID') + if case_id: + deleteUrl = f"{self.baseServerUrl}/cases/{case_id}" + requests.delete(deleteUrl, headers=caseListHeaders) + + def test_case_list(self): + # Create a known number of cases to test the list functionality + self.create_test_case(description='Case for List Test 1') + self.create_test_case(description='Case for List Test 2') + + caseListUrl = self.baseServerUrl + "/cases" + caseListHeaders = { + 'Authorization': f'Bearer {self.dummyUserAccessToken}' + } + response = requests.get(caseListUrl, headers=caseListHeaders) + responseData = None + # Check if the request was successful (status code 200) + if response.status_code == 200: + # Parse the JSON response + responseData = response.json() + else: + raise Exception("Case list request failed.") + # Check if response is not an empty list + casesList = responseData['data'] + + self.assertNotEqual(casesList, []) + self.assertEqual(len(casesList), 2) + + + def test_case_create(self): + caseCreateUrl = self.baseServerUrl + "/cases" + caseCreateHeaders = { + 'Authorization': f'Bearer {self.dummyUserAccessToken}' + } + + # Prepare data to send as form-data + data = { + 'Coverage': 'INDIVIDUAL', + 'Description': 'Test Description', + 'CaseTypes': ','.join(map(str, [1])), + 'PsnTypes': ','.join(map(str, [1])) + } + + # Send the POST request with form-data + response = requests.post(caseCreateUrl, headers=caseCreateHeaders, data=data) + + if response.status_code == 201: + responseData = response.json() + caseCreated = responseData.get('data', {}) + + self.assertIsNotNone(caseCreated) + self.assertEqual(caseCreated.get('Description'), data['Description']) + self.assertEqual(caseCreated.get('Coverage'), data['Coverage']) + self.assertEqual(caseCreated.get('PsnTypes'), [1]) + self.assertEqual(caseCreated.get('CaseTypes'), [1]) + else: + raise Exception(f"Case creation request failed with status code {response.status_code} and response: {response.text}") + + def test_case_get(self): + # Create a case to get + case_id = self.create_test_case(description='Case for Get Test') + + caseGetUrl = f"{self.baseServerUrl}/cases/{case_id}" + caseGetHeaders = { + 'Authorization': f'Bearer {self.dummyUserAccessToken}' + } + + response = requests.get(caseGetUrl, headers=caseGetHeaders) + + if response.status_code == 200: + responseData = response.json() + caseRetrieved = responseData.get('data', {}) + + self.assertIsNotNone(caseRetrieved) + self.assertEqual(caseRetrieved.get('ID'), case_id) + self.assertEqual(caseRetrieved.get('Description'), 'Case for Get Test') + else: + raise Exception(f"Case retrieval request failed with status code {response.status_code} and response: {response.text}") + + def test_case_delete(self): + # Create a case to be deleted + case_id = self.create_test_case(description='Case for Deletion') + + caseDeleteUrl = f"{self.baseServerUrl}/cases/{case_id}" + caseDeleteHeaders = { + 'Authorization': f'Bearer {self.dummyUserAccessToken}' + } + + response = requests.delete(caseDeleteUrl, headers=caseDeleteHeaders) + + self.assertEqual(response.status_code, 200) + + response = requests.get(caseDeleteUrl, headers=caseDeleteHeaders) + + self.assertEqual(response.status_code, 404) + + def create_test_case(self, description='Test Case'): + caseCreateUrl = self.baseServerUrl + "/cases" + caseCreateHeaders = { + 'Authorization': f'Bearer {self.dummyUserAccessToken}' + } + + # Prepare data to send as form-data + data = { + 'Coverage': 'INDIVIDUAL', + 'Description': description, + 'CaseTypes': ','.join(map(str, [1])), + 'PsnTypes': ','.join(map(str, [1])) + } + + response = requests.post(caseCreateUrl, headers=caseCreateHeaders, data=data) + + if response.status_code == 201: + responseData = response.json() + caseCreated = responseData.get('data', {}) + + return caseCreated.get('ID') + else: + raise Exception(f"Case creation request failed with status code {response.status_code} and response: {response.text}") + + + def test_case_update(self): + case_id = self.create_test_case(description='Original Case Description') + + caseUpdateUrl = f"{self.baseServerUrl}/cases/{case_id}" + caseUpdateHeaders = { + 'Authorization': f'Bearer {self.dummyUserAccessToken}', + 'Content-Type': 'application/json' + } + + updatedData = { + 'Description': 'Updated Case Description', + } + + response = requests.patch(caseUpdateUrl, headers=caseUpdateHeaders, json=updatedData) + + if response.status_code == 200: + responseData = response.json() + caseUpdated = responseData.get('data', {}) + + + self.assertIsNotNone(caseUpdated) + self.assertEqual(caseUpdated.get('Description'), updatedData['Description']) + else: + raise Exception(f"Case update request failed with status code {response.status_code} and response: {response.text}") \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29