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'),
|
2014-04-01 07:16:39 -04:00
|
|
|
Auth = require('js/bo/auth'),
|
2014-09-15 11:09:13 -04:00
|
|
|
AdminDao = require('js/dao/admin-dao'),
|
2014-01-27 12:50:13 -05:00
|
|
|
appController = require('js/app-controller');
|
|
|
|
|
|
|
|
describe('Add Account Controller unit test', function() {
|
2014-09-15 11:09:13 -04:00
|
|
|
var scope, location, ctrl, authStub, origAuth, adminStub;
|
2014-08-13 10:16:28 -04:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
// remember original module to restore later, then replace it
|
|
|
|
origAuth = appController._auth;
|
|
|
|
appController._auth = authStub = sinon.createStubInstance(Auth);
|
2014-09-15 11:09:13 -04:00
|
|
|
appController._adminDao = adminStub = sinon.createStubInstance(AdminDao);
|
2014-08-13 10:16:28 -04:00
|
|
|
|
|
|
|
angular.module('addaccounttest', []);
|
|
|
|
mocks.module('addaccounttest');
|
|
|
|
mocks.inject(function($controller, $rootScope, $location) {
|
|
|
|
location = $location;
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
2014-09-15 11:09:13 -04:00
|
|
|
scope.form = {};
|
2014-09-19 12:59:13 -04:00
|
|
|
scope.formValidate = {};
|
2014-08-13 10:16:28 -04:00
|
|
|
|
|
|
|
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-08-13 10:16:28 -04:00
|
|
|
});
|
2014-01-27 12:50:13 -05:00
|
|
|
|
2014-08-13 10:16:28 -04:00
|
|
|
afterEach(function() {
|
|
|
|
// restore the app controller module
|
|
|
|
appController._auth = origAuth;
|
2014-01-27 12:50:13 -05:00
|
|
|
|
2014-08-13 10:16:28 -04:00
|
|
|
location.path.restore();
|
|
|
|
location.search.restore();
|
2014-09-15 11:09:13 -04:00
|
|
|
if (scope.$apply.restore) {
|
|
|
|
scope.$apply.restore();
|
|
|
|
}
|
2014-08-13 10:16:28 -04:00
|
|
|
});
|
|
|
|
|
2014-09-15 11:09:13 -04:00
|
|
|
describe('createWhiteoutAccount', function() {
|
|
|
|
it('should return early for invalid form', function() {
|
|
|
|
scope.form.$invalid = true;
|
|
|
|
scope.createWhiteoutAccount();
|
|
|
|
expect(adminStub.createUser.called).to.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail to error creating user', function(done) {
|
|
|
|
scope.form.$invalid = false;
|
2014-09-19 12:59:13 -04:00
|
|
|
scope.betaCode = 'asfd';
|
2014-09-22 07:59:05 -04:00
|
|
|
scope.phone = '12345';
|
2014-09-15 11:09:13 -04:00
|
|
|
adminStub.createUser.yieldsAsync(new Error('asdf'));
|
|
|
|
|
|
|
|
scope.$apply = function() {
|
|
|
|
expect(scope.busy).to.be.false;
|
|
|
|
expect(scope.errMsg).to.equal('asdf');
|
|
|
|
expect(adminStub.createUser.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.createWhiteoutAccount();
|
|
|
|
expect(scope.busy).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
|
|
|
scope.form.$invalid = false;
|
2014-09-19 12:59:13 -04:00
|
|
|
scope.betaCode = 'asfd';
|
2014-09-22 07:59:05 -04:00
|
|
|
scope.phone = '12345';
|
2014-09-15 11:09:13 -04:00
|
|
|
adminStub.createUser.yieldsAsync();
|
|
|
|
|
2014-09-19 12:59:13 -04:00
|
|
|
scope.$apply = function() {
|
|
|
|
expect(scope.busy).to.be.false;
|
2014-09-15 11:09:13 -04:00
|
|
|
expect(scope.errMsg).to.be.undefined;
|
2014-09-19 12:59:13 -04:00
|
|
|
expect(scope.step).to.equal(3);
|
2014-09-15 11:09:13 -04:00
|
|
|
expect(adminStub.createUser.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.createWhiteoutAccount();
|
|
|
|
expect(scope.busy).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-09-19 12:59:13 -04:00
|
|
|
describe('validateUser', function() {
|
|
|
|
it('should return early for invalid form', function() {
|
|
|
|
scope.formValidate.$invalid = true;
|
|
|
|
scope.validateUser();
|
|
|
|
expect(adminStub.validateUser.called).to.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail to error creating user', function(done) {
|
|
|
|
scope.formValidate.$invalid = false;
|
|
|
|
scope.token = 'asfd';
|
|
|
|
adminStub.validateUser.yieldsAsync(new Error('asdf'));
|
|
|
|
|
|
|
|
scope.$apply = function() {
|
|
|
|
expect(scope.busyValidate).to.be.false;
|
|
|
|
expect(scope.errMsgValidate).to.equal('asdf');
|
|
|
|
expect(adminStub.validateUser.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.validateUser();
|
|
|
|
expect(scope.busyValidate).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
|
|
|
scope.formValidate.$invalid = false;
|
|
|
|
scope.token = 'asfd';
|
|
|
|
adminStub.validateUser.yieldsAsync();
|
|
|
|
|
|
|
|
scope.login = function() {
|
|
|
|
expect(scope.busyValidate).to.be.true;
|
|
|
|
expect(scope.errMsgValidate).to.be.undefined;
|
|
|
|
expect(adminStub.validateUser.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.validateUser();
|
|
|
|
expect(scope.busyValidate).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-15 11:09:13 -04:00
|
|
|
describe('login', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
scope.form.$invalid = false;
|
|
|
|
authStub.setCredentials.returns();
|
|
|
|
|
|
|
|
scope.login();
|
|
|
|
expect(authStub.setCredentials.calledOnce).to.be.true;
|
|
|
|
expect(location.path.calledWith('/login')).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('connectToGoogle', function() {
|
2014-08-13 10:16:28 -04:00
|
|
|
it('should forward to login', function() {
|
2014-07-01 13:49:19 -04:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not use oauth for gmail', function() {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not forward to login when oauth fails', function(done) {
|
|
|
|
authStub._oauth = {
|
|
|
|
isSupported: function() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
authStub.getOAuthToken.yields(new Error());
|
|
|
|
|
2014-01-27 12:50:13 -05:00
|
|
|
scope.onError = function(err) {
|
2014-07-01 13:49:19 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(location.path.called).to.be.false;
|
|
|
|
expect(location.search.called).to.be.false;
|
|
|
|
|
2014-01-27 12:50:13 -05:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.connectToGoogle();
|
|
|
|
});
|
2014-07-01 13:49:19 -04:00
|
|
|
});
|
2014-01-27 12:50:13 -05:00
|
|
|
|
2014-09-15 11:09:13 -04:00
|
|
|
describe('connectTo', function() {
|
2014-07-01 13:49:19 -04:00
|
|
|
it('should forward to login', function() {
|
2014-09-15 11:09:13 -04:00
|
|
|
var provider = 'wmail';
|
|
|
|
scope.connectTo(provider);
|
2014-07-01 13:49:19 -04:00
|
|
|
|
|
|
|
expect(location.path.calledWith('/login-set-credentials')).to.be.true;
|
|
|
|
expect(location.search.calledWith({
|
2014-09-15 11:09:13 -04:00
|
|
|
provider: provider
|
2014-07-01 13:49:19 -04:00
|
|
|
})).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
2014-02-06 05:55:24 -05:00
|
|
|
|
2014-01-27 12:50:13 -05:00
|
|
|
});
|
|
|
|
});
|