define(function(require) { 'use strict'; var OAuth = require('js/util/oauth'), RestDAO = require('js/dao/rest-dao'), expect = chai.expect; describe('OAuth unit tests', function() { var oauth, googleApiStub, identityStub, getPlatformInfoStub, testEmail = 'test@example.com'; beforeEach(function() { googleApiStub = sinon.createStubInstance(RestDAO); oauth = new OAuth(googleApiStub); 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'); 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(); }); describe('isSupported', function() { it('should work', function() { expect(oauth.isSupported()).to.be.true; }); }); describe('getOAuthToken', function() { it('should work for empty emailAddress', function(done) { getPlatformInfoStub.yields({ os: 'android' }); identityStub.withArgs({ interactive: true }).yields('token'); oauth.getOAuthToken(undefined, function(err, token) { expect(err).to.not.exist; expect(token).to.equal('token'); done(); }); }); it('should work on android app', function(done) { getPlatformInfoStub.yields({ os: 'android' }); identityStub.withArgs({ interactive: true, accountHint: testEmail }).yields('token'); oauth.getOAuthToken(testEmail, function(err, token) { expect(err).to.not.exist; expect(token).to.equal('token'); done(); }); }); it('should work on desktop chrome', function(done) { getPlatformInfoStub.yields({ os: 'mac' }); identityStub.withArgs({ interactive: true }).yields('token'); oauth.getOAuthToken(testEmail, function(err, token) { expect(err).to.not.exist; expect(token).to.equal('token'); done(); }); }); it('should fail', function(done) { getPlatformInfoStub.yields({ os: 'android' }); identityStub.yields(); oauth.getOAuthToken(testEmail, function(err, token) { expect(err).to.exist; expect(token).to.not.exist; done(); }); }); }); describe('queryEmailAddress', function() { it('should work', function(done) { googleApiStub.get.withArgs({ uri: '/oauth2/v3/userinfo?access_token=token' }).yields(null, { email: 'asdf@example.com' }); oauth.queryEmailAddress('token', function(err, emailAddress) { expect(err).to.not.exist; expect(emailAddress).to.equal('asdf@example.com'); done(); }); }); it('should fail due to invalid token', function(done) { oauth.queryEmailAddress('', function(err, emailAddress) { expect(err).to.exist; expect(emailAddress).to.not.exist; done(); }); }); it('should fail due to error in rest api', function(done) { googleApiStub.get.withArgs({ uri: '/oauth2/v3/userinfo?access_token=token' }).yields(new Error()); oauth.queryEmailAddress('token', function(err, emailAddress) { expect(err).to.exist; expect(emailAddress).to.not.exist; done(); }); }); }); }); });