Skip to content

Commit

Permalink
[IMP] shopinvader_api_signin_jwt: Call promote directly at signin
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Dec 12, 2024
1 parent 0c7a3f1 commit 98f70b6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
7 changes: 6 additions & 1 deletion shopinvader_api_signin_jwt/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Shopinvader Api Signin JWT
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:59c45a1d4cbece13c06acf5a0b621bf113fd94ed74b05de2143494c755320354
!! source digest: sha256:7337f1eba6afcff7201a9185515fa52033fa3c544861e8b59aa71ed2545d6a7b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -25,6 +25,11 @@ Shopinvader Api Signin JWT
This addon adds a web API to signin into the application and create a partner
if the email in the jwt payload is unknown.

This addon supports the "anonymous partner" feature, which allows to create
carts for user that are not loggedin.
When you login from an anonymous partner, your cart is transfered to your real
partner, and your anonymous partner is deleted.

**Table of contents**

.. contents::
Expand Down
5 changes: 5 additions & 0 deletions shopinvader_api_signin_jwt/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
This addon adds a web API to signin into the application and create a partner
if the email in the jwt payload is unknown.

This addon supports the "anonymous partner" feature, which allows to create
carts for user that are not loggedin.
When you login from an anonymous partner, your cart is transfered to your real
partner, and your anonymous partner is deleted.
3 changes: 3 additions & 0 deletions shopinvader_api_signin_jwt/routers/signin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ def signin(
Authenticate the partner based on a JWT token or a session cookie.
Set the session cookie if allowed.
Return HTTP code 201 if res.partner created (case of the first signin).
Promote anonymous partner and delete it if any.
"""
if not partner:
partner = env[
"shopinvader_api_signin_jwt.signin_router.helper"
]._create_partner_from_payload(payload)
response.status_code = status.HTTP_201_CREATED

env["res.partner"]._promote_anonymous_partner(partner, request.cookies, response)


@signin_router.post("/signout")
def signout(
Expand Down
13 changes: 8 additions & 5 deletions shopinvader_api_signin_jwt/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -276,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -302,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -368,11 +367,15 @@ <h1 class="title">Shopinvader Api Signin JWT</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:59c45a1d4cbece13c06acf5a0b621bf113fd94ed74b05de2143494c755320354
!! source digest: sha256:d2a37ed8f608aa991e96f29205867b36c47bf9ec58fd392ec4ee8d6f386da0f2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/shopinvader/odoo-shopinvader/tree/16.0/shopinvader_api_signin_jwt"><img alt="shopinvader/odoo-shopinvader" src="https://img.shields.io/badge/github-shopinvader%2Fodoo--shopinvader-lightgray.png?logo=github" /></a></p>
<p>This addon adds a web API to signin into the application and create a partner
if the email in the jwt payload is unknown.</p>
<p>This addon supports the “anonymous partner” feature, which allows to create
carts for user that are not loggedin.
When you login from an anonymous partner, your cart is transfered to your real
partner, and your anonymous partner is deleted.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand Down
33 changes: 33 additions & 0 deletions shopinvader_api_signin_jwt/tests/test_signin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from odoo.addons.fastapi.tests.common import FastAPITransactionCase
from odoo.addons.fastapi_auth_jwt.dependencies import auth_jwt_default_validator_name
from odoo.addons.shopinvader_anonymous_partner.models.res_partner import COOKIE_NAME

from ..routers import signin_router

Expand Down Expand Up @@ -79,6 +80,38 @@ def test_signin(self):
res = client.post("/signin", headers={"Authorization": token})
self.assertEqual(res.status_code, 200)

def test_signin_anonymous_cart(self):
anonymous_partner = self.env["res.partner"].create(
{"name": "Test anonymous", "anonymous_token": "1234", "active": False}
)
product = self.env["product.product"].create(
{"name": "product", "uom_id": self.env.ref("uom.product_uom_unit").id}
)
anonymous_cart = self.env["sale.order"].create(
{
"partner_id": anonymous_partner.id,
"order_line": [
(0, 0, {"product_id": product.id, "product_uom_qty": 1}),
],
"typology": "cart",
}
)

token = self._get_token()
with self._create_test_client() as client:
res = client.post(
"/signin",
headers={"Authorization": token},
cookies={COOKIE_NAME: "1234"},
)
self.assertFalse(res.cookies.get(COOKIE_NAME))
self.assertFalse(anonymous_partner.exists())
self.assertFalse(anonymous_cart.exists())
partner = self.env["res.partner"].search([("email", "=", "[email protected]")])
cart = self.env["sale.order"].search([("partner_id", "=", partner.id)])
self.assertEqual(len(cart.order_line), 1)
self.assertEqual(cart.order_line[0].product_id, product)

def test_signout(self):
self.validator.write({"cookie_enabled": True, "cookie_name": "test_cookie"})
token = self._get_token()
Expand Down

0 comments on commit 98f70b6

Please sign in to comment.