Debian Codegraph @

addons/website_sale_loyalty/tests/test_free_product_reward.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import Command, http
4 from odoo.tests import tagged
5
6 from odoo.addons.base.tests.common import HttpCaseWithUserPortal
7 from odoo.addons.website_sale.tests.common import MockRequest, WebsiteSaleCommon
8 from odoo.addons.website_sale_loyalty.controllers.cart import Cart
9 from odoo.addons.website_sale_loyalty.controllers.main import WebsiteSale
10
11
12 @tagged('post_install', '-at_install')
13 class TestFreeProductReward(HttpCaseWithUserPortal, WebsiteSaleCommon):
14
15 @classmethod
16 def setUpClass(cls):
17 super().setUpClass()
18
19 cls.WebsiteSaleCartController = Cart()
20 cls.WebsiteSaleController = WebsiteSale()
21
22 cls.website = cls.website.with_user(cls.user_portal)
23 cls.empty_cart.partner_id = cls.partner_portal
24
25 cls.sofa, cls.carpet = cls.env['product.product'].create([
26 {
27 'name': "Test Sofa",
28 'list_price': 2950.0,
29 'website_published': True,
30 },
31 {
32 'name': "Test Carpet",
33 'list_price': 500.0,
34 'website_published': True,
35 },
36 ])
37
38 # Disable any other program
39 cls.program = cls.env['loyalty.program'].search([]).write({'active': False})
40
41 cls.program = cls.env['loyalty.program'].create({
42 'name': 'Get a product for free',
43 'program_type': 'promotion',
44 'applies_on': 'current',
45 'trigger': 'auto',
46 'rule_ids': [Command.create({
47 'minimum_qty': 1,
48 'minimum_amount': 0.00,
49 'reward_point_amount': 1,
50 'reward_point_mode': 'order',
51 'product_ids': cls.sofa,
52 })],
53 'reward_ids': [Command.create({
54 'reward_type': 'product',
55 'reward_product_id': cls.carpet.id,
56 'reward_product_qty': 1,
57 'required_points': 1,
58 })],
59 })
60
61 installed_modules = cls.env['ir.module.module'].search([('state', '=', 'installed')])
62 for _ in http._generate_routing_rules(installed_modules.mapped('name'), nodb_only=False):
63 pass
64
65 def test_add_product_to_cart_when_it_exist_as_free_product(self):
66 # This test the flow when we claim a reward in the cart page and then we
67 # want to add the product again
68 order = self.empty_cart
69 with MockRequest(self.website.env, website=self.website, sale_order_id=order.id):
70 self.WebsiteSaleCartController.add_to_cart(
71 product_template_id=self.sofa.product_tmpl_id,
72 product_id=self.sofa.id,
73 quantity=1,
74 )
75 self.WebsiteSaleController.claim_reward(self.program.reward_ids[0].id)
76 self.WebsiteSaleCartController.add_to_cart(
77 product_template_id=self.carpet.product_tmpl_id,
78 product_id=self.carpet.id,
79 quantity=1,
80 )
81 sofa_line = order.order_line.filtered(lambda line: line.product_id.id == self.sofa.id)
82 carpet_reward_line = order.order_line.filtered(lambda line: line.product_id.id == self.carpet.id and line.is_reward_line)
83 carpet_line = order.order_line.filtered(lambda line: line.product_id.id == self.carpet.id and not line.is_reward_line)
84 self.assertEqual(sofa_line.product_uom_qty, 1, "Should have only 1 qty of Sofa")
85 self.assertEqual(carpet_reward_line.product_uom_qty, 1, "Should have only 1 qty for the carpet as reward")
86 self.assertEqual(carpet_line.product_uom_qty, 1, "Should have only 1 qty for carpet as non reward")
87
88 def test_get_claimable_free_shipping(self):
89 cart = self.empty_cart
90 self.program.write({
91 'program_type': 'next_order_coupons',
92 'applies_on': 'future',
93 'coupon_ids': [
94 Command.clear(),
95 Command.create({'partner_id': cart.partner_id.id, 'points': 100}),
96 ],
97 'reward_ids': [Command.update(self.program.reward_ids.id, {
98 'reward_type': 'shipping',
99 'reward_product_id': None,
100 })],
101 })
102 coupon = self.program.coupon_ids
103
104 with MockRequest(self.website.env, website=self.website, sale_order_id=cart.id):
105 self.assertDictEqual(cart._get_claimable_and_showable_rewards(), {
106 coupon: self.program.reward_ids,
107 })
108 self.WebsiteSaleCartController.add_to_cart(
109 product_template_id=self.sofa.product_tmpl_id,
110 product_id=self.sofa.id,
111 quantity=1,
112 )
113 self.WebsiteSaleController.claim_reward(self.program.reward_ids.id, code=coupon.code)
114 self.assertTrue(cart.order_line.reward_id)
115 self.assertFalse(
116 cart._get_claimable_and_showable_rewards(),
117 "Rewards should no longer be claimable if already claimed",
118 )