diff --git a/source/tests/base_user_types.py b/source/tests/base_user_types.py index 2c4a9a6..d4b104f 100644 --- a/source/tests/base_user_types.py +++ b/source/tests/base_user_types.py @@ -202,11 +202,11 @@ def power_user_task(self): logger.info("Opening project at URL %s", self.project_url) self.client.get(self.project_url) - # TODO: create JupyterLab app + # TODO: create JupyterLab app # TODO: open the app - # TODO: delete the app + # TODO: delete the app # Delete the project self.delete_project() @@ -279,6 +279,64 @@ def delete_project(self): # logger.debug(response.content) response.failure("Delete project failed. Response URL does not contain /projects.") + def create_project(self, project_name): + # Update the csrf token + self.get_token("/projects/create?template=Default project") + + project_data = dict( + name=project_name, + template_id=1, + description="Project desc", + csrfmiddlewaretoken=self.csrftoken, + ) + + with self.client.post( + url="/projects/create/?template=Default%20project", + data=project_data, + headers={"Referer": "foo"}, + name="---CREATE-NEW-PROJECT", + catch_response=True, + ) as response: + logger.debug("create project response.status_code = %s, %s", response.status_code, response.reason) + # If succeeds then url = /projects// + logger.debug("create project response.url = %s", response.url) + if project_name in response.url: + logger.info("Successfully created project %s", project_name) + self.project_url = response.url + else: + logger.warning(f"Create project failed. Response URL {response.url} does not contain project name {project_name}") + #logger.debug(response.content) + response.failure("Create project failed. Response URL does not contain project name.") + + def delete_project(self): + # Update the csrf token + self.get_token("/projects") + + delete_project_url = f"{self.project_url}delete/" # The project_url already contains a trailing slash + logger.info("Deleting the project at URL: %s", delete_project_url) + + delete_project_data = dict(csrfmiddlewaretoken=self.csrftoken) + + with self.client.get( + url=delete_project_url, + data=delete_project_data, + headers={"Referer": "foo"}, + name="---DELETE-PROJECT", + catch_response=True, + ) as response: + logger.debug("delete project response.status_code = %s, %s", response.status_code, response.reason) + # If succeeds then status_code == 200 and url = /projects/ + logger.debug("delete project response.url = %s", response.url) + if response.status_code == 200 and "/projects" in response.url: + logger.info("Successfully deleted project at %s", self.project_url) + else: + logger.warning( + f"Delete project failed for project {self.project_url}. \ + Response status not 200 or URL does not contain /projects." + ) + # logger.debug(response.content) + response.failure("Delete project failed. Response URL does not contain /projects.") + def login(self): logger.info("Login as user %s", self.username) @@ -365,6 +423,49 @@ def open_user_app(self): self.client.get(APP_SHINYPROXY, name="user-app-shiny-proxy") +class AppViewerUser(HttpUser): + """ Base class for API client system that makes API calls. """ + abstract = True + + user_type = None + + def on_start(self): + """Called when a User starts running.""" + self.client.verify = False # Don't to check if certificate is valid + + # Tasks + + @task + def open_user_app(self): + """Note that this approach does not actually create any resources on the k8s cluster.""" + print(f"executing task open_user_app, running on host: {self.host}") + APP_SHINYPROXY = "UNSET" + + if self.host == "https://serve-dev.scilifelab.se": + # Dev + # ex: https://loadtest-shinyproxy.staging.serve-dev.scilifelab.se/app/loadtest-shinyproxy + # from host: https://staging.serve-dev.scilifelab.se + APP_SHINYPROXY = self.host.replace("https://", "https://loadtest-shinyproxy.") + APP_SHINYPROXY += "/app/loadtest-shinyproxy" + + elif "staging" in self.host: + # Staging + # ex: https://loadtest-shinyproxy.serve-staging.serve-dev.scilifelab.se/app/loadtest-shinyproxy + # from host: https://serve-staging.serve-dev.scilifelab.se + APP_SHINYPROXY = self.host.replace("https://", "https://loadtest-shinyproxy.") + APP_SHINYPROXY += "/app/loadtest-shinyproxy" + + elif "serve.scilifelab.se" in self.host: + # Production + # ex: https://adhd-medication-sweden.serve.scilifelab.se/app/adhd-medication-sweden + APP_SHINYPROXY = self.host.replace("https://", "https://adhd-medication-sweden.") + APP_SHINYPROXY += "/app/adhd-medication-sweden" + + logger.debug("making GET request to user app URL: %s", APP_SHINYPROXY) + + self.client.get(APP_SHINYPROXY, name="user-app-shiny-proxy") + + class OpenAPIClientBaseUser(HttpUser): """Base class for API client system that makes API calls."""