diff --git a/backend/locust_athlete.py b/backend/locust_athlete.py new file mode 100644 index 0000000..93a8c45 --- /dev/null +++ b/backend/locust_athlete.py @@ -0,0 +1,54 @@ +from locust import HttpUser, task, between + +class SecFitUser(HttpUser): + host = "http://tdt4242-18.idi.ntnu.no:21817" # URL of the Django backend + wait_time = between(1, 3) # Users will wait 1 to 3 seconds between tasks + token = None # To store the authentication token + exercise_url = None # To store the URL of an existing exercise + + def on_start(self): + """Authenticate and prepare before sending requests.""" + # Authentication request + response = self.client.post("/api/token/", json={ + "username": "test-athlete", # Replace with actual username + "password": "test-athlete-password" # Replace with actual password + }) + + # If authentication succeeds + if response.status_code == 200: + self.token = response.json().get("access") # Store the token + print("\nAuthenticated! Token:", self.token) + + # Fetch an existing exercise using the token + headers = {"Authorization": f"Bearer {self.token}"} + exercise_response = self.client.get("/api/exercises/", headers=headers) + if exercise_response.status_code == 200 and len(exercise_response.json()) > 0: + self.exercise_url = exercise_response.json()[0]["url"] # Get the first exercise URL + print("\nUsing existing exercise:", self.exercise_url) + else: + print("\nNo exercises found! Ensure you have exercises in the database.") + else: + print("\nFailed to authenticate. Response:", response.json()) + + + + @task + def add_exercise(self): + """Send a POST request to create a new exercise.""" + if self.token: + headers = {"Authorization": f"Bearer {self.token}"} + exercise_data = { + "name": "Push-ups", # Replace with the desired exercise name + "description": "Upper body strength exercise", # Replace with a meaningful description + "unit": "repetitions" # Replace with the measurement unit (e.g., repetitions, minutes, etc.) + } + response = self.client.post("/api/exercises/", headers=headers, json=exercise_data) + + if response.status_code == 201: + print("\nSuccessfully added exercise:", exercise_data["name"]) + else: + print("\nFailed to add exercise. Response:", response.status_code, response.json()) + else: + print("\nSkipping exercise creation. No valid token available.") + + \ No newline at end of file