|
| 1 | +""" |
| 2 | +UI tests for admin-only content visibility. |
| 3 | +Tests verify that admin-only buttons and features are: |
| 4 | +1. NOT visible to anonymous users |
| 5 | +2. NOT visible to regular authenticated users |
| 6 | +3. VISIBLE to admin/staff users |
| 7 | +""" |
| 8 | + |
| 9 | +from django.test import TestCase |
| 10 | +from django.contrib.auth import get_user_model |
| 11 | +from helium import ( |
| 12 | + start_chrome, |
| 13 | + kill_browser, |
| 14 | + get_driver, |
| 15 | + Text, |
| 16 | + Button, |
| 17 | +) |
| 18 | +import requests |
| 19 | + |
| 20 | +from works.models import Work |
| 21 | + |
| 22 | +User = get_user_model() |
| 23 | + |
| 24 | + |
| 25 | +def get_work_from_api(): |
| 26 | + """Helper function to get a work (id, doi) from the API instead of database.""" |
| 27 | + response = requests.get('http://localhost:8000/api/v1/works/', timeout=5) |
| 28 | + if response.status_code == 200: |
| 29 | + data = response.json() |
| 30 | + if data.get('results') and len(data['results']) > 0: |
| 31 | + work = data['results']['features'][0] |
| 32 | + return {'id': work.get('id'), 'doi': work.get('properties').get('doi'), 'title': work.get('properties').get('title')} |
| 33 | + |
| 34 | + |
| 35 | +class AdminContentVisibilityTests(TestCase): |
| 36 | + """Test that admin-only content is properly restricted.""" |
| 37 | + |
| 38 | + fixtures = ['test_data_optimap.json'] |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def setUpClass(cls): |
| 42 | + """Create test users.""" |
| 43 | + super().setUpClass() |
| 44 | + |
| 45 | + def setUp(self): |
| 46 | + """Set up test users for each test.""" |
| 47 | + # Create admin user |
| 48 | + self.admin_user = User.objects.create_superuser( |
| 49 | + username='admin', |
| 50 | + |
| 51 | + password='adminpass123' |
| 52 | + ) |
| 53 | + |
| 54 | + # Create regular user |
| 55 | + self.regular_user = User.objects.create_user( |
| 56 | + username='regular', |
| 57 | + |
| 58 | + password='regularpass123' |
| 59 | + ) |
| 60 | + |
| 61 | + def test_work_landing_admin_buttons_not_visible_anonymous(self): |
| 62 | + """Test that admin buttons are not visible on work landing page for anonymous users.""" |
| 63 | + # Get a work from fixtures - works are loaded with specific IDs from fixture |
| 64 | + # Try a few common IDs or skip if no works exist |
| 65 | + work = Work.objects.filter(status="p",doi__isnull=False).first() |
| 66 | + if work: |
| 67 | + response = self.client.get(f'/work/{work.doi}/') |
| 68 | + if response.status_code == 200: |
| 69 | + # Admin edit button should not be present |
| 70 | + self.assertNotContains(response, 'Edit in Admin') |
| 71 | + self.assertNotContains(response, '/admin/works/work/') |
| 72 | + else: |
| 73 | + self.skipTest('No works available in test database') |
| 74 | + |
| 75 | + def test_work_landing_admin_buttons_not_visible_regular_user(self): |
| 76 | + """Test that admin buttons are not visible to regular authenticated users.""" |
| 77 | + # Login as regular user |
| 78 | + self.client.login(username='regular', password='regularpass123') |
| 79 | + |
| 80 | + work = Work.objects.filter(status="p",doi__isnull=False).first() |
| 81 | + if work: |
| 82 | + response = self.client.get(f'/work/{work.doi}/') |
| 83 | + if response.status_code == 200: |
| 84 | + # Admin edit button should not be present |
| 85 | + self.assertNotContains(response, 'Edit in Admin') |
| 86 | + self.assertNotContains(response, '/admin/works/work/') |
| 87 | + else: |
| 88 | + self.skipTest('No works available in test database') |
| 89 | + |
| 90 | + def test_work_landing_admin_buttons_visible_to_staff(self): |
| 91 | + """Test that admin buttons ARE visible to staff users.""" |
| 92 | + # Login as admin user |
| 93 | + self.client.login(username='admin', password='adminpass123') |
| 94 | + |
| 95 | + work = Work.objects.filter(status="p",doi__isnull=False).first() |
| 96 | + if work: |
| 97 | + response = self.client.get(f'/work/{work.doi}/') |
| 98 | + if response.status_code == 200: |
| 99 | + # Admin edit button should be present |
| 100 | + self.assertContains(response, 'Edit in Admin') |
| 101 | + self.assertContains(response, '/admin/works/work/') |
| 102 | + else: |
| 103 | + self.skipTest('No works available in test database') |
| 104 | + |
| 105 | + def test_admin_panel_not_accessible_anonymous(self): |
| 106 | + """Test that admin panel redirects anonymous users to login.""" |
| 107 | + response = self.client.get('/admin/') |
| 108 | + # Should redirect to login page |
| 109 | + self.assertEqual(response.status_code, 302) |
| 110 | + self.assertIn('/admin/login', response.url) |
| 111 | + |
| 112 | + def test_admin_panel_not_accessible_regular_user(self): |
| 113 | + """Test that admin panel is not accessible to regular users.""" |
| 114 | + self.client.login(username='regular', password='regularpass123') |
| 115 | + response = self.client.get('/admin/') |
| 116 | + # Should redirect to login page (regular users can't access admin) |
| 117 | + self.assertEqual(response.status_code, 302) |
| 118 | + |
| 119 | + def test_admin_panel_accessible_to_staff(self): |
| 120 | + """Test that admin panel is accessible to staff users.""" |
| 121 | + self.client.login(username='admin', password='adminpass123') |
| 122 | + response = self.client.get('/admin/') |
| 123 | + # Should show admin page |
| 124 | + self.assertEqual(response.status_code, 200) |
| 125 | + self.assertContains(response, 'Site administration') |
| 126 | + |
| 127 | + |
| 128 | +class AdminButtonsBrowserTests(TestCase): |
| 129 | + """Browser-based tests for admin button visibility.""" |
| 130 | + |
| 131 | + fixtures = ['test_data_optimap.json'] |
| 132 | + |
| 133 | + def setUp(self): |
| 134 | + """Set up test users for each test.""" |
| 135 | + # Create admin user |
| 136 | + self.admin_user = User.objects.create_superuser( |
| 137 | + username='admin', |
| 138 | + |
| 139 | + password='adminpass123' |
| 140 | + ) |
| 141 | + |
| 142 | + def test_work_landing_page_anonymous_no_admin_buttons(self): |
| 143 | + """Test that work landing page doesn't show admin buttons to anonymous users.""" |
| 144 | + |
| 145 | + # Get work from API instead of database |
| 146 | + work_data = get_work_from_api() |
| 147 | + |
| 148 | + try: |
| 149 | + start_chrome(f'localhost:8000/work/{work_data["doi"]}/', headless=True) |
| 150 | + driver = get_driver() |
| 151 | + |
| 152 | + # Wait for page to load |
| 153 | + self.assertIn("OPTIMAP", driver.title) |
| 154 | + |
| 155 | + # Check that admin buttons are not present |
| 156 | + edit_buttons = driver.find_elements("xpath", "//a[contains(text(), 'Edit in Admin')]") |
| 157 | + self.assertEqual(len(edit_buttons), 0, "Edit in Admin button should not be visible") |
| 158 | + |
| 159 | + finally: |
| 160 | + kill_browser() |
| 161 | + |
| 162 | + def test_contribute_page_anonymous_no_publish_buttons(self): |
| 163 | + """Test that contribute page doesn't show publish buttons to anonymous users.""" |
| 164 | + try: |
| 165 | + start_chrome('localhost:8000/contribute/', headless=True) |
| 166 | + driver = get_driver() |
| 167 | + |
| 168 | + # Wait for page to load |
| 169 | + self.assertIn("OPTIMAP", driver.title) |
| 170 | + |
| 171 | + # Check for absence of admin-only buttons |
| 172 | + publish_buttons = driver.find_elements("xpath", "//button[contains(text(), 'Publish')]") |
| 173 | + |
| 174 | + # Should have no visible publish buttons for anonymous users |
| 175 | + self.assertEqual(len(publish_buttons), 0, "Publish buttons should not be visible to anonymous users") |
| 176 | + |
| 177 | + finally: |
| 178 | + kill_browser() |
0 commit comments