'use strict';
var assert = require('assert'),
    sinon = require('sinon'),
    request = require('request'),
    makeRequest = require('./../lib/request'),
    _ = require('lodash');

describe('eMerchantPay API Request', function () {

    var validXML = '<?xml version="1.0" encoding="UTF-8"?><orders><num_records>0</num_records></orders>';
    //     clientId = 123456,
    //     apiKey = '164684684684684',
    //     validJSON = JSON.parse('{"orders":{"num_records":["0"]}}'),
    //     invalidXML = '<?xml version="1.0" encoding="UTF-8"?><orders<num_records>0</num_records></orders>';

    describe('configuration', function () {

        it('should throw an error if no callback is set', function (done) {
            assert.throws(function () {
                makeRequest();
            }, Error);
            done();
        });

        it('should throw if options object is empty', function (done) {
            makeRequest({}, function (err, results) {
                assert.equal(results, null);
                assert.equal(_.isError(err), true);
                done();
            });
        });

        // var requestParams = {
        //     uri: '/order/search/',
        //     options: {
        //         method: 'POST',
        //         form: params
        //     },
        //     validationSchema: validationSchema,
        //     context: self
        // };

        it('should throw if either api_key is missing', function (done) {
            makeRequest({
                options: {
                    form: {
                        'client_id': 123
                    }
                }
            }, function (err, results) {
                assert.equal(results, null);
                assert.equal(_.isError(err), true);
                done();
            });
        });

        it('should throw if client_id is missing', function (done) {
            makeRequest({
                options: {
                    form: {
                        'api_key': '123'
                    }
                }
            }, function (err, results) {
                assert.equal(results, null);
                assert.equal(_.isError(err), true);
                done();
            });
        });

        it('should throw if uri is missing', function (done) {
            makeRequest({
                options: {
                    form: {
                        'client_id': 123,
                        'api_key': '123'
                    }
                }
            }, function (err, results) {
                assert.equal(results, null);
                assert.equal(_.isError(err), true);
                done();
            });
        });

        it('should throw if uri is missing', function (done) {

            sinon
              .stub(request, 'post')
              .yields(null, {}, validXML);

            makeRequest({
                uri: 'http://proxy.aws/service/order/search',
                options: {
                    method: 'POST',
                    form: {
                        'client_id': 123,
                        'api_key': '123'
                    }
                }
            }, function (err, results) {
                assert.equal(err, null);
                assert.equal(results, validXML);
                done();
            });
        });


    });

});

@@ -338,256 +338,154 @@
 rs%3E'
-;%0A    //     clientId = 123456
 ,%0A    
-//
     
- apiKey = '164684684684684',%0A    //     validJSON = JSON.parse('%7B%22orders%22:%7B%22num_records%22:%5B%220%22%5D%7D%7D'),%0A    //     invalidXML = '%3C?xml version=%221.0%22 encoding=%22UTF-8%22?%3E%3Corders%3Cnum_records%3E0%3C/num_records%3E%3C/orders
+errorXML = '%3C?xml version=%221.0%22 encoding=%22UTF-8%22?%3E%3Cfailure%3E%3Cerrors%3E%3Cerror%3E%3Ccode%3EB0GUS%3C/code%3E%3Ctext%3Eerror%3C/text%3E%3C/error%3E%3C/errors%3E%3C/failure
 %3E';%0A
@@ -1013,285 +1013,8 @@
 );%0A%0A
-        // var requestParams = %7B%0A        //     uri: '/order/search/',%0A        //     options: %7B%0A        //         method: 'POST',%0A        //         form: params%0A        //     %7D,%0A        //     validationSchema: validationSchema,%0A        //     context: self%0A        // %7D;%0A%0A
     
@@ -2335,39 +2335,37 @@
 'should 
-throw if uri is missing
+succeed with validXML
 ', funct
@@ -2910,16 +2910,58 @@
 idXML);%0A
+%0A                request.post.restore();%0A%0A
         
@@ -3005,16 +3005,885 @@
    %7D);%0A%0A
+        it('should throw an error with a 200 %22failure%22 response from eMerchantPay', function (done) %7B%0A%0A            sinon%0A              .stub(request, 'post')%0A              .yields(null, %7B%7D, errorXML);%0A%0A            makeRequest(%7B%0A                uri: 'http://proxy.aws/service/order/search',%0A                options: %7B%0A                    method: 'POST',%0A                    form: %7B%0A                        'client_id': 123,%0A                        'api_key': '123'%0A                    %7D%0A                %7D,%0A                context: %7B%0A                    config: %7B%0A                        parseXML: true%0A                    %7D%0A                %7D%0A            %7D, function (err, result) %7B%0A                assert.equal(_.isError(err), true);%0A                assert.equal(result, null);%0A                request.post.restore();%0A                done();%0A            %7D);%0A        %7D);%0A
 %0A    %7D);

add test for xml error response