captcha.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import assert from "assert";
  2. import React from 'react'; // jshint ignore:line
  3. import ReactDOM from 'react-dom'; // jshint ignore:line
  4. import ajax from 'misago/services/ajax';
  5. import { NoCaptcha, QACaptcha, ReCaptcha, Captcha } from 'misago/services/captcha';
  6. import snackbar from 'misago/services/snackbar';
  7. let captcha = null;
  8. let snackbarStore = null;
  9. describe("NoCaptcha", function() {
  10. it("always loads instantly", function(done) {
  11. captcha = new NoCaptcha();
  12. captcha.init({}, {}, {}, {});
  13. captcha.load().then(function() {
  14. assert.ok(true, "service loaded immediately");
  15. done();
  16. });
  17. });
  18. it("has no validator", function() {
  19. captcha = new NoCaptcha();
  20. assert.equal(captcha.validator(), null, "nocaptcha has no validator");
  21. });
  22. it("has no component", function() {
  23. captcha = new NoCaptcha();
  24. assert.equal(captcha.component(), null, "nocaptcha has no component");
  25. });
  26. });
  27. describe("QACaptcha", function() {
  28. beforeEach(function() {
  29. snackbarStore = window.snackbarStoreMock();
  30. snackbar.init(snackbarStore);
  31. });
  32. afterEach(function() {
  33. window.emptyTestContainers();
  34. window.snackbarClear(snackbar);
  35. $.mockjax.clear();
  36. });
  37. it("handles successful load", function(done) {
  38. $.mockjax({
  39. url: '/test-api/captcha/',
  40. status: 200,
  41. responseText: {
  42. 'question': "Test question",
  43. 'help_text': "This is test quesiton for tests"
  44. }
  45. });
  46. captcha = new QACaptcha();
  47. captcha.init({
  48. get: function(setting) {
  49. assert.equal(setting, 'CAPTCHA_API_URL', "valid setting is used");
  50. return '/test-api/captcha/';
  51. }
  52. }, ajax, {}, {});
  53. captcha.load().then(function() {
  54. assert.equal(captcha.question, "Test question", "question was loaded");
  55. assert.equal(captcha.helpText, "This is test quesiton for tests",
  56. "question's help text was loaded");
  57. done();
  58. });
  59. });
  60. it("handles failed load", function(done) {
  61. $.mockjax({
  62. url: '/test-api/captcha/',
  63. status: 400,
  64. responseText: {
  65. 'question': "Test question",
  66. 'help_text': "This is test quesiton for tests"
  67. }
  68. });
  69. snackbarStore.callback(function(message) {
  70. assert.deepEqual(message, {
  71. message: "Failed to load CAPTCHA.",
  72. type: 'error'
  73. }, "question failed to load from API");
  74. done();
  75. });
  76. captcha = new QACaptcha();
  77. captcha.init({
  78. get: function(setting) {
  79. assert.equal(setting, 'CAPTCHA_API_URL', "valid setting is used");
  80. return '/test-api/captcha/';
  81. }
  82. }, ajax, {}, snackbar);
  83. captcha.load().then(function() {
  84. assert.ok(false, "captcha should fail to load");
  85. }, function() {
  86. assert.ok(true, "captcha failed to load");
  87. });
  88. });
  89. it("is required", function() {
  90. captcha = new QACaptcha();
  91. assert.deepEqual(captcha.validator(), [], "qa-captcha is required");
  92. });
  93. it("renders component", function(done) {
  94. $.mockjax({
  95. url: '/test-api/captcha/',
  96. status: 200,
  97. responseText: {
  98. 'question': "Test question",
  99. 'help_text': "This is test quesiton for tests"
  100. }
  101. });
  102. captcha = new QACaptcha();
  103. captcha.init({
  104. get: function(setting) {
  105. assert.equal(setting, 'CAPTCHA_API_URL', "valid setting is used");
  106. return '/test-api/captcha/';
  107. }
  108. }, ajax, {}, {});
  109. captcha.load().then(function() {
  110. /* jshint ignore:start */
  111. ReactDOM.render(
  112. <div>
  113. {captcha.component({
  114. form: {
  115. state: {
  116. errors: {},
  117. isLoading: false,
  118. captcha: ''
  119. },
  120. bindInput: function() {
  121. return function() {
  122. /* noop */
  123. };
  124. }
  125. }
  126. })}
  127. </div>,
  128. document.getElementById('test-mount')
  129. );
  130. /* jshint ignore:end */
  131. let element = $('#test-mount');
  132. assert.ok(element.length, "component renders");
  133. assert.equal(element.find('label').text().trim(), "Test question:",
  134. "label contains test question");
  135. assert.equal(element.find('p.help-block').text().trim(),
  136. "This is test quesiton for tests",
  137. "question's help text is displayed");
  138. done();
  139. });
  140. });
  141. });
  142. describe("ReCaptcha", function() {
  143. it("is required", function() {
  144. captcha = new ReCaptcha();
  145. assert.deepEqual(captcha.validator(), [], "recaptcha is required");
  146. });
  147. });
  148. describe("Captcha", function() {
  149. it("delegates load calls", function(done) {
  150. let captcha = new Captcha();
  151. captcha._captcha = {
  152. load: function() {
  153. assert.ok(true, "load call was delegated to strategy");
  154. done();
  155. }
  156. };
  157. captcha.load();
  158. });
  159. it("delegates validator calls", function(done) {
  160. let captcha = new Captcha();
  161. captcha._captcha = {
  162. validator: function() {
  163. assert.ok(true, "validator call was delegated to strategy");
  164. done();
  165. }
  166. };
  167. captcha.validator();
  168. });
  169. it("delegates component calls", function(done) {
  170. let captcha = new Captcha();
  171. captcha._captcha = {
  172. component: function() {
  173. assert.ok(true, "component call was delegated to strategy");
  174. done();
  175. }
  176. };
  177. captcha.component();
  178. });
  179. });