Add the missing portal layer for subscription_oca, modelled on the portal of the OCA contract module: - sale.subscription inherits portal.mixin; access_url resolves to /my/subscriptions/<id> - read-only ACLs (base.group_portal) on subscription, line, stage and template models, with record rules restricting both subscription and lines to the user's own commercial partner - CustomerPortal extension: home counter, paginated and sortable list, detail page with lines, totals and a portal chatter - related invoices are listed only when posted and actually readable by the visitor, since subscription_oca creates them in sudo - /my home entry with a recurring-cycle icon (circular arrows around a calendar, matching the portal icon palette) - 13 tests, fr.po translation catalog Chatter headings carry mb-4: the frontend portal chatter paints an opaque sticky bar 15px above its container, which would otherwise clip the heading (same overlap exists on core portal pages, hidden behind their taller h3). Portal body loop variables avoid the names `invoice` and `subscription`: qweb renders a t-call body before the layout it calls in the same values dict, so top-level loop variables leak into the breadcrumb context where account's portal_breadcrumbs template tests t-if="invoice".
282 lines
11 KiB
Python
282 lines
11 KiB
Python
# Copyright 2026 Stéphan Sainléger (Elabore)
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
from odoo import Command, fields
|
|
from odoo.tests import HttpCase, tagged
|
|
|
|
INVOICE_LINK_CLASS = "tr_subscription_invoice_link"
|
|
|
|
|
|
@tagged("post_install", "-at_install")
|
|
class TestSubscriptionPortal(HttpCase):
|
|
"""Cover the portal exposure of ``sale.subscription``.
|
|
|
|
The fixtures build a minimal accounting setup on purpose: the CI database
|
|
is installed without demo data nor chart of accounts, so invoices cannot be
|
|
posted without declaring an income account, a receivable account and a sale
|
|
journal first.
|
|
"""
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
super().setUpClass()
|
|
cls.portal_group = cls.env.ref("base.group_portal")
|
|
cls.draft_stage = cls.env.ref("subscription_oca.subscription_stage_draft")
|
|
cls.template = cls.env["sale.subscription.template"].create(
|
|
{"name": "Portal Monthly", "code": "PMON"}
|
|
)
|
|
cls.pricelist = cls.env["product.pricelist"].create(
|
|
{
|
|
"name": "Portal Test Pricelist",
|
|
"currency_id": cls.env.company.currency_id.id,
|
|
}
|
|
)
|
|
cls.product = cls.env["product.product"].create(
|
|
{"name": "Portal Subscription Service", "type": "service"}
|
|
)
|
|
cls.customer = cls.env["res.partner"].create(
|
|
{"name": "Portal Customer", "is_company": True}
|
|
)
|
|
cls.contact = cls.env["res.partner"].create(
|
|
{
|
|
"name": "Portal Contact",
|
|
"parent_id": cls.customer.id,
|
|
"email": "portal.contact@example.org",
|
|
}
|
|
)
|
|
cls.outsider = cls.env["res.partner"].create(
|
|
{"name": "Outsider Customer", "is_company": True}
|
|
)
|
|
cls.portal_user = cls._create_portal_user("portal-subscriber", cls.contact)
|
|
cls.outsider_user = cls._create_portal_user("portal-outsider", cls.outsider)
|
|
cls.subscription = cls._create_subscription(cls.customer)
|
|
cls.outsider_subscription = cls._create_subscription(cls.outsider)
|
|
cls._setup_minimal_accounting()
|
|
|
|
@classmethod
|
|
def _create_portal_user(cls, login, partner):
|
|
"""Create a portal user bound to an existing partner.
|
|
|
|
:param login: login and password of the new user.
|
|
:param partner: ``res.partner`` record used as the user contact.
|
|
:return: the created ``res.users`` record.
|
|
"""
|
|
return cls.env["res.users"].create(
|
|
{
|
|
"name": login,
|
|
"login": login,
|
|
"password": login,
|
|
"partner_id": partner.id,
|
|
"groups_id": [Command.set(cls.portal_group.ids)],
|
|
}
|
|
)
|
|
|
|
@classmethod
|
|
def _create_subscription(cls, partner):
|
|
"""Create a one-line subscription billed to ``partner``.
|
|
|
|
The stage is forced because ``subscription_oca`` only defaults it when
|
|
both ``date_start`` and ``recurring_next_date`` are passed to ``create``.
|
|
|
|
:param partner: ``res.partner`` record billed by the subscription.
|
|
:return: the created ``sale.subscription`` record.
|
|
"""
|
|
return cls.env["sale.subscription"].create(
|
|
{
|
|
"partner_id": partner.id,
|
|
"template_id": cls.template.id,
|
|
"pricelist_id": cls.pricelist.id,
|
|
"stage_id": cls.draft_stage.id,
|
|
"sale_subscription_line_ids": [
|
|
Command.create(
|
|
{
|
|
"product_id": cls.product.id,
|
|
"name": "Portal subscription line",
|
|
"product_uom_qty": 2.0,
|
|
"price_unit": 50.0,
|
|
"tax_ids": [Command.clear()],
|
|
}
|
|
)
|
|
],
|
|
}
|
|
)
|
|
|
|
@classmethod
|
|
def _setup_minimal_accounting(cls):
|
|
"""Declare the accounts and journal required to post a customer invoice.
|
|
|
|
:return: None. Sets ``cls.income_account`` and ``cls.sale_journal``.
|
|
"""
|
|
cls.income_account = cls.env["account.account"].create(
|
|
{
|
|
"name": "Portal Test Income",
|
|
"code": "PTI400",
|
|
"account_type": "income",
|
|
}
|
|
)
|
|
receivable_account = cls.env["account.account"].create(
|
|
{
|
|
"name": "Portal Test Receivable",
|
|
"code": "PTR110",
|
|
"account_type": "asset_receivable",
|
|
"reconcile": True,
|
|
}
|
|
)
|
|
cls.sale_journal = cls.env["account.journal"].create(
|
|
{
|
|
"name": "Portal Test Sales",
|
|
"code": "PTSAJ",
|
|
"type": "sale",
|
|
"default_account_id": cls.income_account.id,
|
|
}
|
|
)
|
|
(cls.customer | cls.contact).property_account_receivable_id = receivable_account
|
|
|
|
def _create_subscription_invoice(self, post):
|
|
"""Create an invoice attached to ``self.subscription``.
|
|
|
|
:param post: whether the invoice must be posted; a draft invoice is
|
|
invisible to portal users by design.
|
|
:return: the created ``account.move`` record.
|
|
"""
|
|
invoice = self.env["account.move"].create(
|
|
{
|
|
"move_type": "out_invoice",
|
|
"partner_id": self.customer.id,
|
|
"journal_id": self.sale_journal.id,
|
|
"invoice_date": fields.Date.today(),
|
|
"subscription_id": self.subscription.id,
|
|
"invoice_line_ids": [
|
|
Command.create(
|
|
{
|
|
"name": "Portal invoice line",
|
|
"quantity": 1.0,
|
|
"price_unit": 100.0,
|
|
"account_id": self.income_account.id,
|
|
"tax_ids": [Command.clear()],
|
|
}
|
|
)
|
|
],
|
|
}
|
|
)
|
|
if post:
|
|
invoice.action_post()
|
|
invoice.message_subscribe(partner_ids=self.customer.ids)
|
|
return invoice
|
|
|
|
def _authenticated_open(self, user, url, allow_redirects=True):
|
|
"""Log ``user`` in and request ``url``.
|
|
|
|
:param user: ``res.users`` record whose login equals its password.
|
|
:param url: portal URL to request.
|
|
:param allow_redirects: whether the client follows redirections.
|
|
:return: the ``requests.Response`` object.
|
|
"""
|
|
self.authenticate(user.login, user.login)
|
|
return self.url_open(url, allow_redirects=allow_redirects)
|
|
|
|
def test_access_url_points_to_portal_page(self):
|
|
self.assertEqual(
|
|
self.subscription.access_url,
|
|
f"/my/subscriptions/{self.subscription.id}",
|
|
)
|
|
|
|
def test_record_rule_hides_other_partners_subscriptions(self):
|
|
visible = self.env["sale.subscription"].with_user(self.portal_user).search([])
|
|
self.assertEqual(visible, self.subscription)
|
|
|
|
def test_record_rule_hides_other_partners_lines(self):
|
|
visible = (
|
|
self.env["sale.subscription.line"].with_user(self.portal_user).search([])
|
|
)
|
|
self.assertEqual(visible, self.subscription.sale_subscription_line_ids)
|
|
|
|
def test_list_shows_only_own_subscriptions(self):
|
|
response = self._authenticated_open(self.portal_user, "/my/subscriptions")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn(self.subscription.name, response.text)
|
|
self.assertNotIn(self.outsider_subscription.name, response.text)
|
|
|
|
def test_list_accepts_every_declared_sorting(self):
|
|
for sortby in ("date_start", "recurring_next_date", "code"):
|
|
with self.subTest(sortby=sortby):
|
|
response = self._authenticated_open(
|
|
self.portal_user, f"/my/subscriptions?sortby={sortby}"
|
|
)
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_detail_is_readable_by_owner(self):
|
|
response = self._authenticated_open(
|
|
self.portal_user, f"/my/subscriptions/{self.subscription.id}"
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn("Portal subscription line", response.text)
|
|
|
|
def test_detail_is_denied_to_other_portal_user(self):
|
|
response = self._authenticated_open(
|
|
self.outsider_user,
|
|
f"/my/subscriptions/{self.subscription.id}",
|
|
allow_redirects=False,
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 303)
|
|
self.assertTrue(response.headers["Location"].endswith("/my"))
|
|
|
|
def test_detail_is_readable_with_access_token(self):
|
|
token = self.subscription._portal_ensure_token()
|
|
|
|
# A public session, so that the request resolves to the test database.
|
|
self.authenticate(None, None)
|
|
response = self.url_open(
|
|
f"/my/subscriptions/{self.subscription.id}?access_token={token}"
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn("Portal subscription line", response.text)
|
|
|
|
def test_home_counter_counts_only_own_subscriptions(self):
|
|
self.authenticate(self.portal_user.login, self.portal_user.login)
|
|
|
|
counters = self.make_jsonrpc_request(
|
|
"/my/counters", {"counters": ["subscription_count"]}
|
|
)
|
|
|
|
self.assertEqual(counters["subscription_count"], 1)
|
|
|
|
def test_home_declares_the_subscription_entry(self):
|
|
response = self._authenticated_open(self.portal_user, "/my")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn("subscription_count", response.text)
|
|
|
|
def test_detail_lists_posted_invoices_only(self):
|
|
self._create_subscription_invoice(post=False)
|
|
posted_invoice = self._create_subscription_invoice(post=True)
|
|
|
|
response = self._authenticated_open(
|
|
self.portal_user, f"/my/subscriptions/{self.subscription.id}"
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(response.text.count(INVOICE_LINK_CLASS), 1)
|
|
self.assertIn(posted_invoice.name, response.text)
|
|
|
|
def test_detail_hides_invoices_from_token_visitor(self):
|
|
self._create_subscription_invoice(post=True)
|
|
token = self.subscription._portal_ensure_token()
|
|
|
|
self.authenticate(None, None)
|
|
response = self.url_open(
|
|
f"/my/subscriptions/{self.subscription.id}?access_token={token}"
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertNotIn(INVOICE_LINK_CLASS, response.text)
|
|
|
|
def test_tour(self):
|
|
self.start_tour(
|
|
"/my", "subscription_oca_portal_tour", login=self.portal_user.login
|
|
)
|