forgotten-password-test.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import Ember from 'ember';
  2. import { module, test } from 'qunit';
  3. import startApp from '../helpers/start-app';
  4. import getToastMessage from '../helpers/toast-message';
  5. var application;
  6. module('Acceptance: Forgotten Password Change', {
  7. beforeEach: function() {
  8. application = startApp();
  9. },
  10. afterEach: function() {
  11. Ember.$('#hidden-login-form').off('submit.stopInTest');
  12. Ember.$('#loginModal').off();
  13. Ember.$('body').removeClass('modal-open');
  14. Ember.run(application, 'destroy');
  15. Ember.$.mockjax.clear();
  16. }
  17. });
  18. test('visiting /forgotten-password', function(assert) {
  19. assert.expect(1);
  20. visit('/forgotten-password');
  21. andThen(function() {
  22. assert.equal(currentPath(), 'forgotten-password.index');
  23. });
  24. });
  25. test('request password change link without entering e-mail', function(assert) {
  26. assert.expect(2);
  27. visit('/forgotten-password');
  28. click('.forgotten-password-page form .btn-primary');
  29. andThen(function() {
  30. assert.equal(currentPath(), 'forgotten-password.index');
  31. assert.equal(getToastMessage(), 'Enter e-mail address.');
  32. });
  33. });
  34. test('request password change link with invalid e-mail', function(assert) {
  35. assert.expect(2);
  36. var message = 'Entered e-mail is invalid.';
  37. Ember.$.mockjax({
  38. url: '/api/change-password/send-link/',
  39. status: 400,
  40. responseText: {
  41. 'detail': message,
  42. 'code': 'invalid_email'
  43. }
  44. });
  45. visit('/forgotten-password');
  46. fillIn('.forgotten-password-page form input', 'not-valid-email');
  47. click('.forgotten-password-page form .btn-primary');
  48. andThen(function() {
  49. assert.equal(currentPath(), 'forgotten-password.index');
  50. assert.equal(getToastMessage(), message);
  51. });
  52. });
  53. test('request password change link with non-existing e-mail', function(assert) {
  54. assert.expect(2);
  55. var message = 'No user with this e-mail exists.';
  56. Ember.$.mockjax({
  57. url: '/api/change-password/send-link/',
  58. status: 400,
  59. responseText: {
  60. 'detail': message,
  61. 'code': 'not_found'
  62. }
  63. });
  64. visit('/forgotten-password');
  65. fillIn('.forgotten-password-page form input', 'not-valid-email');
  66. click('.forgotten-password-page form .btn-primary');
  67. andThen(function() {
  68. assert.equal(currentPath(), 'forgotten-password.index');
  69. assert.equal(getToastMessage(), message);
  70. });
  71. });
  72. test('request password change link with user-activated account', function(assert) {
  73. assert.expect(2);
  74. var message = 'You have to activate your account before you will be able to sign in.';
  75. Ember.$.mockjax({
  76. url: '/api/change-password/send-link/',
  77. status: 400,
  78. responseText: {
  79. 'detail': message,
  80. 'code': 'inactive_user'
  81. }
  82. });
  83. visit('/forgotten-password');
  84. fillIn('.forgotten-password-page form input', 'valid@mail.com');
  85. click('.forgotten-password-page form .btn-primary');
  86. andThen(function() {
  87. assert.equal(currentPath(), 'forgotten-password.index');
  88. assert.equal(getToastMessage(), message);
  89. });
  90. });
  91. test('request password change link with admin-activated account', function(assert) {
  92. assert.expect(2);
  93. var message = 'Your account has to be activated by Administrator before you will be able to sign in.';
  94. Ember.$.mockjax({
  95. url: '/api/change-password/send-link/',
  96. status: 400,
  97. responseText: {
  98. 'detail': message,
  99. 'code': 'inactive_admin'
  100. }
  101. });
  102. visit('/forgotten-password');
  103. fillIn('.forgotten-password-page form input', 'valid@mail.com');
  104. click('.forgotten-password-page form .btn-primary');
  105. andThen(function() {
  106. assert.equal(currentPath(), 'forgotten-password.index');
  107. assert.equal(getToastMessage(), message);
  108. });
  109. });
  110. test('request password change link with banned account', function(assert) {
  111. assert.expect(2);
  112. Ember.$.mockjax({
  113. url: '/api/change-password/send-link/',
  114. status: 400,
  115. responseText: {
  116. 'detail': {
  117. 'expires_on': null,
  118. 'message': {
  119. 'plain': 'You are banned for trolling.',
  120. 'html': '<p>You are banned for trolling.</p>',
  121. }
  122. },
  123. 'code': 'banned'
  124. }
  125. });
  126. visit('/forgotten-password');
  127. fillIn('.forgotten-password-page form input', 'valid@mail.com');
  128. click('.forgotten-password-page form .btn-primary');
  129. andThen(function() {
  130. var errorMessage = find('.lead p').text();
  131. assert.equal(errorMessage, 'You are banned for trolling.');
  132. var expirationMessage = find('.error-message>p').text();
  133. assert.equal(expirationMessage, 'This ban is permanent.');
  134. });
  135. });
  136. test('request password change link', function(assert) {
  137. assert.expect(1);
  138. Ember.$.mockjax({
  139. url: '/api/change-password/send-link/',
  140. status: 200,
  141. responseText: {
  142. 'username': 'BobBoberson',
  143. 'email': 'valid@mail.com'
  144. }
  145. });
  146. visit('/forgotten-password');
  147. fillIn('.forgotten-password-page form input', 'valid@mail.com');
  148. click('.forgotten-password-page form .btn-primary');
  149. andThen(function() {
  150. var pageHeader = Ember.$.trim(find('.page-header h1').text());
  151. assert.equal(pageHeader, 'Change password form link sent');
  152. });
  153. });
  154. test('invalid token is handled', function(assert) {
  155. assert.expect(2);
  156. var message = 'Token was rejected.';
  157. Ember.$.mockjax({
  158. url: '/api/change-password/1/token/validate-token/',
  159. status: 404,
  160. responseText: {
  161. 'detail': message
  162. }
  163. });
  164. visit('/forgotten-password/1/token/');
  165. andThen(function() {
  166. assert.equal(currentPath(), 'forgotten-password.index');
  167. assert.equal(getToastMessage(), message);
  168. });
  169. });
  170. test('permission denied is handled', function(assert) {
  171. assert.expect(2);
  172. var message = 'Token was rejected.';
  173. Ember.$.mockjax({
  174. url: '/api/change-password/1/token/validate-token/',
  175. status: 403,
  176. responseText: {
  177. 'detail': message
  178. }
  179. });
  180. visit('/forgotten-password/1/token/');
  181. andThen(function() {
  182. assert.equal(currentPath(), 'error-403');
  183. var errorMessage = Ember.$.trim(find('.lead').text());
  184. assert.equal(errorMessage, message);
  185. });
  186. });
  187. test('token is validated', function(assert) {
  188. assert.expect(1);
  189. Ember.$.mockjax({
  190. url: '/api/change-password/1/token/validate-token/',
  191. status: 200,
  192. responseText: {
  193. 'user_id': 1,
  194. 'token': 'token',
  195. 'change_password_url': '/api/change-password-url/'
  196. }
  197. });
  198. visit('/forgotten-password/1/token/');
  199. andThen(function() {
  200. assert.equal(currentPath(), 'forgotten-password.change-form');
  201. });
  202. });
  203. test('no new password is entered', function(assert) {
  204. assert.expect(2);
  205. Ember.$.mockjax({
  206. url: '/api/change-password/1/token/validate-token/',
  207. status: 200,
  208. responseText: {
  209. 'user_id': 1,
  210. 'token': 'token',
  211. 'change_password_url': '/api/change-password-url/'
  212. }
  213. });
  214. visit('/forgotten-password/1/token/');
  215. click('.forgotten-password-page form .btn-primary');
  216. andThen(function() {
  217. assert.equal(currentPath(), 'forgotten-password.change-form');
  218. assert.equal(getToastMessage(), 'Enter new password.');
  219. });
  220. });
  221. test('new password is invalid', function(assert) {
  222. assert.expect(2);
  223. Ember.$.mockjax({
  224. url: '/api/change-password/1/token/validate-token/',
  225. status: 200,
  226. responseText: {
  227. 'user_id': 1,
  228. 'token': 'token',
  229. 'change_password_url': '/api/change-password-url/'
  230. }
  231. });
  232. var message = 'Entered password is not allowed.';
  233. Ember.$.mockjax({
  234. url: '/api/change-password-url/',
  235. status: 400,
  236. responseText: {
  237. 'detail': message
  238. }
  239. });
  240. visit('/forgotten-password/1/token/');
  241. fillIn('.forgotten-password-page form .control-input input', 'newp4ssw0rd');
  242. click('.forgotten-password-page form .btn-primary');
  243. andThen(function() {
  244. assert.equal(currentPath(), 'forgotten-password.change-form');
  245. assert.equal(getToastMessage(), message);
  246. });
  247. });
  248. test('new password is accepted', function(assert) {
  249. assert.expect(3);
  250. Ember.$.mockjax({
  251. url: '/api/change-password/1/token/validate-token/',
  252. status: 200,
  253. responseText: {
  254. 'user_id': 1,
  255. 'token': 'token',
  256. 'change_password_url': '/api/change-password-url/'
  257. }
  258. });
  259. var message = 'lul';
  260. Ember.$.mockjax({
  261. url: '/api/change-password-url/',
  262. status: 200
  263. });
  264. visit('/forgotten-password/1/token/');
  265. fillIn('.forgotten-password-page form .control-input input', 'newp4ssw0rd');
  266. click('.forgotten-password-page form .btn-primary');
  267. andThen(function() {
  268. assert.equal(currentPath(), 'forgotten-password.change-form');
  269. assert.equal(getToastMessage(), "Your password has been changed.");
  270. assert.ok(find('#loginModal').hasClass('in'));
  271. });
  272. });