2014-04-01 07:16:39 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Auth = require('js/bo/auth'),
|
|
|
|
OAuth = require('js/util/oauth'),
|
|
|
|
RestDAO = require('js/dao/rest-dao'),
|
|
|
|
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
|
|
|
|
expect = chai.expect;
|
|
|
|
|
|
|
|
describe('Auth unit tests', function() {
|
|
|
|
var auth, appConfigStoreStub, oauthStub, caStub;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
appConfigStoreStub = sinon.createStubInstance(DeviceStorageDAO);
|
|
|
|
oauthStub = sinon.createStubInstance(OAuth);
|
|
|
|
caStub = sinon.createStubInstance(RestDAO);
|
|
|
|
auth = new Auth(appConfigStoreStub, oauthStub, caStub);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
|
|
|
|
|
|
|
describe('getCredentials', function() {
|
2014-05-21 07:01:41 -04:00
|
|
|
var getCertificateStub, queryEmailAddressStub, getEmailAddressFromConfigStub;
|
2014-04-01 07:16:39 -04:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
getCertificateStub = sinon.stub(auth, 'getCertificate');
|
|
|
|
queryEmailAddressStub = sinon.stub(auth, 'queryEmailAddress');
|
2014-05-21 07:01:41 -04:00
|
|
|
getEmailAddressFromConfigStub = sinon.stub(auth, 'getEmailAddressFromConfig');
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
|
|
|
getCertificateStub.yields(null, 'cert');
|
2014-05-21 07:01:41 -04:00
|
|
|
getEmailAddressFromConfigStub.yields(null, 'asdf@example.com');
|
2014-04-01 07:16:39 -04:00
|
|
|
queryEmailAddressStub.withArgs('token').yields(null, 'asdf@example.com');
|
2014-05-21 07:01:41 -04:00
|
|
|
oauthStub.getOAuthToken.withArgs('asdf@example.com').yields(null, 'token');
|
2014-04-01 07:16:39 -04:00
|
|
|
|
|
|
|
auth.getCredentials({}, function(err, credentials) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(credentials.emailAddress).to.equal('asdf@example.com');
|
|
|
|
expect(credentials.oauthToken).to.equal('token');
|
|
|
|
expect(credentials.sslCert).to.equal('cert');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in getCertificate', function(done) {
|
|
|
|
getCertificateStub.yields(new Error());
|
|
|
|
|
|
|
|
auth.getCredentials({}, function(err, credentials) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(credentials).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in getOAuthToken', function(done) {
|
|
|
|
getCertificateStub.yields(null, 'cert');
|
2014-05-21 07:01:41 -04:00
|
|
|
getEmailAddressFromConfigStub.yields(null, 'asdf@example.com');
|
2014-04-01 07:16:39 -04:00
|
|
|
oauthStub.getOAuthToken.yields(new Error());
|
|
|
|
|
|
|
|
auth.getCredentials({}, function(err, credentials) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(credentials).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in queryEmailAddress', function(done) {
|
|
|
|
getCertificateStub.yields(null, 'cert');
|
2014-05-21 07:01:41 -04:00
|
|
|
getEmailAddressFromConfigStub.yields(null, 'asdf@example.com');
|
2014-04-01 07:16:39 -04:00
|
|
|
queryEmailAddressStub.withArgs('token').yields(new Error());
|
|
|
|
oauthStub.getOAuthToken.yields(null, 'token');
|
|
|
|
|
|
|
|
auth.getCredentials({}, function(err, credentials) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(credentials).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getCertificate', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
caStub.get.yields(null, 'cert');
|
|
|
|
|
|
|
|
auth.getCertificate(function(err, cert) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(cert).to.equal('cert');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail', function(done) {
|
|
|
|
caStub.get.yields(null, '');
|
|
|
|
|
|
|
|
auth.getCertificate(function(err, cert) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cert).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getEmailAddress', function() {
|
|
|
|
var getEmailAddressFromConfigStub;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
getEmailAddressFromConfigStub = sinon.stub(auth, 'getEmailAddressFromConfig');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
|
|
|
getEmailAddressFromConfigStub.yields(null, 'asdf@example.com');
|
|
|
|
|
|
|
|
auth.getEmailAddress(function(err, emailAddress) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(emailAddress).to.equal('asdf@example.com');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail', function(done) {
|
|
|
|
getEmailAddressFromConfigStub.yields(new Error());
|
|
|
|
|
|
|
|
auth.getEmailAddress(function(err, emailAddress) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(emailAddress).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getEmailAddressFromConfig', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
appConfigStoreStub.listItems.withArgs('emailaddress', 0, null).yields(null, ['asdf@example.com']);
|
|
|
|
|
|
|
|
auth.getEmailAddressFromConfig(function(err, emailAddress) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(emailAddress).to.equal('asdf@example.com');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return empty result', function(done) {
|
|
|
|
appConfigStoreStub.listItems.withArgs('emailaddress', 0, null).yields(null, []);
|
|
|
|
|
|
|
|
auth.getEmailAddressFromConfig(function(err, emailAddress) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(emailAddress).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail', function(done) {
|
|
|
|
appConfigStoreStub.listItems.withArgs('emailaddress', 0, null).yields(new Error());
|
|
|
|
|
|
|
|
auth.getEmailAddressFromConfig(function(err, emailAddress) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(emailAddress).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('queryEmailAddress', function() {
|
|
|
|
var getEmailAddressFromConfigStub;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
getEmailAddressFromConfigStub = sinon.stub(auth, 'getEmailAddressFromConfig');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should if already cached', function(done) {
|
|
|
|
getEmailAddressFromConfigStub.yields(null, 'asdf@example.com');
|
|
|
|
|
|
|
|
auth.queryEmailAddress('token', function(err, emailAddress) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(emailAddress).to.equal('asdf@example.com');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should when querying oauth api', function(done) {
|
|
|
|
getEmailAddressFromConfigStub.yields();
|
|
|
|
oauthStub.queryEmailAddress.withArgs('token').yields(null, 'asdf@example.com');
|
|
|
|
appConfigStoreStub.storeList.withArgs(['asdf@example.com'], 'emailaddress').yields();
|
|
|
|
|
|
|
|
auth.queryEmailAddress('token', function(err, emailAddress) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(emailAddress).to.equal('asdf@example.com');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in cache lookup', function(done) {
|
|
|
|
getEmailAddressFromConfigStub.yields(new Error());
|
|
|
|
|
|
|
|
auth.queryEmailAddress('token', function(err, emailAddress) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(emailAddress).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in oauth api', function(done) {
|
|
|
|
getEmailAddressFromConfigStub.yields();
|
|
|
|
oauthStub.queryEmailAddress.withArgs('token').yields(new Error());
|
|
|
|
|
|
|
|
auth.queryEmailAddress('token', function(err, emailAddress) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(emailAddress).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in oauth api', function(done) {
|
|
|
|
getEmailAddressFromConfigStub.yields();
|
|
|
|
oauthStub.queryEmailAddress.withArgs('token').yields(null, 'asdf@example.com');
|
|
|
|
appConfigStoreStub.storeList.withArgs(['asdf@example.com'], 'emailaddress').yields(new Error());
|
|
|
|
|
|
|
|
auth.queryEmailAddress('token', function(err, emailAddress) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(emailAddress).to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|