diff --git a/openlibrary/plugins/upstream/addbook.py b/openlibrary/plugins/upstream/addbook.py index 2fdd1fedf43..de4a4ca7a9b 100644 --- a/openlibrary/plugins/upstream/addbook.py +++ b/openlibrary/plugins/upstream/addbook.py @@ -5,7 +5,7 @@ import io import logging import re -import urllib +import urllib.parse from typing import TYPE_CHECKING, Literal, NoReturn, overload import web @@ -967,6 +967,10 @@ def GET(self, key): if not page: raise web.notfound() + if page.ocaid: + ia_item_url = f"https://archive.org/details/{urllib.parse.quote(page.ocaid, safe='')}" + raise web.seeother(ia_item_url) + return render_template("books/daisy", page) diff --git a/openlibrary/plugins/upstream/tests/test_addbook.py b/openlibrary/plugins/upstream/tests/test_addbook.py index c0a9529ead5..69c07ad6084 100644 --- a/openlibrary/plugins/upstream/tests/test_addbook.py +++ b/openlibrary/plugins/upstream/tests/test_addbook.py @@ -1,5 +1,6 @@ """py.test tests for addbook""" +import pytest import web from openlibrary import accounts @@ -489,6 +490,55 @@ def test_moving_edition_to_new_work_copy_when_none(self, monkeypatch): assert not new_work.subjects +class TestDaisyPage: + def setup_method(self, method): + web.ctx.site = MockSite() + + def test_redirects_to_archive_item_for_edition_with_ocaid(self, monkeypatch): + web.ctx.site.save( + { + "type": {"key": "/type/edition"}, + "key": "/books/OL1M", + "title": "Accessible Book", + "ocaid": "testitem00archive", + } + ) + + redirects = [] + + def seeother(url): + redirects.append(url) + raise RuntimeError("redirect") + + monkeypatch.setattr(addbook.web, "seeother", seeother) + + with pytest.raises(RuntimeError, match="redirect"): + addbook.daisy().GET("/books/OL1M") + assert redirects == ["https://archive.org/details/testitem00archive"] + + def test_redirect_escapes_archive_item_identifier(self, monkeypatch): + web.ctx.site.save( + { + "type": {"key": "/type/edition"}, + "key": "/books/OL1M", + "title": "Accessible Book", + "ocaid": "test item/archive", + } + ) + + redirects = [] + + def seeother(url): + redirects.append(url) + raise RuntimeError("redirect") + + monkeypatch.setattr(addbook.web, "seeother", seeother) + + with pytest.raises(RuntimeError, match="redirect"): + addbook.daisy().GET("/books/OL1M") + assert redirects == ["https://archive.org/details/test%20item%2Farchive"] + + class TestMakeWork: def test_make_author_adds_the_correct_key(self): author_key = "OL123A"