       expect(@authService.setLoggedIn).toHaveBeenCalled()
 
   describe 'login', ->
+    beforeEach ->
+      @httpBackend.whenGET("#{@urlBase}/auth/user").respond 401, {}
+
     it 'posts user credentials to API', ->
       @httpBackend.expectPOST("#{@urlBase}/auth/login?login=mrpollo&password=password").respond @user
 


  describe 'create', ->
    it 'should post payload to API /create', ->
      spyOn(@authService, 'postId').and.callThrough()
      payload = jasmine.createSpy('payload')
      @authService.create(payload)
      expect(@authService.postId).toHaveBeenCalledWith 'create', payload

    it 'should login user upon creation', ->
      @httpBackend.expectPOST("#{@urlBase}/auth/create").respond @user

      spyOn(@authService, 'setLoggedIn')

      expect(@authService.user.loggedIn).not.toBeTruthy()

  describe 'getUser', ->
    beforeEach ->
      @httpBackend.whenGET("#{@urlBase}/auth/user").respond @user

    it 'should return current user object', ->
      @authService.user = @user
      @scope.$apply()
      expect(@authService.getUser()).toEqual @user

  describe 'checkLogin', ->
    afterEach ->
      @httpBackend.verifyNoOutstandingExpectation()
      @httpBackend.verifyNoOutstandingRequest()

    it 'should login is user if succesful', ->
      @httpBackend.whenGET("#{@urlBase}/auth/user").respond @user
      spyOn(@authService, 'setLoggedIn')
      @authService.checkLogin()

      @scope.$apply()
      @httpBackend.flush()

      expect(@authService.setLoggedIn).toHaveBeenCalledWith @user

    it 'should call setLoggedOut on error', ->
      @httpBackend.whenGET("#{@urlBase}/auth/user").respond(401, '')
      spyOn(@authService, 'setLoggedOut')
      @authService.checkLogin()

      @scope.$apply()
      @httpBackend.flush()

      expect(@authService.setLoggedOut).toHaveBeenCalled()
