|
| 1 | +import logging |
| 2 | +from locust import HttpUser, task, between, events |
| 3 | + |
| 4 | + |
| 5 | +def _get_product_json(response_json, page_type): |
| 6 | + logging.info(f'User was displayed {response_json["count"]} {page_type}(s)') |
| 7 | + pages = response_json["results"][:5] |
| 8 | + return [page["readable_id"] for page in pages] |
| 9 | + |
| 10 | + |
| 11 | +class DjangoAdminUser(HttpUser): |
| 12 | + wait_time = between(1, 5) |
| 13 | + |
| 14 | + def on_start(self): |
| 15 | + self.client.get("/admin/login/") |
| 16 | + self.login() |
| 17 | + |
| 18 | + def login(self): |
| 19 | + csrf_token = self.client.cookies['csrftoken'] |
| 20 | + self.client.post( |
| 21 | + "/admin/login/", |
| 22 | + { |
| 23 | + |
| 24 | + "password": "edx", |
| 25 | + "csrfmiddlewaretoken": csrf_token, |
| 26 | + }, |
| 27 | + headers={"Referer": "/admin/login/"}, |
| 28 | + ) |
| 29 | + |
| 30 | + @task |
| 31 | + def navigate_through_course_areas(self): |
| 32 | + logging.info(f"{self} has started to surf") |
| 33 | + self._navigate_to_home_page() |
| 34 | + programs, courses = self._navigate_to_catalog() |
| 35 | + self._navigate_to_product_page("program", programs) |
| 36 | + self._navigate_to_product_page("course", courses) |
| 37 | + self._navigate_to_checkout_page() |
| 38 | + logging.info(f"{self} has gone off to learn") |
| 39 | + |
| 40 | + def _navigate_to_catalog(self): |
| 41 | + endpoints = [ |
| 42 | + "/api/users/me", |
| 43 | + "/api/departments/", |
| 44 | + "/api/users/me", |
| 45 | + ] |
| 46 | + logging.info(f"User {self} opening catalog") |
| 47 | + self._navigate_to_endpoints(endpoints) |
| 48 | + with self.client.get("/api/programs/?page=1&live=true", catch_response=True) as response: |
| 49 | + program_json = _get_product_json(response.json(), "program") |
| 50 | + with self.client.get("/api/courses/?page=1&live=true&page__live=true&courserun_is_enrollable=true", |
| 51 | + catch_response=True) as response: |
| 52 | + course_json = _get_product_json(response.json(), "course") |
| 53 | + return program_json, course_json |
| 54 | + |
| 55 | + def _navigate_to_home_page(self): |
| 56 | + logging.info("User (%r) opening Home Page") |
| 57 | + with self.client.get("/api/users/me", catch_response=True) as response: |
| 58 | + logging.info(response.status_code) |
| 59 | + |
| 60 | + def _navigate_to_checkout_page(self): |
| 61 | + logging.info("User (%r) opening Checkout Page") |
| 62 | + endpoints = [ |
| 63 | + "/api/users/me", |
| 64 | + "/api/checkout/cart", |
| 65 | + ] |
| 66 | + self._navigate_to_endpoints(endpoints) |
| 67 | + |
| 68 | + def _navigate_to_product_page(self, product_type, response_json): |
| 69 | + logging.info(response_json) |
| 70 | + for page in response_json: |
| 71 | + logging.info(f"User opened {product_type} page for {page}") |
| 72 | + endpoints = [ |
| 73 | + "/api/users/me", |
| 74 | + "/api/program_enrollments/", |
| 75 | + "/api/users/me", |
| 76 | + f"/api/course_runs/?relevant_to={page}", |
| 77 | + ] |
| 78 | + if product_type == "course": |
| 79 | + endpoints += [ |
| 80 | + f"/api/courses/?readable_id={page}&live=true", |
| 81 | + ] |
| 82 | + elif product_type == "program": |
| 83 | + endpoints += [ |
| 84 | + f"/api/programs/?readable_id={page}", |
| 85 | + ] |
| 86 | + self._navigate_to_endpoints(endpoints) |
| 87 | + |
| 88 | + def _navigate_to_endpoints(self, endpoints): |
| 89 | + for endpoint in endpoints: |
| 90 | + logging.info(f"endpoint: {endpoint}") |
| 91 | + with self.client.get(endpoint, catch_response=True) as response: |
| 92 | + logging.info(f"status: {response.status_code}") |
0 commit comments