dareid / chakram

REST API test framework. BDD and exploits promises
http://dareid.github.io/chakram/
MIT License
908 stars 99 forks source link

Invalid validation keywords and types #27

Open segaz2002 opened 8 years ago

segaz2002 commented 8 years ago
    it("should list all avilable players", function () {
        var responseStructure = {
            type: "array",
            properties:{
                enabled : {type:"string"},  //This is expected to be an integer, but test passes
                defaultLanguage :{type:"integer"},
                defaultCurrency :{type:"integer"},
                lastname :{type:"string"},
                firstname:{type:"string"},
                passwordSalt:{type:"string"},
                password:{type:"string"},
                email:{type:"string"},
                id:{type:"string"} //Also this, expected to be an integer, but it passes the test
            }
        }
        var response = chakram.get(endpoint);
        return expect(response).to.have.schema(playerDataStructure);

I observed that the properties are not checked even when the properties expected doesn't match the types in the reponse, the test still passes.

dareid commented 8 years ago

Thanks for the issue, and sorry for the late response. I believe your JSON schema is invalid, I don't believe you can define 'properties' for arrays. However, ideally, this would have been flagged. Chakram uses tv4 for its schema validation - I will adjust the title of this issue to provide alerts for invalid type and validation keyword combos. Hopefully it is something which could be added to tv4.

segaz2002 commented 8 years ago

Thanks I will re check. Encountered this when I first met chakram. On 6 Jan 2016 16:50, "Daniel Reid" notifications@github.com wrote:

Thanks for the issue, and sorry for the late response. I believe your JSON schema is invalid, I don't believe you can define 'properties' for arrays. However, ideally, this would have been flagged. Chakram uses tv4 for its schema validation - I will adjust the title of this issue to provide alerts for invalid type and validation keyword combos. Hopefully it is something which could be added to tv4.

— Reply to this email directly or view it on GitHub https://github.com/dareid/chakram/issues/27#issuecomment-169343362.

shahankit2812 commented 7 years ago

I suppose you need to add the bold part in your schema . Check last example : http://json-schema.org/example1.html

it("should list all avilable players", function () { var responseStructure = { type: "array", items:{ properties:{ enabled : {type:"string"}, //This is expected to be an integer, but test passes defaultLanguage :{type:"integer"}, defaultCurrency :{type:"integer"}, lastname :{type:"string"}, firstname:{type:"string"}, passwordSalt:{type:"string"}, password:{type:"string"}, email:{type:"string"}, id:{type:"string"} //Also this, expected to be an integer, but it passes the test } } } var response = chakram.get(endpoint); return expect(response).to.have.schema(playerDataStructure);

zephrose commented 7 years ago

I just solved this issue for my own needs earlier today. I was looking around for a similar issue and realized I was building out the JSON object incorrectly. Below are some samples I've created to solve for this. Note the structure specifies a type object. I thought I had to specify an array type as well but was met with errors looking for something else.

var responseStrct  = {
        type: "object",
        required: [
                    "rownumber", 
                    "field_1",
                    "field_2",
                    "field_3"
                ],
                properties: {
                    rownumber: {"type": "integer"},
                    field_1: {type: "number"},
                    field_2: {type: "string"},
                    field_3: {type: "number"}
                }               
       }

This function creates and uses a session authToken before executing the intended API call.

it("should return all required fields", function () {
        return chakram.request("GET", authRequest)
        .then(function(authRes) {
            var authTokenRaw = authRes.body['authToken'];
            var sessionReq = chakram.request("GET", apiRequest, {headers : {'Authorization' : authTokenRaw}});
            return expect(sessionReq).to.have.schema('[0]', responseStrct);
        }); 
    });

Hope this helps.