forked from mathialm/secfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from locust import HttpUser, task, between | ||
|
|
||
| class SecFitUser(HttpUser): | ||
| host = "http://tdt4242-05.idi.ntnu.no:20027" # Replace with your actual backend URL | ||
| wait_time = between(1, 3) | ||
|
|
||
| token = None # Store authentication token | ||
| exercise_url = None # Store an existing exercise URL | ||
|
|
||
| def on_start(self): | ||
| """ Log in and fetch a valid exercise URL when a Locust user starts """ | ||
| response = self.client.post("/api/token/", json={ | ||
| "username": "locustuser", | ||
| "password": "locustpass" | ||
| }) | ||
|
|
||
| if response.status_code == 200: | ||
| self.token = response.json().get("access") # Store JWT token | ||
| print("\nAuthenticated! Token:", self.token) | ||
|
|
||
| # Fetch an existing exercise | ||
| 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 first available exercise | ||
| 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 fetch_exercise_data(self): | ||
| """ Simulate fetching an exercise """ | ||
| if self.exercise_url: | ||
| headers = {"Authorization": f"Bearer {self.token}"} | ||
| self.client.get(self.exercise_url, headers=headers) | ||
|
|
||
| @task | ||
| def fetch_user_profile(self): | ||
| """ Simulate fetching user profile data """ | ||
| headers = {"Authorization": f"Bearer {self.token}"} | ||
| self.client.get("/api/profile/", headers=headers) |