Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missed methods to the SecurityScheme model #669

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/models/security-requirement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { BaseModel } from './base';
import type { SecuritySchemeInterface } from './security-scheme';

export interface SecurityRequirementInterface extends BaseModel {
scheme(): SecuritySchemeInterface
scheme(): SecuritySchemeInterface;
scopes(): string[];
}
16 changes: 11 additions & 5 deletions src/models/security-scheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import type { OAuthFlowsInterface } from './oauth-flows';
import type { DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';

export interface SecuritySchemeInterface extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {
id(): string
id(): string;
type(): string;
hasName(): boolean;
name(): string | undefined;
hasIn(): boolean;
in(): string | undefined;
hasScheme(): boolean;
scheme(): string | undefined;
hasBearerFormat(): boolean;
bearerFormat(): string | undefined;
openIdConnectUrl(): string | undefined;
scheme(): string | undefined;
hasFlows(): boolean;
flows(): OAuthFlowsInterface | undefined;
type(): string;
in(): string | undefined;
hasOpenIdConnectUrl(): boolean;
openIdConnectUrl(): string | undefined;
}
44 changes: 34 additions & 10 deletions src/models/v2/security-scheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class SecurityScheme extends BaseModel<v2.SecuritySchemeObject, { id: str
return this._meta.id;
}

type(): v2.SecuritySchemeType {
return this._json.type;
}

hasDescription(): boolean {
return hasDescription(this);
}
Expand All @@ -22,33 +26,53 @@ export class SecurityScheme extends BaseModel<v2.SecuritySchemeObject, { id: str
return description(this);
}

hasBearerFormat(): boolean {
return !!this._json.bearerFormat;
hasName(): boolean {
return !!this._json.name;
}

bearerFormat(): string | undefined {
return this._json.bearerFormat;
name(): string | undefined {
return this._json.name;
}

openIdConnectUrl(): string | undefined {
return this._json.openIdConnectUrl;
hasIn(): boolean {
return !!this._json.in;
}

in(): v2.SecuritySchemaLocation | undefined {
return this._json.in;
}

hasScheme(): boolean {
return !!this._json.scheme;
}

scheme(): string | undefined {
return this._json.scheme;
}

hasBearerFormat(): boolean {
return !!this._json.bearerFormat;
}

bearerFormat(): string | undefined {
return this._json.bearerFormat;
}

hasFlows(): boolean {
return !!this._json.flows;
}

flows(): OAuthFlowsInterface | undefined {
if (!this._json.flows) return undefined;
return new OAuthFlows(this._json.flows);
}

type(): v2.SecuritySchemeType {
return this._json.type;
hasOpenIdConnectUrl(): boolean {
return !!this._json.openIdConnectUrl;
}

in(): v2.SecuritySchemaLocation | undefined {
return this._json.in;
openIdConnectUrl(): string | undefined {
return this._json.openIdConnectUrl;
}

extensions(): ExtensionsInterface {
Expand Down
1 change: 1 addition & 0 deletions test/models/v2/security-requirement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('SecurityRequirement model', function() {
expect(d.scheme()).toEqual(expectedScheme);
});
});

describe('.scopes()', function() {
it('should return scopes', function() {
const scopes = ['scope_one'];
Expand Down
112 changes: 101 additions & 11 deletions test/models/v2/security-scheme.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { SecurityScheme } from '../../../src/models/v2/security-scheme';
import { OAuthFlows } from '../../../src/models/v2/oauth-flows';
import { assertDescription, assertExtensions } from './utils';

import type { v2 } from '../../../src/spec-types';

const doc1: v2.SecuritySchemeObject = {
type: 'http',
name: 'api_key',
in: 'header',
scheme: 'bearer',
bearerFormat: 'JWT',
Expand All @@ -26,38 +28,103 @@ const emptyItem = new SecurityScheme({ type: 'X509' });
describe('Security Scheme', function () {
describe('.id()', function () {
it('should return name if present', function () {
expect(sc1.id()).toMatch('api_key');
expect(sc1.id()).toEqual('api_key');
});
});

describe('.type()', function () {
it('should return type when it is present', function () {
expect(sc1.type()).toMatch(doc1.type);
expect(sc1.type()).toEqual(doc1.type);
});
});

describe('.hasBearerFormat()', function () {
it('should return true if bearerFormat is present', function () {
expect(sc1.hasBearerFormat()).toBeTruthy();
describe('.hasName()', function () {
it('should return true if name is present', function () {
const data = new SecurityScheme({ type: 'apiKey', name: 'name' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasName()).toEqual(true);
});

it('should return false if bearerFormat is not present', function () {
expect(emptyItem.hasBearerFormat()).toBeFalsy();
it('should return false if name is not present', function () {
const data = new SecurityScheme({ type: 'apiKey' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasName()).toEqual(false);
});
});

describe('.name()', function () {
it('should return name if present', function () {
expect(sc1.name()).toEqual(doc1.name);
});
});

describe('.hasIn()', function () {
it('should return true if in is present', function () {
const data = new SecurityScheme({ type: 'apiKey', in: 'header' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasIn()).toEqual(true);
});

it('should return false if in is not present', function () {
const data = new SecurityScheme({ type: 'apiKey' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasIn()).toEqual(false);
});
});

describe('.in()', function () {
it('should return in if present', function () {
expect(sc1.in()).toMatch(doc1.in as string);
expect(sc1.in()).toEqual(doc1.in);
expect(emptyItem.in()).toBeUndefined();
});
});

describe('.openIdConnectUrl()', function () {
it('should return openIdConnectUrl value', function () {
expect(sc1.openIdConnectUrl()).toMatch(doc1.openIdConnectUrl as string);
describe('.hasBearerFormat()', function () {
it('should return true if bearerFormat is present', function () {
const data = new SecurityScheme({ type: 'apiKey', bearerFormat: 'bearerFormat' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasBearerFormat()).toEqual(true);
});

it('should return false if bearerFormat is not present', function () {
const data = new SecurityScheme({ type: 'apiKey' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasBearerFormat()).toEqual(false);
});
});

describe('.bearerFormat()', function () {
it('should return bearerFormat if present', function () {
expect(sc1.bearerFormat()).toEqual(doc1.bearerFormat);
expect(emptyItem.bearerFormat()).toBeUndefined();
});
});

describe('.hasScheme()', function () {
it('should return true if bearerFormat is present', function () {
const data = new SecurityScheme({ type: 'apiKey', scheme: 'scheme' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasScheme()).toEqual(true);
});

it('should return false if bearerFormat is not present', function () {
const data = new SecurityScheme({ type: 'apiKey' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasScheme()).toEqual(false);
});
});

describe('.scheme()', function () {
it('should return scheme if present', function () {
expect(sc1.scheme()).toEqual(doc1.scheme);
expect(emptyItem.scheme()).toBeUndefined();
});
});

describe('.hasFlows()', function () {
it('should return true if flows is present', function () {
const data = new SecurityScheme({ type: 'apiKey', flows: {} }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasFlows()).toEqual(true);
});

it('should return false if flows is not present', function () {
const data = new SecurityScheme({ type: 'apiKey' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasFlows()).toEqual(false);
});
});

describe('.flows()', function () {
it('should return undefined if flow object is not present', function () {
expect(emptyItem.flows()).toBeUndefined();
Expand All @@ -67,4 +134,27 @@ describe('Security Scheme', function () {
expect(sc1.flows() instanceof OAuthFlows).toBeTruthy();
});
});

describe('.hasOpenIdConnectUrl()', function () {
it('should return true if openIdConnectUrl is present', function () {
const data = new SecurityScheme({ type: 'apiKey', openIdConnectUrl: 'openIdConnectUrl' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasOpenIdConnectUrl()).toEqual(true);
});

it('should return false if openIdConnectUrl is not present', function () {
const data = new SecurityScheme({ type: 'apiKey' }, { asyncapi: {} as any, pointer: '', id: 'security-model' });
expect(data.hasOpenIdConnectUrl()).toEqual(false);
});
});

describe('.openIdConnectUrl()', function () {
it('should return openIdConnectUrl value', function () {
expect(sc1.openIdConnectUrl()).toMatch(doc1.openIdConnectUrl as string);
});
});

describe('mixins inheritance', function () {
assertDescription(SecurityScheme);
assertExtensions(SecurityScheme);
});
});
15 changes: 14 additions & 1 deletion test/models/v2/server-variable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ServerVariable } from '../../../src/models/v2/server-variable';
import { assertDescription, assertExtensions } from './utils';

const doc = {
description: 'Secure connection (TLS) is available through port 8883.',
default: '1883',
enum: ['1883', '8883']
enum: ['1883', '8883'],
examples: ['1883', '8883'],
};

const sv = new ServerVariable(doc, { asyncapi: {} as any, pointer: '', id: 'doc' });
Expand Down Expand Up @@ -36,4 +38,15 @@ describe('Server Variable ', function() {
expect(sv.allowedValues()).toEqual(doc.enum);
});
});

describe('.examples()', function() {
it('should return array', function() {
expect(sv.examples()).toEqual(doc.examples);
});
});

describe('mixins inheritance', function () {
assertDescription(ServerVariable);
assertExtensions(ServerVariable);
});
});