Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions olms/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ def fetch(session, rpt_id, form):

def fetch_assigned(session, rpt_id, form):
"""True if `rpt_id` is assigned to `form`.

Real form pages embed an Angular app (`ng-app="LM20App"` etc.) and
fetch their data asynchronously. The OLMS "not found" page is a
plain HTML stub without ng-app. Body size is unreliable as a
discriminator (form templates are ~8.5K, the stub is ~8.2K).

Body size is unreliable as a discriminator (form templates are ~8.5K,
the stub is ~8.2K). Error pages are labeled as such.
"""
content_type, body = fetch(session, rpt_id, form)
if content_type == "application/pdf":
return True
return content_type == "text/html" and b"ng-app=" in body
return content_type == "text/html" and b"Error Page" not in body


def is_assigned(session, rpt_id):
Expand Down Expand Up @@ -182,7 +180,7 @@ def discover(config, db):
if max_known and not is_assigned(sess, max_known):
raise RuntimeError(
f"rptId {max_known} is in {db} but the OLMS probe reports"
" it unassigned; the ng-app/content-type heuristics are broken"
" it unassigned; the error label/content-type heuristics are broken"
)

max_assigned = bisect_max_assigned(sess, max_known)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)


def response(status=200, content_type="text/html", body=b"ng-app="):
def response(status=200, content_type="text/html", body=b"success body"):
r = Mock()
r.status_code = status
r.headers = {"Content-Type": content_type}
Expand All @@ -31,7 +31,7 @@ def test_fetch_backs_off_then_succeeds():
with patch.object(discover.time, "sleep") as sleep:
content_type, body = fetch(session, 1, "LM10Form")
assert content_type == "text/html"
assert body == b"ng-app="
assert body == b"success body"
assert [c.args[0] for c in sleep.call_args_list] == [5, 10]


Expand All @@ -55,8 +55,8 @@ def test_fetch_assigned():
session = Mock()
session.get.side_effect = [
response(content_type="application/pdf"),
response(body=b"<html>not found stub</html>"),
response(body=b'<html ng-app="LM20App">'),
response(body=b"Error Page"),
response(),
]
assert fetch_assigned(session, 1, "LM2Form") is True
assert fetch_assigned(session, 2, "LM2Form") is False
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_fetch_hit_html_falls_through_mismatched_forms():

session = Mock()
session.get.side_effect = [
response(body=b'<html ng-app="LM20App">bare shell</html>'),
response(body=b'<html>bare shell</html>'),
response(body=b"<html>Signature ... rendered LM-21 ...</html>"),
]
body = fetch_hit_html(session, 941283, ("LM20Form", "LM21Form"))
Expand Down
Loading