diff mbox series

toaster/tests/functional: Improve project creation tests

Message ID 20241023104434.3371528-1-richard.purdie@linuxfoundation.org
State New
Headers show
Series toaster/tests/functional: Improve project creation tests | expand

Commit Message

Richard Purdie Oct. 23, 2024, 10:44 a.m. UTC
Mixing database access and access via a running server is fraught with
danger and problems. The "django_db" marker means the transactions are
dropped at the end of the test but the transactions made via the webapi
remain so the database ends up confused at best.

Drop the database accesses and use the server API. This means slightly
abusing the typeahead to get lists of projects in the database.

Add code to delete a project if it already exists. This allows tests
to re-run against an existing database. Deletion is done using the
server API but this means handling CSRF tokens.

Add requests module requirement to requirements file since the project
creation code now uses requests.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 .../functional/test_create_new_project.py     | 28 +++++++++++++------
 .../tests/toaster-tests-requirements.txt      |  2 ++
 2 files changed, 22 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/lib/toaster/tests/functional/test_create_new_project.py b/lib/toaster/tests/functional/test_create_new_project.py
index f7d17847ae..577ca7239d 100644
--- a/lib/toaster/tests/functional/test_create_new_project.py
+++ b/lib/toaster/tests/functional/test_create_new_project.py
@@ -8,15 +8,12 @@ 
 
 import re
 import pytest
+import requests
 from django.urls import reverse
 from selenium.webdriver.support.select import Select
 from tests.functional.functional_helpers import SeleniumFunctionalTestCase
-from orm.models import Project
 from selenium.webdriver.common.by import By
 
-
-@pytest.mark.django_db
-@pytest.mark.order("last")
 class TestCreateNewProject(SeleniumFunctionalTestCase):
 
     def _create_test_new_project(
@@ -31,6 +28,20 @@  class TestCreateNewProject(SeleniumFunctionalTestCase):
           - Release: Any string
           - Merge Toaster settings: True or False
         """
+
+        # Obtain a CSRF token from a suitable URL
+        projs = requests.get(self.live_server_url + reverse('newproject'))
+        csrftoken = projs.cookies.get('csrftoken')
+
+        # Use the projects typeahead to find out if the project already exists
+        req = requests.get(self.live_server_url + reverse('xhr_projectstypeahead'), {'search': project_name, 'format' : 'json'})
+        data = req.json()
+        # Delete any existing projects
+        for result in data['results']:
+            del_url = reverse('xhr_project', args=(result['id'],))
+            del_response = requests.delete(self.live_server_url + del_url, cookies={'csrftoken': csrftoken}, headers={'X-CSRFToken': csrftoken})
+            self.assertEqual(del_response.status_code, 200)
+
         self.get(reverse('newproject'))
         self.wait_until_visible('#new-project-name', poll=3)
         self.driver.find_element(By.ID,
@@ -59,10 +70,11 @@  class TestCreateNewProject(SeleniumFunctionalTestCase):
             project_name in element.text,
             f"New project name:{project_name} not in new project notification"
         )
-        self.assertTrue(
-            Project.objects.filter(name=project_name).count(),
-            f"New project:{project_name} not found in database"
-        )
+
+        # Use the projects typeahead again to check the project now exists
+        req = requests.get(self.live_server_url + reverse('xhr_projectstypeahead'), {'search': project_name, 'format' : 'json'})
+        data = req.json()
+        self.assertGreater(len(data['results']), 0, f"New project:{project_name} not found in database")
 
         # check release
         self.assertTrue(re.search(
diff --git a/lib/toaster/tests/toaster-tests-requirements.txt b/lib/toaster/tests/toaster-tests-requirements.txt
index 71cc083436..6243c00a36 100644
--- a/lib/toaster/tests/toaster-tests-requirements.txt
+++ b/lib/toaster/tests/toaster-tests-requirements.txt
@@ -5,3 +5,5 @@  pytest-env==1.1.0
 pytest-html==4.0.2
 pytest-metadata==3.0.0
 pytest-order==1.1.0
+requests
+