import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from '../helpers/start-app';

var App;

module('Acceptance: Login', {
  setup: function() {
    if (window.localStorage.getItem('token-testAuth')) {
      window.localStorage.removeItem('token-testAuth');
    }
    App = startApp();
  },

  teardown: function() {
    window.history.pushState('','','/tests');
    Ember.run(App, 'destroy');
  }
});

test('visiting /login when there is not valid login should not redirect', function(assert) {
  visit('/login');

  andThen(function() {
    assert.equal(currentURL(), '/login');
  });
});

test('visiting /login should redirect to index when there is a valid login', function(assert) {
  visit('/login');
  click('button#login');

  andThen(function() {
    assert.equal(currentURL(), '/');
  });
});

test('login fails', function() {
  OAuth2.reopen({
    openWindow: function() {
      return new Ember.RSVP.Promise(function(resolve,reject) {
        reject(new Error('Opening dialog login window failed.'));
      });
    }
  });

  visit('/login');
  click('button#login');

  andThen(function() {
    equal(currentURL(), '/login');
    equal(find('p.error').text(), 'There was an error logging in. Please try again.');
  });
});

test('logging in is successful but current_user returns error', function() {

  window.EmberENV['ember-oauth2'].testAuth.currentUser = window.EmberENV['ember-oauth2'].testAuth.currentUserError;

  visit('/login');
  click('button#login');

  andThen(function() {
    equal(currentURL(), '/login');
    equal(find('p.error').text(), 'There was an error logging in. Please try again.');
  });
});

@@ -829,441 +829,8 @@
 );%0A%0A
-test('login fails', function() %7B%0A  OAuth2.reopen(%7B%0A    openWindow: function() %7B%0A      return new Ember.RSVP.Promise(function(resolve,reject) %7B%0A        reject(new Error('Opening dialog login window failed.'));%0A      %7D);%0A    %7D%0A  %7D);%0A%0A  visit('/login');%0A  click('button#login');%0A%0A  andThen(function() %7B%0A    equal(currentURL(), '/login');%0A    equal(find('p.error').text(), 'There was an error logging in. Please try again.');%0A  %7D);%0A%7D);%0A%0A
 test
@@ -894,24 +894,30 @@
 ', function(
+assert
 ) %7B%0A%0A  windo
@@ -1094,24 +1094,31 @@
 ion() %7B%0A    
+assert.
 equal(curren
@@ -1140,16 +1140,23 @@
 ');%0A    
+assert.
 equal(fi

remove test for if dialog window opens