2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
var OAuth = require('../../../src/js/service/oauth'),
|
|
|
|
RestDAO = require('../../../src/js/service/rest');
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('OAuth unit tests', function() {
|
|
|
|
var oauth, googleApiStub, identityStub, getPlatformInfoStub, removeCachedStub,
|
|
|
|
testEmail = 'test@example.com';
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
googleApiStub = sinon.createStubInstance(RestDAO);
|
2014-12-11 12:12:37 -05:00
|
|
|
oauth = new OAuth(googleApiStub);
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
window.chrome = window.chrome || {};
|
|
|
|
|
|
|
|
window.chrome.identity = window.chrome.identity || {};
|
|
|
|
if (typeof window.chrome.identity.getAuthToken !== 'function') {
|
|
|
|
window.chrome.identity.getAuthToken = function() {};
|
|
|
|
}
|
|
|
|
identityStub = sinon.stub(window.chrome.identity, 'getAuthToken');
|
|
|
|
|
|
|
|
if (typeof window.chrome.identity.removeCachedAuthToken !== 'function') {
|
|
|
|
window.chrome.identity.removeCachedAuthToken = function() {};
|
|
|
|
}
|
|
|
|
removeCachedStub = sinon.stub(window.chrome.identity, 'removeCachedAuthToken');
|
|
|
|
|
|
|
|
window.chrome.runtime = window.chrome.runtime || {};
|
|
|
|
if (typeof window.chrome.runtime.getPlatformInfo !== 'function') {
|
|
|
|
window.chrome.runtime.getPlatformInfo = function() {};
|
|
|
|
}
|
|
|
|
getPlatformInfoStub = sinon.stub(window.chrome.runtime, 'getPlatformInfo');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
identityStub.restore();
|
|
|
|
getPlatformInfoStub.restore();
|
|
|
|
removeCachedStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isSupported', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
expect(oauth.isSupported()).to.be.true;
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('refreshToken', function() {
|
|
|
|
var getOAuthTokenStub;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
getOAuthTokenStub = sinon.stub(oauth, 'getOAuthToken');
|
|
|
|
});
|
2014-04-01 07:16:39 -04:00
|
|
|
afterEach(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
getOAuthTokenStub.restore();
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function() {
|
|
|
|
removeCachedStub.withArgs({
|
|
|
|
token: 'oldToken'
|
2014-12-11 08:46:05 -05:00
|
|
|
}).returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-11 08:46:05 -05:00
|
|
|
getOAuthTokenStub.withArgs(testEmail).returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
oauth.refreshToken({
|
|
|
|
oldToken: 'oldToken',
|
|
|
|
emailAddress: testEmail
|
2014-12-11 08:46:05 -05:00
|
|
|
}).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(removeCachedStub.calledOnce).to.be.true;
|
|
|
|
expect(getOAuthTokenStub.calledOnce).to.be.true;
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work without email', function() {
|
|
|
|
removeCachedStub.withArgs({
|
|
|
|
token: 'oldToken'
|
2014-12-11 08:46:05 -05:00
|
|
|
}).returns(resolves());
|
2014-08-05 10:44:39 -04:00
|
|
|
|
2014-12-11 08:46:05 -05:00
|
|
|
getOAuthTokenStub.withArgs(undefined).returns(resolves());
|
2014-08-05 10:44:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
oauth.refreshToken({
|
|
|
|
oldToken: 'oldToken',
|
2014-12-11 08:46:05 -05:00
|
|
|
}).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(removeCachedStub.calledOnce).to.be.true;
|
|
|
|
expect(getOAuthTokenStub.calledOnce).to.be.true;
|
|
|
|
expect(getOAuthTokenStub.calledWith(undefined)).to.be.true;
|
2014-08-05 10:44:39 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-08-05 10:44:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail without all options', function() {
|
|
|
|
oauth.refreshToken({
|
|
|
|
emailAddress: testEmail
|
2014-12-11 08:46:05 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(removeCachedStub.called).to.be.false;
|
|
|
|
expect(getOAuthTokenStub.called).to.be.false;
|
2014-08-05 10:44:39 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-08-05 10:44:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('getOAuthToken', function() {
|
|
|
|
it('should work for empty emailAddress', function(done) {
|
|
|
|
getPlatformInfoStub.yields({
|
|
|
|
os: 'android'
|
|
|
|
});
|
|
|
|
identityStub.withArgs({
|
|
|
|
interactive: true
|
|
|
|
}).yields('token');
|
|
|
|
|
2014-12-11 08:46:05 -05:00
|
|
|
oauth.getOAuthToken(undefined).then(function(token) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(token).to.equal('token');
|
|
|
|
done();
|
2014-08-05 10:44:39 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work on android app', function(done) {
|
|
|
|
getPlatformInfoStub.yields({
|
|
|
|
os: 'android'
|
|
|
|
});
|
|
|
|
identityStub.withArgs({
|
|
|
|
interactive: true,
|
|
|
|
accountHint: testEmail
|
|
|
|
}).yields('token');
|
|
|
|
|
2014-12-11 08:46:05 -05:00
|
|
|
oauth.getOAuthToken(testEmail).then(function(token) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(token).to.equal('token');
|
|
|
|
done();
|
2014-05-21 07:01:41 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work on desktop chrome', function(done) {
|
|
|
|
getPlatformInfoStub.yields({
|
|
|
|
os: 'mac'
|
|
|
|
});
|
|
|
|
identityStub.withArgs({
|
|
|
|
interactive: true
|
|
|
|
}).yields('token');
|
|
|
|
|
2014-12-11 08:46:05 -05:00
|
|
|
oauth.getOAuthToken(testEmail).then(function(token) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(token).to.equal('token');
|
|
|
|
done();
|
2014-05-21 07:01:41 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-21 07:01:41 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail', function(done) {
|
|
|
|
getPlatformInfoStub.yields({
|
|
|
|
os: 'android'
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
identityStub.yields();
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-12-11 08:46:05 -05:00
|
|
|
oauth.getOAuthToken(testEmail).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('queryEmailAddress', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
googleApiStub.get.withArgs({
|
|
|
|
uri: '/oauth2/v3/userinfo?access_token=token'
|
2014-12-11 08:46:05 -05:00
|
|
|
}).returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
email: 'asdf@example.com'
|
2014-12-11 08:46:05 -05:00
|
|
|
}));
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-12-11 08:46:05 -05:00
|
|
|
oauth.queryEmailAddress('token').then(function(emailAddress) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(emailAddress).to.equal('asdf@example.com');
|
|
|
|
done();
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to invalid token', function(done) {
|
2014-12-11 08:46:05 -05:00
|
|
|
oauth.queryEmailAddress('').catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error in rest api', function(done) {
|
|
|
|
googleApiStub.get.withArgs({
|
|
|
|
uri: '/oauth2/v3/userinfo?access_token=token'
|
|
|
|
}).yields(new Error());
|
|
|
|
|
2014-12-11 08:46:05 -05:00
|
|
|
oauth.queryEmailAddress('token').catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|