I'm trying to test if i receive and stock the token sent by the API.
I got my controller who is asking to the server if credentials are ok:
var app = angular.module('App', ['ngMaterial', 'ngMessages'])
app.controller('Ctrl', ['$rootScope', '$scope','$http','$timeout','$mdDialog','Global', 'projects', 'screens', 'users', 'licenses', function($rootScope, $scope, $http, $timeout, $mdDialog,Global, projects, screens, users, licenses) {
//Ask for connexion to the server
$rootScope.Connect = () => { // === $rootScope.Connect = function() { ... }
  //Set params to request
  var request = Global.url_api+'action=Connection&username='+$scope.username+
  '&password='+$scope.pwd+
  '&project='+$rootScope.project;
  //Ask to the server if credentials is correct
  $http.get(request)
  .then((response) => {
    if(response.status == 200){ //Status 200 : Everything OK
      var jwt_token = response.data.jwt;
      localStorage.setItem('tokenAPI',jwt_token); //Set the token sent by server in localStorage
      Global.update_key(jwt_token); //Update global.url_api
      if(response.data.admin) $scope.admin_connected = true;
      $scope.credentials = true; //Set visible the tab
      $rootScope.getProjectData(); //Get projects data from DB
    }
  })
}
}]);
And my test file :
describe('MainController', ()=>{
  beforeEach(()=>{module('App')});
  var $controller, $scope , $rootScope ;
  beforeEach(angular.mock.inject((_$controller_,_$rootScope_)=>{
    $rootScope = _$rootScope_;
    $scope = _$rootScope_.$new();
    $rootScope.project = "IDRIS";
    $controller = _$controller_('Ctrl',{$scope : $scope});
  }))
  it("Should be defined",()=>{
    expect($controller).toBeDefined();
  })
  describe('Connection',()=>{
    beforeEach(()=>{
      $scope.username = "adminUsername";
      $scope.pwd = "adminPassword";
      $rootScope.Connect()
    })
    it("Should return a token if credentials are true", ()=>{
        expect(localStorage.getItem('tokenAPI')).not.toBe(null);
    })
  })
})
My localStorage if always null.
I don't know if have to use the $httpBackend service because actually it's my controller who is making the request and obviously, i can't predict what's token the server will deliver me.
I don't know if i'm on the good way. Thanks you
 
    