toaster/tests/functional: Improve project creation tests
PATCH 1/1 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.
Signed-off-by Richard Purdie <[email protected]>
--- .../functional/test_create_new_project.py | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-)
lib/toaster/tests/functional/test_create_new_project.py +22 -8
@@ -6,17 +6,16 @@ # SPDX-License-Identifier: GPL-2.0-only # -import re +import json import pytest +import re +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 - [email protected]_db [email protected]("last") class TestCreateNewProject(SeleniumFunctionalTestCase): def _create_test_new_project(
@@ -31,6 +30,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 +72,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(