Exemplo n.º 1
0
    beforeAll(async (done) => {
        client = await pool.connect();
        user = await createUser(client, userFixture);
        uri = getApiUri(user.usr_id);

        chakram.addMethod("body", function (respObj, text) {
            const body = respObj.response.body;
            this.assert(body.indexOf(text) > -1, `Expected '${text}' to be found within responses body ${body}`);
        });
        done();
    });
Exemplo n.º 2
0
 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'
                 }
               }
             }
           }
         }
       }
     });
   });
 });
function createCustomMethods(apiFolderNames, next) {
    // A custom error method can also be added, to assert a valid error

    //custom method to assert a successful call of an API
    chakram.addMethod("success", function (responseObject, apiName, expectedOutput) {
        var responseSchema = require(path.join(process.cwd(), apiFolderName, apiName, "Expected Schema.json"));

        //Parsing the body to a json object, if it isnt one.
        if (typeof (responseObject.body) !== 'object') {
            responseObject.body = JSON.parse(responseObject.body);
        }

        //these expected values can also be a url, that has the json to be expected
        expect(responseObject).to.have.schema(responseSchema);
        expect(responseObject).to.have.status(200);
        expect(responseObject).to.comprise.of.json(expectedOutput);
    });

    next(null);
}
Exemplo n.º 4
0
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);
    });
});
Exemplo n.º 5
0
 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'
                         }
                       }
                     }
                   }
                 }
               }
             }
           }
         }
       }
     });
   });
 });