test_link_preview.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from functools import partial
  4. from odoo.addons.mail.tests.common import mail_new_test_user
  5. from odoo.addons.mail.tests.common import MailCommon
  6. from unittest.mock import patch
  7. import requests
  8. mail_channel_new_test_user = partial(mail_new_test_user, context={'mail_channel_nosubscribe': False})
  9. def _patched_get_html(*args, **kwargs):
  10. response = requests.Response()
  11. response.status_code = 200
  12. response._content = b"""
  13. <html>
  14. <head>
  15. <meta property="og:title" content="Test title">
  16. <meta property="og:description" content="Test description">
  17. </head>
  18. </html>
  19. """
  20. response.headers["Content-Type"] = 'text/html'
  21. return response
  22. def _patch_head_html(*args, **kwargs):
  23. response = requests.Response()
  24. response.status_code = 200
  25. response.headers["Content-Type"] = 'text/html'
  26. return response
  27. class TestLinkPreview(MailCommon):
  28. @classmethod
  29. def setUpClass(cls):
  30. super().setUpClass()
  31. cls.user_1 = mail_channel_new_test_user(
  32. cls.env, login='user_1',
  33. name='User 1',
  34. groups='base.group_user')
  35. cls.public_channel = cls.env['mail.channel'].create({
  36. 'name': 'Public channel of user 1',
  37. 'channel_type': 'channel',
  38. })
  39. cls.public_channel.channel_member_ids.unlink()
  40. def test_01_link_preview_throttle(self):
  41. with patch.object(requests.Session, 'get', _patched_get_html), patch.object(requests.Session, 'head', _patch_head_html):
  42. throttle = int(self.env['ir.config_parameter'].sudo().get_param('mail.link_preview_throttle', 99))
  43. link_previews = []
  44. for _ in range(throttle):
  45. link_previews.append({'source_url': 'https://thisdomainedoentexist.nothing', 'message_id': 1})
  46. self.env['mail.link.preview'].create(link_previews)
  47. message = self.env['mail.message'].create({
  48. 'model': 'mail.channel',
  49. 'res_id': self.public_channel.id,
  50. 'body': '<a href="https://thisdomainedoentexist.nothing">Nothing link</a>',
  51. })
  52. self.env['mail.link.preview']._create_link_previews(message)
  53. link_preview_count = self.env['mail.link.preview'].search_count([('source_url', '=', 'https://thisdomainedoentexist.nothing')])
  54. self.assertEqual(link_preview_count, throttle + 1)
  55. def test_02_link_preview_create(self):
  56. with patch.object(requests.Session, 'get', _patched_get_html), patch.object(requests.Session, 'head', _patch_head_html):
  57. message = self.env['mail.message'].create({
  58. 'model': 'mail.channel',
  59. 'res_id': self.public_channel.id,
  60. 'body': '<a href="https://thisdomainedoentexist.nothing">Nothing link</a>',
  61. })
  62. self.env['mail.link.preview']._create_link_previews(message)
  63. self.assertBusNotifications(
  64. [(self.cr.dbname, 'mail.channel', self.public_channel.id)],
  65. message_items=[{
  66. 'type': 'mail.link.preview/insert',
  67. 'payload': [{
  68. 'id': link_preview.id,
  69. 'message': {'id': message.id},
  70. 'image_mimetype': False,
  71. 'og_description': 'Test description',
  72. 'og_image': False,
  73. 'og_mimetype': False,
  74. 'og_title': 'Test title',
  75. 'og_type': False,
  76. 'source_url': 'https://thisdomainedoentexist.nothing',
  77. } for link_preview in message.link_preview_ids]
  78. }]
  79. )