|
1 | 1 | import pytest |
2 | 2 |
|
3 | | -from ckanext.switzerland.helpers import map_to_valid_format |
| 3 | +from ckanext.switzerland.helpers import get_robots_meta_content, map_to_valid_format |
4 | 4 |
|
5 | 5 | CSV_URI = "http://publications.europa.eu/resource/authority/file-type/CSV" |
6 | 6 | GEOJSON_URI = "http://publications.europa.eu/resource/authority/file-type/GEOJSON" |
|
61 | 61 | ) |
62 | 62 | def test_map_to_valid_format_known_values(input_format, expected): |
63 | 63 | assert map_to_valid_format(input_format) == expected |
| 64 | + |
| 65 | + |
| 66 | +INDEX = "index, follow" |
| 67 | +NOINDEX = "noindex, follow" |
| 68 | + |
| 69 | + |
| 70 | +class _FakeArgs(dict): |
| 71 | + """Minimal stand-in for Flask request.args (truthy when non-empty).""" |
| 72 | + |
| 73 | + def __bool__(self): |
| 74 | + return bool(dict(self)) |
| 75 | + |
| 76 | + |
| 77 | +class _FakeRequest: |
| 78 | + def __init__(self, endpoint, path="/", args=None): |
| 79 | + self.endpoint = endpoint |
| 80 | + self.path = path |
| 81 | + self.args = _FakeArgs(args or {}) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "endpoint,path,args,expected", |
| 86 | + [ |
| 87 | + ("dataset.search", "/dataset/", None, INDEX), |
| 88 | + ("dataset.search", "/dataset/", {"q": "fahrplan"}, NOINDEX), |
| 89 | + ("dataset.search", "/dataset/", {"groups": "timetables"}, NOINDEX), |
| 90 | + ("dataset.search", "/dataset/", {"page": "2"}, NOINDEX), |
| 91 | + ("ogdch_home.search", "/", None, INDEX), |
| 92 | + ("ogdch_home.search", "/", {"organization": "oevch"}, NOINDEX), |
| 93 | + ("dataset.read", "/dataset/bike-and-car-parking", None, INDEX), |
| 94 | + ("showcase_blueprint.index", "/showcase/", None, INDEX), |
| 95 | + ( |
| 96 | + "showcase_blueprint.index", |
| 97 | + "/showcase/", |
| 98 | + {"q": "viz"}, |
| 99 | + NOINDEX, |
| 100 | + ), |
| 101 | + ( |
| 102 | + "showcase_blueprint.read", |
| 103 | + "/showcase/visualisierung-der-oev-tagesentwicklungen", |
| 104 | + None, |
| 105 | + INDEX, |
| 106 | + ), |
| 107 | + ("group.index", "/group/", None, INDEX), |
| 108 | + ("group.read", "/group/accessibilitydata", None, INDEX), |
| 109 | + ( |
| 110 | + "group.read", |
| 111 | + "/group/accessibilitydata", |
| 112 | + {"q": "test"}, |
| 113 | + NOINDEX, |
| 114 | + ), |
| 115 | + ("organization.index", "/organization/", None, NOINDEX), |
| 116 | + ("organization.read", "/organization/oevch", None, NOINDEX), |
| 117 | + ( |
| 118 | + "organization.read", |
| 119 | + "/organization/oevch", |
| 120 | + {"q": "x"}, |
| 121 | + NOINDEX, |
| 122 | + ), |
| 123 | + ("harvest.search", "/harvest/", None, NOINDEX), |
| 124 | + ("harvest.read", "/harvest/some-source", None, NOINDEX), |
| 125 | + # Path fallback when endpoint is missing/unknown |
| 126 | + (None, "/harvest/", None, NOINDEX), |
| 127 | + (None, "/organization/oevch", None, NOINDEX), |
| 128 | + ( |
| 129 | + "dataset_resource.read", |
| 130 | + "/dataset/bike-and-car-parking/resource/eb892409-d24f-4484-a19b-2d6e26108d9a", |
| 131 | + None, |
| 132 | + NOINDEX, |
| 133 | + ), |
| 134 | + ( |
| 135 | + "resource.read", |
| 136 | + "/dataset/bike-and-car-parking/resource/eb892409-d24f-4484-a19b-2d6e26108d9a", |
| 137 | + None, |
| 138 | + NOINDEX, |
| 139 | + ), |
| 140 | + # Path fallback when endpoint is missing/unknown but URL is a resource |
| 141 | + ( |
| 142 | + None, |
| 143 | + "/dataset/bike-and-car-parking/resource/eb892409-d24f-4484-a19b-2d6e26108d9a", |
| 144 | + None, |
| 145 | + NOINDEX, |
| 146 | + ), |
| 147 | + ("user.read", "/user/admin", None, INDEX), |
| 148 | + ], |
| 149 | +) |
| 150 | +def test_get_robots_meta_content(monkeypatch, endpoint, path, args, expected): |
| 151 | + monkeypatch.setattr( |
| 152 | + "ckanext.switzerland.helpers.tk.request", |
| 153 | + _FakeRequest(endpoint, path, args), |
| 154 | + ) |
| 155 | + assert get_robots_meta_content() == expected |
0 commit comments