sign-in-credentials.js 9.0 KB

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