-
Notifications
You must be signed in to change notification settings - Fork 3
/
locustfile.py
78 lines (62 loc) · 2.07 KB
/
locustfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from bs4 import BeautifulSoup
from locust import HttpUser, between, task
class AnonymousUser(HttpUser):
"""Test key endpoints"""
wait_time = between(1, 5)
@task
def landing_page(self):
"""Landing page tests"""
self.client.get("/")
@task
def about(self):
"""About page"""
self.client.get("/about/")
class LoggedInUser(HttpUser):
"""Authenticated user tests"""
wait_time = between(1, 5)
def on_start(self):
"""Log into the application"""
# First get the login page to retrieve the CSRF token
response = self.client.get("/account/login/")
csrf_token = self._extract_csrf_token(response.text)
# Post request with CSRF token included
response = self.client.post(
"/account/login/",
{
"login": self.username,
"password": self.password,
"csrfmiddlewaretoken": csrf_token,
},
headers={
"X-CSRFToken": csrf_token,
"Referer": f"{self.host}/account/login/",
},
)
@task
def subscriptions(self):
"""Subscriptions page."""
self.client.get("/subscriptions/")
@task
def episodes(self):
"""Episodes page."""
self.client.get("/new/")
@task
def discover(self):
"""Discover page."""
self.client.get("/discover/")
@task
def search(self):
"""Search page."""
self.client.get("/search/", params={"search": "python"})
def _extract_csrf_token(self, content) -> str:
soup = self._soup(content)
"""Extract the CSRF token from the response body"""
# Logic to extract the CSRF token from the response body
value = ""
if input := soup.find("input", {"name": "csrfmiddlewaretoken"}):
value = input.get("value", "") or ""
if isinstance(value, list):
value = value[0]
return value
def _soup(self, content: str) -> BeautifulSoup:
return BeautifulSoup(content, "html.parser")