2019-08-07 11:31:03 +00:00
|
|
|
import * as Promise from 'bluebird';
|
2019-11-15 09:45:14 +00:00
|
|
|
import { expect } from 'chai';
|
2019-08-07 11:31:03 +00:00
|
|
|
import rewire = require('rewire');
|
|
|
|
import * as sinon from 'sinon';
|
|
|
|
import * as url from 'url';
|
|
|
|
import tokens from './tokens';
|
2019-08-07 11:31:03 +00:00
|
|
|
|
2019-08-07 11:31:03 +00:00
|
|
|
const utils = rewire('../../build/auth/utils');
|
|
|
|
const balena = utils.__get__('balena');
|
2019-08-07 11:31:03 +00:00
|
|
|
|
|
|
|
describe('Utils:', function() {
|
|
|
|
describe('.getDashboardLoginURL()', function() {
|
|
|
|
it('should eventually be a valid url', () =>
|
2019-08-07 11:31:03 +00:00
|
|
|
utils
|
|
|
|
.getDashboardLoginURL('https://127.0.0.1:3000/callback')
|
|
|
|
.then((loginUrl: string) =>
|
2019-11-15 09:45:14 +00:00
|
|
|
expect(() => url.parse(loginUrl)).to.not.throw(Error),
|
2019-08-07 11:31:03 +00:00
|
|
|
));
|
2019-08-07 11:31:03 +00:00
|
|
|
|
|
|
|
it('should eventually contain an https protocol', () =>
|
|
|
|
Promise.props({
|
|
|
|
dashboardUrl: balena.settings.get('dashboardUrl'),
|
2019-08-07 11:31:03 +00:00
|
|
|
loginUrl: utils.getDashboardLoginURL('https://127.0.0.1:3000/callback'),
|
|
|
|
}).then(function({ dashboardUrl, loginUrl }) {
|
|
|
|
const { protocol } = url.parse(loginUrl);
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(protocol).to.equal(url.parse(dashboardUrl).protocol);
|
2019-08-07 11:31:03 +00:00
|
|
|
}));
|
2019-08-07 11:31:03 +00:00
|
|
|
|
|
|
|
it('should correctly escape a callback url without a path', () =>
|
|
|
|
Promise.props({
|
|
|
|
dashboardUrl: balena.settings.get('dashboardUrl'),
|
2019-08-07 11:31:03 +00:00
|
|
|
loginUrl: utils.getDashboardLoginURL('http://127.0.0.1:3000'),
|
|
|
|
}).then(function({ dashboardUrl, loginUrl }) {
|
|
|
|
const expectedUrl = `${dashboardUrl}/login/cli/http%253A%252F%252F127.0.0.1%253A3000`;
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(loginUrl).to.equal(expectedUrl);
|
2019-08-07 11:31:03 +00:00
|
|
|
}));
|
2019-08-07 11:31:03 +00:00
|
|
|
|
|
|
|
return it('should correctly escape a callback url with a path', () =>
|
|
|
|
Promise.props({
|
|
|
|
dashboardUrl: balena.settings.get('dashboardUrl'),
|
2019-08-07 11:31:03 +00:00
|
|
|
loginUrl: utils.getDashboardLoginURL('http://127.0.0.1:3000/callback'),
|
|
|
|
}).then(function({ dashboardUrl, loginUrl }) {
|
|
|
|
const expectedUrl = `${dashboardUrl}/login/cli/http%253A%252F%252F127.0.0.1%253A3000%252Fcallback`;
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(loginUrl).to.equal(expectedUrl);
|
2019-08-07 11:31:03 +00:00
|
|
|
}));
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return describe('.loginIfTokenValid()', function() {
|
|
|
|
it('should eventually be false if token is undefined', function() {
|
2019-08-07 11:31:03 +00:00
|
|
|
const promise = utils.loginIfTokenValid(undefined);
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(promise).to.eventually.be.false;
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should eventually be false if token is null', function() {
|
2019-08-07 11:31:03 +00:00
|
|
|
const promise = utils.loginIfTokenValid(null);
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(promise).to.eventually.be.false;
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should eventually be false if token is an empty string', function() {
|
2019-08-07 11:31:03 +00:00
|
|
|
const promise = utils.loginIfTokenValid('');
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(promise).to.eventually.be.false;
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should eventually be false if token is a string containing only spaces', function() {
|
2019-08-07 11:31:03 +00:00
|
|
|
const promise = utils.loginIfTokenValid(' ');
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(promise).to.eventually.be.false;
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('given the token does not authenticate with the server', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn');
|
|
|
|
return this.balenaAuthIsLoggedInStub.returns(Promise.resolve(false));
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
return this.balenaAuthIsLoggedInStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should eventually be false', function() {
|
2019-08-07 11:31:03 +00:00
|
|
|
const promise = utils.loginIfTokenValid(tokens.johndoe.token);
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(promise).to.eventually.be.false;
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('given there was a token already', function() {
|
|
|
|
beforeEach(() => balena.auth.loginWithToken(tokens.janedoe.token));
|
|
|
|
|
|
|
|
return it('should preserve the old token', () =>
|
2019-08-07 11:31:03 +00:00
|
|
|
balena.auth
|
|
|
|
.getToken()
|
|
|
|
.then(function(originalToken: string) {
|
2019-11-15 09:45:14 +00:00
|
|
|
expect(originalToken).to.equal(tokens.janedoe.token);
|
2019-08-07 11:31:03 +00:00
|
|
|
return utils.loginIfTokenValid(tokens.johndoe.token);
|
|
|
|
})
|
|
|
|
.then(balena.auth.getToken)
|
|
|
|
.then((currentToken: string) =>
|
2019-11-15 09:45:14 +00:00
|
|
|
expect(currentToken).to.equal(tokens.janedoe.token),
|
2019-08-07 11:31:03 +00:00
|
|
|
));
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return describe('given there was no token', function() {
|
|
|
|
beforeEach(() => balena.auth.logout());
|
|
|
|
|
|
|
|
return it('should stay without a token', () =>
|
2019-08-07 11:31:03 +00:00
|
|
|
utils
|
|
|
|
.loginIfTokenValid(tokens.johndoe.token)
|
|
|
|
.then(() => balena.auth.isLoggedIn())
|
2019-11-15 09:45:14 +00:00
|
|
|
.then((isLoggedIn: boolean) => expect(isLoggedIn).to.equal(false)));
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return describe('given the token does authenticate with the server', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn');
|
|
|
|
return this.balenaAuthIsLoggedInStub.returns(Promise.resolve(true));
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
return this.balenaAuthIsLoggedInStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
return it('should eventually be true', function() {
|
2019-08-07 11:31:03 +00:00
|
|
|
const promise = utils.loginIfTokenValid(tokens.johndoe.token);
|
2019-11-15 09:45:14 +00:00
|
|
|
return expect(promise).to.eventually.be.true;
|
2019-08-07 11:31:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|