From f13cab6aa6d3181336abbfbe52dbc6330dab6fc2 Mon Sep 17 00:00:00 2001 From: xmo-odoo Date: Wed, 25 Oct 2017 15:31:58 +0200 Subject: [PATCH] [FIX] website_mail_channel: unsubscription Fixes 27280fefa9e7cad0143aca688149f19ebdb684f6: * `partner_id` is an `int` not a recordset * qweb Response is lazy (the template is only looked up and rendered when we actually need to send the content over the wire) => the error would occur outside of the try..except block. Force rendering (`Response.flatten`) so we can handle the issue here * Uses the HttpCase interface to contact the server over a new socket (same as tours & al) because using `werkzeug.test.Client` would not use the correct cursor (?) and so the test data would not be visible --- .../website_mail_channel/controllers/main.py | 14 ++-- addons/website_mail_channel/tests/__init__.py | 2 + .../tests/test_unsubscribe.py | 71 +++++++++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 addons/website_mail_channel/tests/__init__.py create mode 100644 addons/website_mail_channel/tests/test_unsubscribe.py diff --git a/addons/website_mail_channel/controllers/main.py b/addons/website_mail_channel/controllers/main.py index 4a9e2c449a8..3aa9491bf54 100644 --- a/addons/website_mail_channel/controllers/main.py +++ b/addons/website_mail_channel/controllers/main.py @@ -228,15 +228,19 @@ class MailGroup(http.Controller): def confirm_unsubscribe(self, channel, partner_id, token, **kw): subscriber = request.env['mail.channel.partner'].search([('channel_id', '=', channel.id), ('partner_id', '=', partner_id)]) if not subscriber: + partner = request.env['res.partner'].browse(partner_id).sudo().exists() # FIXME: remove try/except in master try: - return request.render( - 'website_mail_channel.not_subscribed', { - 'partner_id': partner_id - }) + response = request.render( + 'website_mail_channel.not_subscribed', + {'partner_id': partner}) + # make sure the rendering (and thus error if template is + # missing) happens inside the try block + response.flatten() + return response except ValueError: return _("The address %s is already unsubscribed or was never subscribed to any mailing list") % ( - partner_id.email + partner.email ) subscriber_token = channel._generate_action_token(partner_id, action='unsubscribe') diff --git a/addons/website_mail_channel/tests/__init__.py b/addons/website_mail_channel/tests/__init__.py new file mode 100644 index 00000000000..2e3364d701d --- /dev/null +++ b/addons/website_mail_channel/tests/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +from . import test_unsubscribe diff --git a/addons/website_mail_channel/tests/test_unsubscribe.py b/addons/website_mail_channel/tests/test_unsubscribe.py new file mode 100644 index 00000000000..44128c30b86 --- /dev/null +++ b/addons/website_mail_channel/tests/test_unsubscribe.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +import requests + +from odoo.api import Environment +from odoo.tests import common +from odoo.tools.misc import mute_logger + + +class TestConfirmUnsubscribe(common.HttpCase): + def setUp(self): + super(TestConfirmUnsubscribe, self).setUp() + + cr = self.registry.cursor() + # apparently HttpCase does not properly update self.env? + self.env2 = env = Environment(cr, self.uid, {}) + self.partner = env['res.partner'].create({ + 'name': 'Bob', + 'email': 'bob@bob.bob' + }) + self.mailing_list = env['mail.channel'].create({ + 'name': 'Test Mailing List', + 'public': 'public', + }) + self.token = self.mailing_list._generate_action_token(self.partner.id, action='unsubscribe') + + def test_not_subscribed(self): + """Test warning works""" + self.env2.cr.release() + + self._unsubscribe_check("The address %s is already unsubscribed" % self.partner.email) + + @mute_logger('odoo.addons.website.models.ir_ui_view') + def test_not_subscribed_no_template(self): + """ Test warning works on db without template (code update w/o module update) """ + self.env2.ref('website_mail_channel.not_subscribed').unlink() + self.assertEquals( + self.env2['ir.model.data'].search_count([ + ('module', '=', 'website_mail_channel'), + ('name', '=', 'not_subscribed'), + ]), 0, 'XID for template should have been deleted') + self.env2.cr.release() + + self._unsubscribe_check("The address %s is already unsubscribed or was never subscribed to any mailing list" % self.partner.email) + + def test_wrong_token(self): + self.mailing_list.sudo().write({ + 'channel_partner_ids': [(4, self.partner.id, False)] + }) + self.env2.cr.release() + self.token = 'XXX' + + self._unsubscribe_check("Invalid or expired confirmation link.") + + def test_successful_unsubscribe(self): + self.mailing_list.sudo().write({ + 'channel_partner_ids': [(4, self.partner.id, False)] + }) + self.env2.cr.release() + + self._unsubscribe_check("You have been correctly unsubscribed") + + def _unsubscribe_check(self, text): + url = "/groups/unsubscribe/{}/{}/{}".format( + self.mailing_list.id, self.partner.id, + self.token + ) + # FIXME: may need to be altered during 11.0 forward port + r = self.url_open(url) + body = r.read().decode('utf-8') + # normalize space to make matching simpler + self.assertIn(text, u' '.join(body.split()))