diff --git a/.env b/.env index ed56af1..51e9b24 100644 --- a/.env +++ b/.env @@ -18,6 +18,6 @@ DJANGO_SUPERUSER_USERNAME=admin DJANGO_SUPERUSER_EMAIL=admin@mail.com -LOCUST_USER_PASSWORD=locustpass -LOCUST_USER_USERNAME=locustuser +LOCUST_USER_PASSWORD=xalami123 +LOCUST_USER_USERNAME=koko LOCUST_USER_EMAIL=locustuser@mail.com diff --git a/backend/requirements.txt b/backend/requirements.txt index 346da44..98aeebd 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -32,3 +32,5 @@ urllib3==2.2.2 validators==0.33.0 win32-setctime==1.1.0 djangorestframework-simplejwt==5.3.1 +locust==2.33.2 +python-dotenv==1.0.0 \ No newline at end of file diff --git a/backend/tests/locustfile.py b/backend/tests/locustfile.py new file mode 100644 index 0000000..a730c65 --- /dev/null +++ b/backend/tests/locustfile.py @@ -0,0 +1,45 @@ +import os +from locust import HttpUser, task, between +from datetime import datetime, timezone +from dotenv import load_dotenv +load_dotenv() # load variables from .env file when running locust command locally + +class SecFitUser(HttpUser): + host = "http://tdt4242-23.idi.ntnu.no" # Match Django backend + wait_time = between(10, 10.5) + token = None # Store authentication token + exercise_url = None # Store existing exercise URL + + + workout_num = "1" + + def on_start(self): + + response = self.client.post("/api/token/", json={"username": os.environ.get("LOCUST_USER_USERNAME") , "password": os.environ.get("LOCUST_USER_PASSWORD")}) + if response.status_code == 200: + self.token = response.json().get("access") + if self.token: + self.client.headers.update({"Authorization": f"Bearer {self.token}"}) + else: + print("\nFailed to retrieve token from response.") + else: + print("\nFailed to authenticate. Response:", response.json()) + + @task + def update_workout(self): + """Updates the first workout""" + new_date = datetime.now(timezone.utc).isoformat() + workout = { + "url": self.host + "/api/workouts/" + self.workout_num + "/", + "name": "My Great Workout", + "date": new_date, + "notes": "Great workout!", + "exercise_instances": [] + } + + headers = {"Authorization": f"Bearer {self.token}"} if hasattr(self, "token") else {} + workout_response = self.client.put("/api/workouts/" + self.workout_num + "/", json=workout, headers=headers) + if workout_response.status_code == 200: + print("\nUpdate OK") + else: + print("\nError when updating workout")