Browse Source

Auth: register account with Ember.js

Rafał Pitoń 10 years ago
parent
commit
6d5651aaeb

+ 4 - 8
misago/emberapp/app/components/forms/register-form.js

@@ -14,19 +14,15 @@ export default Ember.Component.extend({
   email: '',
   password: '',
 
-  clearForm: function() {
-    this.setProperties({
-      username: '',
-      email: '',
-      password: ''
-    });
-  }.on('willDestroyElement'),
-
   validation: null,
   setValidation: function() {
     this.set('validation', Ember.Object.create({}));
   }.on('init'),
 
+  router: function() {
+    return this.container.lookup('router:main');
+  }.property(),
+
   passwordScore: function() {
     return this.get('zxcvbn').scorePassword(this.get('password'), [
       this.get('username'), this.get('email')

+ 2 - 3
misago/emberapp/app/services/registration-modal.js

@@ -4,15 +4,14 @@ export default Ember.Service.extend({
   stage: 'form',
 
   isForm: Ember.computed.equal('stage', 'form'),
-  isDone: Ember.computed.equal('stage', 'done'),
   isClosed: Ember.computed.equal('stage', 'closed'),
 
   resolveStage: function() {
-    if (!this.get('isDone') && this.get('settings.account_activation') === 'closed') {
+    if (this.get('settings.account_activation') === 'closed') {
       // we didn't complete prior registration and registrations aren't open
       this.set('stage', 'closed');
     }
-  },
+  }.on('init'),
 
   template: function() {
     return 'register.' + this.get('stage');

+ 2 - 2
misago/emberapp/app/templates/modals/register/done.hbs

@@ -15,14 +15,14 @@
       </p>
       {{else if model.needsAdminActivation}}
       <p class="lead">
-        {{gettext "%(username)s, your account has been registered successfully, but site administrator has to activate it." username=model.username}}
+        {{gettext "%(username)s, your account has been registered successfully, but site administrator has to activate it before you will be able to sing in." username=model.username}}
       </p>
       <p>
         {{gettext "Notification will be sent to %(email)s once it happens." email=model.email}}
       </p>
       {{else}}
       <p class="lead">
-        {{gettext "%(username)s, your account has been registered successfully, but you have to activate it." username=model.username}}
+        {{gettext "%(username)s, your account has been registered successfully, but you have to activate it before you will be able to sign in." username=model.username}}
       </p>
       <p>
         {{gettext "E-mail with activation link was sent to %(email)s." email=model.email}}

+ 1 - 1
misago/emberapp/app/templates/navbar.hbs

@@ -14,7 +14,7 @@
         {{gettext "Sign in"}}
       </button>
 
-      {{#register-button class="btn btn-info btn-logout navbar-btn btn-sm"}}
+      {{#register-button class="btn btn-info btn-join navbar-btn btn-sm"}}
         {{gettext "Join now"}}
       {{/register-button}}
     </div>

+ 0 - 1
misago/emberapp/tests/acceptance/forgotten-password-test.js

@@ -11,7 +11,6 @@ module('Acceptance: Forgotten Password Change', {
   },
 
   afterEach: function() {
-    Ember.$('#hidden-login-form').off('submit.stopInTest');
     Ember.$('#appModal').off();
     Ember.$('body').removeClass('modal-open');
     Ember.run(application, 'destroy');

+ 195 - 0
misago/emberapp/tests/acceptance/registration-test.js

@@ -0,0 +1,195 @@
+import Ember from 'ember';
+import { module, test } from 'qunit';
+import startApp from '../helpers/start-app';
+import PreloadStore from 'misago/services/preload-store';
+import getToastMessage from '../helpers/toast-message';
+
+var application;
+
+module('Acceptance: Register', {
+  beforeEach: function() {
+    application = startApp();
+  },
+
+  afterEach: function() {
+    window.MisagoData['misagoSettings']['account_activation'] = 'none';
+    Ember.$('#appModal').off();
+    Ember.$('body').removeClass('modal-open');
+    Ember.run(application, 'destroy');
+    Ember.$.mockjax.clear();
+  }
+});
+
+test('registration is closed', function(assert) {
+  window.MisagoData['misagoSettings']['account_activation'] = 'closed';
+  assert.expect(1);
+
+  visit('/');
+  click('.guest-nav button.btn-join');
+
+  andThen(function() {
+    assert.equal(Ember.$.trim(find('.modal-register-closed .lead').text()), 'New registrations are currently not being accepted.');
+  });
+});
+
+test('register with empty credentials', function(assert) {
+  assert.expect(1);
+
+  visit('/');
+  click('.guest-nav button.btn-join');
+  click('#appModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(getToastMessage(), 'Fill out all fields.');
+  });
+});
+
+test('register with invalid credentials', function(assert) {
+  assert.expect(1);
+
+  visit('/');
+  click('.guest-nav button.btn-join');
+  fillIn('#appModal #id_username', 'a');
+  fillIn('#appModal #id_password', 'b');
+  fillIn('#appModal #id_email', 'c');
+  click('#appModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(getToastMessage(), 'Form contains errors.');
+  });
+});
+
+test('register with rejected credentials', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: "/api/users/",
+    status: 400
+  });
+
+  visit('/');
+  click('.guest-nav button.btn-join');
+  fillIn('#appModal #id_username', 'Lorem');
+  fillIn('#appModal #id_password', 'ipsum123');
+  fillIn('#appModal #id_email', 'lorem@ipsum.com');
+  click('#appModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(getToastMessage(), 'Form contains errors.');
+  });
+});
+
+test('register banned', function(assert) {
+  assert.expect(3);
+
+  Ember.$.mockjax({
+    url: "/api/users/",
+    status: 403,
+    responseText: {
+      'detail': 'Thy are banned!',
+      'ban': {
+        'expires_on': null,
+        'message': {
+          'plain': 'You are banned for trolling.',
+          'html': '<p>You are banned for trolling.</p>',
+        }
+      }
+    }
+  });
+
+  visit('/');
+  click('.guest-nav button.btn-join');
+  fillIn('#appModal #id_username', 'Lorem');
+  fillIn('#appModal #id_password', 'ipsum123');
+  fillIn('#appModal #id_email', 'lorem@ipsum.com');
+  click('#appModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'error-banned');
+
+    var banMessage = find('.lead p').text();
+    assert.equal(banMessage, 'You are banned for trolling.');
+
+    var expirationMessage = Ember.$.trim(find('.error-message>p').text());
+    assert.equal(expirationMessage, 'This ban is permanent.');
+  });
+});
+
+test('register active user', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: "/api/users/",
+    status: 200,
+    responseText: {
+      'activation': 'active',
+      'username': 'BobBoberson',
+      'email': 'bob@weebl.com'
+    }
+  });
+
+  visit('/');
+  click('.guest-nav button.btn-join');
+  fillIn('#appModal #id_username', 'Lorem');
+  fillIn('#appModal #id_password', 'ipsum123');
+  fillIn('#appModal #id_email', 'lorem@ipsum.com');
+  click('#appModal .btn-primary');
+
+  andThen(function() {
+    var expectedMessage = 'BobBoberson, your account has been registered successfully.';
+    assert.equal(Ember.$.trim(find('.modal-register-done .lead').text()), expectedMessage);
+  });
+});
+
+test('register admin-activated user', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: "/api/users/",
+    status: 200,
+    responseText: {
+      'activation': 'activation_by_admin',
+      'username': 'BobBoberson',
+      'email': 'bob@weebl.com'
+    }
+  });
+
+  visit('/');
+  click('.guest-nav button.btn-join');
+  fillIn('#appModal #id_username', 'Lorem');
+  fillIn('#appModal #id_password', 'ipsum123');
+  fillIn('#appModal #id_email', 'lorem@ipsum.com');
+  click('#appModal .btn-primary');
+
+  andThen(function() {
+    var expectedMessage = 'BobBoberson, your account has been registered successfully, but site administrator has to activate it before you will be able to sing in.';
+    assert.equal(Ember.$.trim(find('.modal-register-done .lead').text()), expectedMessage);
+  });
+});
+
+test('register self-activated user', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: "/api/users/",
+    status: 200,
+    responseText: {
+      'activation': 'activation_by_user',
+      'username': 'BobBoberson',
+      'email': 'bob@weebl.com'
+    }
+  });
+
+  visit('/');
+  click('.guest-nav button.btn-join');
+  fillIn('#appModal #id_username', 'Lorem');
+  fillIn('#appModal #id_password', 'ipsum123');
+  fillIn('#appModal #id_email', 'lorem@ipsum.com');
+  click('#appModal .btn-primary');
+
+  andThen(function() {
+    var expectedMessage = 'BobBoberson, your account has been registered successfully, but you have to activate it before you will be able to sign in.';
+    assert.equal(Ember.$.trim(find('.modal-register-done .lead').text()), expectedMessage);
+  });
+});
+

+ 2 - 2
misago/users/api/users.py

@@ -44,9 +44,9 @@ class UserViewSet(viewsets.ViewSet):
 
 @csrf_protect
 def _create_user(request):
-    if settings.account_activation == 'disabled':
+    if settings.account_activation == 'closed':
         raise PermissionDenied(
-            _("New users registrations are currently disabled."))
+            _("New users registrations are currently closed."))
 
     form = RegisterForm(request.data)
 

+ 2 - 2
misago/users/tests/test_users_api.py

@@ -24,11 +24,11 @@ class CreateTests(UserTestCase):
 
     def test_registration_off_request(self):
         """registrations off request errors with code 403"""
-        settings.override_setting('account_activation', 'disabled')
+        settings.override_setting('account_activation', 'closed')
 
         response = self.client.post('/api/users/')
         self.assertEqual(response.status_code, 403)
-        self.assertIn('disabled', response.content)
+        self.assertIn('closed', response.content)
 
     def test_registration_creates_active_user(self):
         """api creates active and signed in user on POST"""