Skip to content

Commit

Permalink
Merge pull request #11 from DigitalProductInnovationAndDevelopment/te…
Browse files Browse the repository at this point in the history
…st-endusermanagement-cases

tests(no-ticket): add tests for end user management - cases
  • Loading branch information
betulkaraagoz authored Aug 13, 2024
2 parents f1cb3ee + 1af7102 commit 8432858
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand All @@ -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"
7 changes: 7 additions & 0 deletions EndUserManagement/dummyData/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
18 changes: 12 additions & 6 deletions EndUserManagement/management/commands/createDummyData.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
Empty file.
Empty file.
188 changes: 188 additions & 0 deletions tests/EndUserManagement/testCaseEndpoints.py
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
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}")
Empty file added tests/__init__.py
Empty file.

0 comments on commit 8432858

Please sign in to comment.