before(function () { chakram.addProperty('smp', function(){}); chakram.addMethod('galleriesResponse', function (response, items) { expect(response).to.have.header('content-type', 'application/json'); expect(response).to.have.status(200); expect(response).to.have.schema({ properties: { require: ['status', 'data'], status: { type: 'integer' }, data: { type: 'object', required: ['perPage', 'page', 'total', 'results'], results: { type: 'object', required: ['id', 'title', 'thumb', 'thumbs', 'images'], items: { type: 'object', properties: { id: { type: 'integer' }, title: { type: 'string' }, thumb: { type: 'string' }, thumbs: { type: 'array', items: { type: 'string' } }, images: { type: 'integer' } } } } } } }); }); });
before(function () { chakram.addProperty("api", function () {}); chakram.addMethod("error", function (respObj, status, errors) { // allow single error strings to be passed in (but not returned by API) expect(respObj).to.have.status(status); expect(respObj).to.have.json("code", errors.toUpperCase()); }); // generic method for successful responses (doesn't validate schema) chakram.addMethod("genericSuccess", function (respObj, status) { // for debugging if (typeof respObj.body.errors !== "undefined") console.log(respObj.body.errors); expect(respObj).to.have.json("success", true); if (typeof status !== "undefined" && status !== null) expect(respObj).to.have.status(status); // usually 200, but 201 for POST }); // generic methods for different HTTP types (POST = 201 status code, others = 200) chakram.addProperty("postSuccess", function (respObj) { expect(respObj).to.be.an.api.genericSuccess(201); }); chakram.addProperty("putSuccess", function (respObj) { expect(respObj).to.be.an.api.genericSuccess(200); }); chakram.addProperty("getSuccess", function (respObj) { expect(respObj).to.be.an.api.genericSuccess(200); }); chakram.addProperty("deleteSuccess", function (respObj) { expect(respObj).to.be.an.api.genericSuccess(200); }); // take an object schema and the slug for the list of objects, and validate a list // response (e.g., GET /patients) chakram.addMethod("genericListSuccess", function (respObj, slug, itemSchema, countRequired) { expect(respObj).to.be.an.api.genericSuccess(200); // remove success from schema (in case it's present) itemSchema = JSON.parse(JSON.stringify(itemSchema)); if (typeof itemSchema.required !== "undefined" && itemSchema.required !== null) { var successIndex = itemSchema.required.indexOf("success"); if (successIndex >= 0) itemSchema.required.splice(successIndex, 1); } if (typeof itemSchema.properties !== "undefined" && itemSchema.properties !== null) { delete itemSchema.properties.success; } // require a count by default if (countRequired !== false) countRequired = true; var required = ["success", slug]; if (countRequired) required.push("count"); // build up schema for overall response /*eslint-disable key-spacing */ var schema = { required: required, properties: { success: { type: "boolean" }, count: { type: "number" } }, additionalProperties: !countRequired, // allow extra properties in place of count, definitions: itemSchema.definitions }; schema.properties[slug] = { type: "array", items: [itemSchema] }; /*eslint-enable key-spacing */ expect(respObj).to.have.schema(schema); }); });
before(function () { chakram.addProperty('smp', function(){}); chakram.addMethod('searchResponse', function (response, items) { expect(response).to.have.header('content-type', 'application/json'); expect(response).to.have.status(200); expect(response).to.have.schema({ properties: { status: { type: 'integer' }, data: { type: 'object', properties: { vendors: { type: 'array', maxItems: items, uniqueItems: true, items: { type: 'object', properties: { id: { type: 'integer' }, name: { type: 'object', properties: { value: { type: 'string' }, thumb: { type: 'string' }, matched: { type: 'array', items: { type: 'string' } } } } } } }, images: { type: 'array', maxItems: items, uniqueItems: true, items: { type: 'object', properties: { id: { type: 'integer' }, thumb: { type: 'string' }, gallery: { type: 'object', properties: { id: { type: 'integer' }, title: { type: 'string' } } } } } }, galleries: { type: 'array', maxItems: items, uniqueItems: true, items: { type: 'object', properties: { id: { type: 'integer' }, thumb: { type: 'string' }, title: { type: 'object', properties: { value: { type: 'string' }, matched: { type: 'array', items: { type: 'string' } } } } } } } } } } }); }); });