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