Skip to content

Commit

Permalink
resolve Coding-Coach#55 - Restrict avatar image URL to https in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moshfeu committed Mar 12, 2019
1 parent 83a7a04 commit b74cdce
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Just go to https://mentors.codingcoach.io/ and find her / him.
{
"id": "your email",
"name": "your name", // minLength: 2
"avatar": "https://link-to-your/avatar.jpg", // url
"avatar": "https://link-to-your/avatar.jpg", // url, must start with https://
"title": "NodeJS developer", // minLength: 2, maxLength: 30
"description": "Hi, I'm NodeJs developer", // minLength: 5, maxLength: 80 optional
"country": "Israel", // Full name. please avoid synonyms (check if it's not already exist)
Expand Down
143 changes: 82 additions & 61 deletions src/__tests__/mentors.json-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,92 @@ import lists from '../lists.json';

import generateLists from '../../scripts/generate-lists';

it('should mentors json contains all fields', () => {
var ajv = new Ajv({ removeAdditional: false });
const valid = ajv.validate({
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "email"
},
"name": {
"type": "string",
"minLength": 2
},
"avatar": {
"type": "string",
"format": "uri"
},
"title": {
"type": "string",
"minLength": 2,
"maxLength": 35
},
"description": {
"type": "string",
"minLength": 5,
"maxLength": 80
},
"country": {
expect.extend({
toBeValid(isValid, errorMessage) {
return {
message: () => isValid ? '' : errorMessage,
pass: isValid
}
}
});

var ajv = new Ajv({ removeAdditional: false });

const validate = function (schema, uri) {
validate.errors = [{keyword: 'secured', message: 'avatar url must be "https" schema', params: {keyword: 'secured'}}];
return uri.indexOf('https://') === 0;
};

ajv.addKeyword('securedUrl', {
validate,
errors: true
});

const mentorSchema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "email"
},
"name": {
"type": "string",
"minLength": 2
},
"avatar": {
"type": "string",
"format": "uri",
"securedUrl": true,
},
"title": {
"type": "string",
"minLength": 2,
"maxLength": 35
},
"description": {
"type": "string",
"minLength": 5,
"maxLength": 80
},
"country": {
"type": "string",
},
"tags": {
"type": "array",
"minItems": 1,
"maxItems": 5,
"items": {
"type": "string",
},
"tags": {
"type": "array",
"minItems": 1,
"maxItems": 5,
"items": {
"type": "string",
"minLength": 1,
"maxLength": 15
}
},
"channels": {
"type": "array",
"minItems": 1,
"maxItems": 3,
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["slack", "email", "linkedin", "facebook", "twitter"]
}
},
"required": ["type", "id"]
}
"minLength": 1,
"maxLength": 15
}
},
"required": ["id", "name", "avatar", "title", "country", "tags", "channels"]
}
}, mentors);
if (ajv.errors) {
console.log(ajv.errors);
"channels": {
"type": "array",
"minItems": 1,
"maxItems": 3,
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["slack", "email", "linkedin", "facebook", "twitter"]
}
},
"required": ["type", "id"]
}
}
},
"required": ["id", "name", "avatar", "title", "country", "tags", "channels"]
}
expect(valid).toBeTruthy();
}

it('should mentors shema should be valid', () => {
const valid = ajv.validate(mentorSchema, mentors);
const errorMessage = (ajv.errors || []).map(error => error.message).join('\n');
expect(valid).toBeValid(errorMessage);
});

it('should lists be synced with the mentors details', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/mentors.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
{
"id": "[email protected]",
"name": "Andrew Miracle",
"avatar": "http://andrewmiracle.com/images/headshot.jpg",
"avatar": "https://andrewmiracle.com/images/headshot.jpg",
"title": "Software Developer at @sendboxng",
"description": "When the journey looks rough, I promise you won't have to do it alone",
"country": "Nigeria",
Expand Down

0 comments on commit b74cdce

Please sign in to comment.