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.

220 lines
8.5 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
  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 .. import post_init_hook
  11. _logger = logging.getLogger(__name__)
  12. try:
  13. from checksumdir import dirhash
  14. except ImportError:
  15. _logger.debug('Cannot `import checksumdir`.')
  16. model = 'openerp.addons.module_auto_update.models.module'
  17. class TestModule(TransactionCase):
  18. def setUp(self):
  19. super(TestModule, self).setUp()
  20. module_name = 'module_auto_update'
  21. self.own_module = self.env['ir.module.module'].search([
  22. ('name', '=', module_name),
  23. ])
  24. self.own_dir_path = get_module_path(module_name)
  25. self.own_checksum = dirhash(
  26. self.own_dir_path,
  27. 'sha1',
  28. excluded_extensions=['pyc', 'pyo'],
  29. )
  30. self.own_writeable = os.access(self.own_dir_path, os.W_OK)
  31. @mock.patch('%s.get_module_path' % model)
  32. def create_test_module(self, vals, get_module_path_mock):
  33. get_module_path_mock.return_value = self.own_dir_path
  34. test_module = self.env['ir.module.module'].create(vals)
  35. return test_module
  36. def test_compute_checksum_dir(self):
  37. """It should compute the directory's SHA-1 hash"""
  38. self.assertEqual(
  39. self.own_module.checksum_dir, self.own_checksum,
  40. 'Module directory checksum not computed properly',
  41. )
  42. def test_compute_checksum_dir_ignore_excluded(self):
  43. """It should exclude .pyc/.pyo extensions from checksum
  44. calculations"""
  45. if not self.own_writeable:
  46. self.skipTest("Own directory not writeable")
  47. with tempfile.NamedTemporaryFile(
  48. suffix='.pyc', dir=self.own_dir_path):
  49. self.assertEqual(
  50. self.own_module.checksum_dir, self.own_checksum,
  51. 'SHA1 checksum does not ignore excluded extensions',
  52. )
  53. def test_compute_checksum_dir_recomputes_when_file_added(self):
  54. """It should return a different value when a non-.pyc/.pyo file is
  55. added to the module directory"""
  56. if not self.own_writeable:
  57. self.skipTest("Own directory not writeable")
  58. with tempfile.NamedTemporaryFile(
  59. suffix='.py', dir=self.own_dir_path):
  60. self.assertNotEqual(
  61. self.own_module.checksum_dir, self.own_checksum,
  62. 'SHA1 checksum not recomputed',
  63. )
  64. def test_store_checksum_installed_state_installed(self):
  65. """It should set the module's checksum_installed equal to
  66. checksum_dir when vals contain state 'installed'"""
  67. self.own_module.checksum_installed = 'test'
  68. self.own_module._store_checksum_installed({'state': 'installed'})
  69. self.assertEqual(
  70. self.own_module.checksum_installed, self.own_module.checksum_dir,
  71. 'Setting state to installed does not store checksum_dir '
  72. 'as checksum_installed',
  73. )
  74. def test_store_checksum_installed_state_uninstalled(self):
  75. """It should clear the module's checksum_installed when vals
  76. contain state 'uninstalled'"""
  77. self.own_module.checksum_installed = 'test'
  78. self.own_module._store_checksum_installed({'state': 'uninstalled'})
  79. self.assertEqual(
  80. self.own_module.checksum_installed, False,
  81. 'Setting state to uninstalled does not clear checksum_installed',
  82. )
  83. def test_store_checksum_installed_vals_contain_checksum_installed(self):
  84. """It should not set checksum_installed to False or checksum_dir when
  85. a checksum_installed is included in vals"""
  86. self.own_module.checksum_installed = 'test'
  87. self.own_module._store_checksum_installed({
  88. 'state': 'installed',
  89. 'checksum_installed': 'test',
  90. })
  91. self.assertEqual(
  92. self.own_module.checksum_installed, 'test',
  93. 'Providing checksum_installed in vals did not prevent overwrite',
  94. )
  95. def test_store_checksum_installed_with_retain_context(self):
  96. """It should not set checksum_installed to False or checksum_dir when
  97. self has context retain_checksum_installed=True"""
  98. self.own_module.checksum_installed = 'test'
  99. self.own_module.with_context(
  100. retain_checksum_installed=True,
  101. )._store_checksum_installed({'state': 'installed'})
  102. self.assertEqual(
  103. self.own_module.checksum_installed, 'test',
  104. 'Providing retain_checksum_installed context did not prevent '
  105. 'overwrite',
  106. )
  107. def test_button_uninstall_cancel(self):
  108. """It should preserve checksum_installed when cancelling uninstall"""
  109. self.own_module.write({'state': 'to remove'})
  110. self.own_module.checksum_installed = 'test'
  111. self.own_module.button_uninstall_cancel()
  112. self.assertEqual(
  113. self.own_module.checksum_installed, 'test',
  114. 'Uninstall cancellation does not preserve checksum_installed',
  115. )
  116. def test_button_upgrade_cancel(self):
  117. """It should preserve checksum_installed when cancelling upgrades"""
  118. self.own_module.write({'state': 'to upgrade'})
  119. self.own_module.checksum_installed = 'test'
  120. self.own_module.button_upgrade_cancel()
  121. self.assertEqual(
  122. self.own_module.checksum_installed, 'test',
  123. 'Upgrade cancellation does not preserve checksum_installed',
  124. )
  125. def test_create(self):
  126. """It should call _store_checksum_installed method"""
  127. _store_checksum_installed_mock = mock.MagicMock()
  128. self.env['ir.module.module']._patch_method(
  129. '_store_checksum_installed',
  130. _store_checksum_installed_mock,
  131. )
  132. vals = {
  133. 'name': 'module_auto_update_test_module',
  134. 'state': 'installed',
  135. }
  136. self.create_test_module(vals)
  137. _store_checksum_installed_mock.assert_called_once_with(vals)
  138. self.env['ir.module.module']._revert_method(
  139. '_store_checksum_installed',
  140. )
  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. )
  185. def test_post_init_hook(self):
  186. """It should set checksum_installed equal to checksum_dir for all
  187. installed modules"""
  188. installed_modules = self.env['ir.module.module'].search([
  189. ('state', '=', 'installed'),
  190. ])
  191. post_init_hook(self.env.cr, None)
  192. self.assertListEqual(
  193. installed_modules.mapped('checksum_dir'),
  194. installed_modules.mapped('checksum_installed'),
  195. 'Installed modules did not have checksum_installed stored',
  196. )