Resolve testing.py

This commit is contained in:
Imbus 2024-03-18 23:43:14 +01:00
commit 71caf37642

View file

@ -28,6 +28,23 @@ addUserToProjectPath = base_url + "/api/addUserToProject"
promoteToAdminPath = base_url + "/api/promoteToAdmin" promoteToAdminPath = base_url + "/api/promoteToAdmin"
getUserProjectsPath = base_url + "/api/getUserProjects" getUserProjectsPath = base_url + "/api/getUserProjects"
def test_get_user_projects():
print("Testing get user projects")
loginResponse = login("user2", "123")
# Check if the user is added to the project
response = requests.get(
getUserProjectsPath,
json={"username": "user2"},
headers={"Authorization": "Bearer " + loginResponse.json()["token"]},
)
print(response.text)
print(response.json())
assert response.status_code == 200, "Get user projects failed"
print("got user projects successfully")
# 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) print("Registering with username: ", username, " and password: ", password)
@ -47,6 +64,7 @@ def login(username: string, password: string):
print(response.text) print(response.text)
return response return response
# Test function to login # Test function to login
def test_login(): def test_login():
response = login(username, "always_same") response = login(username, "always_same")
@ -54,12 +72,14 @@ def test_login():
print("Login successful") print("Login successful")
return response.json()["token"] return response.json()["token"]
# Test function to create a new user # Test function to create a new user
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") print("Registration successful")
# Test function to add a project # Test function to add a project
def test_add_project(): def test_add_project():
loginResponse = login(username, "always_same") loginResponse = login(username, "always_same")
@ -73,6 +93,7 @@ def test_add_project():
assert response.status_code == 200, "Add project failed" assert response.status_code == 200, "Add project failed"
print("Add project successful") print("Add project successful")
# Test function to submit a report # Test function to submit a report
def test_submit_report(): def test_submit_report():
token = login(username, "always_same").json()["token"] token = login(username, "always_same").json()["token"]
@ -94,17 +115,19 @@ def test_submit_report():
assert response.status_code == 200, "Submit report failed" assert response.status_code == 200, "Submit report failed"
print("Submit report successful") print("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():
token = login(username, "always_same").json()["token"] token = login(username, "always_same").json()["token"]
response = requests.get( response = requests.get(
getWeeklyReportPath, getWeeklyReportPath,
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) print(response.text)
assert response.status_code == 200, "Get weekly report failed" assert response.status_code == 200, "Get weekly report failed"
# Tests getting a project by id # Tests getting a project by id
def test_get_project(): def test_get_project():
token = login(username, "always_same").json()["token"] token = login(username, "always_same").json()["token"]
@ -115,19 +138,26 @@ def test_get_project():
print(response.text) print(response.text)
assert response.status_code == 200, "Get project failed" assert response.status_code == 200, "Get project failed"
# Test function to add a user to a project # Test function to add a user to a project
def test_add_user_to_project(): 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("Registering with username: ", admin_username, " and password: ", admin_password) print(
"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) print(response.text)
admin_token = login(admin_username, admin_password).json()["token"] admin_token = login(admin_username, admin_password).json()["token"]
response = requests.post(promoteToAdminPath, json={"username": admin_username}, headers={"Authorization": "Bearer " + admin_token}) response = requests.post(
promoteToAdminPath,
json={"username": admin_username},
headers={"Authorization": "Bearer " + admin_token},
)
print(response.text) print(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") print("Admin promoted to site admin successfully")
@ -147,15 +177,6 @@ def test_add_user_to_project():
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") print("Add user to project successful")
# Check if the user is added to the project
response = requests.get(
getUserProjectsPath,
json={"username": new_user},
headers={"Authorization": "Bearer " + admin_token},
)
print(response.text)
assert response.status_code == 200, "Get user projects failed"
print("got user projects successfully")
# Test function to sign a report # Test function to sign a report
def test_sign_report(): def test_sign_report():
@ -166,7 +187,9 @@ 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("Registering with username: ", admin_username, " and password: ", admin_password) print(
"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}
) )
@ -174,18 +197,28 @@ def test_sign_report():
# 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"]
response = requests.post(promoteToAdminPath, json={"username": admin_username}, headers={"Authorization": "Bearer " + admin_token}) response = requests.post(
promoteToAdminPath,
json={"username": admin_username},
headers={"Authorization": "Bearer " + admin_token},
)
response = requests.put( response = requests.put(
addUserToProjectPath, addUserToProjectPath,
json={"projectName": projectName, "username": project_manager, "role": "project_manager"}, json={
"projectName": projectName,
"username": project_manager,
"role": "project_manager",
},
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") print("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()["token"] project_manager_token = login(project_manager, "project_manager_password").json()[
"token"
]
# Submit a report for the project # Submit a report for the project
token = login(username, "always_same").json()["token"] token = login(username, "always_same").json()["token"]
@ -210,7 +243,7 @@ def test_sign_report():
response = requests.get( response = requests.get(
getWeeklyReportPath, getWeeklyReportPath,
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) print(response.text)
report_id = response.json()["reportId"] report_id = response.json()["reportId"]
@ -228,12 +261,13 @@ def test_sign_report():
response = requests.get( response = requests.get(
getWeeklyReportPath, getWeeklyReportPath,
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) print(response.text)
if __name__ == "__main__": if __name__ == "__main__":
test_get_user_projects()
test_create_user() test_create_user()
test_login() test_login()
test_add_project() test_add_project()