Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should be able to login successfully', inject(['AuthService','$httpBackend','$rootScope', function( AuthService, $httpBackend, $rootScope ) {
$httpBackend.whenPOST('user/authenticate', null).respond( 200, 'Done' );
$httpBackend.whenGET('user/authenticated/retrieve').respond( {"password":null,"username":"user","authorities":[{"authority":"ROLE_USER"}],"accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true,"enabled":true});
var spy = sinon.spy($rootScope, "$emit");
var spy2 = sinon.spy($rootScope, '$broadcast');
AuthService.login( $rootScope, "user", "user" );
expect(spy).to.have.been.calledWith("event:loginRequest");
$rootScope.$apply();
$httpBackend.flush();
// Verify login function
expect(spy2).to.have.been.calledWith("event:loginConfirmed");
expect(store.session.get("userInfo")).not.to.be.null;
expect($rootScope.user).not.to.be.null;
expect($rootScope.userInfo).not.to.be.null;
// Verify isLoggedIn function
expect(AuthService.isLoggedIn()).to.be.true;
// Verify logout function
AuthService.logout( $rootScope ).then(function() {
expect(store.session.get("userInfo")).to.be.null;
expect(spy).to.have.been.calledWith("event:logoutRequest");
expect($rootScope.user).to.be.null;
expect($rootScope.userInfo).to.be.null;
});
}]));
.then(function(results) {
// console.log("results[1] ", results[ 1 ] );
if( results[ 1 ] )
{
$rootScope.user = results[ 1 ].data;
var userInfo = {
authorities: results[ 1 ].data.authorities,
userName: results[ 1 ].data.username
};
store.session.set( "userInfo", angular.toJson( userInfo ) );
$rootScope.userInfo = userInfo;
// console.log("Got the user to be ", user );
$rootScope.$broadcast('event:loginConfirmed');
delete $rootScope.error;
}
deferred.resolve(results[1].data);
},
function(error) {
AuthService.logout( $rootScope ).then(function() {
expect(store.session.get("userInfo")).to.be.null;
expect(spy).to.have.been.calledWith("event:logoutRequest");
expect($rootScope.user).to.be.null;
expect($rootScope.userInfo).to.be.null;
});
export const set = (key = '', value = null) => {
if (!key && !value) return;
store.session(key, value);
}
function AuthService(){
if (store.session.has("userInfo")) {
$rootScope.userInfo = angular.toJson(store.session.get("userInfo"));
}
}
export const get = (key = '') => store.session(key);