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.

255 lines
9.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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 tempfile
  6. import mock
  7. from odoo.modules import get_module_path
  8. from odoo.tests.common import TransactionCase
  9. from odoo.tools import mute_logger
  10. from ..addon_hash import addon_hash
  11. from .. import post_init_hook
  12. model = 'odoo.addons.module_auto_update.models.module_deprecated'
  13. class EndTestException(Exception):
  14. pass
  15. class TestModule(TransactionCase):
  16. def setUp(self):
  17. super(TestModule, self).setUp()
  18. module_name = 'module_auto_update'
  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_compute_checksum_dir(self):
  36. """It should compute the directory's SHA-1 hash"""
  37. self.assertEqual(
  38. self.own_module.checksum_dir, self.own_checksum,
  39. 'Module directory checksum not computed properly',
  40. )
  41. def test_compute_checksum_dir_ignore_excluded(self):
  42. """It should exclude .pyc/.pyo extensions from checksum
  43. calculations"""
  44. if not self.own_writeable:
  45. self.skipTest("Own directory not writeable")
  46. with tempfile.NamedTemporaryFile(
  47. suffix='.pyc', dir=self.own_dir_path):
  48. self.assertEqual(
  49. self.own_module.checksum_dir, self.own_checksum,
  50. 'SHA1 checksum does not ignore excluded extensions',
  51. )
  52. def test_compute_checksum_dir_recomputes_when_file_added(self):
  53. """It should return a different value when a non-.pyc/.pyo file is
  54. added to the module directory"""
  55. if not self.own_writeable:
  56. self.skipTest("Own directory not writeable")
  57. with tempfile.NamedTemporaryFile(
  58. suffix='.py', dir=self.own_dir_path):
  59. self.assertNotEqual(
  60. self.own_module.checksum_dir, self.own_checksum,
  61. 'SHA1 checksum not recomputed',
  62. )
  63. def test_store_checksum_installed_state_installed(self):
  64. """It should set the module's checksum_installed equal to
  65. checksum_dir when vals contain a ``latest_version`` str."""
  66. self.own_module.checksum_installed = 'test'
  67. self.own_module._store_checksum_installed({'latest_version': '1.0'})
  68. self.assertEqual(
  69. self.own_module.checksum_installed, self.own_module.checksum_dir,
  70. )
  71. def test_store_checksum_installed_state_uninstalled(self):
  72. """It should clear the module's checksum_installed when vals
  73. contain ``"latest_version": False``"""
  74. self.own_module.checksum_installed = 'test'
  75. self.own_module._store_checksum_installed({'latest_version': False})
  76. self.assertIs(self.own_module.checksum_installed, False)
  77. def test_store_checksum_installed_vals_contain_checksum_installed(self):
  78. """It should not set checksum_installed to False or checksum_dir when
  79. a checksum_installed is included in vals"""
  80. self.own_module.checksum_installed = 'test'
  81. self.own_module._store_checksum_installed({
  82. 'state': 'installed',
  83. 'checksum_installed': 'test',
  84. })
  85. self.assertEqual(
  86. self.own_module.checksum_installed, 'test',
  87. 'Providing checksum_installed in vals did not prevent overwrite',
  88. )
  89. def test_store_checksum_installed_with_retain_context(self):
  90. """It should not set checksum_installed to False or checksum_dir when
  91. self has context retain_checksum_installed=True"""
  92. self.own_module.checksum_installed = 'test'
  93. self.own_module.with_context(
  94. retain_checksum_installed=True,
  95. )._store_checksum_installed({'state': 'installed'})
  96. self.assertEqual(
  97. self.own_module.checksum_installed, 'test',
  98. 'Providing retain_checksum_installed context did not prevent '
  99. 'overwrite',
  100. )
  101. @mock.patch('%s.get_module_path' % model)
  102. def test_button_uninstall_no_recompute(self, module_path_mock):
  103. """It should not attempt update on `button_uninstall`."""
  104. module_path_mock.return_value = self.own_dir_path
  105. vals = {
  106. 'name': 'module_auto_update_test_module',
  107. 'state': 'installed',
  108. }
  109. test_module = self.create_test_module(vals)
  110. test_module.checksum_installed = 'test'
  111. uninstall_module = self.env['ir.module.module'].search([
  112. ('name', '=', 'web'),
  113. ])
  114. uninstall_module.button_uninstall()
  115. self.assertNotEqual(
  116. test_module.state, 'to upgrade',
  117. 'Auto update logic was triggered during uninstall.',
  118. )
  119. def test_button_immediate_uninstall_no_recompute(self):
  120. """It should not attempt update on `button_immediate_uninstall`."""
  121. uninstall_module = self.env['ir.module.module'].search([
  122. ('name', '=', 'web'),
  123. ])
  124. try:
  125. mk = mock.MagicMock()
  126. uninstall_module._patch_method('button_uninstall', mk)
  127. mk.side_effect = EndTestException
  128. with self.assertRaises(EndTestException):
  129. uninstall_module.button_immediate_uninstall()
  130. finally:
  131. uninstall_module._revert_method('button_uninstall')
  132. def test_button_uninstall_cancel(self):
  133. """It should preserve checksum_installed when cancelling uninstall"""
  134. self.own_module.write({'state': 'to remove'})
  135. self.own_module.checksum_installed = 'test'
  136. self.own_module.button_uninstall_cancel()
  137. self.assertEqual(
  138. self.own_module.checksum_installed, 'test',
  139. 'Uninstall cancellation does not preserve checksum_installed',
  140. )
  141. def test_button_upgrade_cancel(self):
  142. """It should preserve checksum_installed when cancelling upgrades"""
  143. self.own_module.write({'state': 'to upgrade'})
  144. self.own_module.checksum_installed = 'test'
  145. self.own_module.button_upgrade_cancel()
  146. self.assertEqual(
  147. self.own_module.checksum_installed, 'test',
  148. 'Upgrade cancellation does not preserve checksum_installed',
  149. )
  150. def test_create(self):
  151. """It should call _store_checksum_installed method"""
  152. _store_checksum_installed_mock = mock.MagicMock()
  153. try:
  154. self.env['ir.module.module']._patch_method(
  155. '_store_checksum_installed',
  156. _store_checksum_installed_mock,
  157. )
  158. vals = {
  159. 'name': 'module_auto_update_test_module',
  160. 'state': 'installed',
  161. }
  162. self.create_test_module(vals)
  163. _store_checksum_installed_mock.assert_called_once_with(vals)
  164. finally:
  165. self.env['ir.module.module']._revert_method(
  166. '_store_checksum_installed',
  167. )
  168. @mute_logger("openerp.modules.module")
  169. @mock.patch('%s.get_module_path' % model)
  170. def test_get_module_list(self, module_path_mock):
  171. """It should change the state of modules with different
  172. checksum_dir and checksum_installed to 'to upgrade'"""
  173. module_path_mock.return_value = self.own_dir_path
  174. vals = {
  175. 'name': 'module_auto_update_test_module',
  176. 'state': 'installed',
  177. }
  178. test_module = self.create_test_module(vals)
  179. test_module.checksum_installed = 'test'
  180. self.env['base.module.upgrade'].get_module_list()
  181. self.assertEqual(
  182. test_module.state, 'to upgrade',
  183. 'List update does not mark upgradeable modules "to upgrade"',
  184. )
  185. @mock.patch('%s.get_module_path' % model)
  186. def test_get_module_list_only_changes_installed(self, module_path_mock):
  187. """It should not change the state of a module with a former state
  188. other than 'installed' to 'to upgrade'"""
  189. module_path_mock.return_value = self.own_dir_path
  190. vals = {
  191. 'name': 'module_auto_update_test_module',
  192. 'state': 'uninstalled',
  193. }
  194. test_module = self.create_test_module(vals)
  195. self.env['base.module.upgrade'].get_module_list()
  196. self.assertNotEqual(
  197. test_module.state, 'to upgrade',
  198. 'List update changed state of an uninstalled module',
  199. )
  200. def test_write(self):
  201. """It should call _store_checksum_installed method"""
  202. _store_checksum_installed_mock = mock.MagicMock()
  203. self.env['ir.module.module']._patch_method(
  204. '_store_checksum_installed',
  205. _store_checksum_installed_mock,
  206. )
  207. vals = {'state': 'installed'}
  208. self.own_module.write(vals)
  209. _store_checksum_installed_mock.assert_called_once_with(vals)
  210. self.env['ir.module.module']._revert_method(
  211. '_store_checksum_installed',
  212. )
  213. def test_post_init_hook(self):
  214. """It should set checksum_installed equal to checksum_dir for all
  215. installed modules"""
  216. installed_modules = self.env['ir.module.module'].search([
  217. ('state', '=', 'installed'),
  218. ])
  219. post_init_hook(self.env.cr, None)
  220. self.assertListEqual(
  221. installed_modules.mapped('checksum_dir'),
  222. installed_modules.mapped('checksum_installed'),
  223. 'Installed modules did not have checksum_installed stored',
  224. )