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.

217 lines
8.4 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
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. import logging
  5. import os
  6. import tempfile
  7. import mock
  8. from openerp.modules import get_module_path
  9. from openerp.tests.common import TransactionCase
  10. from openerp.tools import mute_logger
  11. from .. import post_init_hook
  12. _logger = logging.getLogger(__name__)
  13. try:
  14. from checksumdir import dirhash
  15. except ImportError:
  16. _logger.debug('Cannot `import checksumdir`.')
  17. model = 'openerp.addons.module_auto_update.models.module'
  18. class TestModule(TransactionCase):
  19. def setUp(self):
  20. super(TestModule, self).setUp()
  21. module_name = 'module_auto_update'
  22. self.own_module = self.env['ir.module.module'].search([
  23. ('name', '=', module_name),
  24. ])
  25. self.own_dir_path = get_module_path(module_name)
  26. self.own_checksum = dirhash(
  27. self.own_dir_path,
  28. 'sha1',
  29. excluded_extensions=['pyc', 'pyo'],
  30. )
  31. self.own_writeable = os.access(self.own_dir_path, os.W_OK)
  32. @mock.patch('%s.get_module_path' % model)
  33. def create_test_module(self, vals, get_module_path_mock):
  34. get_module_path_mock.return_value = self.own_dir_path
  35. test_module = self.env['ir.module.module'].create(vals)
  36. return test_module
  37. def test_compute_checksum_dir(self):
  38. """It should compute the directory's SHA-1 hash"""
  39. self.assertEqual(
  40. self.own_module.checksum_dir, self.own_checksum,
  41. 'Module directory checksum not computed properly',
  42. )
  43. def test_compute_checksum_dir_ignore_excluded(self):
  44. """It should exclude .pyc/.pyo extensions from checksum
  45. calculations"""
  46. if not self.own_writeable:
  47. self.skipTest("Own directory not writeable")
  48. with tempfile.NamedTemporaryFile(
  49. suffix='.pyc', dir=self.own_dir_path):
  50. self.assertEqual(
  51. self.own_module.checksum_dir, self.own_checksum,
  52. 'SHA1 checksum does not ignore excluded extensions',
  53. )
  54. def test_compute_checksum_dir_recomputes_when_file_added(self):
  55. """It should return a different value when a non-.pyc/.pyo file is
  56. added to the module directory"""
  57. if not self.own_writeable:
  58. self.skipTest("Own directory not writeable")
  59. with tempfile.NamedTemporaryFile(
  60. suffix='.py', dir=self.own_dir_path):
  61. self.assertNotEqual(
  62. self.own_module.checksum_dir, self.own_checksum,
  63. 'SHA1 checksum not recomputed',
  64. )
  65. def test_store_checksum_installed_state_installed(self):
  66. """It should set the module's checksum_installed equal to
  67. checksum_dir when vals contain a ``latest_version`` str."""
  68. self.own_module.checksum_installed = 'test'
  69. self.own_module._store_checksum_installed({'latest_version': '1.0'})
  70. self.assertEqual(
  71. self.own_module.checksum_installed, self.own_module.checksum_dir,
  72. )
  73. def test_store_checksum_installed_state_uninstalled(self):
  74. """It should clear the module's checksum_installed when vals
  75. contain ``"latest_version": False``"""
  76. self.own_module.checksum_installed = 'test'
  77. self.own_module._store_checksum_installed({'latest_version': False})
  78. self.assertIs(self.own_module.checksum_installed, False)
  79. def test_store_checksum_installed_vals_contain_checksum_installed(self):
  80. """It should not set checksum_installed to False or checksum_dir when
  81. a checksum_installed is included in vals"""
  82. self.own_module.checksum_installed = 'test'
  83. self.own_module._store_checksum_installed({
  84. 'state': 'installed',
  85. 'checksum_installed': 'test',
  86. })
  87. self.assertEqual(
  88. self.own_module.checksum_installed, 'test',
  89. 'Providing checksum_installed in vals did not prevent overwrite',
  90. )
  91. def test_store_checksum_installed_with_retain_context(self):
  92. """It should not set checksum_installed to False or checksum_dir when
  93. self has context retain_checksum_installed=True"""
  94. self.own_module.checksum_installed = 'test'
  95. self.own_module.with_context(
  96. retain_checksum_installed=True,
  97. )._store_checksum_installed({'state': 'installed'})
  98. self.assertEqual(
  99. self.own_module.checksum_installed, 'test',
  100. 'Providing retain_checksum_installed context did not prevent '
  101. 'overwrite',
  102. )
  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. self.env['ir.module.module']._patch_method(
  125. '_store_checksum_installed',
  126. _store_checksum_installed_mock,
  127. )
  128. vals = {
  129. 'name': 'module_auto_update_test_module',
  130. 'state': 'installed',
  131. }
  132. self.create_test_module(vals)
  133. _store_checksum_installed_mock.assert_called_once_with(vals)
  134. self.env['ir.module.module']._revert_method(
  135. '_store_checksum_installed',
  136. )
  137. @mute_logger("openerp.modules.module")
  138. @mock.patch('%s.get_module_path' % model)
  139. def test_get_module_list(self, module_path_mock):
  140. """It should change the state of modules with different
  141. checksum_dir and checksum_installed to 'to upgrade'"""
  142. module_path_mock.return_value = self.own_dir_path
  143. vals = {
  144. 'name': 'module_auto_update_test_module',
  145. 'state': 'installed',
  146. }
  147. test_module = self.create_test_module(vals)
  148. test_module.checksum_installed = 'test'
  149. self.env['base.module.upgrade'].get_module_list()
  150. self.assertEqual(
  151. test_module.state, 'to upgrade',
  152. 'List update does not mark upgradeable modules "to upgrade"',
  153. )
  154. @mock.patch('%s.get_module_path' % model)
  155. def test_get_module_list_only_changes_installed(self, module_path_mock):
  156. """It should not change the state of a module with a former state
  157. other than 'installed' to 'to upgrade'"""
  158. module_path_mock.return_value = self.own_dir_path
  159. vals = {
  160. 'name': 'module_auto_update_test_module',
  161. 'state': 'uninstalled',
  162. }
  163. test_module = self.create_test_module(vals)
  164. self.env['base.module.upgrade'].get_module_list()
  165. self.assertNotEqual(
  166. test_module.state, 'to upgrade',
  167. 'List update changed state of an uninstalled module',
  168. )
  169. def test_write(self):
  170. """It should call _store_checksum_installed method"""
  171. _store_checksum_installed_mock = mock.MagicMock()
  172. self.env['ir.module.module']._patch_method(
  173. '_store_checksum_installed',
  174. _store_checksum_installed_mock,
  175. )
  176. vals = {'state': 'installed'}
  177. self.own_module.write(vals)
  178. _store_checksum_installed_mock.assert_called_once_with(vals)
  179. self.env['ir.module.module']._revert_method(
  180. '_store_checksum_installed',
  181. )
  182. def test_post_init_hook(self):
  183. """It should set checksum_installed equal to checksum_dir for all
  184. installed modules"""
  185. installed_modules = self.env['ir.module.module'].search([
  186. ('state', '=', 'installed'),
  187. ])
  188. post_init_hook(self.env.cr, None)
  189. self.assertListEqual(
  190. installed_modules.mapped('checksum_dir'),
  191. installed_modules.mapped('checksum_installed'),
  192. 'Installed modules did not have checksum_installed stored',
  193. )