1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-01 21:32:17 -05:00
mail/test/unit/add-account-ctrl-test.js

168 lines
5.2 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-10-09 09:56:35 -04:00
var mocks = angular.mock,
2014-10-07 14:32:23 -04:00
AddAccountCtrl = require('../../src/js/controller/add-account'),
Auth = require('../../src/js/bo/auth'),
2014-11-10 12:36:26 -05:00
appController = require('../../src/js/app-controller'),
cfg = require('../../src/js/app-config').config;
2014-10-07 14:32:23 -04:00
describe('Add Account Controller unit test', function() {
var scope, location, mailConfigMock, ctrl, authStub, origAuth;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
// remember original module to restore later, then replace it
origAuth = appController._auth;
appController._auth = authStub = sinon.createStubInstance(Auth);
angular.module('addaccounttest', ['woServices']);
2014-10-07 14:32:23 -04:00
mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location, mailConfig) {
2014-10-07 14:32:23 -04:00
location = $location;
mailConfigMock = mailConfig;
2014-10-07 14:32:23 -04:00
scope = $rootScope.$new();
scope.state = {};
scope.form = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
sinon.stub(scope, '$apply', function() {});
ctrl = $controller(AddAccountCtrl, {
$location: location,
$scope: scope,
$routeParams: {},
mailConfig: mailConfigMock
2014-01-27 12:50:13 -05:00
});
});
2014-10-07 14:32:23 -04:00
});
2014-01-27 12:50:13 -05:00
2014-10-07 14:32:23 -04:00
afterEach(function() {
// restore the app controller module
appController._auth = origAuth;
2014-10-07 14:32:23 -04:00
location.path.restore();
location.search.restore();
if (scope.$apply.restore) {
scope.$apply.restore();
}
});
2014-11-10 12:36:26 -05:00
describe('getAccountSettings', function() {
var url, oauthPossibleStub, setCredentialsStub, mailConfigStub, mailConfig;
beforeEach(inject(function($q) {
2014-10-07 14:32:23 -04:00
scope.form.$invalid = false;
2014-11-10 12:36:26 -05:00
scope.emailAddress = 'test@example.com';
url = cfg.settingsUrl + 'example.com';
mailConfig = {
imap: {
hostname: 'imap.example.com',
source: 'guess'
}
2014-10-07 14:32:23 -04:00
};
var deferred = $q.defer();
mailConfigStub = sinon.stub(mailConfigMock, 'get');
mailConfigStub.returns(deferred.promise);
deferred.resolve(mailConfig);
oauthPossibleStub = sinon.stub(scope, 'oauthPossible');
2014-11-10 12:36:26 -05:00
setCredentialsStub = sinon.stub(scope, 'setCredentials');
}));
2014-10-07 14:32:23 -04:00
2014-11-10 12:36:26 -05:00
afterEach(function() {
mailConfigStub.restore();
oauthPossibleStub.restore();
2014-11-10 12:36:26 -05:00
setCredentialsStub.restore();
});
it('should work for gmail', inject(function($rootScope) {
authStub.useOAuth.returns(true);
2014-10-07 14:32:23 -04:00
2014-11-10 12:36:26 -05:00
scope.getAccountSettings();
$rootScope.$apply();
2014-09-19 12:59:13 -04:00
expect(oauthPossibleStub.calledOnce).to.be.true;
}));
2014-09-19 12:59:13 -04:00
it('should work for other domain', inject(function($rootScope) {
authStub.useOAuth.returns(false);
2014-09-19 12:59:13 -04:00
2014-11-10 12:36:26 -05:00
scope.getAccountSettings();
$rootScope.$apply();
2014-09-19 12:59:13 -04:00
expect(setCredentialsStub.calledOnce).to.be.true;
}));
2014-10-07 14:32:23 -04:00
it('should fail for mailConfig error', inject(function($q, $rootScope) {
authStub.useOAuth.returns(false);
2014-09-19 12:59:13 -04:00
var deferred = $q.defer();
mailConfigStub.returns(deferred.promise);
deferred.reject(new Error());
2014-11-10 12:36:26 -05:00
scope.getAccountSettings();
$rootScope.$apply();
2014-11-10 12:36:26 -05:00
expect(scope.errMsg).to.exist;
}));
2014-10-07 14:32:23 -04:00
});
describe('oauthPossible', function() {
2014-11-10 12:36:26 -05:00
var setCredentialsStub;
beforeEach(function() {
setCredentialsStub = sinon.stub(scope, 'setCredentials');
});
afterEach(function() {
setCredentialsStub.restore();
});
it('should use oauth', function() {
scope.onError = function(options) {
options.callback(true);
};
2014-10-07 14:32:23 -04:00
authStub.getOAuthToken.yields();
scope.oauthPossible();
expect(setCredentialsStub.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
expect(authStub.getOAuthToken.calledOnce).to.be.true;
});
2014-11-10 12:36:26 -05:00
it('should not use oauth', function() {
scope.onError = function(options) {
options.callback(false);
};
scope.oauthPossible();
expect(setCredentialsStub.calledOnce).to.be.true;
2014-11-10 12:36:26 -05:00
expect(authStub.getOAuthToken.called).to.be.false;
});
2014-11-10 12:36:26 -05:00
it('should not forward to login when oauth fails', function(done) {
scope.onError = function(options) {
scope.onError = function(err) {
expect(err).to.exist;
expect(setCredentialsStub.called).to.be.false;
done();
};
options.callback(true);
2014-10-07 14:32:23 -04:00
};
2014-11-10 12:36:26 -05:00
authStub.getOAuthToken.yields(new Error());
2014-01-27 12:50:13 -05:00
scope.oauthPossible();
});
2014-10-07 14:32:23 -04:00
});
2014-01-27 12:50:13 -05:00
2014-11-10 12:36:26 -05:00
describe('setCredentials', function() {
it('should work', function() {
scope.setCredentials();
2014-10-07 14:32:23 -04:00
expect(location.path.calledWith('/login-set-credentials')).to.be.true;
});
2014-01-27 12:50:13 -05:00
});
2014-10-07 14:32:23 -04:00
2014-01-27 12:50:13 -05:00
});