{"size": 7611, "ext": "js", "max_stars_repo_path": "test/get-after-res-test.js", "max_stars_repo_name": "mitchallen/richmond-web-controller", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/get-after-res-test.js", "max_issues_repo_name": "mitchallen/richmond-web-controller", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/get-after-res-test.js", "max_forks_repo_name": "mitchallen/richmond-web-controller", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 37.3088235294, "max_line_length": 117, "alphanum_fraction": 0.4764157141}
/**
 * File: get-after-res-test.js
 */

"use strict";

/*global describe, it, before, after*/

var request = require('supertest'),
    should = require('should'),
    jwt = require('jwt-simple'),
    ngrok = require('ngrok'),
    TestConfig = require('./test-config'),
    config = new TestConfig(),
    micro = config.richmond,
    controller = config.controller,
    getRandomInt = require('./test-lib').getRandomInt,
    service       = config.service,
    port     = service.port,
    prefix     = service.prefix,
    dbConfig = config.mongoose,
    // testSecret = 'supersecret',
    testSecret = service.secret,
    testHost = service.host,
    sslHost  = service.hostSsl,
    modelName = "GetBeforeAfterTest",
    ownerEmail = "test@owner.com",
    afterTestEmail = "test" + getRandomInt(1000, 1000000) + "@after.com",
    testAfterDocStatus = "UPDATED by afterGet";

var MochaTestDoc = null;

describe('get after error injection', function () {
    before(function () {
        var testExtraMessage = 'Testing 123',
            beforeMany = null,
            afterMany = null,
            beforeOne = null,
            afterOne = null,
            dbOptions = {};
        beforeMany = function (prop, next) {
            should.exists(prop.req);
            var req = prop.req,
                filter = req.query.filter,
                fields = req.query.fields,
                options = req.query.options,
                token = req.token,
                extras = { message: testExtraMessage },
                f2 = null;
            if (filter) {
                f2 = JSON.parse(filter);    // parse object 
                f2.email.should.eql(token.email);
            }
            next(filter, fields, extras, options);
        };
        afterMany = function (prop, next) {
            should.exist(next);
            should.exist(prop.req);
            should.exist(prop.res);
            should.exist(prop.docs);
            var res = prop.res,
                extras = prop.extras;
            extras.message.should.eql(testExtraMessage);
            // Testing Response
            res.status(402).json({ error: "Payment required." });
            // next(prop.docs);    // Don't call next when intercepting response
        };
        beforeOne = function (prop, next) {
            should.exist(prop);
            should.exist(prop.req);
            // Token may not always exist, but for these tests it should.
            should.exist(prop.req.token);
            var req = prop.req,
                fields = req.query.fields,
                extras = { message: testExtraMessage };
            next(fields, extras);
        };
        afterOne = function (prop, next) {
            should.exist(next);
            should.exist(prop.req);
            should.exist(prop.res);
            should.exist(prop.doc);
            var res = prop.res,
                extras = prop.extras;
            extras.message.should.eql(testExtraMessage);
            res.status(402).json({ error: "Payment required." });
            // next(prop.doc);    // Don't call when intercepting response
        };
        controller.clear();
        micro
            .logFile(config.logFolder + "get-after-res-test.log")
            .controller(
                controller.setup({
                    getOne:   [{ model: modelName, rights: "PUBLIC", before: beforeOne, after: afterOne }],
                    getMany:  [{ model: modelName, rights: "USER", ssl: 302, before: beforeMany, after: afterMany }],
                    post:     [{ model: modelName, rights: "PUBLIC" }],
                })
            )
            .secret(testSecret)
            .prefix(prefix);    // API prefix, i.e. http://localhost/v1/testdoc
        dbOptions = {
            user: dbConfig.user,
            pass: dbConfig.pass
        };
        micro.connect(dbConfig.uri, dbOptions);
        MochaTestDoc = micro.addModel(modelName, {
            email:     { type: String, required: true },
            status: { type: String, required: true },
            password: { type: String, select: false },
        });
        micro.listen(port);
    });

    afterEach(function () {
        ngrok.disconnect();
    });

    after(function () {
        micro.closeService();
    });

    it('should return the injected error instead of a document', function (done) {
        var testUrl = prefix.toLowerCase() + "/" + modelName.toLowerCase(),
            testId = null,
            testObject = {};
        testObject = {
            email: "test" + getRandomInt(1000, 1000000) + "@get.com",
            status: "TEST GET DOCUMENT"
        };
        // SETUP - need to post at least one record
        request(testHost)
            .post(testUrl)
            .send(testObject)
            .set('Content-Type', 'application/json')
            .expect(201)
            .end(function (err, res) {
                should.not.exist(err);
                /*jslint nomen: true*/
                testId = res.body._id;
                /*jslint nomen: false*/
                // GET by ID
                request(testHost)
                    .get(testUrl + "/" + testId)
                    .set('x-auth', jwt.encode({ email: ownerEmail, role: "user" }, testSecret))
                    .expect('Content-Type', /json/)
                    .expect(402)
                    .end(function (err, res) {
                        should.not.exist(err);
                        should.exist(res);
                        // PURGE all records 
                        MochaTestDoc.remove({"email": /@/ }, function (err) {
                            if (err) {
                                console.error(err);
                            }
                            done();
                        });
                    });
            });
    });

    it('should return the injected error instead of a collection', function (done) {
        var testUrl = prefix.toLowerCase() + "/" + modelName.toLowerCase(),
            testEmail = afterTestEmail,
            testObject = {};
        testObject = {
            email: testEmail,
            status: "TEST GET filter"
        };
        // SETUP - need to post at least one record
        request(testHost)
            .post(testUrl)
            .send(testObject)
            // .set('Content-Type', 'application/json') // If move, returns HTML
            // .expect(201) // Post to NON-SSL
            // .expect(302)    // Post to SSL
            .end(function (err, res) {
                should.not.exist(err);
                should.exist(res);
                // GET
                var options = {
                    proto: 'http',
                    addr: service.port
                }
                ngrok.connect( options, function( err, grokHostSSL ) {
                    if(err) done(err);
                    // request(sslHost)
                    request(grokHostSSL)
                        .get(testUrl)
                        .set('x-auth', jwt.encode({ email: testEmail, role: "user" }, testSecret))
                        .query( { filter: '{"email":"' + testEmail + '"}' } )
                        // MUST USE DOUBLE QUOTES - or JSON.parse bombs in GET.
                        // .expect('Content-Type', /json/)    // Sometimes returns 302 / HTML (nginx)
                        .expect(402)
                        .end(function (err, res) {
                            should.not.exist(err);
                            should.exist(res);
                            done();
                        });
                });
            });
    });

});
