sign-in-credentials.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import Root, { ChangeEmail, ChangePassword } from 'misago/components/options/sign-in-credentials'; // jshint ignore:line
  4. import misago from 'misago/index';
  5. import snackbar from 'misago/services/snackbar';
  6. import * as testUtils from 'misago/utils/test-utils';
  7. let snackbarStore = null;
  8. let user = testUtils.mockUser();
  9. describe("Change E-mail Form", function() {
  10. beforeEach(function() {
  11. snackbarStore = testUtils.snackbarStoreMock();
  12. snackbar.init(snackbarStore);
  13. });
  14. afterEach(function() {
  15. testUtils.unmountComponents();
  16. testUtils.snackbarClear(snackbar);
  17. $.mockjax.clear();
  18. });
  19. it("renders", function(done) {
  20. /* jshint ignore:start */
  21. testUtils.render(<ChangeEmail user={user} />);
  22. /* jshint ignore:end */
  23. testUtils.onElement('#test-mount form', function() {
  24. assert.ok(true, "component renders");
  25. done();
  26. });
  27. });
  28. it("handles empty submit", function(done) {
  29. /* jshint ignore:start */
  30. testUtils.render(<ChangeEmail user={user} />);
  31. /* jshint ignore:end */
  32. snackbarStore.callback(function(message) {
  33. assert.deepEqual(message, {
  34. message: "Fill out all fields.",
  35. type: 'error'
  36. }, "error message was shown");
  37. done();
  38. });
  39. testUtils.onElement('#test-mount form', function() {
  40. testUtils.simulateSubmit('#test-mount form');
  41. });
  42. });
  43. it("handles backend rejection", function(done) {
  44. $.mockjax({
  45. url: user.api_url.change_email,
  46. status: 400,
  47. responseText: {
  48. password: "Lol nope!"
  49. }
  50. });
  51. /* jshint ignore:start */
  52. testUtils.render(<ChangeEmail user={user} />);
  53. /* jshint ignore:end */
  54. snackbarStore.callback(function(message) {
  55. assert.deepEqual(message, {
  56. message: "Lol nope!",
  57. type: 'error'
  58. }, "error message from backend was shown");
  59. done();
  60. });
  61. testUtils.onElement('#test-mount form', function() {
  62. testUtils.simulateChange('#test-mount form #id_new_email', 'new@wt.com');
  63. testUtils.simulateChange('#test-mount form #id_password', 'p4ssw0rd');
  64. testUtils.simulateSubmit('#test-mount form');
  65. });
  66. });
  67. it("handles backend error", function(done) {
  68. $.mockjax({
  69. url: user.api_url.change_email,
  70. status: 500
  71. });
  72. /* jshint ignore:start */
  73. testUtils.render(<ChangeEmail user={user} />);
  74. /* jshint ignore:end */
  75. snackbarStore.callback(function(message) {
  76. assert.deepEqual(message, {
  77. message: "Unknown error has occured.",
  78. type: 'error'
  79. }, "error message from backend was shown");
  80. done();
  81. });
  82. testUtils.onElement('#test-mount form', function() {
  83. testUtils.simulateChange('#test-mount form #id_new_email', 'new@wt.com');
  84. testUtils.simulateChange('#test-mount form #id_password', 'p4ssw0rd');
  85. testUtils.simulateSubmit('#test-mount form');
  86. });
  87. });
  88. it("handles successful submission", function(done) {
  89. $.mockjax({
  90. url: user.api_url.change_email,
  91. status: 200,
  92. responseText: {
  93. detail: "Well done gud!"
  94. }
  95. });
  96. /* jshint ignore:start */
  97. testUtils.render(<ChangeEmail user={user} />);
  98. /* jshint ignore:end */
  99. snackbarStore.callback(function(message) {
  100. assert.deepEqual(message, {
  101. message: "Well done gud!",
  102. type: 'success'
  103. }, "success message from backend was shown");
  104. done();
  105. });
  106. testUtils.onElement('#test-mount form', function() {
  107. testUtils.simulateChange('#test-mount form #id_new_email', 'new@wt.com');
  108. testUtils.simulateChange('#test-mount form #id_password', 'p4ssw0rd');
  109. testUtils.simulateSubmit('#test-mount form');
  110. });
  111. });
  112. });
  113. describe("Change Password Form", function() {
  114. beforeEach(function() {
  115. snackbarStore = testUtils.snackbarStoreMock();
  116. snackbar.init(snackbarStore);
  117. misago._context = {
  118. SETTINGS: {
  119. password_length_min: 4
  120. }
  121. };
  122. });
  123. afterEach(function() {
  124. testUtils.unmountComponents();
  125. testUtils.snackbarClear(snackbar);
  126. $.mockjax.clear();
  127. });
  128. it("renders", function(done) {
  129. /* jshint ignore:start */
  130. testUtils.render(<ChangePassword user={user} />);
  131. /* jshint ignore:end */
  132. testUtils.onElement('#test-mount form', function() {
  133. assert.ok(true, "component renders");
  134. done();
  135. });
  136. });
  137. it("handles empty submit", function(done) {
  138. /* jshint ignore:start */
  139. testUtils.render(<ChangePassword user={user} />);
  140. /* jshint ignore:end */
  141. snackbarStore.callback(function(message) {
  142. assert.deepEqual(message, {
  143. message: "Fill out all fields.",
  144. type: 'error'
  145. }, "error message was shown");
  146. done();
  147. });
  148. testUtils.onElement('#test-mount form', function() {
  149. testUtils.simulateSubmit('#test-mount form');
  150. });
  151. });
  152. it("handles passwords mismatch", function(done) {
  153. /* jshint ignore:start */
  154. testUtils.render(<ChangePassword user={user} />);
  155. /* jshint ignore:end */
  156. snackbarStore.callback(function(message) {
  157. assert.deepEqual(message, {
  158. message: "New passwords are different.",
  159. type: 'error'
  160. }, "error message was shown");
  161. done();
  162. });
  163. testUtils.onElement('#test-mount form', function() {
  164. testUtils.simulateChange('#test-mount form #id_new_password', 'newps');
  165. testUtils.simulateChange('#test-mount form #id_repeat_password', 'nesss');
  166. testUtils.simulateChange('#test-mount form #id_password', 'p4ssw0rd');
  167. testUtils.simulateSubmit('#test-mount form');
  168. });
  169. });
  170. it("handles backend rejection", function(done) {
  171. $.mockjax({
  172. url: user.api_url.change_password,
  173. status: 400,
  174. responseText: {
  175. password: "Lol nope!"
  176. }
  177. });
  178. /* jshint ignore:start */
  179. testUtils.render(<ChangePassword user={user} />);
  180. /* jshint ignore:end */
  181. snackbarStore.callback(function(message) {
  182. assert.deepEqual(message, {
  183. message: "Lol nope!",
  184. type: 'error'
  185. }, "error message from backend was shown");
  186. done();
  187. });
  188. testUtils.onElement('#test-mount form', function() {
  189. testUtils.simulateChange('#test-mount form #id_new_password', 'newps');
  190. testUtils.simulateChange('#test-mount form #id_repeat_password', 'newps');
  191. testUtils.simulateChange('#test-mount form #id_password', 'p4ssw0rd');
  192. testUtils.simulateSubmit('#test-mount form');
  193. });
  194. });
  195. it("handles backend error", function(done) {
  196. $.mockjax({
  197. url: user.api_url.change_password,
  198. status: 500
  199. });
  200. /* jshint ignore:start */
  201. testUtils.render(<ChangePassword user={user} />);
  202. /* jshint ignore:end */
  203. snackbarStore.callback(function(message) {
  204. assert.deepEqual(message, {
  205. message: "Unknown error has occured.",
  206. type: 'error'
  207. }, "error message from backend was shown");
  208. done();
  209. });
  210. testUtils.onElement('#test-mount form', function() {
  211. testUtils.simulateChange('#test-mount form #id_new_password', 'newps');
  212. testUtils.simulateChange('#test-mount form #id_repeat_password', 'newps');
  213. testUtils.simulateChange('#test-mount form #id_password', 'p4ssw0rd');
  214. testUtils.simulateSubmit('#test-mount form');
  215. });
  216. });
  217. it("handles successful submission", function(done) {
  218. $.mockjax({
  219. url: user.api_url.change_password,
  220. status: 200,
  221. responseText: {
  222. detail: "Well done gud!"
  223. }
  224. });
  225. /* jshint ignore:start */
  226. testUtils.render(<ChangePassword user={user} />);
  227. /* jshint ignore:end */
  228. snackbarStore.callback(function(message) {
  229. assert.deepEqual(message, {
  230. message: "Well done gud!",
  231. type: 'success'
  232. }, "success message from backend was shown");
  233. done();
  234. });
  235. testUtils.onElement('#test-mount form', function() {
  236. testUtils.simulateChange('#test-mount form #id_new_password', 'newps');
  237. testUtils.simulateChange('#test-mount form #id_repeat_password', 'newps');
  238. testUtils.simulateChange('#test-mount form #id_password', 'p4ssw0rd');
  239. testUtils.simulateSubmit('#test-mount form');
  240. });
  241. });
  242. });
  243. describe("Change Sign In Credentials Root", function() {
  244. beforeEach(function() {
  245. snackbarStore = testUtils.snackbarStoreMock();
  246. snackbar.init(snackbarStore);
  247. misago._context = {
  248. FORGOTTEN_PASSWORD_URL: '/lolo/toto/',
  249. SETTINGS: {
  250. password_length_min: 4
  251. }
  252. };
  253. });
  254. it("renders", function(done) {
  255. /* jshint ignore:start */
  256. testUtils.render(<Root user={user} />);
  257. /* jshint ignore:end */
  258. testUtils.onElement('#test-mount .message-line', function() {
  259. assert.equal($("#test-mount .message-line a").attr('href'),
  260. misago._context.FORGOTTEN_PASSWORD_URL,
  261. "change forgotten password url is rendered");
  262. done();
  263. });
  264. });
  265. });