Skip to content

Commit eb422e2

Browse files
committed
[IMP] shopinvader_api_signin_jwt: Call promote directly at signin
1 parent 0c7a3f1 commit eb422e2

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

shopinvader_api_signin_jwt/README.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Shopinvader Api Signin JWT
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:59c45a1d4cbece13c06acf5a0b621bf113fd94ed74b05de2143494c755320354
10+
!! source digest: sha256:7337f1eba6afcff7201a9185515fa52033fa3c544861e8b59aa71ed2545d6a7b
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
1313
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
@@ -25,6 +25,11 @@ Shopinvader Api Signin JWT
2525
This addon adds a web API to signin into the application and create a partner
2626
if the email in the jwt payload is unknown.
2727

28+
This addon supports the "anonymous partner" feature, which allows to create
29+
carts for user that are not loggedin.
30+
When you login from an anonymous partner, your cart is transfered to your real
31+
partner, and your anonymous partner is deleted.
32+
2833
**Table of contents**
2934

3035
.. contents::
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
This addon adds a web API to signin into the application and create a partner
22
if the email in the jwt payload is unknown.
3+
4+
This addon supports the "anonymous partner" feature, which allows to create
5+
carts for user that are not loggedin.
6+
When you login from an anonymous partner, your cart is transfered to your real
7+
partner, and your anonymous partner is deleted.

shopinvader_api_signin_jwt/routers/signin.py

+3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ def signin(
3434
Authenticate the partner based on a JWT token or a session cookie.
3535
Set the session cookie if allowed.
3636
Return HTTP code 201 if res.partner created (case of the first signin).
37+
Promote anonymous partner and delete it if any.
3738
"""
3839
if not partner:
3940
partner = env[
4041
"shopinvader_api_signin_jwt.signin_router.helper"
4142
]._create_partner_from_payload(payload)
4243
response.status_code = status.HTTP_201_CREATED
4344

45+
env["res.partner"]._promote_anonymous_partner(partner, request.cookies, response)
46+
4447

4548
@signin_router.post("/signout")
4649
def signout(

shopinvader_api_signin_jwt/static/description/index.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,15 @@ <h1 class="title">Shopinvader Api Signin JWT</h1>
368368
!! This file is generated by oca-gen-addon-readme !!
369369
!! changes will be overwritten. !!
370370
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
371-
!! source digest: sha256:59c45a1d4cbece13c06acf5a0b621bf113fd94ed74b05de2143494c755320354
371+
!! source digest: sha256:7337f1eba6afcff7201a9185515fa52033fa3c544861e8b59aa71ed2545d6a7b
372372
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
373373
<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>
374374
<p>This addon adds a web API to signin into the application and create a partner
375375
if the email in the jwt payload is unknown.</p>
376+
<p>This addon supports the “anonymous partner” feature, which allows to create
377+
carts for user that are not loggedin.
378+
When you login from an anonymous partner, your cart is transfered to your real
379+
partner, and your anonymous partner is deleted.</p>
376380
<p><strong>Table of contents</strong></p>
377381
<div class="contents local topic" id="contents">
378382
<ul class="simple">

shopinvader_api_signin_jwt/tests/test_signin.py

+33
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from odoo.addons.fastapi.tests.common import FastAPITransactionCase
99
from odoo.addons.fastapi_auth_jwt.dependencies import auth_jwt_default_validator_name
10+
from odoo.addons.shopinvader_anonymous_partner.models.res_partner import COOKIE_NAME
1011

1112
from ..routers import signin_router
1213

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

83+
def test_signin_anonymous_cart(self):
84+
anonymous_partner = self.env["res.partner"].create(
85+
{"name": "Test anonymous", "anonymous_token": "1234", "active": False}
86+
)
87+
product = self.env["product.product"].create(
88+
{"name": "product", "uom_id": self.env.ref("uom.product_uom_unit").id}
89+
)
90+
anonymous_cart = self.env["sale.order"].create(
91+
{
92+
"partner_id": anonymous_partner.id,
93+
"order_line": [
94+
(0, 0, {"product_id": product.id, "product_uom_qty": 1}),
95+
],
96+
"typology": "cart",
97+
}
98+
)
99+
100+
token = self._get_token()
101+
with self._create_test_client() as client:
102+
res = client.post(
103+
"/signin",
104+
headers={"Authorization": token},
105+
cookies={COOKIE_NAME: "1234"},
106+
)
107+
self.assertFalse(res.cookies.get(COOKIE_NAME))
108+
self.assertFalse(anonymous_partner.exists())
109+
self.assertFalse(anonymous_cart.exists())
110+
partner = self.env["res.partner"].search([("email", "=", "[email protected]")])
111+
cart = self.env["sale.order"].search([("partner_id", "=", partner.id)])
112+
self.assertEqual(len(cart.order_line), 1)
113+
self.assertEqual(cart.order_line[0].product_id, product)
114+
82115
def test_signout(self):
83116
self.validator.write({"cookie_enabled": True, "cookie_name": "test_cookie"})
84117
token = self._get_token()

0 commit comments

Comments
 (0)