|
| 1 | +from django.test import TestCase, Client |
| 2 | +from django.urls import reverse |
| 3 | +from publications.models import Publication, Source |
| 4 | +from django.contrib.gis.geos import Point, GeometryCollection |
| 5 | +from django.utils.timezone import now |
| 6 | +from datetime import timedelta |
| 7 | + |
| 8 | + |
| 9 | +class WorkLandingPageTest(TestCase): |
| 10 | + """Tests for the work landing page view and its links.""" |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + self.client = Client() |
| 14 | + |
| 15 | + # Create a test source |
| 16 | + self.source = Source.objects.create( |
| 17 | + name="Test Journal", |
| 18 | + url_field="https://example.com/oai", |
| 19 | + homepage_url="https://example.com/journal", |
| 20 | + issn_l="1234-5678" |
| 21 | + ) |
| 22 | + |
| 23 | + # Create a test publication with DOI |
| 24 | + self.pub_with_doi = Publication.objects.create( |
| 25 | + title="Test Publication with DOI", |
| 26 | + abstract="Test abstract for publication with DOI", |
| 27 | + url="https://example.com/pub1", |
| 28 | + status="p", |
| 29 | + publicationDate=now() - timedelta(days=30), |
| 30 | + doi="10.1234/test-doi", |
| 31 | + geometry=GeometryCollection(Point(12.4924, 41.8902)), |
| 32 | + source=self.source |
| 33 | + ) |
| 34 | + |
| 35 | + # Create a test publication without source homepage_url |
| 36 | + self.source_no_homepage = Source.objects.create( |
| 37 | + name="Test Journal No Homepage", |
| 38 | + url_field="https://example.com/oai2", |
| 39 | + homepage_url=None, |
| 40 | + issn_l="8765-4321" |
| 41 | + ) |
| 42 | + |
| 43 | + self.pub_no_homepage = Publication.objects.create( |
| 44 | + title="Test Publication No Homepage", |
| 45 | + abstract="Test abstract", |
| 46 | + url="https://example.com/pub2", |
| 47 | + status="p", |
| 48 | + publicationDate=now() - timedelta(days=20), |
| 49 | + doi="10.5678/another-doi", |
| 50 | + geometry=GeometryCollection(Point(13.4050, 52.5200)), |
| 51 | + source=self.source_no_homepage |
| 52 | + ) |
| 53 | + |
| 54 | + def test_work_landing_page_loads(self): |
| 55 | + """Test that the work landing page loads successfully.""" |
| 56 | + response = self.client.get(f"/work/{self.pub_with_doi.doi}/") |
| 57 | + self.assertEqual(response.status_code, 200) |
| 58 | + self.assertContains(response, self.pub_with_doi.title) |
| 59 | + |
| 60 | + def test_doi_link_is_correct(self): |
| 61 | + """Test that the DOI link points to the correct DOI resolver URL.""" |
| 62 | + response = self.client.get(f"/work/{self.pub_with_doi.doi}/") |
| 63 | + self.assertEqual(response.status_code, 200) |
| 64 | + |
| 65 | + # Check that the DOI link is present and correct |
| 66 | + expected_doi_url = f'https://doi.org/{self.pub_with_doi.doi}' |
| 67 | + self.assertContains(response, expected_doi_url) |
| 68 | + self.assertContains(response, f'<a href="{expected_doi_url}"') |
| 69 | + |
| 70 | + def test_source_link_with_homepage_url(self): |
| 71 | + """Test that the source link points to the homepage_url when available.""" |
| 72 | + response = self.client.get(f"/work/{self.pub_with_doi.doi}/") |
| 73 | + self.assertEqual(response.status_code, 200) |
| 74 | + |
| 75 | + # Check that the source homepage link is present |
| 76 | + self.assertContains(response, self.source.homepage_url) |
| 77 | + self.assertContains(response, f'<a href="{self.source.homepage_url}"') |
| 78 | + self.assertContains(response, self.source.name) |
| 79 | + |
| 80 | + def test_source_without_homepage_url(self): |
| 81 | + """Test that source name is displayed as text when homepage_url is not available.""" |
| 82 | + response = self.client.get(f"/work/{self.pub_no_homepage.doi}/") |
| 83 | + self.assertEqual(response.status_code, 200) |
| 84 | + |
| 85 | + # Check that the source name is present but not as a link |
| 86 | + self.assertContains(response, self.source_no_homepage.name) |
| 87 | + # Should not have a link to the source since homepage_url is None |
| 88 | + self.assertNotContains(response, f'<a href="None"') |
| 89 | + |
| 90 | + def test_raw_json_api_link_is_correct(self): |
| 91 | + """Test that the raw JSON API link is correct and uses the publication ID.""" |
| 92 | + response = self.client.get(f"/work/{self.pub_with_doi.doi}/") |
| 93 | + self.assertEqual(response.status_code, 200) |
| 94 | + |
| 95 | + # Check that the API link is present |
| 96 | + expected_api_url = f'/api/v1/publications/{self.pub_with_doi.id}/' |
| 97 | + self.assertContains(response, expected_api_url) |
| 98 | + self.assertContains(response, 'View raw JSON from API') |
| 99 | + |
| 100 | + def test_raw_json_api_returns_valid_json(self): |
| 101 | + """Test that the raw JSON API endpoint returns valid JSON data.""" |
| 102 | + # First get the work landing page to ensure publication exists |
| 103 | + response = self.client.get(f"/work/{self.pub_with_doi.doi}/") |
| 104 | + self.assertEqual(response.status_code, 200) |
| 105 | + |
| 106 | + # Now test the API endpoint |
| 107 | + api_response = self.client.get(f'/api/v1/publications/{self.pub_with_doi.id}/') |
| 108 | + self.assertEqual(api_response.status_code, 200) |
| 109 | + self.assertIn('application/json', api_response['Content-Type']) |
| 110 | + |
| 111 | + # Check that the JSON contains expected fields |
| 112 | + data = api_response.json() |
| 113 | + |
| 114 | + # GeoJSON Feature format has properties |
| 115 | + if 'properties' in data: |
| 116 | + # GeoFeatureModelSerializer returns GeoJSON Feature |
| 117 | + self.assertEqual(data['type'], 'Feature') |
| 118 | + self.assertEqual(data['properties']['title'], self.pub_with_doi.title) |
| 119 | + self.assertEqual(data['properties']['doi'], self.pub_with_doi.doi) |
| 120 | + self.assertEqual(data['properties']['abstract'], self.pub_with_doi.abstract) |
| 121 | + else: |
| 122 | + # Regular serializer format |
| 123 | + self.assertEqual(data['title'], self.pub_with_doi.title) |
| 124 | + self.assertEqual(data['doi'], self.pub_with_doi.doi) |
| 125 | + self.assertEqual(data['abstract'], self.pub_with_doi.abstract) |
| 126 | + |
| 127 | + def test_all_links_have_security_attributes(self): |
| 128 | + """Test that external links have target='_blank' and rel='noopener'.""" |
| 129 | + response = self.client.get(f"/work/{self.pub_with_doi.doi}/") |
| 130 | + self.assertEqual(response.status_code, 200) |
| 131 | + |
| 132 | + # Check DOI link |
| 133 | + self.assertContains(response, 'target="_blank"') |
| 134 | + self.assertContains(response, 'rel="noopener"') |
| 135 | + |
| 136 | + def test_html_title_contains_publication_title_and_doi(self): |
| 137 | + """Test that the HTML <title> tag contains the publication title and DOI.""" |
| 138 | + response = self.client.get(f"/work/{self.pub_with_doi.doi}/") |
| 139 | + self.assertEqual(response.status_code, 200) |
| 140 | + |
| 141 | + # Extract the title tag content |
| 142 | + content = response.content.decode('utf-8') |
| 143 | + |
| 144 | + # Check that <title> tag contains the publication title |
| 145 | + self.assertIn(f'<title>{self.pub_with_doi.title}', content) |
| 146 | + |
| 147 | + # Check that <title> tag contains the DOI in parentheses |
| 148 | + self.assertIn(f'({self.pub_with_doi.doi})', content) |
| 149 | + |
| 150 | + # Check that OPTIMAP is also in the title |
| 151 | + self.assertIn('OPTIMAP', content) |
| 152 | + |
| 153 | + # Verify the complete expected format: "Title (DOI) - OPTIMAP" |
| 154 | + expected_title = f'<title>{self.pub_with_doi.title} ({self.pub_with_doi.doi}) - OPTIMAP</title>' |
| 155 | + self.assertIn(expected_title, content) |
| 156 | + |
| 157 | + def test_html_title_format(self): |
| 158 | + """Test that the HTML <title> tag has proper format and structure.""" |
| 159 | + response = self.client.get(f"/work/{self.pub_with_doi.doi}/") |
| 160 | + self.assertEqual(response.status_code, 200) |
| 161 | + |
| 162 | + # Verify title has opening and closing tags |
| 163 | + content = response.content.decode('utf-8') |
| 164 | + self.assertIn('<title>', content) |
| 165 | + self.assertIn('</title>', content) |
| 166 | + |
| 167 | + # Verify the title appears in the <head> section (use re.DOTALL for multiline) |
| 168 | + import re |
| 169 | + self.assertIsNotNone(re.search(r'<head>.*<title>.*</title>.*</head>', content, re.DOTALL)) |
0 commit comments