var expect = require('expect.js');
var request = require('request');

describe('SampleTarget', function() {
  var base_url = 'http://localhost:8000';
  var logon_url = base_url + '/public/siteminderagent/login.fcc';
  var protected_url = base_url + '/protected';
  var logoff_url = base_url + '/system/logout';
  var options = {};

  beforeEach(function() {
    options.followAllRedirects = true;
    options.maxRedirects = 3;
    options.jar = true;
    options.form = {};
  });

  describe('Login', function() {
    beforeEach(function() {
      options.url = logon_url;
      options.form.TARGET = protected_url;
    });

    describe('with valid credentials', function() {
      beforeEach(function() {
        options.form.USERNAME = 'bob';
        options.form.PASSWORD = 'test1234';
      });

      it('is redirected to the target URL', function(done) {
        request.post(options, function(err, res, body) {
          expect(res.statusCode).to.equal(200);
          expect(res.request.uri.href).to.equal(protected_url);
          done();
        });
      });
    });

    describe('with a bad username', function() {
      beforeEach(function() {
        options.form.USERNAME = 'bob1';
        options.form.PASSWORD = 'test1234';
      });

      it('is redirected to the bad login page', function(done) {
        request.post(options, function(err, res, body) {
          expect(res.statusCode).to.equal(200);
          expect(res.request.uri.href).to.equal(base_url + '/system/error/badlogin');
          done();
        });
      });
    });

    describe('with a bad password', function() {
      beforeEach(function() {
        options.form.USERNAME = 'bob';
        options.form.PASSWORD = 'test12341212';
      });

      it('is redirected to the bad login page', function(done) {
        request.post(options, function(err, res, body) {
          expect(res.statusCode).to.equal(200);
          expect(res.request.uri.href).to.equal(base_url + '/system/error/badpassword');
          done();
        });
      });
    });

    describe('with bad credentials three times', function() {
      beforeEach(function() {
        options.form.USERNAME = 'bob';
        options.form.PASSWORD = 'test12341212';
      });

      it('is redirected to the account locked page', function(done) {
        var count = 0;

        var callback = function(err, res, body) {
          count++;

          if (count > 2) {
            expect(res.statusCode).to.equal(200);
            expect(res.request.uri.href).to.equal(base_url + '/system/error/accountlocked');
            return done();
          }

          request.post(options, callback);
        };

        request.post(options, callback);
      });
    });
  });
});
@@ -304,16 +304,91 @@
 ogout';%0A
+  var not_authenticated_url = base_url + '/system/error/notauthenticated';%0A
   var op
@@ -437,17 +437,27 @@
  options
-.
+ = %7B%0A      
 followAl
@@ -466,37 +466,30 @@
 edirects
- =
+:
  true
-;%0A    options.
+,%0A      
 maxRedir
@@ -496,85 +496,118 @@
 ects
- = 3;
+: 3,
 %0A    
-options.jar = true;%0A    options.form = %7B%7D;%0A  %7D);%0A%0A  describe('Login
+  jar: request.jar(),%0A      form: %7B%7D%0A    %7D;%0A  %7D);%0A%0A  describe('POST /public/siteminderagent/login.fcc
 ', f
@@ -2831,12 +2831,507 @@
 );%0A  %7D);
+%0A%0A  describe('GET /protected', function() %7B%0A    beforeEach(function() %7B%0A      options.url = protected_url;%0A      options.form = %7B%7D;%0A    %7D);%0A%0A    describe('without a valid session', function() %7B%0A      it('is redirected to the not authenticated URL', function(done) %7B%0A        request.get(options, function(err, res, body) %7B%0A          expect(res.statusCode).to.equal(200);%0A          expect(res.request.uri.href).to.equal(not_authenticated_url);%0A          done();%0A        %7D);%0A      %7D);%0A    %7D);%0A  %7D);
 %0A%7D);

Add test for unauthenticated access to the protected application