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.

210 lines
8.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # Copyright 2017 LasLabs Inc.
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. import os
  4. import mock
  5. from odoo.modules import get_module_path
  6. from odoo.tests.common import TransactionCase
  7. from odoo.tools import mute_logger
  8. from odoo.addons.module_auto_update.addon_hash import addon_hash
  9. from ..models.module_deprecated import PARAM_DEPRECATED
  10. model = 'odoo.addons.module_auto_update.models.module'
  11. class EndTestException(Exception):
  12. pass
  13. class TestModule(TransactionCase):
  14. def setUp(self):
  15. super(TestModule, self).setUp()
  16. module_name = 'module_auto_update'
  17. self.env["ir.config_parameter"].set_param(PARAM_DEPRECATED, "1")
  18. self.own_module = self.env['ir.module.module'].search([
  19. ('name', '=', module_name),
  20. ])
  21. self.own_dir_path = get_module_path(module_name)
  22. keep_langs = self.env['res.lang'].search([]).mapped('code')
  23. self.own_checksum = addon_hash(
  24. self.own_dir_path,
  25. exclude_patterns=['*.pyc', '*.pyo', '*.pot', 'static/*'],
  26. keep_langs=keep_langs,
  27. )
  28. self.own_writeable = os.access(self.own_dir_path, os.W_OK)
  29. @mock.patch('%s.get_module_path' % model)
  30. def create_test_module(self, vals, get_module_path_mock):
  31. get_module_path_mock.return_value = self.own_dir_path
  32. test_module = self.env['ir.module.module'].create(vals)
  33. return test_module
  34. def test_store_checksum_installed_state_installed(self):
  35. """It should set the module's checksum_installed equal to
  36. checksum_dir when vals contain a ``latest_version`` str."""
  37. self.own_module.checksum_installed = 'test'
  38. self.own_module._store_checksum_installed({'latest_version': '1.0'})
  39. self.assertEqual(
  40. self.own_module.checksum_installed, self.own_module.checksum_dir,
  41. )
  42. def test_store_checksum_installed_state_uninstalled(self):
  43. """It should clear the module's checksum_installed when vals
  44. contain ``"latest_version": False``"""
  45. self.own_module.checksum_installed = 'test'
  46. self.own_module._store_checksum_installed({'latest_version': False})
  47. self.assertIs(self.own_module.checksum_installed, False)
  48. def test_store_checksum_installed_vals_contain_checksum_installed(self):
  49. """It should not set checksum_installed to False or checksum_dir when
  50. a checksum_installed is included in vals"""
  51. self.own_module.checksum_installed = 'test'
  52. self.own_module._store_checksum_installed({
  53. 'state': 'installed',
  54. 'checksum_installed': 'test',
  55. })
  56. self.assertEqual(
  57. self.own_module.checksum_installed, 'test',
  58. 'Providing checksum_installed in vals did not prevent overwrite',
  59. )
  60. def test_store_checksum_installed_with_retain_context(self):
  61. """It should not set checksum_installed to False or checksum_dir when
  62. self has context retain_checksum_installed=True"""
  63. self.own_module.checksum_installed = 'test'
  64. self.own_module.with_context(
  65. retain_checksum_installed=True,
  66. )._store_checksum_installed({'state': 'installed'})
  67. self.assertEqual(
  68. self.own_module.checksum_installed, 'test',
  69. 'Providing retain_checksum_installed context did not prevent '
  70. 'overwrite',
  71. )
  72. @mock.patch('%s.get_module_path' % model)
  73. def test_button_uninstall_no_recompute(self, module_path_mock):
  74. """It should not attempt update on `button_uninstall`."""
  75. module_path_mock.return_value = self.own_dir_path
  76. vals = {
  77. 'name': 'module_auto_update_test_module',
  78. 'state': 'installed',
  79. }
  80. test_module = self.create_test_module(vals)
  81. test_module.checksum_installed = 'test'
  82. uninstall_module = self.env['ir.module.module'].search([
  83. ('name', '=', 'web'),
  84. ])
  85. uninstall_module.button_uninstall()
  86. self.assertNotEqual(
  87. test_module.state, 'to upgrade',
  88. 'Auto update logic was triggered during uninstall.',
  89. )
  90. def test_button_immediate_uninstall_no_recompute(self):
  91. """It should not attempt update on `button_immediate_uninstall`."""
  92. uninstall_module = self.env['ir.module.module'].search([
  93. ('name', '=', 'web'),
  94. ])
  95. try:
  96. mk = mock.MagicMock()
  97. uninstall_module._patch_method('button_uninstall', mk)
  98. mk.side_effect = EndTestException
  99. with self.assertRaises(EndTestException):
  100. uninstall_module.button_immediate_uninstall()
  101. finally:
  102. uninstall_module._revert_method('button_uninstall')
  103. def test_button_uninstall_cancel(self):
  104. """It should preserve checksum_installed when cancelling uninstall"""
  105. self.own_module.write({'state': 'to remove'})
  106. self.own_module.checksum_installed = 'test'
  107. self.own_module.button_uninstall_cancel()
  108. self.assertEqual(
  109. self.own_module.checksum_installed, 'test',
  110. 'Uninstall cancellation does not preserve checksum_installed',
  111. )
  112. def test_button_upgrade_cancel(self):
  113. """It should preserve checksum_installed when cancelling upgrades"""
  114. self.own_module.write({'state': 'to upgrade'})
  115. self.own_module.checksum_installed = 'test'
  116. self.own_module.button_upgrade_cancel()
  117. self.assertEqual(
  118. self.own_module.checksum_installed, 'test',
  119. 'Upgrade cancellation does not preserve checksum_installed',
  120. )
  121. def test_create(self):
  122. """It should call _store_checksum_installed method"""
  123. _store_checksum_installed_mock = mock.MagicMock()
  124. try:
  125. self.env['ir.module.module']._patch_method(
  126. '_store_checksum_installed',
  127. _store_checksum_installed_mock,
  128. )
  129. vals = {
  130. 'name': 'module_auto_update_test_module',
  131. 'state': 'installed',
  132. }
  133. self.create_test_module(vals)
  134. _store_checksum_installed_mock.assert_called_once_with(vals)
  135. finally:
  136. self.env['ir.module.module']._revert_method(
  137. '_store_checksum_installed',
  138. )
  139. @mute_logger("openerp.modules.module")
  140. @mock.patch('%s.get_module_path' % model)
  141. def test_get_module_list(self, module_path_mock):
  142. """It should change the state of modules with different
  143. checksum_dir and checksum_installed to 'to upgrade'"""
  144. module_path_mock.return_value = self.own_dir_path
  145. vals = {
  146. 'name': 'module_auto_update_test_module',
  147. 'state': 'installed',
  148. }
  149. test_module = self.create_test_module(vals)
  150. test_module.checksum_installed = 'test'
  151. self.env['base.module.upgrade'].get_module_list()
  152. self.assertEqual(
  153. test_module.state, 'to upgrade',
  154. 'List update does not mark upgradeable modules "to upgrade"',
  155. )
  156. @mock.patch('%s.get_module_path' % model)
  157. def test_get_module_list_only_changes_installed(self, module_path_mock):
  158. """It should not change the state of a module with a former state
  159. other than 'installed' to 'to upgrade'"""
  160. module_path_mock.return_value = self.own_dir_path
  161. vals = {
  162. 'name': 'module_auto_update_test_module',
  163. 'state': 'uninstalled',
  164. }
  165. test_module = self.create_test_module(vals)
  166. self.env['base.module.upgrade'].get_module_list()
  167. self.assertNotEqual(
  168. test_module.state, 'to upgrade',
  169. 'List update changed state of an uninstalled module',
  170. )
  171. def test_write(self):
  172. """It should call _store_checksum_installed method"""
  173. _store_checksum_installed_mock = mock.MagicMock()
  174. self.env['ir.module.module']._patch_method(
  175. '_store_checksum_installed',
  176. _store_checksum_installed_mock,
  177. )
  178. vals = {'state': 'installed'}
  179. self.own_module.write(vals)
  180. _store_checksum_installed_mock.assert_called_once_with(vals)
  181. self.env['ir.module.module']._revert_method(
  182. '_store_checksum_installed',
  183. )