Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Try to connect on login page before going further - Closes #283 #307

Merged
merged 2 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/components/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ app.component('login', {
/* eslint no-param-reassign: ["error", { "props": false }] */

constructor($scope, $rootScope, $timeout, $document, $mdMedia,
$cookies, $location, Passphrase, $state, Account, Peers) {
$cookies, $location, Passphrase, $state, Account, Peers, dialog) {
this.$scope = $scope;
this.$rootScope = $rootScope;
this.$timeout = $timeout;
Expand All @@ -19,9 +19,11 @@ app.component('login', {
this.$state = $state;
this.account = Account;
this.peers = Peers;
this.dialog = dialog;

this.Passphrase = Passphrase;
this.generatingNewPassphrase = false;
this.$rootScope.loggingIn = false;

this.networks = [{
name: 'Mainnet',
Expand Down Expand Up @@ -58,14 +60,20 @@ app.component('login', {
* @param {String} [_passphrase=this.input_passphrase]
*/
passConfirmSubmit(_passphrase = this.input_passphrase) {
this.$rootScope.loggingIn = true;
if (this.Passphrase.normalize.constructor === Function) {
this.peers.setActive(this.network);

this.account.set({
passphrase: this.Passphrase.normalize(_passphrase),
network: this.network,
this.peers.setActive(this.network).then(() => {
this.$rootScope.loggingIn = false;
if (this.peers.online) {
this.account.set({
passphrase: this.Passphrase.normalize(_passphrase),
network: this.network,
});
this.$state.go(this.$rootScope.landingUrl || 'main.transactions');
} else {
this.dialog.errorToast(`Failed to connect to node ${this.network.address}`);
}
});
this.$state.go(this.$rootScope.landingUrl || 'main.transactions');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/login/login.pug
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ md-card
md-content(layout='row', layout-align='center center')
// md-button(ng-disabled='$ctrl.generatingNewPassphrase', ng-click='$ctrl.devTestAccount()') Dev Test Account
md-button.md-primary.new-account-button(ng-disabled='$ctrl.random || $ctrl.generatingNewPassphrase', ng-click='$ctrl.generatePassphrase()') NEW ACCOUNT
md-button.md-raised.md-primary.login-button(md-autofocus, ng-disabled='$ctrl.valid != undefined && $ctrl.valid !== 1', type='submit') Login
md-button.md-raised.md-primary.login-button(md-autofocus, ng-disabled='($ctrl.valid != undefined && $ctrl.valid !== 1) || $root.loggingIn', type='submit') Login
passphrase(ng-if='$ctrl.generatingNewPassphrase', data-on-login='$ctrl.onLogin', data-target='primary-pass')
2 changes: 1 addition & 1 deletion src/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ html
div.body-wrapper
md-content(id="main", flex='100', flex-gt-sm='80', flex-offset-gt-sm='10')
header
div.absolutely-positioned-loading(layout='row', layout-align='space-around', ng-show='prelogged')
div.absolutely-positioned-loading(layout='row', layout-align='space-around', ng-show='prelogged || loggingIn')
md-progress-circular.md-warn(md-mode='indeterminate', md-diameter='80')
div(ng-class='{ online: $root.peers.online, offline: !$root.peers.online }')
div(data-ui-view)
8 changes: 3 additions & 5 deletions src/services/api/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope) => {
conf = network;
if (network.address) {
conf.node = network.address.split(':')[1].replace('//', '');
conf.port = network.address.match(/:([0-9]{1,5})$/)[1];
conf.port = network.address.match(/:([0-9]{1,5})\/?$/) && network.address.match(/:([0-9]{1,5})\/?$/)[1];
conf.ssl = network.address.split(':')[0] === 'https';
}
if (conf.testnet === undefined && conf.port !== undefined) {
Expand All @@ -63,7 +63,7 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope) => {
}

this.active = lisk.api(conf);
this.check();
return this.check();
}

/**
Expand Down Expand Up @@ -95,9 +95,7 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope) => {
* @method check
*/
check() {
this.reset();

this.sendRequestPromise('loader/status', {})
return this.sendRequestPromise('loader/status', {})
.then(() => this.online = true)
.catch(() => this.online = false);
}
Expand Down
29 changes: 28 additions & 1 deletion test/components/login/login.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ describe('Login controller', () => {
/* eslint-disable no-unused-vars */
let $timeout;
/* eslint-enable no-unused-vars */
let $q;

beforeEach(inject((_$componentController_, _$rootScope_, _$state_,
_Passphrase_, _$cookies_, _$timeout_, _Account_) => {
_Passphrase_, _$cookies_, _$timeout_, _Account_, _$q_) => {
$componentController = _$componentController_;
$rootScope = _$rootScope_;
$state = _$state_;
Expand All @@ -81,6 +82,7 @@ describe('Login controller', () => {
/* eslint-disable no-unused-vars */
$timeout = _$timeout_;
/* eslint-enable no-unused-vars */
$q = _$q_;
}));

beforeEach(() => {
Expand Down Expand Up @@ -122,24 +124,49 @@ describe('Login controller', () => {
});

describe('passConfirmSubmit()', () => {
let peersMock;
let deferred;

beforeEach(() => {
deferred = $q.defer();
peersMock = sinon.mock(controller.peers);
peersMock.expects('setActive').returns(deferred.promise);
controller.peers.online = true;
});

it('sets account.phassphrase as this.input_passphrase processed by normalizer', () => {
controller.input_passphrase = '\tTEST PassPHrASe ';
controller.passConfirmSubmit();
deferred.resolve();
$scope.$apply();
expect(account.get().passphrase).to.equal('test passphrase');
});

it('calls Passphrase.normalize()', () => {
const spy = sinon.spy(Passphrase, 'normalize');
controller.passConfirmSubmit();
deferred.resolve();
$scope.$apply();
expect(spy).to.have.been.calledWith();
});

it('redirects to main if passphrase is valid', () => {
controller.input_passphrase = testPassphrase;
const spy = sinon.spy($state, 'go');
controller.passConfirmSubmit();
deferred.resolve();
$scope.$apply();
expect(spy).to.have.been.calledWith();
});

it('shows error toast if peers.setActive sets peers.online = false', () => {
const spy = sinon.spy(controller.dialog, 'errorToast');
controller.peers.online = false;
controller.passConfirmSubmit();
deferred.resolve();
$scope.$apply();
expect(spy).to.have.been.calledWith(`Failed to connect to node ${controller.network.address}`);
});
});

describe('devTestAccount()', () => {
Expand Down