'use strict';

var request = require('supertest');

var helpers = require('../helpers');

describe('logout', function () {
  var app;
  var stormpathApplication;

  before(function (done) {
    helpers.createApplication(helpers.createClient(), function (err, a) {
      if (err) {
        return done(err);
      }

      stormpathApplication = a;
      app = helpers.createStormpathExpressApp({
        application: {
          href: stormpathApplication.href
        },
        web: {
          logout: {
            enabled: true
          }
        }
      });

      app.on('stormpath.ready', done);
    });
  });

  after(function (done) {
    helpers.destroyApplication(stormpathApplication, done);
  });

  it('should not respond to GET request', function (done) {
    var config = app.get('stormpathConfig');
    request(app)
      .get(config.web.logout.uri)
      .expect(404)
      .end(done);
  });

  it('should bind to /logout by default', function (done) {
    var config = app.get('stormpathConfig');
    request(app)
      .post(config.web.logout.uri)
      .expect(302)
      .end(done);
  });

  it('should delete the access token and refresh token cookies', function (done) {
    var config = app.get('stormpathConfig');
    request(app)
      .post(config.web.logout.uri)
      .expect('Set-Cookie', /access_token=;/)
      .expect('Set-Cookie', /refresh_token=;/)
      .end(done);
  });

  it.skip('should delete the token resources', function () {
    // write a test to assert that the access token and refresh token resources
    // are deleted
  });

  describe('when Accept header is set to text/html', function () {
    it('should respond with 302', function (done) {
      var config = app.get('stormpathConfig');
      request(app)
        .post(config.web.logout.uri)
        .set('Accept', 'text/html')
        .expect(302)
        .end(done);
    });
  });

  describe('when Accept header is set to application/json', function () {
    it('should respond with 200', function (done) {
      var config = app.get('stormpathConfig');
      request(app)
        .post(config.web.logout.uri)
        .set('Accept', 'application/json')
        .expect(200)
        .end(done);
    });
  });

  it('should follow the next param if present', function (done) {
    var config = app.get('stormpathConfig');
    request(app)
      .post(config.web.logout.uri + '?next=/goodbye')
      .expect(302)
      .expect('Location', '/goodbye')
      .end(done);
  });
});

@@ -8,16 +8,78 @@
 rict';%0A%0A
+var sinon = require('sinon');%0Avar assert = require('assert');%0A
 var requ
@@ -106,17 +106,16 @@
 test');%0A
-%0A
 var help
@@ -194,36 +194,93 @@
 ;%0A  var 
-stormpathApplication
+config;%0A  var sandbox;%0A  var stormpathApplication;%0A  var postLogoutHandlerSpy
 ;%0A%0A  bef
@@ -371,16 +371,26 @@
  (err, a
+pplication
 ) %7B%0A    
@@ -437,16 +437,162 @@
      %7D%0A%0A
+      sandbox = sinon.sandbox.create();%0A%0A      postLogoutHandlerSpy = sandbox.spy(function (account, req, res, next) %7B%0A        next();%0A      %7D);%0A%0A
       st
@@ -613,17 +613,28 @@
 tion = a
-;
+pplication;%0A
 %0A      a
@@ -836,56 +836,245 @@
    %7D
-%0A      %7D);%0A%0A      app.on('stormpath.ready', done
+,%0A        postLogoutHandler: postLogoutHandlerSpy%0A      %7D);%0A%0A      app.on('stormpath.ready', function (err) %7B%0A        if (err) %7B%0A          return done(err);%0A        %7D%0A%0A        config = app.get('stormpathConfig');%0A%0A        done();%0A      %7D
 );%0A 
@@ -1105,32 +1105,55 @@
 nction (done) %7B%0A
+    sandbox.restore();%0A
     helpers.dest
@@ -1267,53 +1267,8 @@
 ) %7B%0A
-    var config = app.get('stormpathConfig');%0A
     
@@ -1422,53 +1422,8 @@
 ) %7B%0A
-    var config = app.get('stormpathConfig');%0A
     
@@ -1601,53 +1601,8 @@
 ) %7B%0A
-    var config = app.get('stormpathConfig');%0A
     
@@ -2057,55 +2057,8 @@
 ) %7B%0A
-      var config = app.get('stormpathConfig');%0A
     
@@ -2331,55 +2331,8 @@
 ) %7B%0A
-      var config = app.get('stormpathConfig');%0A
     
@@ -2446,25 +2446,16 @@
 ect(200)
-%0A        
 .end(don
@@ -2543,53 +2543,8 @@
 ) %7B%0A
-    var config = app.get('stormpathConfig');%0A
     
@@ -2690,13 +2690,123 @@
 );%0A  %7D);
+%0A%0A  it('should call the postLogoutHandler', function () %7B%0A    assert(postLogoutHandlerSpy.callCount, 3);%0A  %7D);
 %0A%7D);%0A

add very basic postLogoutHandler test