UNPKG

client-ui

Version:

Testing implementation of nodeJs Backend, angular frontend, and hopefully in a way that this can be deployed to s3/cloudfront

131 lines (124 loc) 4.55 kB
describe('Auth Factory', function() { var server = 'http://qloans:8080/v1/'; beforeEach(function() { angular.mock.module('clientApp', function($provide) { $provide.constant('rootConstants', { 'server': server }); $provide.service('$state', function() { this.go = function(){}; }); }); }); beforeEach(function() { inject(function($injector) { $httpBackend = $injector.get('$httpBackend'); store = $injector.get('aiStorage'); }); }); var auth ,$httpBackend ,store; beforeEach(inject(function(_AuthStateFactory_) { AuthStateFactory = _AuthStateFactory_; if (store.get('client')) { store.remove('client'); } })); afterEach(function() { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); }); describe('check + perform login', function() { it('check', function(){ assert.equal(AuthStateFactory.isLoggedIn, false); AuthStateFactory.check(); assert.equal(AuthStateFactory.isLoggedIn, false); var content = {client: true}; store.set('client', content); AuthStateFactory.check(); assert.equal(AuthStateFactory.isLoggedIn, true); store.remove('client'); AuthStateFactory.check(); assert.equal(AuthStateFactory.isLoggedIn, false); }); it('perform login', function(){ var content = {client: true}; AuthStateFactory.check(); assert.equal(AuthStateFactory.isLoggedIn, false); AuthStateFactory.performLogin(content); assert.equal(AuthStateFactory.isLoggedIn, true); store.remove('client'); AuthStateFactory.check(); assert.equal(AuthStateFactory.isLoggedIn, false); }); }); describe('login http', function() { it('login success', function() { var content = {client: true}; var credentials = { email: 'unit.test@gmail.com', password: 'password' }; $httpBackend .expectPOST(server + 'login') .respond(200, content); AuthStateFactory.login(credentials) .then(function(clientData) { assert.deepEqual(clientData, content); assert.deepEqual(store.get('client'), content); }, function(error) { }); $httpBackend.flush(); }); it('login failure', function() { var content = {client: true}; var message = 'Failed to login'; var credentials = { email: 'unit.test@gmail.com', password: 'password' }; $httpBackend .expectPOST(server + 'login') .respond(400, message); AuthStateFactory.login(credentials) .then(function(clientData) { }, function(error) { assert.deepEqual(error, message); assert.deepEqual(store.get('client'), null); }); $httpBackend.flush(); }); }); describe('logout http', function() { it('logout success', function() { var content = {client: true}; store.set('client', content); AuthStateFactory.check(); $httpBackend .expectPOST(server + 'logout') .respond(200, content); AuthStateFactory.logout() .then(function(success) { assert.deepEqual(store.get('client'), null); }, function(error) { }); $httpBackend.flush(); }); it('logout failure', function() { var content = {client: true}; store.set('client', content); var message = 'Failed to login'; $httpBackend .expectPOST(server + 'logout') .respond(400, message); AuthStateFactory.logout() .then(function(success) { }, function(error) { assert.deepEqual(error, message); assert.deepEqual(store.get('client'), content); }); $httpBackend.flush(); }); }); });