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.

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