forked from ChelseaKR/queer-the-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_source_allowlist.py
More file actions
67 lines (52 loc) · 2 KB
/
Copy pathtest_source_allowlist.py
File metadata and controls
67 lines (52 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Source-ethics guardrail — "Goodreads requests = 0" (merge-blocking).
The recommender may only fetch from ethical, non-gatekept catalogs. Goodreads
(and its Amazon parent) are blocked at the single network choke point, before any
socket opens. These tests prove the choke point is default-deny and that
Goodreads is explicitly excluded, not merely absent.
"""
from __future__ import annotations
import pytest
from recommender.catalogs import (
ALLOWED_HOSTS,
BLOCKED_HOSTS,
SourceNotAllowed,
assert_allowed,
)
def test_allowlisted_hosts_pass() -> None:
assert assert_allowed("https://openlibrary.org/subjects/transgender.json")
assert assert_allowed("https://api.hardcover.app/v1/graphql")
assert assert_allowed("https://bookwyrm.social/list/123")
@pytest.mark.parametrize(
"url",
[
"https://www.goodreads.com/book/show/1",
"https://goodreads.com/list/1",
"https://www.amazon.com/dp/123",
],
)
def test_blocked_hosts_raise(url: str) -> None:
with pytest.raises(SourceNotAllowed):
assert_allowed(url)
def test_unknown_host_is_default_denied() -> None:
with pytest.raises(SourceNotAllowed):
assert_allowed("https://some-random-tracker.example/api")
def test_missing_host_raises() -> None:
with pytest.raises(SourceNotAllowed):
assert_allowed("not-a-url")
@pytest.mark.parametrize(
"url",
[
"http://bookwyrm.social/list/123",
"https://reader:secret@bookwyrm.social/list/123",
"https://bookwyrm.social/list/123#private-note",
"https://bookwyrm.social:8443/list/123",
"https://bookwyrm.social:not-a-port/list/123",
],
)
def test_transport_and_url_ambiguities_are_rejected(url: str) -> None:
with pytest.raises(SourceNotAllowed):
assert_allowed(url)
def test_goodreads_is_blocked_not_allowlisted() -> None:
assert "goodreads.com" in BLOCKED_HOSTS
assert not any("goodreads" in h for h in ALLOWED_HOSTS)
assert not any("amazon" in h for h in ALLOWED_HOSTS)