1
0
mirror of https://github.com/moparisthebest/mail synced 2024-08-13 16:43:47 -04:00
mail/test/unit/add-account-ctrl-test.js

203 lines
6.1 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() {
2014-11-10 12:36:26 -05:00
var scope, location, httpBackend, 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', []);
mocks.module('addaccounttest');
2014-11-10 12:36:26 -05:00
mocks.inject(function($controller, $rootScope, $location, $httpBackend) {
2014-10-07 14:32:23 -04:00
location = $location;
2014-11-10 12:36:26 -05:00
httpBackend = $httpBackend;
2014-10-07 14:32:23 -04:00
scope = $rootScope.$new();
scope.state = {};
scope.form = {};
scope.formValidate = {};
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: {}
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
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
2014-10-07 14:32:23 -04:00
});
2014-11-10 12:36:26 -05:00
describe('getAccountSettings', function() {
var url, connectToGoogleStub, setCredentialsStub, mailConfig;
2014-11-10 12:36:26 -05:00
beforeEach(function() {
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
};
2014-11-10 12:36:26 -05:00
connectToGoogleStub = sinon.stub(scope, 'connectToGoogle');
setCredentialsStub = sinon.stub(scope, 'setCredentials');
});
2014-10-07 14:32:23 -04:00
2014-11-10 12:36:26 -05:00
afterEach(function() {
connectToGoogleStub.restore();
setCredentialsStub.restore();
});
2014-11-10 12:36:26 -05:00
it('should work for gmail', function() {
mailConfig.imap.hostname = 'imap.gmail.com';
httpBackend.expectGET(url).respond(mailConfig);
2014-10-07 14:32:23 -04:00
2014-11-10 12:36:26 -05:00
scope.getAccountSettings();
httpBackend.flush();
2014-09-19 12:59:13 -04:00
2014-11-10 12:36:26 -05:00
expect(connectToGoogleStub.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
});
2014-09-19 12:59:13 -04:00
2014-11-10 12:36:26 -05:00
it('should work for guessed domain', function() {
httpBackend.expectGET(url).respond(mailConfig);
2014-09-19 12:59:13 -04:00
2014-11-10 12:36:26 -05:00
scope.getAccountSettings();
httpBackend.flush();
2014-09-19 12:59:13 -04:00
2014-11-10 12:36:26 -05:00
expect(setCredentialsStub.calledWith('custom')).to.be.true;
2014-10-07 14:32:23 -04:00
});
2014-09-19 12:59:13 -04:00
2014-11-10 12:36:26 -05:00
it('should work for dns domain', function() {
mailConfig.imap.source = 'dns';
httpBackend.expectGET(url).respond(mailConfig);
2014-09-19 12:59:13 -04:00
2014-11-10 12:36:26 -05:00
scope.getAccountSettings();
httpBackend.flush();
2014-10-07 14:32:23 -04:00
2014-11-10 12:36:26 -05:00
expect(setCredentialsStub.calledWith(undefined)).to.be.true;
2014-09-19 12:59:13 -04:00
});
2014-11-10 12:36:26 -05:00
it('should fail with http 500', function() {
httpBackend.expectGET(url).respond(500, '');
scope.getAccountSettings();
httpBackend.flush();
2014-11-10 12:36:26 -05:00
expect(scope.errMsg).to.exist;
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('connectToGoogle', 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() {
2014-10-07 14:32:23 -04:00
authStub._oauth = {
isSupported: function() {
return true;
}
};
2014-11-10 12:36:26 -05:00
scope.onError = function(options) {
options.callback(true);
};
2014-10-07 14:32:23 -04:00
authStub.getOAuthToken.yields();
2014-10-07 14:32:23 -04:00
scope.connectToGoogle();
2014-11-10 12:36:26 -05:00
expect(setCredentialsStub.calledWith('gmail')).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() {
2014-10-07 14:32:23 -04:00
authStub._oauth = {
isSupported: function() {
2014-11-10 12:36:26 -05:00
return true;
2014-10-07 14:32:23 -04:00
}
};
2014-11-10 12:36:26 -05:00
scope.onError = function(options) {
options.callback(false);
};
2014-10-07 14:32:23 -04:00
scope.connectToGoogle();
2014-11-10 12:36:26 -05:00
expect(setCredentialsStub.calledWith('gmail')).to.be.true;
2014-10-07 14:32:23 -04:00
expect(authStub.getOAuthToken.called).to.be.false;
});
2014-11-10 12:36:26 -05:00
it('should not use oauth if not supported', function() {
2014-10-07 14:32:23 -04:00
authStub._oauth = {
isSupported: function() {
2014-11-10 12:36:26 -05:00
return false;
2014-10-07 14:32:23 -04:00
}
};
2014-11-10 12:36:26 -05:00
scope.connectToGoogle();
2014-11-10 12:36:26 -05:00
expect(setCredentialsStub.calledWith('gmail')).to.be.true;
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) {
authStub._oauth = {
isSupported: function() {
return true;
}
};
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
2014-10-07 14:32:23 -04:00
scope.connectToGoogle();
});
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('gmail');
2014-10-07 14:32:23 -04:00
expect(location.path.calledWith('/login-set-credentials')).to.be.true;
expect(location.search.calledWith({
2014-11-10 12:36:26 -05:00
provider: 'gmail'
2014-10-07 14:32:23 -04:00
})).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
});