request-actiavtion-link.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import ReactDOM from 'react-dom'; // jshint ignore:line
  4. import misago from 'misago/index';
  5. import { RequestLinkForm, LinkSent } from 'misago/components/request-activation-link'; // jshint ignore:line
  6. import snackbar from 'misago/services/snackbar';
  7. let snackbarStore = null;
  8. describe("Request Activation Link Form", function() {
  9. beforeEach(function() {
  10. snackbarStore = window.snackbarStoreMock();
  11. snackbar.init(snackbarStore);
  12. misago._context = {
  13. 'SETTINGS': {
  14. 'forum_name': 'Test forum'
  15. },
  16. 'SEND_ACTIVATION_API': '/test-api/send-activation/'
  17. };
  18. /* jshint ignore:start */
  19. ReactDOM.render(
  20. <RequestLinkForm />,
  21. document.getElementById('test-mount')
  22. );
  23. /* jshint ignore:end */
  24. });
  25. afterEach(function() {
  26. window.emptyTestContainers();
  27. window.snackbarClear(snackbar);
  28. $.mockjax.clear();
  29. });
  30. it("renders", function() {
  31. let element = $('#test-mount .well-form-request-activation-link');
  32. assert.ok(element.length, "component renders");
  33. });
  34. it("handles empty submit", function(done) {
  35. snackbarStore.callback(function(message) {
  36. assert.deepEqual(message, {
  37. message: "Enter a valid email address.",
  38. type: 'error'
  39. }, "form brought error about no input");
  40. done();
  41. });
  42. window.simulateSubmit('#test-mount form');
  43. });
  44. it("handles invalid submit", function(done) {
  45. snackbarStore.callback(function(message) {
  46. assert.deepEqual(message, {
  47. message: "Enter a valid email address.",
  48. type: 'error'
  49. }, "form brought error about invalid input");
  50. done();
  51. });
  52. window.simulateChange('#test-mount input', 'loremipsum');
  53. window.simulateSubmit('#test-mount form');
  54. });
  55. it("handles backend error", function(done) {
  56. snackbarStore.callback(function(message) {
  57. assert.deepEqual(message, {
  58. message: "Unknown error has occured.",
  59. type: 'error'
  60. }, "form raised alert about backend error");
  61. done();
  62. });
  63. $.mockjax({
  64. url: '/test-api/send-activation/',
  65. status: 500
  66. });
  67. window.simulateChange('#test-mount input', 'lorem@ipsum.com');
  68. window.simulateSubmit('#test-mount form');
  69. });
  70. it("handles backend rejection", function(done) {
  71. snackbarStore.callback(function(message) {
  72. assert.deepEqual(message, {
  73. message: "Nope nope nope!",
  74. type: 'error'
  75. }, "form raised alert about backend rejection");
  76. done();
  77. });
  78. $.mockjax({
  79. url: '/test-api/send-activation/',
  80. status: 400,
  81. responseText: {
  82. detail: "Nope nope nope!"
  83. }
  84. });
  85. window.simulateChange('#test-mount input', 'lorem@ipsum.com');
  86. window.simulateSubmit('#test-mount form');
  87. });
  88. it("handles backend info", function(done) {
  89. snackbarStore.callback(function(message) {
  90. assert.deepEqual(message, {
  91. message: "Your account is already active!",
  92. type: 'info'
  93. }, "form raised alert about backend info");
  94. done();
  95. });
  96. $.mockjax({
  97. url: '/test-api/send-activation/',
  98. status: 400,
  99. responseText: {
  100. code: 'already_active',
  101. detail: "Your account is already active!"
  102. }
  103. });
  104. window.simulateChange('#test-mount input', 'lorem@ipsum.com');
  105. window.simulateSubmit('#test-mount form');
  106. });
  107. it("from banned IP", function(done) {
  108. $.mockjax({
  109. url: '/test-api/send-activation/',
  110. status: 403,
  111. responseText: {
  112. 'ban': {
  113. 'expires_on': null,
  114. 'message': {
  115. 'plain': 'Your ip is banned for spamming.',
  116. 'html': '<p>Your ip is banned for spamming.</p>',
  117. }
  118. }
  119. }
  120. });
  121. window.simulateChange('#test-mount input', 'lorem@ipsum.com');
  122. window.simulateSubmit('#test-mount form');
  123. window.onElement('.page-error-banned .lead', function() {
  124. assert.equal(
  125. $('.page .message-body .lead p').text().trim(),
  126. "Your ip is banned for spamming.",
  127. "displayed error banned page with ban message.");
  128. done();
  129. });
  130. });
  131. it("handles success", function(done) { // jshint ignore:line
  132. $.mockjax({
  133. url: '/test-api/send-activation/',
  134. status: 200,
  135. responseText: {
  136. 'username': 'Bob',
  137. 'email': 'bob@boberson.com'
  138. }
  139. });
  140. /* jshint ignore:start */
  141. let callback = function(apiResponse) {
  142. assert.deepEqual(apiResponse, {
  143. 'username': 'Bob',
  144. 'email': 'bob@boberson.com'
  145. }, "callback function was called on ajax success");
  146. done();
  147. };
  148. ReactDOM.render(
  149. <RequestLinkForm callback={callback} />,
  150. document.getElementById('test-mount')
  151. );
  152. /* jshint ignore:end */
  153. window.simulateChange('#test-mount input', 'lorem@ipsum.com');
  154. window.simulateSubmit('#test-mount form');
  155. });
  156. });
  157. describe("Activation Link Sent", function() {
  158. afterEach(function() {
  159. window.emptyTestContainers();
  160. });
  161. it("renders message", function(done) { // jshint ignore:line
  162. /* jshint ignore:start */
  163. let callback = function() {
  164. assert.ok(true, "callback function was called on button press");
  165. done();
  166. };
  167. ReactDOM.render(
  168. <LinkSent user={{email: 'bob@boberson.com' }}
  169. callback={callback} />,
  170. document.getElementById('test-mount')
  171. );
  172. /* jshint ignore:end */
  173. let element = $('#test-mount .well-done');
  174. assert.ok(element.length, "component renders");
  175. assert.equal(element.find('p').text().trim(),
  176. "Activation link was sent to bob@boberson.com",
  177. "component renders valid message");
  178. window.simulateClick('#test-mount .btn-primary');
  179. });
  180. });