       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
 

      expect(@authService.postId).toHaveBeenCalledWith "pwreset/mrpollo/token", JSON.stringify('password')

  describe 'email_confirm', ->
    it 'should confirm user email', ->
      spyOn(@authService, 'postId')
      @authService.email_confirm 'mrpollo', 'token'
      expect(@authService.postId).toHaveBeenCalledWith "emailconfirm/mrpollo/token", {}

  describe 'setLoggedIn', ->
    it 'should flag @user.loggedIn as true and return user', ->
      spyOn(@authService, 'setLoggedIn').and.callThrough()
      expect(@user.loggedIn).toBeUndefined()

  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()

