2024-03-17 01:55:24 +01:00
|
|
|
import requests
|
|
|
|
import string
|
|
|
|
import random
|
|
|
|
|
2024-03-17 03:39:31 +01:00
|
|
|
|
2024-03-17 01:55:24 +01:00
|
|
|
def randomString(len=10):
|
2024-03-17 03:39:31 +01:00
|
|
|
"""Generate a random string of fixed length"""
|
2024-03-17 01:55:24 +01:00
|
|
|
letters = string.ascii_lowercase
|
2024-03-17 03:39:31 +01:00
|
|
|
return "".join(random.choice(letters) for i in range(len))
|
|
|
|
|
2024-03-17 01:55:24 +01:00
|
|
|
|
|
|
|
# Defined once per test run
|
|
|
|
username = randomString()
|
2024-03-17 04:16:54 +01:00
|
|
|
projectName = randomString()
|
2024-03-17 01:55:24 +01:00
|
|
|
|
2024-03-17 03:39:31 +01:00
|
|
|
# The base URL of the API
|
2024-03-17 01:55:24 +01:00
|
|
|
base_url = "http://localhost:8080"
|
|
|
|
|
2024-03-17 03:39:31 +01:00
|
|
|
# Endpoint to test
|
2024-03-17 01:55:24 +01:00
|
|
|
registerPath = base_url + "/api/register"
|
|
|
|
loginPath = base_url + "/api/login"
|
2024-03-17 03:39:31 +01:00
|
|
|
addProjectPath = base_url + "/api/project"
|
2024-03-17 04:16:54 +01:00
|
|
|
submitReportPath = base_url + "/api/submitReport"
|
2024-03-17 22:57:19 +01:00
|
|
|
getWeeklyReportPath = base_url + "/api/getWeeklyReport"
|
2024-03-18 16:42:35 +01:00
|
|
|
getProjectPath = base_url + "/api/project"
|
2024-03-17 03:39:31 +01:00
|
|
|
|
2024-03-17 01:55:24 +01:00
|
|
|
|
2024-03-17 03:46:16 +01:00
|
|
|
# Posts the username and password to the register endpoint
|
2024-03-17 03:39:31 +01:00
|
|
|
def register(username: string, password: string):
|
|
|
|
print("Registering with username: ", username, " and password: ", password)
|
|
|
|
response = requests.post(
|
|
|
|
registerPath, json={"username": username, "password": password}
|
|
|
|
)
|
|
|
|
print(response.text)
|
2024-03-17 01:55:24 +01:00
|
|
|
return response
|
|
|
|
|
2024-03-17 03:39:31 +01:00
|
|
|
|
2024-03-17 03:46:16 +01:00
|
|
|
# Posts the username and password to the login endpoint
|
2024-03-17 03:39:31 +01:00
|
|
|
def login(username: string, password: string):
|
|
|
|
print("Logging in with username: ", username, " and password: ", password)
|
|
|
|
response = requests.post(
|
|
|
|
loginPath, json={"username": username, "password": password}
|
|
|
|
)
|
|
|
|
print(response.text)
|
2024-03-17 01:55:24 +01:00
|
|
|
return response
|
|
|
|
|
2024-03-17 03:39:31 +01:00
|
|
|
|
2024-03-17 01:55:24 +01:00
|
|
|
def test_login():
|
|
|
|
response = login(username, "always_same")
|
|
|
|
assert response.status_code == 200, "Login failed"
|
|
|
|
print("Login successful")
|
2024-03-17 03:39:31 +01:00
|
|
|
return response.json()["token"]
|
|
|
|
|
2024-03-17 01:55:24 +01:00
|
|
|
|
|
|
|
def test_create_user():
|
2024-03-17 03:39:31 +01:00
|
|
|
response = register(username, "always_same")
|
2024-03-17 01:55:24 +01:00
|
|
|
assert response.status_code == 200, "Registration failed"
|
|
|
|
print("Registration successful")
|
|
|
|
|
2024-03-17 03:39:31 +01:00
|
|
|
|
|
|
|
def test_add_project():
|
|
|
|
loginResponse = login(username, "always_same")
|
|
|
|
token = loginResponse.json()["token"]
|
|
|
|
response = requests.post(
|
|
|
|
addProjectPath,
|
|
|
|
json={"name": projectName, "description": "This is a project"},
|
|
|
|
headers={"Authorization": "Bearer " + token},
|
|
|
|
)
|
|
|
|
print(response.text)
|
|
|
|
assert response.status_code == 200, "Add project failed"
|
|
|
|
print("Add project successful")
|
|
|
|
|
|
|
|
|
2024-03-17 04:16:54 +01:00
|
|
|
def test_submit_report():
|
|
|
|
token = login(username, "always_same").json()["token"]
|
|
|
|
response = requests.post(
|
|
|
|
submitReportPath,
|
|
|
|
json={
|
2024-03-17 23:17:06 +01:00
|
|
|
"projectName": projectName,
|
2024-03-17 04:16:54 +01:00
|
|
|
"week": 1,
|
|
|
|
"developmentTime": 10,
|
|
|
|
"meetingTime": 5,
|
|
|
|
"adminTime": 5,
|
|
|
|
"ownWorkTime": 10,
|
|
|
|
"studyTime": 10,
|
|
|
|
"testingTime": 10,
|
|
|
|
},
|
|
|
|
headers={"Authorization": "Bearer " + token},
|
|
|
|
)
|
|
|
|
print(response.text)
|
|
|
|
assert response.status_code == 200, "Submit report failed"
|
|
|
|
print("Submit report successful")
|
|
|
|
|
2024-03-17 22:57:19 +01:00
|
|
|
def test_get_weekly_report():
|
|
|
|
token = login(username, "always_same").json()["token"]
|
|
|
|
response = requests.get(
|
|
|
|
getWeeklyReportPath,
|
|
|
|
headers={"Authorization": "Bearer " + token},
|
2024-03-17 23:17:06 +01:00
|
|
|
params={"username": username, "projectName": projectName , "week": 1}
|
2024-03-17 22:57:19 +01:00
|
|
|
)
|
|
|
|
print(response.text)
|
2024-03-18 16:42:35 +01:00
|
|
|
assert response.status_code == 200, "Get weekly report failed"
|
|
|
|
|
|
|
|
# Tests getting a project by id
|
|
|
|
def test_get_project():
|
|
|
|
token = login(username, "always_same").json()["token"]
|
|
|
|
response = requests.get(
|
|
|
|
getProjectPath + "/1", # Assumes that the project with id 1 exists
|
|
|
|
headers={"Authorization": "Bearer " + token},
|
|
|
|
)
|
|
|
|
print(response.text)
|
|
|
|
assert response.status_code == 200, "Get project failed"
|
2024-03-17 04:16:54 +01:00
|
|
|
|
2024-03-17 01:55:24 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_create_user()
|
|
|
|
test_login()
|
2024-03-17 03:39:31 +01:00
|
|
|
test_add_project()
|
2024-03-17 04:16:54 +01:00
|
|
|
test_submit_report()
|
2024-03-18 16:42:35 +01:00
|
|
|
test_get_weekly_report()
|
|
|
|
test_get_project()
|