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';
|
2020-02-27 14:55:30 +00:00
|
|
|
import { getBalenaSdk } from '../../build/utils/lazy';
|
2019-08-07 11:31:03 +00:00
|
|
|
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');
|
2020-02-27 14:55:30 +00:00
|
|
|
const balena = getBalenaSdk();
|
2019-08-07 11:31:03 +00:00
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
describe('Utils:', function () {
|
|
|
|
describe('.getDashboardLoginURL()', function () {
|
2019-08-07 11:31:03 +00:00
|
|
|
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'),
|
2020-06-15 22:53:07 +00:00
|
|
|
}).then(function ({ dashboardUrl, loginUrl }) {
|
2019-08-07 11:31:03 +00:00
|
|
|
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'),
|
2020-06-15 22:53:07 +00:00
|
|
|
}).then(function ({ dashboardUrl, loginUrl }) {
|
2019-08-07 11:31:03 +00:00
|
|
|
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'),
|
2020-06-15 22:53:07 +00:00
|
|
|
}).then(function ({ dashboardUrl, loginUrl }) {
|
2019-08-07 11:31:03 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +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
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +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
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +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
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +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
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
describe('given the token does not authenticate with the server', function () {
|
|
|
|
beforeEach(function () {
|
2019-08-07 11:31:03 +00:00
|
|
|
this.balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn');
|
|
|
|
return this.balenaAuthIsLoggedInStub.returns(Promise.resolve(false));
|
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
afterEach(function () {
|
2019-08-07 11:31:03 +00:00
|
|
|
return this.balenaAuthIsLoggedInStub.restore();
|
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
describe('given there was a token already', function () {
|
2019-08-07 11:31:03 +00:00
|
|
|
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()
|
2020-06-15 22:53:07 +00:00
|
|
|
.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
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
return describe('given there was no token', function () {
|
2019-08-07 11:31:03 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
return describe('given the token does authenticate with the server', function () {
|
|
|
|
beforeEach(function () {
|
2019-08-07 11:31:03 +00:00
|
|
|
this.balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn');
|
|
|
|
return this.balenaAuthIsLoggedInStub.returns(Promise.resolve(true));
|
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
afterEach(function () {
|
2019-08-07 11:31:03 +00:00
|
|
|
return this.balenaAuthIsLoggedInStub.restore();
|
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|