summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2024-10-16 23:55:04 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2024-10-24 11:24:03 +0100
commit38a29dff866604ec37f983f11b487a630cd87b56 (patch)
treebe53e877f897c7fc470e4e603fe5ae3f1c260cb0
parentdab2dcae89d349366edc3cfa09c312fab8cf14b8 (diff)
downloadpoky-38a29dff866604ec37f983f11b487a630cd87b56.tar.gz
bitbake: toaster/tests/functional: Improve project creation tests
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. (Bitbake rev: 738270c53a08ddc95400de70f3dd8c08b2940182) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--bitbake/lib/toaster/tests/functional/test_create_new_project.py28
-rw-r--r--bitbake/lib/toaster/tests/toaster-tests-requirements.txt2
2 files changed, 22 insertions, 8 deletions
diff --git a/bitbake/lib/toaster/tests/functional/test_create_new_project.py b/bitbake/lib/toaster/tests/functional/test_create_new_project.py
index f7d17847ae..577ca7239d 100644
--- a/bitbake/lib/toaster/tests/functional/test_create_new_project.py
+++ b/bitbake/lib/toaster/tests/functional/test_create_new_project.py
@@ -8,15 +8,12 @@
8 8
9import re 9import re
10import pytest 10import pytest
11import requests
11from django.urls import reverse 12from django.urls import reverse
12from selenium.webdriver.support.select import Select 13from selenium.webdriver.support.select import Select
13from tests.functional.functional_helpers import SeleniumFunctionalTestCase 14from tests.functional.functional_helpers import SeleniumFunctionalTestCase
14from orm.models import Project
15from selenium.webdriver.common.by import By 15from selenium.webdriver.common.by import By
16 16
17
18@pytest.mark.django_db
19@pytest.mark.order("last")
20class TestCreateNewProject(SeleniumFunctionalTestCase): 17class TestCreateNewProject(SeleniumFunctionalTestCase):
21 18
22 def _create_test_new_project( 19 def _create_test_new_project(
@@ -31,6 +28,20 @@ class TestCreateNewProject(SeleniumFunctionalTestCase):
31 - Release: Any string 28 - Release: Any string
32 - Merge Toaster settings: True or False 29 - Merge Toaster settings: True or False
33 """ 30 """
31
32 # Obtain a CSRF token from a suitable URL
33 projs = requests.get(self.live_server_url + reverse('newproject'))
34 csrftoken = projs.cookies.get('csrftoken')
35
36 # Use the projects typeahead to find out if the project already exists
37 req = requests.get(self.live_server_url + reverse('xhr_projectstypeahead'), {'search': project_name, 'format' : 'json'})
38 data = req.json()
39 # Delete any existing projects
40 for result in data['results']:
41 del_url = reverse('xhr_project', args=(result['id'],))
42 del_response = requests.delete(self.live_server_url + del_url, cookies={'csrftoken': csrftoken}, headers={'X-CSRFToken': csrftoken})
43 self.assertEqual(del_response.status_code, 200)
44
34 self.get(reverse('newproject')) 45 self.get(reverse('newproject'))
35 self.wait_until_visible('#new-project-name', poll=3) 46 self.wait_until_visible('#new-project-name', poll=3)
36 self.driver.find_element(By.ID, 47 self.driver.find_element(By.ID,
@@ -59,10 +70,11 @@ class TestCreateNewProject(SeleniumFunctionalTestCase):
59 project_name in element.text, 70 project_name in element.text,
60 f"New project name:{project_name} not in new project notification" 71 f"New project name:{project_name} not in new project notification"
61 ) 72 )
62 self.assertTrue( 73
63 Project.objects.filter(name=project_name).count(), 74 # Use the projects typeahead again to check the project now exists
64 f"New project:{project_name} not found in database" 75 req = requests.get(self.live_server_url + reverse('xhr_projectstypeahead'), {'search': project_name, 'format' : 'json'})
65 ) 76 data = req.json()
77 self.assertGreater(len(data['results']), 0, f"New project:{project_name} not found in database")
66 78
67 # check release 79 # check release
68 self.assertTrue(re.search( 80 self.assertTrue(re.search(
diff --git a/bitbake/lib/toaster/tests/toaster-tests-requirements.txt b/bitbake/lib/toaster/tests/toaster-tests-requirements.txt
index 71cc083436..6243c00a36 100644
--- a/bitbake/lib/toaster/tests/toaster-tests-requirements.txt
+++ b/bitbake/lib/toaster/tests/toaster-tests-requirements.txt
@@ -5,3 +5,5 @@ pytest-env==1.1.0
5pytest-html==4.0.2 5pytest-html==4.0.2
6pytest-metadata==3.0.0 6pytest-metadata==3.0.0
7pytest-order==1.1.0 7pytest-order==1.1.0
8requests
9