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.

124 lines
4.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)
  4. from mock import patch
  5. import os
  6. import sys
  7. from odoo.modules.module import get_module_path
  8. from odoo.tests.common import TransactionCase
  9. module_path = get_module_path('auth_totp')
  10. migration_path = os.path.join(module_path, 'migrations', '10.0.2.0.0')
  11. sys.path.insert(0, migration_path)
  12. sys.modules.pop('auth_totp', None)
  13. post_migrate = __import__('post-migrate')
  14. migrate = post_migrate.migrate
  15. HELPER_PATH = 'odoo.addons.base.ir.ir_model.IrModelFields._prepare_update'
  16. @patch(HELPER_PATH, autospec=True)
  17. class TestPostMigrate(TransactionCase):
  18. def setUp(self):
  19. super(TestPostMigrate, self).setUp()
  20. self.test_user = self.env['res.users'].create({
  21. 'name': 'Test User',
  22. 'login': 'test_user',
  23. })
  24. self.env['res.users.authenticator'].create({
  25. 'name': 'Test Name',
  26. 'secret_key': 'Test Key',
  27. 'user_id': self.test_user.id,
  28. })
  29. self.test_user.mfa_enabled = True
  30. self.test_user.active = False
  31. self.test_user_2 = self.env['res.users'].create({
  32. 'name': 'Test User 2',
  33. 'login': 'test_user_2',
  34. })
  35. self.test_device_model = self.env['ir.model'].create({
  36. 'name': 'Test Device Model',
  37. 'model': 'res.users.device',
  38. 'state': 'base',
  39. })
  40. def test_migrate_mfa_enabled(self, helper_mock):
  41. """It should give users with MFA enabled a new key"""
  42. old_key = self.test_user.trusted_device_cookie_key
  43. migrate(self.env.cr, None)
  44. self.assertTrue(self.test_user.trusted_device_cookie_key)
  45. self.assertNotEqual(self.test_user.trusted_device_cookie_key, old_key)
  46. def test_migrate_mfa_disabled(self, helper_mock):
  47. """It should leave users with MFA disabled without a key"""
  48. migrate(self.env.cr, None)
  49. self.assertFalse(self.test_user_2.trusted_device_cookie_key)
  50. def test_migrate_call_field_helper(self, helper_mock):
  51. """It should call update helper on all device model field records"""
  52. test_field = self.test_device_model.field_id
  53. test_field_2 = self.env['ir.model.fields'].create({
  54. 'name': 'test_field_2',
  55. 'model': self.test_device_model.model,
  56. 'model_id': self.test_device_model.id,
  57. 'ttype': 'char',
  58. 'state': 'base',
  59. })
  60. test_field_set = test_field + test_field_2
  61. migrate(self.env.cr, None)
  62. helper_mock.assert_called_once_with(test_field_set)
  63. def test_migrate_clean_up_constraints(self, helper_mock):
  64. """It should clean up all constraints associated with device model"""
  65. test_module_record = self.env['ir.module.module'].search([], limit=1)
  66. self.env['ir.model.constraint'].create({
  67. 'name': 'Test Constraint',
  68. 'model': self.test_device_model.id,
  69. 'module': test_module_record.id,
  70. 'type': 'u',
  71. })
  72. self.env['ir.model.constraint'].create({
  73. 'name': 'Test Constraint 2',
  74. 'model': self.test_device_model.id,
  75. 'module': test_module_record.id,
  76. 'type': 'u',
  77. })
  78. migrate(self.env.cr, None)
  79. resulting_constraints = self.env['ir.model.constraint'].search([
  80. ('model', '=', self.test_device_model.id),
  81. ])
  82. self.assertFalse(resulting_constraints)
  83. def test_migrate_clean_up_xml_ids(self, helper_mock):
  84. """It should clean up XML IDs tied to device model"""
  85. self.env['ir.model.data'].create({
  86. 'name': 'Test XML ID',
  87. 'model': 'ir.model',
  88. 'res_id': self.test_device_model.id,
  89. })
  90. self.env['ir.model.data'].create({
  91. 'name': 'Test XML ID 2',
  92. 'model': 'ir.model',
  93. 'res_id': self.test_device_model.id,
  94. })
  95. migrate(self.env.cr, None)
  96. resulting_xml_ids = self.env['ir.model.data'].search([
  97. ('model', '=', 'ir.model'),
  98. ('res_id', '=', self.test_device_model.id),
  99. ])
  100. self.assertFalse(resulting_xml_ids)
  101. def test_migrate_clean_up_ir_record(self, helper_mock):
  102. """It should clean up device model ir.model record"""
  103. migrate(self.env.cr, None)
  104. self.assertFalse(self.test_device_model.exists())