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.

167 lines
5.5 KiB

  1. /* Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
  3. odoo.define_section('web_readonly_bypass', [], function(test) {
  4. "use strict";
  5. test('ignore_readonly', function(assert) {
  6. var data = {};
  7. var mode_create = true;
  8. var options = {};
  9. var context = {};
  10. openerp.web_readonly_bypass.ignore_readonly(data, options,
  11. mode_create, context);
  12. assert.deepEqual(data,
  13. {},
  14. "Empty context and options mode create"
  15. );
  16. mode_create = false;
  17. data = {};
  18. openerp.web_readonly_bypass.ignore_readonly(data, options,
  19. mode_create, context);
  20. assert.deepEqual(data,
  21. {},
  22. "Empty context and options mode write"
  23. );
  24. mode_create = false;
  25. data = {};
  26. context = {'readonly_by_pass': true};
  27. options = {'readonly_fields': {'field_1': 'va1-1',
  28. 'field_2': false,
  29. 'field_3': 'val-3'}};
  30. openerp.web_readonly_bypass.ignore_readonly(data, options,
  31. mode_create, context);
  32. assert.deepEqual(data,
  33. {'field_1': 'va1-1', 'field_2': false, 'field_3': 'val-3'},
  34. "all fields mode write"
  35. );
  36. mode_create = true;
  37. data = {};
  38. context = {'readonly_by_pass': true};
  39. options = {'readonly_fields': {'field_1': 'va1-1',
  40. 'field_2': false,
  41. 'field_3': 'val-3'}};
  42. openerp.web_readonly_bypass.ignore_readonly(data, options,
  43. mode_create, context);
  44. assert.deepEqual(data,
  45. {'field_1': 'va1-1', 'field_3': 'val-3'},
  46. "all fields mode create (false value are escaped)"
  47. );
  48. mode_create = true;
  49. data = {};
  50. context = {};
  51. options = {'readonly_fields': {'field_1': 'va1-1',
  52. 'field_2': false,
  53. 'field_3': 'val-3'}};
  54. openerp.web_readonly_bypass.ignore_readonly(data, options,
  55. mode_create, context);
  56. assert.deepEqual(data,
  57. {},
  58. "without context, default, we won't save readonly fields"
  59. );
  60. });
  61. test('retrieve_readonly_by_pass_fields', function(assert) {
  62. var context = {'readonly_by_pass': true}
  63. var options = {'readonly_fields': {'field_1': 'va1-1',
  64. 'field_2': 'val-2',
  65. 'field_3': 'val-3'}};
  66. assert.deepEqual(
  67. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  68. options, context),
  69. {'field_1': 'va1-1', 'field_2': 'val-2', 'field_3': 'val-3'},
  70. "All fields should be accepted!"
  71. );
  72. context = {'readonly_by_pass': ['field_1', 'field_3']};
  73. assert.deepEqual(
  74. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  75. options, context),
  76. {'field_1': 'va1-1','field_3': 'val-3'},
  77. "two field s1"
  78. );
  79. context = {'readonly_by_pass': ['field_1',]};
  80. assert.deepEqual(
  81. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  82. options, context),
  83. {'field_1': 'va1-1'},
  84. "Only field 1"
  85. );
  86. context = {'readonly_by_pass': []};
  87. assert.deepEqual(
  88. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  89. options, context),
  90. {},
  91. "Empty context field"
  92. );
  93. context = null;
  94. assert.deepEqual(
  95. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  96. options, context),
  97. {},
  98. "Null context"
  99. );
  100. context = false;
  101. assert.deepEqual(
  102. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  103. options, context),
  104. {},
  105. "false context"
  106. );
  107. context = {'readonly_by_pass': true}
  108. options = {'readonly_fields': {'field_1': 'va1-1'}};
  109. assert.deepEqual(
  110. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  111. options, context),
  112. {'field_1': 'va1-1'},
  113. "Only one option"
  114. );
  115. options = {'readonly_fields': {}};
  116. assert.deepEqual(
  117. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  118. options, context),
  119. {},
  120. "Empty readonly_fields option"
  121. );
  122. options = {};
  123. assert.deepEqual(
  124. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  125. options, context),
  126. {},
  127. "Empty option"
  128. );
  129. options = null;
  130. assert.deepEqual(
  131. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  132. options, context),
  133. {},
  134. "null option"
  135. );
  136. options = false;
  137. assert.deepEqual(
  138. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  139. options, context),
  140. {},
  141. "false option"
  142. );
  143. context = false;
  144. assert.deepEqual(
  145. openerp.web_readonly_bypass.retrieve_readonly_by_pass_fields(
  146. options, context),
  147. {},
  148. "false option and false context"
  149. );
  150. });
  151. });