Skip to content

Instantly share code, notes, and snippets.

@kheremos
Last active October 30, 2015 16:14
Show Gist options
  • Save kheremos/c40f9c5ba5876302f115 to your computer and use it in GitHub Desktop.
Save kheremos/c40f9c5ba5876302f115 to your computer and use it in GitHub Desktop.
A first-pass of API testing against Edmunds. With a toggle that can switch between fixtures or external API calls. The edmundsService should not be in-line when the tests near completion, but serve to help during development and provide better context for this gist.
/** Created by: greg.a.galloway@gmail.com Oct 2015
* Issue: TSL-105
*
* This file should demonstrate desired functionality for
* leveraging the Edmund's API. If configuration is
* required, check the issue link above.
*/
'use strict';
var sinon = require('sinon');
var should = require('should');
var request = require('request');
var chai = require('chai');
var expect = chai.expect;
var Q = require('q');
var config = require('../../../../config/environment');
var EDMUNDS = config.edmunds;
// Determine if the tests should be run against external APIs.
var ENABLE_EXTERNAL_API = config.testAPIs;
//var ENABLE_EXTERNAL_API = true;
var API_NOTICE = ' *** WARNING: Tests running against external APIs...';
var fixtures = {
makes: require('./edmunds_makes.json'),
makesCount: require('./edmunds_makesCount.json'),
vin2C3CDYAG3DH601064: require('./vin2C3CDYAG3DH601064.json')
};
var edmundsService = (function () {
// https://api.edmunds.com/api/vehicle/v2/squishvins/{squish VIN}/?fmt=json&api_key={api key}
var endPoints = {
host: 'http://api.edmunds.com/api/vehicle/v2/',
makesCount: 'makes/count?fmt=json&api_key=' + EDMUNDS.api_key,
squishVin: 'squishvins/{squish VIN}/?fmt=json&api_key=' + EDMUNDS.api_key,
makes: 'makes?fmt=json&api_key=' + EDMUNDS.api_key
};
/** Helper Function to set up request call.
*
* @param destination HTTP destination of call
* @param deferment Promise to resolve during request callback
*/
var makeRequestTo = function (destination, deferment) {
request.get(destination, function (error, response, body) {
deferment.resolve({response: response, error: error, body: body});
});
return deferment.promise;
};
/**
* RETURN: Returns a squish VIN from a non-squished VIN
* ASSUME: Assume provided VIN is at least 11 characters
* @param vin The vin to squish
* TODO: Add verification and errors?
*/
var getSquishVin = function (vin) {
return vin.substring(0, 8) + vin.substring(9, 11);
};
var requestMakesCount = function () {
var deferred = Q.defer();
// TODO: Error handling?
return makeRequestTo(endPoints.host + endPoints.makesCount, deferred);
};
/** Calls to:
* http://api.edmunds.com/api/vehicle/v2/makes/count?fmt=json&api_key=...'
*
* @returns {deferred.promise|{then, always}}
*/
var requestMakes = function () {
var deferred = Q.defer();
// TODO: Error handling?
return makeRequestTo(endPoints.host + endPoints.makes, deferred);
};
/** Requests vehicle details from a VIN or Squish VIN.
// https://api.edmunds.com/api/vehicle/v2/squishvins/{squish VIN}/?fmt=json&api_key={api key}
*
*/
var requestDetailsFromVin = function (vin) {
var deferred = Q.defer();
if (vin.length > 10) {
vin = getSquishVin(vin);
}
var requestURL = endPoints.host + endPoints.squishVin.replace('{squish VIN}', vin);
// TODO: Error handling?
return makeRequestTo(requestURL, deferred);
};
return {
requestMakes: requestMakes,
requestMakesCount: requestMakesCount,
requestDetailsFromVin: requestDetailsFromVin,
squishVin: getSquishVin
};
})();
var stubGetRequest = function (){
/** TODO: Create pattern for the following block of code:
* sinon
.stub(request, 'get')//Fake the yield of (Error, Response, Body)
.yields(undefined, {statusCode: 200}, JSON.stringify(fixtures.makesCount));
*
*/
};
describe('Edmunds Test Cases', function () {
describe('helper method test cases', function () {
it('squishVin', function (done) {
var inputVin = '2C3CDYAG3DH601064';
var squishedVin = edmundsService.squishVin(inputVin);
expect(squishedVin).to.exist.and.to.have.length(10);
expect(squishedVin).to.eql('2C3CDYAGDH');
done();
});
});
/** Tests written against the API results of
* October 2015. Numbers is guaranteed to change
* when testing against external API.
*/
describe('simple requests ', function () {
var error = function () {
should.fail('Error callback should not be called.');
done();
};
context('When call is made.', function () {
it('makesCount should return a number over 60', function (done) {
var success = function (data) {
try {
console.log(data.body);
expect(data.body).to.exist;
var body = JSON.parse(data.body);
expect(body).to.exist;
expect(body.makesCount).to.exist.and.to.be.above(60);
expect(data.error).to.not.exist;
data.response.statusCode.should.be.exactly(200);
done();
} catch (e) {
done(e);
}
};
if (ENABLE_EXTERNAL_API) {
console.log(API_NOTICE);
} else {
sinon
.stub(request, 'get')
//Fake the yield of (Error, Response, Body)
.yields(undefined, {statusCode: 200}, JSON.stringify(fixtures.makesCount));
}
edmundsService.requestMakesCount().then(success, error);
});
});
describe('YMMS from VIN', function () {
beforeEach(function () {
try {
// Unwrap the spy
console.log('beforeEach called\n');
if (typeof request.get.restore === "function") {
request.get.restore();
}
} catch (e) {
console.log(e);
}
});
it('Should return vehicle info from full Vin', function (done) {
var success = function (data) {
try {
var body = JSON.parse(data.body);
expect(body).to.exist;
expect(body.model).to.exist;
expect(body.model.id).to.exist.and.to.eql("Dodge_Challenger");
expect(data.error).to.not.exist;
data.response.statusCode.should.be.exactly(200);
done();
} catch (e) {
done(e);
}
};
if (ENABLE_EXTERNAL_API) {
console.log(API_NOTICE);
} else {
sinon
.stub(request, 'get')//Fake the yield of (Error, Response, Body)
.yields(undefined, {statusCode: 200}, JSON.stringify(fixtures.vin2C3CDYAG3DH601064));
}
edmundsService.requestDetailsFromVin('2C3CDYAG3DH601064').then(success, error);
});
it('Should return vehicle info from Squish Vin', function (done) {
var success = function (data) {
try {
var body = JSON.parse(data.body);
expect(body).to.exist;
expect(body.model).to.exist;
expect(body.model.id).to.exist.and.to.eql("Dodge_Challenger");
expect(data.error).to.not.exist;
data.response.statusCode.should.be.exactly(200);
done();
} catch (e) {
console.log(e);
done(e);
}
};
if (ENABLE_EXTERNAL_API) {
console.log(API_NOTICE);
} else {
sinon
.stub(request, 'get')//Fake the yield of (Error, Response, Body)
.yields(undefined, {statusCode: 200}, JSON.stringify(fixtures.vin2C3CDYAG3DH601064));
}
edmundsService.requestDetailsFromVin('2C3CDYAGDH').then(success, error);
});
});
describe('process data for makes', function () {
it('Should correctly process makes json', function (done) {
done("Tests not written yet.");
});
});
});
describe('Process requests to makes', function () {
it('should work in testing', function (done) {
done("Tests not written yet.");
});
it('should get a valid response from makes 200', function (done) {
var success = function (data) {
try {
expect(data.body).to.exist;
expect(data.error).to.not.exist;
data.response.statusCode.should.be.exactly(200);
done();
} catch (e) {
done(e);
}
};
var error = function () {
should.fail('Error callback should not be called.');
done();
};
edmundsService.requestMakes().then(success, error);
});
});
describe('Get year listings', function () {
it('should work in testing', function (done) {
done("Tests not written yet.");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment