}).then(function (connectorSettings) { if (!connectorSettings) { throw new errors.Http404Error('invalid connector settings key'); } var bouncerToken = new Model.BouncerToken({ returnUrl: request.query.returnUrl, application: connectorSettings.application, environment: connectorSettings.environment, connectorKey: connectorSettings.key, connectorType: connectorSettings.connectorType }); var bouncerSetup = BBPromise.resolve(null); /*istanbul ignore if */ if (request.query.bucketKey) { bouncerSetup = Model.Bucket.findOneAsync({ key: request.query.bucketKey, application: connectorSettings.application, environment: connectorSettings.environment }).then(function (bucket) { if (!bucket) { //throw new errors.connector.request.InvalidError('invalid bucket key'); var b = new Model.Bucket({ key: request.query.bucketKey, application: connectorSettings.application, environment: connectorSettings.environment }); return b.saveAsync(); } else { return bucket; } }).then(function (bucket) { bouncerToken.saveTo = 'bucket'; bouncerToken.saveId = bucket._id; }); } /*istanbul ignore if */ if (request.query.userId) { bouncerSetup = Model.AppUser.findOneAsync({ _id: request.query.userId, application: connectorSettings.application, environment: connectorSettings.environment }).then(function (user) { if (!user) { throw new errors.connector.request.InvalidError('invalid user id'); } bouncerToken.saveTo = 'user'; bouncerToken.saveId = user._id; }); } return bouncerSetup.then(function () { return bouncerToken.saveAsync(); }); }).then(function (token) {
before(function () { sinon.stub(AppUser, 'findOneAsync').returns(BBPromise.resolve(appUser)); return appUser.setPassword('Password123'); });
describe('#login', function () { var application = new Application({ _id: 'appid' }); var appUser = new AppUser({ application: 'appid', emailAddresses: [{ address: '*****@*****.**' }] }); before(function () { sinon.stub(AppUser, 'findOneAsync').returns(BBPromise.resolve(appUser)); return appUser.setPassword('Password123'); }); afterEach(function () { AppUser.findOneAsync.reset(); }); after(function () { AppUser.findOneAsync.restore(); }); describe('with valid username and password', function () { var _result; before(function (done) { HoistContext.get().then(function (context) { context.application = application; pipeline.login('*****@*****.**', 'Password123').then(function (result) { _result = result; done(); }); }); }); it('loads the correct user', function () { expect(AppUser.findOneAsync) .to.have.been.calledWith({ application: 'appid', 'emailAddresses.address': '*****@*****.**' }); }); it('returns true', function () { return expect(_result).to.be.true; }); }); describe('with invalid username', function () { var _error; before(function (done) { AppUser.findOneAsync.returns(BBPromise.resolve(null)); HoistContext.get().then(function (context) { context.application = application; pipeline.login('*****@*****.**', 'Password123', function (error) { _error = error; done(); }); }); }); after(function () { AppUser.findOneAsync.returns(appUser); }); it('throws incorrect credentials exception', function () { expect(_error) .to.be.instanceOf(errors.user.credentials.IncorrectError).and.to.have.property('message', 'The username and/or password were not correct'); }); }); describe('with invalid password', function () { var _error; before(function (done) { HoistContext.get().then(function (context) { context.application = application; pipeline.login('*****@*****.**', 'password!@3', function (error) { _error = error; done(); }); }); }); it('throws incorrect credentials exception', function () { expect(_error) .to.be.instanceOf(errors.user.credentials.IncorrectError).and.to.have.property('message', 'The username and/or password were not correct'); }); }); });