[Fix] Frontend Testing Fixtures (#249)

Co-authored-by: Tim Lorsbach <tim@lorsba.ch>
Co-authored-by: Liam Brydon <lbry121@aucklanduni.ac.nz>
Reviewed-on: enviPath/enviPy#249
This commit is contained in:
2025-12-03 10:49:23 +13:00
parent 901de4640c
commit d6440f416c
5 changed files with 214 additions and 97 deletions

View File

@ -1,108 +1,38 @@
import os
from django.conf import settings as s
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.test import tag
from playwright.sync_api import expect, sync_playwright
from playwright.sync_api import expect
from epdb.logic import UserManager
from epdb.models import User, ExternalDatabase
from .frontend_base import EnviPyStaticLiveServerTestCase
class TestHomepage(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
super().setUpClass()
cls.playwright = sync_playwright().start()
cls.browser = cls.playwright.chromium.launch()
def setUp(self):
# Create test data
s.SERVER_URL = self.live_server_url
self.anonymous = UserManager.create_user(
"anonymous",
"anon@envipath.com",
"SuperSafe",
is_active=True,
add_to_group=False,
set_setting=False,
)
databases = [
{
"name": "PubChem Compound",
"full_name": "PubChem Compound Database",
"description": "Chemical database of small organic molecules",
"base_url": "https://pubchem.ncbi.nlm.nih.gov",
"url_pattern": "https://pubchem.ncbi.nlm.nih.gov/compound/{id}",
},
{
"name": "PubChem Substance",
"full_name": "PubChem Substance Database",
"description": "Database of chemical substances",
"base_url": "https://pubchem.ncbi.nlm.nih.gov",
"url_pattern": "https://pubchem.ncbi.nlm.nih.gov/substance/{id}",
},
{
"name": "ChEBI",
"full_name": "Chemical Entities of Biological Interest",
"description": "Dictionary of molecular entities",
"base_url": "https://www.ebi.ac.uk/chebi",
"url_pattern": "https://www.ebi.ac.uk/chebi/searchId.do?chebiId=CHEBI:{id}",
},
{
"name": "RHEA",
"full_name": "RHEA Reaction Database",
"description": "Comprehensive resource of biochemical reactions",
"base_url": "https://www.rhea-db.org",
"url_pattern": "https://www.rhea-db.org/rhea/{id}",
},
{
"name": "KEGG Reaction",
"full_name": "KEGG Reaction Database",
"description": "Database of biochemical reactions",
"base_url": "https://www.genome.jp",
"url_pattern": "https://www.genome.jp/entry/{id}",
},
{
"name": "UniProt",
"full_name": "MetaCyc Metabolic Pathway Database",
"description": "UniProt is a freely accessible database of protein sequence and functional information",
"base_url": "https://www.uniprot.org",
"url_pattern": 'https://www.uniprot.org/uniprotkb?query="{id}"',
},
]
for db_info in databases:
ExternalDatabase.objects.get_or_create(name=db_info["name"], defaults=db_info)
self.username = "testuser"
self.password = "password123"
self.user = User.objects.create_user(username=self.username, password=self.password)
@classmethod
def tearDownClass(cls):
super().tearDownClass()
cls.browser.close()
cls.playwright.stop()
class TestHomepage(EnviPyStaticLiveServerTestCase):
@tag("frontend")
def test_predict(self):
page = self.login()
page.get_by_role("textbox", name="canonical SMILES string").click()
page.get_by_role("textbox", name="canonical SMILES string").fill("CCCN")
page.get_by_role("button", name="Predict!").click()
# Check that the pathway box is visible
expect(page.locator("rect")).to_be_visible(timeout=10000)
@tag("frontend")
def test_login(self):
def test_advanced_predict(self):
page = self.login()
expect(page.locator("#loggedInButton")).to_be_visible()
page.close()
page.get_by_role("link", name="Advanced").click()
# Check predict page opens correctly
expect(page.get_by_role("heading", name="Predict a Pathway in")).to_be_visible()
page.get_by_role("textbox", name="Name").click()
page.get_by_role("textbox", name="Name").fill("Test Pathway")
page.get_by_role("textbox", name="Description").click()
page.get_by_role("textbox", name="Description").fill("Test Description")
page.get_by_role("textbox", name="SMILES").click()
page.get_by_role("textbox", name="SMILES").fill("OCCCN")
page.locator("#predict-submit-button").click()
# Check that the pathway box is visible
expect(page.locator("rect")).to_be_visible(timeout=10000)
@tag("frontend")
def test_go_home(self) -> None:
page = self.login()
page.get_by_role("link").first.click()
# Check the homepage predict box is visible
expect(page.get_by_text("SMILES Draw Predict! Caffeine")).to_be_visible()
def login(self):
page = self.browser.new_page()
page.goto(self.live_server_url + "/login/")
page.get_by_role("textbox", name="Username").click()
page.get_by_role("textbox", name="Username").fill(self.username)
page.get_by_role("textbox", name="Password").click()
page.get_by_role("textbox", name="Password").fill(self.password)
page.get_by_role("button", name="Sign In").click()
return page