-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from DigitalProductInnovationAndDevelopment/or…
…igin/case-tests test(no-ticket): add tests for user
- Loading branch information
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import requests | ||
|
||
from django.test import TestCase | ||
from EndUserManagement.models import Case | ||
from EndUserManagement.dummyData import getDummyCases, getDummyUsers | ||
|
||
|
||
class UsersTestCase(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 test_get_user(self): | ||
getUserUrl = self.baseServerUrl + "/users" | ||
getUserHeaders = { | ||
'Authorization': f'Bearer {self.dummyUserAccessToken}' | ||
} | ||
response = requests.get(getUserUrl, headers = getUserHeaders) | ||
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("Get user request failed.") | ||
# Check if response is not an empty list | ||
userFetched = responseData['data'] | ||
self.assertNotEqual(userFetched, []) | ||
|
||
#get Dummy Data | ||
dummyUsers = getDummyUsers() | ||
|
||
name_to_find = "Eduard" | ||
userExpected = [user for user in dummyUsers if user["Name"] == name_to_find] | ||
userExpected = userExpected[0] | ||
|
||
userObserved = userFetched | ||
|
||
# Comparing the expected and observed emails of the cases | ||
self.assertIsNotNone(userExpected, userObserved) | ||
self.assertEqual(userExpected["EmailAddress"], userObserved["EmailAddress"]) | ||
|
||
def test_user_update(self): | ||
#First fetch the related user with that token | ||
getUserUrl = self.baseServerUrl + "/users" | ||
getUserHeadersForFetch = { | ||
'Authorization': f'Bearer {self.dummyUserAccessToken}' | ||
} | ||
responseForFetchedUser = requests.get(getUserUrl, headers=getUserHeadersForFetch) | ||
responseData = responseForFetchedUser.json() | ||
userFetched = responseData['data'] | ||
userFetched = userFetched['ID'] | ||
#set url | ||
patchUserUrl = self.baseServerUrl + "/users/" + str(userFetched) # Replace '1' with the actual user ID you want to update | ||
|
||
# Set the headers, including the Authorization token | ||
getUserHeaders = { | ||
'Authorization': f'Bearer {self.dummyUserAccessToken}', # Assume self.dummyUserAccessToken is defined | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
# Set the body parameters you want to update | ||
update_data = { # Update fields as needed | ||
"Surname": "NewSurname", | ||
# Add more fields as necessary | ||
} | ||
|
||
# Send the PATCH request | ||
response = requests.patch(patchUserUrl, headers=getUserHeaders, json=update_data) | ||
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("Patch user request failed.") | ||
|
||
|
||
responseForFetchedUserAfterUpdate = requests.get(getUserUrl, headers=getUserHeadersForFetch) | ||
responseDataOfUpdated = responseForFetchedUserAfterUpdate.json() | ||
userUpdated = responseDataOfUpdated['data'] | ||
|
||
#get Dummy Data | ||
dummyUsers = getDummyUsers() | ||
|
||
name_to_find = "Eduard" | ||
userExpected = [user for user in dummyUsers if user["Name"] == name_to_find] | ||
userExpected = userExpected[0] | ||
|
||
|
||
# Comparing the expected and observed emails of the cases | ||
self.assertIsNotNone(userExpected, userUpdated) | ||
self.assertNotEqual(userExpected["Surname"], userUpdated["Surname"]) | ||
|
||
|