You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. import mock
  5. from odoo import _
  6. from odoo.exceptions import AccessError
  7. from odoo.tests.common import HttpCase, TransactionCase
  8. class TestExport(TransactionCase):
  9. def setUp(self):
  10. super(TestExport, self).setUp()
  11. export_group = self.env.ref('base_export_security.export_group')
  12. self.authorized_user = self.env['res.users'].create({
  13. 'login': 'exporttestuser',
  14. 'partner_id': self.env['res.partner'].create({
  15. 'name': "Export Test User"
  16. }).id,
  17. 'groups_id': [(4, export_group.id, 0)],
  18. })
  19. self.unauthorized_user = self.env['res.users'].create({
  20. 'login': 'unauthorizedexporttestuser',
  21. 'partner_id': self.env['res.partner'].create({
  22. 'name': "Unauthorized Export Test User"
  23. }).id,
  24. })
  25. self.model_name = 'ir.module.module'
  26. self.recordset = self.env[self.model_name].search([])
  27. self.field_names = ['name', 'id']
  28. self.model = self.env['ir.model'].search([
  29. ('model', '=', self.model_name),
  30. ])
  31. self.records = self.env['ir.model.data'].search([
  32. ('model', '=', self.model_name),
  33. ('res_id', 'in', self.recordset.ids),
  34. ])
  35. self.fields = self.env['ir.model.fields'].search([
  36. ('model', '=', self.model_name),
  37. ('name', 'in', self.field_names),
  38. ])
  39. def test_log_export(self):
  40. """It should create a new Export record with correct data"""
  41. log = self.env['export.event'].sudo(self.authorized_user).log_export(
  42. self.recordset,
  43. self.field_names,
  44. )
  45. self.assertEqual(
  46. [log.model_id, log.field_ids, log.record_ids, log.user_id],
  47. [self.model, self.fields, self.records, self.authorized_user],
  48. 'Log not created properly',
  49. )
  50. def test_log_export_posts_notification(self):
  51. """It should call post_notification method"""
  52. post_notification_mock = mock.MagicMock()
  53. self.env['export.event']._patch_method(
  54. 'post_notification',
  55. post_notification_mock,
  56. )
  57. self.env['export.event'].sudo(self.authorized_user).log_export(
  58. self.recordset,
  59. self.field_names,
  60. )
  61. post_notification_mock.assert_called_once_with()
  62. self.env['export.event']._revert_method('post_notification')
  63. def test_post_notification(self):
  64. """It should post a notification with appropriate data
  65. to the #data export channel"""
  66. export = self.env['export.event'].create({
  67. 'model_id': self.model.id,
  68. 'field_ids': [(4, self.fields.ids)],
  69. 'record_ids': [(4, self.records.ids)],
  70. 'user_id': self.authorized_user.id,
  71. })
  72. message = export.sudo().post_notification()
  73. field_labels = ', '.join(
  74. self.fields.sorted().mapped('field_description'),
  75. )
  76. message_data = {
  77. 'records': len(self.records.ids),
  78. 'model': self.model.name,
  79. 'user': self.authorized_user.name,
  80. 'fields': field_labels,
  81. }
  82. message_body = _(
  83. '<p>%(records)d <b>%(model)s</b> records exported by <b>%(user)s'
  84. '</b>.<br><b>Fields exported:</b> %(fields)s</p>'
  85. ) % message_data
  86. self.assertEqual(
  87. [message.body, message.message_type, message.model],
  88. [message_body, 'notification', 'mail.channel'],
  89. 'Message not posted properly',
  90. )
  91. def test_export_data_access(self):
  92. """It should raise AccessError if user does not have export rights"""
  93. with self.assertRaises(AccessError):
  94. self.env[self.model_name].sudo(
  95. self.unauthorized_user
  96. ).export_data(self, None)
  97. def test_export_data_calls_log_export(self):
  98. """It should call log_export if user has export rights"""
  99. log_export_mock = mock.MagicMock()
  100. self.env['export.event']._patch_method('log_export', log_export_mock)
  101. model = self.env[self.model_name]
  102. model.sudo(self.authorized_user).export_data(self.field_names)
  103. log_export_mock.assert_called_once_with(model, self.field_names)
  104. self.env['export.event']._revert_method('log_export')
  105. class TestJS(HttpCase):
  106. def test_export_visibility(self):
  107. """Test visibility of export menu item"""
  108. self.phantom_js(
  109. "/web/tests?debug=assets&module=base_export_security",
  110. "",
  111. login="admin",
  112. )