forgotten-password-test.js 8.6 KB

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