TTime/testing.py

48 lines
1.3 KiB
Python
Raw Normal View History

2024-03-17 01:55:24 +01:00
import requests
import string
import random
def randomString(len=10):
"""Generate a random string of fixed length """
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(len))
# Defined once per test run
username = randomString()
# Define the base URL of the API
base_url = "http://localhost:8080"
# Define the endpoint to test
registerPath = base_url + "/api/register"
loginPath = base_url + "/api/login"
# Define a function to prform POST request with data and return response
def create_user(data):
response = requests.post(registerPath, json=data)
return response
def login(username, password):
response = requests.post(loginPath, json={"username": username, "password": password})
return response
def test_login():
response = login(username, "always_same")
assert response.status_code == 200, "Login failed"
print("Login successful")
print(response.json()["token"])
# Define a function to test the POST request
def test_create_user():
data = {"username": username, "password": "always_same"}
response = create_user(data)
assert response.status_code == 200, "Registration failed"
print("Registration successful")
# Run the tests
if __name__ == "__main__":
# test_get_users()
test_create_user()
test_login()