/* global describe, it */

var chai = require('chai');
var expect = require('chai').expect;
var sinon = require('sinon');
var factory = require('../../../app/http/handlers/authorize');
var flowstate = require('flowstate');


describe('http/handlers/authorize', function() {
  
  it('should export factory function', function() {
    expect(factory).to.be.a('function');
  });
  
  it('should be annotated', function() {
    expect(factory['@implements']).to.be.undefined;
    expect(factory['@singleton']).to.be.undefined;
  });
  
  describe('handler', function() {
    var manager = new flowstate.Manager();
    manager.use('oauth2/authorize', {
      prompt:  [
        function(req, res, next) {
          res.render('consent');
        }
      ]
    })
    
    function ceremony(name) {
      return manager.flow.apply(manager, arguments);
    }
    
    var server = {
      authorization: function(){
        return function(req, res, next) {
          next();
        };
      }
    }
    function validateClient() {};
    function processTransaction() {};
    function completeTransaction() {};
    
    function authenticate(schemes) {
      return function(req, res, next) {
        req.authInfo = { schemes: schemes };
        next();
      };
    }
    
    describe('default behavior', function() {
      var request, response, view, h;
      
      before(function() {
        sinon.spy(server, 'authorization');
      });
      
      after(function() {
        server.authorization.restore();
      });
      
      before(function(done) {
        var handler = factory(validateClient, processTransaction, completeTransaction, server, authenticate, ceremony);
        
        chai.express.handler(handler)
          .req(function(req) {
            request = req;
            req.session = {};
          })
          .res(function(res) {
            response = res;
            res.locals = {};
          })
          .render(function(res, v) {
            h = Object.keys(request.session.state)[0];
            view = v;
            done();
          })
          .dispatch();
      });
      
      it('should add authorization middleware to stack', function() {
        expect(server.authorization.callCount).to.equal(1);
        expect(server.authorization).to.be.calledWithExactly(validateClient, processTransaction, completeTransaction);
      });
      
      it('should set authentication info', function() {
        expect(request.authInfo).to.deep.equal({
          schemes: ['session', 'anonymous']
        });
      });
      
      it('should set state', function() {
        expect(request.state).to.deep.equal({
          name: 'oauth2/authorize'
        });
        expect(request.state.isComplete()).to.equal(false);
      });
      
      it('should set session', function() {
        var state = {};
        state[h] = {
          name: 'oauth2/authorize'
        };
        
        expect(request.session).to.deep.equal({
          state: state
        });
      });
      
      it('should set locals', function() {
        expect(response.locals).to.deep.equal({
          state: h
        });
      });
      
      it('should render', function() {
        expect(response.statusCode).to.equal(200);
        expect(view).to.equal('consent');
      });
    }); // default behavior
    
  }); // handler
  
});

@@ -707,22 +707,23 @@
     res.
-render
+xprompt
 ('consen
@@ -1274,16 +1274,21 @@
 describe
+.only
 ('defaul
@@ -1344,17 +1344,8 @@
 onse
-, view, h
 ;%0A  
@@ -1777,38 +1777,8 @@
 eq;%0A
-            req.session = %7B%7D;%0A
     
@@ -1865,20 +1865,36 @@
 res.
-locals = %7B%7D;
+xprompt = function(prompt) %7B
 %0A   
@@ -1904,124 +1904,81 @@
     
-%7D)%0A
     
-      .render(function(res, v) %7B%0A            h = Object.keys(request.session.state)%5B0%5D;%0A            view = v;
+this.end();%0A            %7D%0A          %7D)%0A          .end(function(res) %7B
 %0A   
@@ -2724,397 +2724,11 @@
 uld 
-set session', function() %7B%0A        var state = %7B%7D;%0A        state%5Bh%5D = %7B%0A          name: 'oauth2/authorize'%0A        %7D;%0A        %0A        expect(request.session).to.deep.equal(%7B%0A          state: state%0A        %7D);%0A      %7D);%0A      %0A      it('should set locals', function() %7B%0A        expect(response.locals).to.deep.equal(%7B%0A          state: h%0A        %7D);%0A      %7D);%0A      %0A      it('should render
+end
 ', f
@@ -2794,50 +2794,8 @@
 0);%0A
-        expect(view).to.equal('consent');%0A
     

Simplify tests.