change-password.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. (function () {
  2. 'use strict';
  3. var app = null;
  4. QUnit.acceptance("Change Password", {
  5. beforeEach: function() {
  6. app = initTestMisago();
  7. },
  8. afterEach: function() {
  9. app.destroy();
  10. }
  11. });
  12. QUnit.test('with backend banned', function(assert) {
  13. $.mockjax({
  14. url: '/test-api/auth/change-password/123/som3token/',
  15. status: 403,
  16. responseText: {
  17. 'detail': 'You are banned!',
  18. 'ban': {
  19. 'expires_on': null,
  20. 'message': {
  21. 'plain': 'This is test ban.',
  22. 'html': '<p>This is test ban.</p>'
  23. }
  24. }
  25. }
  26. });
  27. app.router.route('/forgotten-password/123/som3token/');
  28. var done = assert.async();
  29. onElement('.page-error.page-error-banned .message-body', function() {
  30. assert.ok(true, "Permission denied error page was displayed.");
  31. assert.equal(
  32. getElementText('.page .message-body .lead'),
  33. "This is test ban.",
  34. "Banned page displayed ban message.");
  35. done();
  36. });
  37. });
  38. QUnit.test('with backend rejection', function(assert) {
  39. var message = 'Some activation error.';
  40. $.mockjax({
  41. url: '/test-api/auth/change-password/123/som3token/',
  42. status: 400,
  43. responseText: {
  44. 'detail': message
  45. }
  46. });
  47. app.router.route('/forgotten-password/123/som3token/');
  48. var done = assert.async();
  49. onElement('.message-body p', function() {
  50. assert.equal(getElementText('.message-body p:last-child'), message,
  51. "activation failed with backend message.");
  52. done();
  53. });
  54. });
  55. QUnit.test('form', function(assert) {
  56. $.mockjax({
  57. url: '/test-api/auth/change-password/123/som3token/',
  58. status: 200,
  59. responseText: {
  60. 'username': 'Bob',
  61. 'email': 'bob@boberson.com'
  62. }
  63. });
  64. app.router.route('/forgotten-password/123/som3token/');
  65. var done = assert.async();
  66. onElement('.well-form', function() {
  67. assert.ok(true, "change password for was displayed.");
  68. done();
  69. });
  70. });
  71. QUnit.test('empty form submission', function(assert) {
  72. $.mockjax({
  73. url: '/test-api/auth/change-password/123/som3token/',
  74. status: 200,
  75. responseText: {
  76. 'username': 'Bob',
  77. 'email': 'bob@boberson.com'
  78. }
  79. });
  80. app.router.route('/forgotten-password/123/som3token/');
  81. var done = assert.async();
  82. waitForElement('.well-form .btn-primary');
  83. click('.well-form .btn-primary');
  84. onElement('.alerts .alert-danger', function() {
  85. assert.equal(getAlertMessage(), "Enter new password.",
  86. "form raised alert about empty input.");
  87. done();
  88. });
  89. });
  90. QUnit.test('invalid form submission', function(assert) {
  91. $.mockjax({
  92. url: '/test-api/auth/change-password/123/som3token/',
  93. status: 200,
  94. responseText: {
  95. 'username': 'Bob',
  96. 'email': 'bob@boberson.com'
  97. }
  98. });
  99. app.router.route('/forgotten-password/123/som3token/');
  100. var done = assert.async();
  101. waitForElement('.well-form input');
  102. fillIn('.well-form input', 'no');
  103. click('.well-form .btn-primary');
  104. onElement('.alerts .alert-danger', function() {
  105. assert.equal(
  106. getAlertMessage(),
  107. "Valid password must be at least 5 characters long.",
  108. "form raised alert about invalid new password.");
  109. done();
  110. });
  111. });
  112. QUnit.test('backend-rejected form submission', function(assert) {
  113. $.mockjax({
  114. url: '/test-api/auth/change-password/123/som3token/',
  115. type: 'get',
  116. status: 200,
  117. responseText: {
  118. 'username': 'Bob',
  119. 'email': 'bob@boberson.com'
  120. }
  121. });
  122. var message = "Backend error is raised!";
  123. $.mockjax({
  124. url: '/test-api/auth/change-password/123/som3token/',
  125. type: 'post',
  126. status: 400,
  127. responseText: {
  128. 'detail': message
  129. }
  130. });
  131. app.router.route('/forgotten-password/123/som3token/');
  132. var done = assert.async();
  133. waitForElement('.well-form input');
  134. fillIn('.well-form input', 'pass123');
  135. click('.well-form .btn-primary');
  136. onElement('.alerts .alert-danger', function() {
  137. assert.equal(getAlertMessage(), message,
  138. "form raised alert with error returned by backend.");
  139. done();
  140. });
  141. });
  142. QUnit.test('backend-accepted form submission', function(assert) {
  143. $.mockjax({
  144. url: '/test-api/auth/change-password/123/som3token/',
  145. type: 'get',
  146. status: 200,
  147. responseText: {
  148. 'username': 'Bob',
  149. 'email': 'bob@boberson.com'
  150. }
  151. });
  152. $.mockjax({
  153. url: '/test-api/auth/change-password/123/som3token/',
  154. type: 'post',
  155. status: 200,
  156. responseText: {
  157. 'username': 'Bob'
  158. }
  159. });
  160. app.router.route('/forgotten-password/123/som3token/');
  161. var doneMessage = assert.async();
  162. var doneButton = assert.async();
  163. waitForElement('.well-form input');
  164. fillIn('.well-form input', 'pass123');
  165. click('.well-form .btn-primary');
  166. onElement('.message-body p.lead', function() {
  167. assert.equal(
  168. getElementText('.message-body p.lead'),
  169. "Bob, your password has been changed successfully.",
  170. "form displayed success message.");
  171. doneMessage();
  172. });
  173. onElement('.message-body .btn-default', function() {
  174. assert.ok(true, "form displayed sign-in button.");
  175. doneButton();
  176. });
  177. });
  178. }());