var chai = require('chai'),
    should = chai.should(),
    PkiStrategy = require('../').DirectStrategy,
    helpers = require('./helpers');

describe('Direct PKI Strategy', function() {

  var strategy = new PkiStrategy(function() {});

  it('should be named pki-direct', function() {
    strategy.name.should.equal('pki-direct');
  });

  it('should require a verify function', function() {
    (function() {
      new PkiStrategy();
    }).should.throw(Error);

    (function() {
      new PkiStrategy({});
    }).should.throw(Error);

    var f = function() {};
    (function() {
      new PkiStrategy(f);
    }).should.not.throw(Error);

    (function() {
      new PkiStrategy({}, f);
    }).should.not.throw(Error);
  });

  describe('strategy authenticating a request', function() {
    var req,
        cert = { CA: 'common name' },
        failed,
        succeeded,
        passedToVerify;

    beforeEach(function() {
      strategy = new PkiStrategy(function(cert, f) {
        passedToVerify = cert;
      });

      failed = false;
      succeeded = false;
      passedToVerify = null;

      strategy.fail = function() { failed = true };
      strategy.success = function() { succeeded = true };
    });

    it('should fail if the cert is not authorized', function() {
      req = helpers.dummyReq(false);

      strategy.authenticate(req);
      failed.should.eq(true);
      succeeded.should.eq(false);
    });

    it('should fail if the cert is missing', function() {
      req = helpers.dummyReq(true, null);

      strategy.authenticate(req);
      failed.should.eq(true);
      succeeded.should.eq(false);
    });

    it('should fail if the cert is empty', function() {
      req = helpers.dummyReq(true, {});

      strategy.authenticate(req);
      failed.should.eq(true);
      succeeded.should.eq(false);
    });

    it('should pass the parsed cert to the verify callback', function() {
      req = helpers.dummyReq(true, cert);

      strategy.authenticate(req);
      passedToVerify.should.eq(cert);
    });

    it('should succeed if the verify callback provided a user', function() {
      strategy = new PkiStrategy(function(cert, done) {
        done(null, {});
      });

      strategy.fail = function() { throw new Error('should not be called') };
      strategy.success = strategy.error = function() { succeeded = true };
      req = helpers.dummyReq(true, cert);

      strategy.authenticate(req);
      succeeded.should.eq(true);

    });

    it('should fail if the verify callback provided -false- instead of a user', function() {
      strategy = new PkiStrategy(function(cert, done) {
        done(null, false);
      });

      strategy.fail = function() { failed = true };
      strategy.success = strategy.error = function() { throw new Error('should not be called')  };

      req = helpers.dummyReq(true, cert);
      strategy.authenticate(req);

      failed.should.eq(true);
    });

    it('should error if the verify callback provided an error', function() {
      strategy = new PkiStrategy(function(cert, done) {
        done(new Error('error from verify'));
      });

      var ok = false;
      strategy.error = function() { ok = true };
      strategy.success = strategy.fail = function() { throw new Error('should not be called')  };

      req = helpers.dummyReq(true, cert);
      strategy.authenticate(req);

      ok.should.eq(true);
    });

  });

});

@@ -540,36 +540,49 @@
     
-var f = function() %7B%7D;%0A    (
+// should not throw an error%0A    var f = 
 func
@@ -581,35 +581,35 @@
 f = function() %7B
-%0A  
+%7D;%0A
     new PkiStrat
@@ -624,114 +624,29 @@
     
-%7D).should.not.throw(Error);%0A%0A    (function() %7B%0A      new PkiStrategy(%7B%7D, f);%0A    %7D).should.not.throw(Error
+new PkiStrategy(%7B%7D, f
 );%0A 
@@ -823,16 +823,183 @@
 erify;%0A%0A
+    var fail = function() %7B failed = true %7D,%0A        success = function() %7B succeeded = true %7D,%0A        err = function() %7B throw new Error('should not be called') %7D;%0A%0A
     befo
@@ -1217,93 +1217,45 @@
  = f
-unction() %7B failed = true %7D;%0A      strategy.success = function() %7B succeeded = true %7D
+ail;%0A      strategy.success = success
 ;%0A  
@@ -1432,42 +1432,8 @@
 e);%0A
-      succeeded.should.eq(false);%0A
     
@@ -1606,42 +1606,8 @@
 e);%0A
-      succeeded.should.eq(false);%0A
     
@@ -1776,42 +1776,8 @@
 e);%0A
-      succeeded.should.eq(false);%0A
     
@@ -2166,69 +2166,25 @@
 egy.
-fail = function() %7B throw new Error('should not be called') %7D
+success = success
 ;%0A  
@@ -2188,39 +2188,36 @@
 %0A      strategy.
-success
+fail
  = strategy.erro
@@ -2224,39 +2224,11 @@
 r = 
-function() %7B succeeded = true %7D
+err
 ;%0A  
@@ -2559,35 +2559,11 @@
  = f
-unction() %7B failed = true %7D
+ail
 ;%0A  
@@ -2606,63 +2606,11 @@
 r = 
-function() %7B throw new Error('should not be called')  %7D
+err
 ;%0A%0A 
@@ -3030,63 +3030,11 @@
 l = 
-function() %7B throw new Error('should not be called')  %7D
+err
 ;%0A%0A 

Clean up direct tests