|
| 1 | +"""Tests for the meta endpoint that serves dynamic OG tags to crawlers.""" |
| 2 | + |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from fastapi.testclient import TestClient |
| 7 | + |
| 8 | +from monarch_py.api.main import app |
| 9 | +from monarch_py.datamodels.model import Node |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def client(): |
| 14 | + return TestClient(app) |
| 15 | + |
| 16 | + |
| 17 | +@patch("monarch_py.implementations.solr.solr_implementation.SolrImplementation.get_entity") |
| 18 | +def test_meta_endpoint_returns_html_with_og_tags(mock_get_entity, client, node): |
| 19 | + """Test that /meta/{entity_id} returns HTML with entity-specific OG tags.""" |
| 20 | + mock_get_entity.return_value = Node(**node) |
| 21 | + response = client.get("/v3/api/meta/MONDO:0020121") |
| 22 | + |
| 23 | + assert response.status_code == 200 |
| 24 | + assert response.headers["content-type"] == "text/html; charset=utf-8" |
| 25 | + |
| 26 | + html = response.text |
| 27 | + assert "MONDO:0020121" in html |
| 28 | + assert "muscular dystrophy" in html.lower() |
| 29 | + assert 'og:title' in html |
| 30 | + assert 'og:description' in html |
| 31 | + assert 'og:url' in html |
| 32 | + assert 'testserver/MONDO:0020121' in html |
| 33 | + |
| 34 | + |
| 35 | +@patch("monarch_py.implementations.solr.solr_implementation.SolrImplementation.get_entity") |
| 36 | +def test_meta_endpoint_returns_404_for_unknown_entity(mock_get_entity, client): |
| 37 | + """Test that /meta/{entity_id} returns 404 for non-existent entities.""" |
| 38 | + mock_get_entity.return_value = None |
| 39 | + response = client.get("/v3/api/meta/FAKE:9999999") |
| 40 | + |
| 41 | + assert response.status_code == 404 |
| 42 | + |
| 43 | + |
| 44 | +@patch("monarch_py.implementations.solr.solr_implementation.SolrImplementation.get_entity") |
| 45 | +def test_meta_endpoint_escapes_html_in_content(mock_get_entity, client): |
| 46 | + """Test that entity content is properly HTML-escaped to prevent XSS.""" |
| 47 | + mock_get_entity.return_value = Node( |
| 48 | + id="TEST:001", |
| 49 | + category="biolink:Disease", |
| 50 | + name='<script>alert("xss")</script>', |
| 51 | + description='A "test" <b>entity</b> with & special chars', |
| 52 | + provided_by="test", |
| 53 | + association_counts=[], |
| 54 | + ) |
| 55 | + response = client.get("/v3/api/meta/TEST:001") |
| 56 | + |
| 57 | + assert response.status_code == 200 |
| 58 | + html = response.text |
| 59 | + # Jinja2 autoescape should escape angle brackets |
| 60 | + assert "<script>" not in html |
| 61 | + assert "<script>" in html |
| 62 | + |
| 63 | + |
| 64 | +@patch("monarch_py.implementations.solr.solr_implementation.SolrImplementation.get_entity") |
| 65 | +def test_meta_endpoint_truncates_long_description(mock_get_entity, client): |
| 66 | + """Test that very long descriptions are truncated at a word boundary.""" |
| 67 | + long_description = "word " * 200 # 1000 chars, well over the 300 limit |
| 68 | + mock_get_entity.return_value = Node( |
| 69 | + id="TEST:002", |
| 70 | + category="biolink:Disease", |
| 71 | + name="Test Entity", |
| 72 | + description=long_description.strip(), |
| 73 | + provided_by="test", |
| 74 | + association_counts=[], |
| 75 | + ) |
| 76 | + response = client.get("/v3/api/meta/TEST:002") |
| 77 | + |
| 78 | + assert response.status_code == 200 |
| 79 | + html = response.text |
| 80 | + assert "..." in html |
| 81 | + # Description (including "Test Entity - " prefix) should be truncated near the limit |
| 82 | + assert long_description.strip() not in html |
| 83 | + |
| 84 | + |
| 85 | +@patch("monarch_py.implementations.solr.solr_implementation.SolrImplementation.get_entity") |
| 86 | +def test_meta_endpoint_entity_with_no_name(mock_get_entity, client): |
| 87 | + """Test that entities without a name use the entity ID instead.""" |
| 88 | + mock_get_entity.return_value = Node( |
| 89 | + id="TEST:003", |
| 90 | + category="biolink:Disease", |
| 91 | + name=None, |
| 92 | + description="A test description", |
| 93 | + provided_by="test", |
| 94 | + association_counts=[], |
| 95 | + ) |
| 96 | + response = client.get("/v3/api/meta/TEST:003") |
| 97 | + |
| 98 | + assert response.status_code == 200 |
| 99 | + html = response.text |
| 100 | + assert "TEST:003 | Monarch Initiative" in html |
| 101 | + assert "A test description" in html |
| 102 | + |
| 103 | + |
| 104 | +@patch("monarch_py.implementations.solr.solr_implementation.SolrImplementation.get_entity") |
| 105 | +def test_meta_endpoint_entity_with_no_description(mock_get_entity, client): |
| 106 | + """Test that entities without a description still produce valid OG tags.""" |
| 107 | + mock_get_entity.return_value = Node( |
| 108 | + id="TEST:004", |
| 109 | + category="biolink:Disease", |
| 110 | + name="Test Entity", |
| 111 | + description=None, |
| 112 | + provided_by="test", |
| 113 | + association_counts=[], |
| 114 | + ) |
| 115 | + response = client.get("/v3/api/meta/TEST:004") |
| 116 | + |
| 117 | + assert response.status_code == 200 |
| 118 | + html = response.text |
| 119 | + assert "Test Entity | Monarch Initiative" in html |
| 120 | + assert 'og:description' in html |
| 121 | + |
| 122 | + |
| 123 | +@patch("monarch_py.implementations.solr.solr_implementation.SolrImplementation.get_entity") |
| 124 | +def test_meta_endpoint_entity_with_no_name_or_description(mock_get_entity, client): |
| 125 | + """Test that entities with neither name nor description use fallback text.""" |
| 126 | + mock_get_entity.return_value = Node( |
| 127 | + id="TEST:005", |
| 128 | + category="biolink:Disease", |
| 129 | + name=None, |
| 130 | + description=None, |
| 131 | + provided_by="test", |
| 132 | + association_counts=[], |
| 133 | + ) |
| 134 | + response = client.get("/v3/api/meta/TEST:005") |
| 135 | + |
| 136 | + assert response.status_code == 200 |
| 137 | + html = response.text |
| 138 | + assert "TEST:005 | Monarch Initiative" in html |
| 139 | + assert "View TEST:005 on Monarch Initiative" in html |
| 140 | + |
| 141 | + |
| 142 | +@patch("monarch_py.implementations.solr.solr_implementation.SolrImplementation.get_entity") |
| 143 | +def test_meta_endpoint_returns_cache_control_header(mock_get_entity, client, node): |
| 144 | + """Test that the response includes a Cache-Control header.""" |
| 145 | + mock_get_entity.return_value = Node(**node) |
| 146 | + response = client.get("/v3/api/meta/MONDO:0020121") |
| 147 | + |
| 148 | + assert response.status_code == 200 |
| 149 | + assert response.headers["cache-control"] == "public, max-age=3600" |
0 commit comments