Browse Source

tests for forgotten password change process

Rafał Pitoń 10 years ago
parent
commit
30a1e88020

+ 5 - 5
misago/emberapp/app/controllers/flash-message.js

@@ -1,9 +1,9 @@
 import Ember from 'ember';
+import ENV from '../config/environment';
 
-export default Ember.Controller.extend({
-  VISIBLE_FOR: 4500,
-  HIDE_ANIMATION_LENGTH: 200,
+var HIDE_ANIMATION_LENGTH = 200;
 
+export default Ember.Controller.extend({
   id: null,
   type: null,
   message: null,
@@ -36,7 +36,7 @@ export default Ember.Controller.extend({
       if (self.get('id') === flashId) {
         self.set('isVisible', false);
       }
-    }, this.get('VISIBLE_FOR'));
+    }, ENV.APP.FLASH_MIN_DISPLAY_TIME);
   },
 
   actions: {
@@ -47,7 +47,7 @@ export default Ember.Controller.extend({
         this.set('isVisible', false);
         Ember.run.later(function () {
           self.showFlash(type, message);
-        }, this.get('HIDE_ANIMATION_LENGTH'));
+        }, HIDE_ANIMATION_LENGTH);
       } else {
         this.showFlash(type, message);
       }

+ 1 - 1
misago/emberapp/app/controllers/forgotten-password/change-form.js

@@ -32,7 +32,7 @@ export default Ember.ObjectController.extend({
       }, function(jqXHR) {
         var rejection = jqXHR.responseJSON;
         if (jqXHR.status === 400){
-          this.send('flashError', rejection.detail);
+          self.send('flashError', rejection.detail);
         } else {
           self.set('password', '');
 

+ 5 - 1
misago/emberapp/config/environment.js

@@ -24,7 +24,10 @@ module.exports = function(environment) {
       API_ADD_TRAILING_SLASHES: true,
 
       // Misago ticks frequency (in ms, used for refreshing timestamps)
-      TICK_FREQUENCY: 15000
+      TICK_FREQUENCY: 15000,
+
+      // Min time flash is displayed for (in ms)
+      FLASH_MIN_DISPLAY_TIME: 4500
     }
   };
 
@@ -58,6 +61,7 @@ module.exports = function(environment) {
     ENV.APP.LOG_VIEW_LOOKUPS = false;
 
     ENV.APP.rootElement = '#ember-testing';
+    ENV.APP.FLASH_MIN_DISPLAY_TIME = 500;
   }
 
   if (environment === 'production') {

+ 151 - 6
misago/emberapp/tests/acceptance/forgotten-password-test.js

@@ -10,6 +10,9 @@ module('Acceptance: ForgottenPassword', {
   },
 
   afterEach: function() {
+    Ember.$('#hidden-login-form').off('submit.stopInTest');
+    Ember.$('#loginModal').off();
+    Ember.$('body').removeClass('modal-open');
     Ember.run(application, 'destroy');
     Ember.$.mockjax.clear();
   }
@@ -38,7 +41,7 @@ test('request password change link without entering e-mail', function(assert) {
 test('request password change link with invalid e-mail', function(assert) {
   var message = 'Entered e-mail is invalid.';
   Ember.$.mockjax({
-    url: "/api/change-password/send-link/",
+    url: '/api/change-password/send-link/',
     status: 400,
     responseText: {
       'detail': message,
@@ -61,7 +64,7 @@ test('request password change link with invalid e-mail', function(assert) {
 test('request password change link with non-existing e-mail', function(assert) {
   var message = 'No user with this e-mail exists.';
   Ember.$.mockjax({
-    url: "/api/change-password/send-link/",
+    url: '/api/change-password/send-link/',
     status: 400,
     responseText: {
       'detail': message,
@@ -84,7 +87,7 @@ test('request password change link with non-existing e-mail', function(assert) {
 test('request password change link with user-activated account', function(assert) {
   var message = 'You have to activate your account before you will be able to sign in.';
   Ember.$.mockjax({
-    url: "/api/change-password/send-link/",
+    url: '/api/change-password/send-link/',
     status: 400,
     responseText: {
       'detail': message,
@@ -107,7 +110,7 @@ test('request password change link with user-activated account', function(assert
 test('request password change link with admin-activated account', function(assert) {
   var message = 'Your account has to be activated by Administrator before you will be able to sign in.';
   Ember.$.mockjax({
-    url: "/api/change-password/send-link/",
+    url: '/api/change-password/send-link/',
     status: 400,
     responseText: {
       'detail': message,
@@ -130,7 +133,7 @@ test('request password change link with admin-activated account', function(asser
 test('request password change link with banned account', function(assert) {
   var done = assert.async();
   Ember.$.mockjax({
-    url: "/api/change-password/send-link/",
+    url: '/api/change-password/send-link/',
     status: 400,
     responseText: {
       'detail': {
@@ -162,7 +165,7 @@ test('request password change link with banned account', function(assert) {
 test('request password change link', function(assert) {
   var done = assert.async();
   Ember.$.mockjax({
-    url: "/api/change-password/send-link/",
+    url: '/api/change-password/send-link/',
     status: 200,
     responseText: {
       'username': 'BobBoberson',
@@ -181,3 +184,145 @@ test('request password change link', function(assert) {
     done();
   });
 });
+
+test('invalid token is handled', function(assert) {
+  var message = 'Token was rejected.';
+  Ember.$.mockjax({
+    url: '/api/change-password/1/token/validate-token/',
+    status: 404,
+    responseText: {
+      'detail': message
+    }
+  });
+
+  visit('/forgotten-password/1/token/');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.index');
+
+    var errorMessage = Ember.$.trim(find('.flash-message>p').text());
+    assert.equal(errorMessage, message);
+  });
+});
+
+test('permission denied is handled', function(assert) {
+  var message = 'Token was rejected.';
+  Ember.$.mockjax({
+    url: '/api/change-password/1/token/validate-token/',
+    status: 403,
+    responseText: {
+      'detail': message
+    }
+  });
+
+  visit('/forgotten-password/1/token/');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'error-403');
+
+    var errorMessage = Ember.$.trim(find('.lead').text());
+    assert.equal(errorMessage, message);
+  });
+});
+
+test('token is validated', function(assert) {
+  Ember.$.mockjax({
+    url: '/api/change-password/1/token/validate-token/',
+    status: 200,
+    responseText: {
+      'user_id': 1,
+      'token': 'token',
+      'change_password_url': '/api/change-password-url/'
+    }
+  });
+
+  visit('/forgotten-password/1/token/');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.change-form');
+  });
+});
+
+test('no new password is entered', function(assert) {
+  Ember.$.mockjax({
+    url: '/api/change-password/1/token/validate-token/',
+    status: 200,
+    responseText: {
+      'user_id': 1,
+      'token': 'token',
+      'change_password_url': '/api/change-password-url/'
+    }
+  });
+
+  visit('/forgotten-password/1/token/');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.change-form');
+
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, 'Enter new password.');
+  });
+});
+
+test('new password is invalid', function(assert) {
+  Ember.$.mockjax({
+    url: '/api/change-password/1/token/validate-token/',
+    status: 200,
+    responseText: {
+      'user_id': 1,
+      'token': 'token',
+      'change_password_url': '/api/change-password-url/'
+    }
+  });
+
+  var message = 'Entered password is not allowed.';
+  Ember.$.mockjax({
+    url: '/api/change-password-url/',
+    status: 400,
+    responseText: {
+      'detail': message
+    }
+  });
+
+  visit('/forgotten-password/1/token/');
+  fillIn('.forgotten-password-page form .control-input input', 'newp4ssw0rd');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.change-form');
+
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, message);
+  });
+});
+
+test('new password is accepted', function(assert) {
+  Ember.$.mockjax({
+    url: '/api/change-password/1/token/validate-token/',
+    status: 200,
+    responseText: {
+      'user_id': 1,
+      'token': 'token',
+      'change_password_url': '/api/change-password-url/'
+    }
+  });
+
+  var message = 'lul';
+  Ember.$.mockjax({
+    url: '/api/change-password-url/',
+    status: 200
+  });
+
+  visit('/forgotten-password/1/token/');
+  fillIn('.forgotten-password-page form .control-input input', 'newp4ssw0rd');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.change-form');
+    assert.ok(find('#loginModal').hasClass('in'));
+
+    var message = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(message, "Your password has been changed.");
+  });
+});