77"""
88
99from django .test import TestCase
10+ from django .contrib .staticfiles .testing import StaticLiveServerTestCase
1011from django .contrib .auth import get_user_model
1112from helium import (
1213 start_chrome ,
2223User = get_user_model ()
2324
2425
25- def get_work_from_api ():
26+ def get_work_from_api (base_url ):
2627 """
2728 Helper function to get a work (id, doi) from the API instead of database.
2829 Returns the first published work found, preferring works with DOI.
2930 Returns identifier field that can be used in URLs (either DOI or ID).
31+
32+ Args:
33+ base_url: The base URL of the test server (e.g., self.live_server_url)
3034 """
31- response = requests .get ('http://localhost:8000 /api/v1/works/' , timeout = 5 )
35+ response = requests .get (f' { base_url } /api/v1/works/' , timeout = 5 )
3236 if response .status_code == 200 :
3337 data = response .json ()
3438 if data .get ('results' ) and len (data ['results' ]) > 0 :
@@ -141,11 +145,20 @@ def test_admin_panel_accessible_to_staff(self):
141145 self .assertContains (response , 'Site administration' )
142146
143147
144- class AdminButtonsBrowserTests (TestCase ):
145- """Browser-based tests for admin button visibility."""
148+ class AdminButtonsBrowserTests (StaticLiveServerTestCase ):
149+ """Browser-based tests for admin button visibility.
150+
151+ Uses StaticLiveServerTestCase to automatically start a live test server
152+ that serves both the application and static files.
153+ """
146154
147155 fixtures = ['test_data_optimap.json' ]
148156
157+ @classmethod
158+ def setUpClass (cls ):
159+ """Set up class-level resources including live server."""
160+ super ().setUpClass ()
161+
149162 def setUp (self ):
150163 """Set up test users for each test."""
151164 # Create admin user
@@ -159,13 +172,13 @@ def test_work_landing_page_anonymous_no_admin_buttons(self):
159172 """Test that work landing page doesn't show admin buttons to anonymous users."""
160173
161174 # Get work from API instead of database
162- work_data = get_work_from_api ()
175+ work_data = get_work_from_api (self . live_server_url )
163176 if not work_data or not work_data .get ('identifier' ):
164177 self .skipTest ('No works available via API' )
165178
166179 try :
167180 # Use the unified identifier (DOI or ID)
168- start_chrome (f'localhost:8000 /work/{ work_data ["identifier" ]} /' , headless = True )
181+ start_chrome (f'{ self . live_server_url } /work/{ work_data ["identifier" ]} /' , headless = True )
169182 driver = get_driver ()
170183
171184 # Wait for page to load
@@ -181,7 +194,7 @@ def test_work_landing_page_anonymous_no_admin_buttons(self):
181194 def test_contribute_page_anonymous_no_publish_buttons (self ):
182195 """Test that contribute page doesn't show publish buttons to anonymous users."""
183196 try :
184- start_chrome ('localhost:8000 /contribute/' , headless = True )
197+ start_chrome (f' { self . live_server_url } /contribute/' , headless = True )
185198 driver = get_driver ()
186199
187200 # Wait for page to load
0 commit comments