Repository: internetarchive/openlibrary

Issue:
Return book button in loans page, opens the book.
<!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->
Return book button in loans page, opens the book.
### Evidence / Screenshot (if possible)

### Relevant url?
<!-- `https://openlibrary.org/...` -->

### Steps to Reproduce
<!-- What steps caused you to find the bug? -->
1. Go to https://openlibrary.org/
2. Borrow a book
3. Access https://openlibrary.org/account/loans
4. Click on return book

<!-- What actually happened after these steps? What did you expect to happen? -->
* Actual: The book is opened
* Expected: The book should be returned

### Details

- **Logged in (Y/N)?** Yes
- **Browser type/version?** Chrome  113.0.5672.127 64 bits
- **Operating system?** Windows 64 bits
- **Environment (prod/dev/local)?** prod
<!-- If not sure, put prod -->

### Proposal & Constraints
<!-- What is the proposed solution / implementation? Is there a precedent of this approach succeeding elsewhere? -->
Make the button do the same as in the "my books" page will work.

### Related files
<!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->

### Stakeholders
<!-- @ tag stakeholders of this bug -->



Discussion:
Hi could I work on this?
It seems that the `/borrow/ia/(.*)` end-point doesn't implement the "return" action:

https://github.com/internetarchive/openlibrary/blob/a85f6591e2e24f13097e37fcf29922be07bcedf1/openlibrary/macros/ReturnForm.html#L3-L6

https://github.com/internetarchive/openlibrary/blob/a85f6591e2e24f13097e37fcf29922be07bcedf1/openlibrary/plugins/upstream/borrow.py#L114-L116

And the "return now" button from Archive's detail page does a POST to its `/services/loans/loan` route, not Open Library's.

Also, tests apparently cover only the UI's behavior, not API's.
Does anyone know whether it is possible to simulate the borrow-a-book mechanics in a development environment?
@netoarmando Hmm it appears to be implementing action=return here:

https://github.com/internetarchive/openlibrary/blob/bc5f3d9034b2fac13179a131ce2f0a77c6b64972/openlibrary/plugins/upstream/borrow.py#L156-L161

There are a few places higher up that cause a redirect to archive.org ; maybe it's getting caught somewhere there?

Testing this from the local dev environment might be a little difficult, I'm afraid. 
This might not be a great first issue as a result of that , removing that label. Initially thought the issue was something slightly different.

Resolving patch:
diff --git a/openlibrary/plugins/upstream/borrow.py b/openlibrary/plugins/upstream/borrow.py
--- a/openlibrary/plugins/upstream/borrow.py
+++ b/openlibrary/plugins/upstream/borrow.py
@@ -112,7 +112,12 @@ def POST(self, key):
         """Called when the user wants to borrow the edition"""
 
         i = web.input(
-            action='borrow', format=None, ol_host=None, _autoReadAloud=None, q=""
+            action='borrow',
+            format=None,
+            ol_host=None,
+            _autoReadAloud=None,
+            q="",
+            redirect="",
         )
 
         ol_host = i.ol_host or 'openlibrary.org'
@@ -137,7 +142,7 @@ def POST(self, key):
             raise web.seeother(archive_url)
 
         error_redirect = archive_url
-        edition_redirect = urllib.parse.quote(edition.url())
+        edition_redirect = urllib.parse.quote(i.redirect or edition.url())
         user = accounts.get_current_user()
 
         if user:
@@ -158,7 +163,7 @@ def POST(self, key):
             stats.increment('ol.loans.return')
             edition.update_loan_status()
             user.update_loan_status()
-            raise web.seeother(web.ctx.path)
+            raise web.seeother(edition_redirect)
         elif action == 'join-waitinglist':
             lending.s3_loan_api(edition.ocaid, s3_keys, action='join_waitlist')
             stats.increment('ol.loans.joinWaitlist')


Test patch: