Skip to content

Commit

Permalink
test(definitions): plain
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed May 21, 2024
1 parent e7097cc commit 66865d1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/definitions/3.0.0/security/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ describe('Security Schemes', () => {
require('./asymmetricEncryption/asymmetricEncryption.js');
require('./gssapi/gssapi.js');
require('./openIdconnect/openIdconnect.js');
require('./plain/plain.js');
});
1 change: 1 addition & 0 deletions test/definitions/3.0.0/security/plain/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions test/definitions/3.0.0/security/plain/extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "plain",
"description": "plain",
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "plain"
}
64 changes: 64 additions & 0 deletions test/definitions/3.0.0/security/plain/plain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const Ajv = require('ajv');
const assert = require('assert');
const addFormats = require('ajv-formats');
const fs = require('fs');

const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: '$id',
logger: false,
validateFormats: true,
strict: false,
});
addFormats(ajv);

const jsonSchema = require('../../../../../definitions/3.0.0/SaslPlainSecurityScheme.json');
const validator = ajv
.addMetaSchema(require('../../../../../definitions/3.0.0/schema.json'))
.addSchema(require('../../../../../definitions/3.0.0/specificationExtension.json'))
.compile(jsonSchema);

describe('Plain', () => {
it('empty', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === false, 'Plain with empty body is not valid');
assert(validator.errors[0].message === 'must have required property \'type\'');
assert(validator.errors.length === 1);
});

it('without required properties', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/without required properties.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === false, 'Plain without required properties is not valid');
assert(validator.errors[0].message === 'must have required property \'type\'');
assert(validator.errors.length === 1);
});

it('only required properties', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/only required properties.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === true, 'Plain is valid with only required properties');
});

it('extended', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/extended.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === true, 'Plain can be extended');
});

it('wrongly extended', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/wrongly extended.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === false, 'Plain is not valid when was wrongly extended');
assert(validator.errors[0].message === 'must NOT have additional properties');
assert(validator.errors[0].params.additionalProperty === 'ext-number');
assert(validator.errors.length === 1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"description": "plain"
}
10 changes: 10 additions & 0 deletions test/definitions/3.0.0/security/plain/wrongly extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "plain",
"description": "plain",
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
},
"ext-number": 1
}

0 comments on commit 66865d1

Please sign in to comment.