       expect(@authService.postId).toHaveBeenCalled()
 
   describe 'password_reset_confirm', ->
+    beforeEach ->
+      @httpBackend.whenGET("#{@urlBase}/auth/user").respond 401, {}
+
     it 'confirms password reset via token', ->
       @httpBackend.expectPOST("#{@urlBase}/auth/pwreset/mrpollo/token").respond @user
       spyOn(@authService, 'postId').and.callThrough()

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

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

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

    it 'should call setLoggedOut on error', ->
      @httpBackend.expectGET("#{@urlBase}/auth/user").respond 401, {"message":"You are not logged in"}
      spyOn(@authService, 'setLoggedOut')
      @authService.checkLogin()
      @httpBackend.flush()
      expect(@authService.setLoggedOut).toHaveBeenCalled()

  beforeEach module 'app'

  beforeEach inject ($rootScope, $controller, _$httpBackend_, authService) ->
    loadJSONFixtures 'user.json'
    @user         = getJSONFixture 'user.json'
    @scope        = $rootScope.$new()
    @urlBase      = 'http://api.droneshare.com/api/v1'
    @authService  = authService
    @httpBackend  = _$httpBackend_

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

    it 'user is logged out on initialize',  ->
      expect(@authService.user.loggedIn).not.toBeTruthy()

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

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

    it 'checks if a user is currently logged in', ->
      spyOn(@authService, 'setLoggedIn')

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

      expect(@authService.setLoggedIn).toHaveBeenCalledWith @user

  describe 'logout', ->
    beforeEach ->
      @httpBackend.whenGET("#{@urlBase}/auth/user").respond @user
      @scope.$apply()
      @httpBackend.flush()

