1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-03 06:12:15 -05:00
mail/test/unit/add-account-ctrl-test.js

241 lines
8.6 KiB
JavaScript
Raw Normal View History

2014-01-27 12:50:13 -05:00
define(function(require) {
'use strict';
var expect = chai.expect,
angular = require('angular'),
mocks = require('angularMocks'),
AddAccountCtrl = require('js/controller/add-account'),
Auth = require('js/bo/auth'),
2014-01-27 12:50:13 -05:00
appController = require('js/app-controller');
describe('Add Account Controller unit test', function() {
var scope, location, ctrl, authStub;
2014-01-27 12:50:13 -05:00
describe('connectToGoogle', function() {
var origAuth;
2014-01-27 12:50:13 -05:00
beforeEach(function() {
// remember original module to restore later, then replace it
origAuth = appController._auth;
appController._auth = authStub = sinon.createStubInstance(Auth);
2014-01-27 12:50:13 -05:00
});
afterEach(function() {
// restore the app controller module
appController._auth = origAuth;
2014-01-27 12:50:13 -05:00
});
it('should forward to login', function() {
2014-01-27 12:50:13 -05:00
angular.module('addaccounttest', []);
mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
scope = $rootScope.$new();
scope.state = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
sinon.stub(scope, '$apply', function() {});
ctrl = $controller(AddAccountCtrl, {
$location: location,
$scope: scope
});
});
authStub._oauth = {
isSupported: function() {
return true;
}
};
authStub.getOAuthToken.yields();
scope.connectToGoogle();
expect(location.path.calledWith('/login-set-credentials')).to.be.true;
expect(location.search.calledWith({
provider: 'gmail'
})).to.be.true;
expect(authStub.getOAuthToken.calledOnce).to.be.true;
location.path.restore();
location.search.restore();
scope.$apply.restore();
});
it('should not use oauth for gmail', function() {
angular.module('addaccounttest', []);
mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
2014-01-27 12:50:13 -05:00
scope = $rootScope.$new();
scope.state = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
sinon.stub(scope, '$apply', function() {});
2014-01-27 12:50:13 -05:00
ctrl = $controller(AddAccountCtrl, {
$location: location,
$scope: scope
});
});
authStub._oauth = {
isSupported: function() {
return false;
}
};
scope.connectToGoogle();
expect(location.path.calledWith('/login-set-credentials')).to.be.true;
expect(location.search.calledWith({
provider: 'gmail'
})).to.be.true;
expect(authStub.getOAuthToken.called).to.be.false;
location.path.restore();
location.search.restore();
scope.$apply.restore();
});
it('should not forward to login when oauth fails', function(done) {
angular.module('addaccounttest', []);
mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
scope = $rootScope.$new();
scope.state = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
sinon.stub(scope, '$apply', function() {});
ctrl = $controller(AddAccountCtrl, {
$location: location,
$scope: scope
});
});
authStub._oauth = {
isSupported: function() {
return true;
}
};
authStub.getOAuthToken.yields(new Error());
2014-01-27 12:50:13 -05:00
scope.onError = function(err) {
expect(err).to.exist;
expect(location.path.called).to.be.false;
expect(location.search.called).to.be.false;
location.path.restore();
location.search.restore();
scope.$apply.restore();
2014-01-27 12:50:13 -05:00
done();
};
scope.connectToGoogle();
});
});
2014-01-27 12:50:13 -05:00
describe('connectToYahoo', function() {
it('should forward to login', function() {
2014-01-27 12:50:13 -05:00
angular.module('addaccounttest', []);
mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
2014-02-06 05:55:24 -05:00
scope = $rootScope.$new();
scope.state = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
sinon.stub(scope, '$apply', function() {});
2014-02-06 05:55:24 -05:00
ctrl = $controller(AddAccountCtrl, {
$location: location,
$scope: scope
2014-01-27 12:50:13 -05:00
});
});
scope.connectToYahoo();
expect(location.path.calledWith('/login-set-credentials')).to.be.true;
expect(location.search.calledWith({
provider: 'yahoo'
})).to.be.true;
location.path.restore();
location.search.restore();
scope.$apply.restore();
});
});
2014-02-06 05:55:24 -05:00
describe('connectToTonline', function() {
it('should forward to login', function() {
angular.module('addaccounttest', []);
mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
scope = $rootScope.$new();
scope.state = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
2014-02-06 05:55:24 -05:00
sinon.stub(scope, '$apply', function() {});
2014-01-27 12:50:13 -05:00
ctrl = $controller(AddAccountCtrl, {
$location: location,
$scope: scope
});
});
scope.connectToTonline();
2014-01-27 12:50:13 -05:00
expect(location.path.calledWith('/login-set-credentials')).to.be.true;
expect(location.search.calledWith({
provider: 'tonline'
})).to.be.true;
location.path.restore();
location.search.restore();
scope.$apply.restore();
});
});
describe('connectOther', function() {
it('should forward to login', function() {
angular.module('addaccounttest', []);
mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
scope = $rootScope.$new();
scope.state = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
sinon.stub(scope, '$apply', function() {});
ctrl = $controller(AddAccountCtrl, {
$location: location,
$scope: scope
});
});
scope.connectOther();
expect(location.path.calledWith('/login-set-credentials')).to.be.true;
expect(location.search.calledWith({
provider: 'custom'
})).to.be.true;
location.path.restore();
location.search.restore();
scope.$apply.restore();
2014-01-27 12:50:13 -05:00
});
});
});
});