import Ember from 'ember';
import DS from 'ember-data';
import User from 'ember-token-auth/models/user';
import { test, moduleFor } from 'ember-qunit';
import {auth, session, reopenConfig} from 'ember-token-auth/tests/helpers/ember-oauth2';

var container;
var store;

moduleFor('controller:session', 'SessionController', {
  needs: ['controller:session'],
  setup: function() { 
    container = new Ember.Container(); 
    container.register('store:main', DS.Store.extend());
    container.register('serializer:-default', DS.JSONSerializer);
    container.register('serializer:-rest', DS.RESTSerializer);
    container.register('adapter:-rest', DS.RESTAdapter);
    container.injection('serializer', 'store', 'store:main');

    container.register('model:user', User);
    store = container.lookup('store:main');
  }
});

// Replace this with your real tests.
test('it exists', function() {
  var controller = this.subject();
  ok(controller);
});

test('loadUser should set the currentUser', function() {
  expect(3); 

  var ctrl = this.subject();
  ctrl.set('session', session);
  ctrl.set('store', store);
  ctrl.transitionToRoute = function() { return true; };

  equal(ctrl.get('currentUser'), null);

  stop();
  ctrl.loadUser().then(function() {
    start();
    equal(ctrl.get('logginError'), false);
    equal(ctrl.get('currentUser').get('lastname'), 'bar');
  });
});


test('should set lgginError to true when ajax fails', function() {
  var oldCurrentUserPath = session.auth.currentUser;
  session.auth.currentUser = session.auth.currentUserError;

  var ctrl = this.subject();
  ctrl.set('session', session);

  stop();
  ctrl.loadUser().then(function() {
    start();
    equal(ctrl.get('logginError'), true);
  });

  session.auth.currentUser = oldCurrentUserPath;
});

@@ -49,16 +49,80 @@
 -data';%0A
+import SessionAdapter from 'ember-token-auth/adapters/session';%0A
 import U
@@ -158,24 +158,24 @@
 dels/user';%0A
-
 import %7B tes
@@ -324,16 +324,36 @@
 r store;
+%0Avar sessionAdapter;
 %0A%0Amodule
@@ -736,24 +736,83 @@
 STAdapter);%0A
+    container.register('adapter:session', SessionAdapter);%0A
     containe
@@ -950,16 +950,118 @@
 main');%0A
+    sessionAdapter = container.lookup('adapter:session');%0A    sessionAdapter.set('session', session);%0A
   %7D%0A%7D);%0A
@@ -1349,16 +1349,53 @@
 store);%0A
+  ctrl.set('container', container);%0A%0A
   ctrl.t
@@ -1675,16 +1675,17 @@
 ld set l
+o
 gginErro
@@ -1860,32 +1860,32 @@
 this.subject();%0A
-
   ctrl.set('sess
@@ -1899,16 +1899,52 @@
 ession);
+%0A  ctrl.set('container', container);
 %0A%0A  stop

add the adapter:session