Merge branch 'imbs' into BumBranch

This commit is contained in:
al8763be 2024-03-19 01:24:38 +01:00
commit de234c12f2

View file

@ -2,6 +2,16 @@ import requests
import string import string
import random import random
debug_output = False
def gprint(*args, **kwargs):
print("\033[92m", *args, "\033[00m", **kwargs)
print("Running Tests...")
def dprint(*args, **kwargs):
if debug_output:
print(*args, **kwargs)
def randomString(len=10): def randomString(len=10):
"""Generate a random string of fixed length""" """Generate a random string of fixed length"""
@ -31,7 +41,7 @@ getUserProjectsPath = base_url + "/api/getUserProjects"
def test_get_user_projects(): def test_get_user_projects():
print("Testing get user projects") dprint("Testing get user projects")
loginResponse = login("user2", "123") loginResponse = login("user2", "123")
# Check if the user is added to the project # Check if the user is added to the project
response = requests.get( response = requests.get(
@ -39,29 +49,29 @@ def test_get_user_projects():
json={"username": "user2"}, json={"username": "user2"},
headers={"Authorization": "Bearer " + loginResponse.json()["token"]}, headers={"Authorization": "Bearer " + loginResponse.json()["token"]},
) )
print(response.text) dprint(response.text)
print(response.json()) dprint(response.json())
assert response.status_code == 200, "Get user projects failed" assert response.status_code == 200, "Get user projects failed"
print("got user projects successfully") gprint("test_get_user_projects successful")
# Posts the username and password to the register endpoint # Posts the username and password to the register endpoint
def register(username: string, password: string): def register(username: string, password: string):
print("Registering with username: ", username, " and password: ", password) dprint("Registering with username: ", username, " and password: ", password)
response = requests.post( response = requests.post(
registerPath, json={"username": username, "password": password} registerPath, json={"username": username, "password": password}
) )
print(response.text) dprint(response.text)
return response return response
# Posts the username and password to the login endpoint # Posts the username and password to the login endpoint
def login(username: string, password: string): def login(username: string, password: string):
print("Logging in with username: ", username, " and password: ", password) dprint("Logging in with username: ", username, " and password: ", password)
response = requests.post( response = requests.post(
loginPath, json={"username": username, "password": password} loginPath, json={"username": username, "password": password}
) )
print(response.text) dprint(response.text)
return response return response
@ -69,7 +79,8 @@ def login(username: string, password: string):
def test_login(): def test_login():
response = login(username, "always_same") response = login(username, "always_same")
assert response.status_code == 200, "Login failed" assert response.status_code == 200, "Login failed"
print("Login successful") dprint("Login successful")
gprint("test_login successful")
return response.json()["token"] return response.json()["token"]
@ -77,8 +88,7 @@ def test_login():
def test_create_user(): def test_create_user():
response = register(username, "always_same") response = register(username, "always_same")
assert response.status_code == 200, "Registration failed" assert response.status_code == 200, "Registration failed"
print("Registration successful") gprint("test_create_user successful")
# Test function to add a project # Test function to add a project
def test_add_project(): def test_add_project():
@ -89,10 +99,9 @@ def test_add_project():
json={"name": projectName, "description": "This is a project"}, json={"name": projectName, "description": "This is a project"},
headers={"Authorization": "Bearer " + token}, headers={"Authorization": "Bearer " + token},
) )
print(response.text) dprint(response.text)
assert response.status_code == 200, "Add project failed" assert response.status_code == 200, "Add project failed"
print("Add project successful") gprint("test_add_project successful")
# Test function to submit a report # Test function to submit a report
def test_submit_report(): def test_submit_report():
@ -111,10 +120,9 @@ def test_submit_report():
}, },
headers={"Authorization": "Bearer " + token}, headers={"Authorization": "Bearer " + token},
) )
print(response.text) dprint(response.text)
assert response.status_code == 200, "Submit report failed" assert response.status_code == 200, "Submit report failed"
print("Submit report successful") gprint("test_submit_report successful")
# Test function to get a weekly report # Test function to get a weekly report
def test_get_weekly_report(): def test_get_weekly_report():
@ -124,8 +132,9 @@ def test_get_weekly_report():
headers={"Authorization": "Bearer " + token}, headers={"Authorization": "Bearer " + token},
params={"username": username, "projectName": projectName, "week": 1}, params={"username": username, "projectName": projectName, "week": 1},
) )
print(response.text) dprint(response.text)
assert response.status_code == 200, "Get weekly report failed" assert response.status_code == 200, "Get weekly report failed"
gprint("test_get_weekly_report successful")
# Tests getting a project by id # Tests getting a project by id
@ -135,8 +144,9 @@ def test_get_project():
getProjectPath + "/1", # Assumes that the project with id 1 exists getProjectPath + "/1", # Assumes that the project with id 1 exists
headers={"Authorization": "Bearer " + token}, headers={"Authorization": "Bearer " + token},
) )
print(response.text) dprint(response.text)
assert response.status_code == 200, "Get project failed" assert response.status_code == 200, "Get project failed"
gprint("test_get_project successful")
# Test function to add a user to a project # Test function to add a user to a project
@ -144,13 +154,13 @@ def test_add_user_to_project():
# Log in as a site admin # Log in as a site admin
admin_username = randomString() admin_username = randomString()
admin_password = "admin_password" admin_password = "admin_password"
print( dprint(
"Registering with username: ", admin_username, " and password: ", admin_password "Registering with username: ", admin_username, " and password: ", admin_password
) )
response = requests.post( response = requests.post(
registerPath, json={"username": admin_username, "password": admin_password} registerPath, json={"username": admin_username, "password": admin_password}
) )
print(response.text) dprint(response.text)
admin_token = login(admin_username, admin_password).json()["token"] admin_token = login(admin_username, admin_password).json()["token"]
response = requests.post( response = requests.post(
@ -158,9 +168,9 @@ def test_add_user_to_project():
json={"username": admin_username}, json={"username": admin_username},
headers={"Authorization": "Bearer " + admin_token}, headers={"Authorization": "Bearer " + admin_token},
) )
print(response.text) dprint(response.text)
assert response.status_code == 200, "Promote to site admin failed" assert response.status_code == 200, "Promote to site admin failed"
print("Admin promoted to site admin successfully") dprint("Admin promoted to site admin successfully")
# Create a new user to add to the project # Create a new user to add to the project
new_user = randomString() new_user = randomString()
@ -173,10 +183,9 @@ def test_add_user_to_project():
headers={"Authorization": "Bearer " + admin_token}, headers={"Authorization": "Bearer " + admin_token},
) )
print(response.text) dprint(response.text)
assert response.status_code == 200, "Add user to project failed" assert response.status_code == 200, "Add user to project failed"
print("Add user to project successful") gprint("test_add_user_to_project successful")
# Test function to sign a report # Test function to sign a report
def test_sign_report(): def test_sign_report():
@ -187,13 +196,13 @@ def test_sign_report():
# Register an admin # Register an admin
admin_username = randomString() admin_username = randomString()
admin_password = "admin_password2" admin_password = "admin_password2"
print( dprint(
"Registering with username: ", admin_username, " and password: ", admin_password "Registering with username: ", admin_username, " and password: ", admin_password
) )
response = requests.post( response = requests.post(
registerPath, json={"username": admin_username, "password": admin_password} registerPath, json={"username": admin_username, "password": admin_password}
) )
print(response.text) dprint(response.text)
# Log in as the admin # Log in as the admin
admin_token = login(admin_username, admin_password).json()["token"] admin_token = login(admin_username, admin_password).json()["token"]
@ -213,7 +222,7 @@ def test_sign_report():
headers={"Authorization": "Bearer " + admin_token}, headers={"Authorization": "Bearer " + admin_token},
) )
assert response.status_code == 200, "Add project manager to project failed" assert response.status_code == 200, "Add project manager to project failed"
print("Project manager added to project successfully") dprint("Project manager added to project successfully")
# Log in as the project manager # Log in as the project manager
project_manager_token = login(project_manager, "project_manager_password").json()[ project_manager_token = login(project_manager, "project_manager_password").json()[
@ -237,7 +246,7 @@ def test_sign_report():
headers={"Authorization": "Bearer " + token}, headers={"Authorization": "Bearer " + token},
) )
assert response.status_code == 200, "Submit report failed" assert response.status_code == 200, "Submit report failed"
print("Submit report successful") dprint("Submit report successful")
# Retrieve the report ID # Retrieve the report ID
response = requests.get( response = requests.get(
@ -245,7 +254,7 @@ def test_sign_report():
headers={"Authorization": "Bearer " + token}, headers={"Authorization": "Bearer " + token},
params={"username": username, "projectName": projectName, "week": 1}, params={"username": username, "projectName": projectName, "week": 1},
) )
print(response.text) dprint(response.text)
report_id = response.json()["reportId"] report_id = response.json()["reportId"]
# Sign the report as the project manager # Sign the report as the project manager
@ -255,7 +264,7 @@ def test_sign_report():
headers={"Authorization": "Bearer " + project_manager_token}, headers={"Authorization": "Bearer " + project_manager_token},
) )
assert response.status_code == 200, "Sign report failed" assert response.status_code == 200, "Sign report failed"
print("Sign report successful") dprint("Sign report successful")
# Retrieve the report ID again for confirmation # Retrieve the report ID again for confirmation
response = requests.get( response = requests.get(
@ -263,7 +272,8 @@ def test_sign_report():
headers={"Authorization": "Bearer " + token}, headers={"Authorization": "Bearer " + token},
params={"username": username, "projectName": projectName, "week": 1}, params={"username": username, "projectName": projectName, "week": 1},
) )
print(response.text) dprint(response.text)
gprint("test_sign_report successful")
if __name__ == "__main__": if __name__ == "__main__":