Skip to content

Commit

Permalink
test(definitions): oauth2
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed May 22, 2024
1 parent 6f30129 commit 32c4733
Show file tree
Hide file tree
Showing 32 changed files with 615 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 @@ -14,4 +14,5 @@ describe('Security Schemes', () => {
require('./httpApiKey/httpApiKey.js');
require('./httpBearer/httpBearer.js');
require('./httpBasic/httpBasic.js');
require('./oauth2/oauth2.js');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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/oauth2Flow.json');
const validator = ajv
.addMetaSchema(require('../../../../../../../definitions/3.0.0/schema.json'))
.addMetaSchema(require('../../../../../../../definitions/3.0.0/oauth2Scopes.json'))
.addSchema(require('../../../../../../../definitions/3.0.0/specificationExtension.json'))
.compile(jsonSchema);

const schemaName = 'Authorization Code Flow';

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

assert(validationResult === true, `${schemaName} is valid with empty body`);
});

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

assert(validationResult === true, `${schemaName} is valid without required properties`);
});

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

assert(validationResult === true, `${schemaName} 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, `${schemaName} 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, `${schemaName} 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 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"authorizationUrl": "https://example.com/api/oauth/dialog",
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
},
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"authorizationUrl": "https://example.com/api/oauth/dialog",
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"authorizationUrl": "https://example.com/api/oauth/dialog",
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
},
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
},
"ext-number": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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/oauth2Flow.json');
const validator = ajv
.addMetaSchema(require('../../../../../../../definitions/3.0.0/schema.json'))
.addMetaSchema(require('../../../../../../../definitions/3.0.0/oauth2Scopes.json'))
.addSchema(require('../../../../../../../definitions/3.0.0/specificationExtension.json'))
.compile(jsonSchema);

const schemaName = 'Client Credentials Flow';

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

assert(validationResult === true, `${schemaName} is valid with empty body`);
});

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

assert(validationResult === true, `${schemaName} is valid without required properties`);
});

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

assert(validationResult === true, `${schemaName} 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, `${schemaName} 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, `${schemaName} 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 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
},
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
},
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
},
"ext-number": 1
}
1 change: 1 addition & 0 deletions test/definitions/3.0.0/security/oauth2/flows/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
45 changes: 45 additions & 0 deletions test/definitions/3.0.0/security/oauth2/flows/extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"type": "oauth2",
"description": "oauth2",
"flows": {
"authorizationCode": {
"authorizationUrl": "https://example.com/api/oauth/dialog",
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
},
"clientCredentials": {
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
},
"implicit": {
"authorizationUrl": "https://example.com/api/oauth/dialog",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
},
"password": {
"tokenUrl": "https://example.com/api/oauth/token",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
},
"scopes": [ "write:pets", "read:pets" ],
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"authorizationUrl": "https://example.com/api/oauth/dialog",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
},
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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/oauth2Flow.json');
const validator = ajv
.addMetaSchema(require('../../../../../../../definitions/3.0.0/schema.json'))
.addMetaSchema(require('../../../../../../../definitions/3.0.0/oauth2Scopes.json'))
.addSchema(require('../../../../../../../definitions/3.0.0/specificationExtension.json'))
.compile(jsonSchema);

const schemaName = 'Implicit Flow';

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

assert(validationResult === true, `${schemaName} is valid with empty body`);
});

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

assert(validationResult === true, `${schemaName} is valid without required properties`);
});

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

assert(validationResult === true, `${schemaName} 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, `${schemaName} 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, `${schemaName} 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 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"authorizationUrl": "https://example.com/api/oauth/dialog",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"authorizationUrl": "https://example.com/api/oauth/dialog",
"refreshUrl": "https://example.com/api/oauth/refresh",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
},
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
},
"ext-number": 1
}
Loading

0 comments on commit 32c4733

Please sign in to comment.