[FIX] website_mail_channel: unsubscription

Fixes 27280fefa9:

* `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
This commit is contained in:
xmo-odoo
2017-10-25 15:31:58 +02:00
committed by GitHub
parent 598ac60a77
commit f13cab6aa6
3 changed files with 82 additions and 5 deletions
@@ -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')
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import test_unsubscribe
@@ -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()))