-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add SecurityRequirement(s) models (#588)
- Loading branch information
Showing
12 changed files
with
169 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { BaseModel } from "./base"; | ||
import type { SecuritySchemeInterface } from "./security-scheme"; | ||
|
||
export interface SecurityRequirementInterface extends BaseModel { | ||
scheme(): SecuritySchemeInterface | ||
scopes(): string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import type { Collection} from './collection'; | ||
import type { SecurityRequirementInterface } from './security-requirement'; | ||
|
||
export interface SecurityRequirementsInterface extends Collection<SecurityRequirementInterface> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { BaseModel } from '../base'; | ||
import type { SecuritySchemeInterface } from '../security-scheme'; | ||
import type { v2 } from "../../spec-types"; | ||
import { SecurityRequirementInterface } from 'models/security-requirement'; | ||
import { SecurityScheme } from './security-scheme'; | ||
|
||
export class SecurityRequirement extends BaseModel<{}, { id: string, scheme: SecuritySchemeInterface }> implements SecurityRequirementInterface { | ||
scheme(): SecuritySchemeInterface { | ||
return this.meta().scheme; | ||
} | ||
|
||
scopes() : string[] { | ||
return this._json as string[]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Collection } from '../collection'; | ||
|
||
import type { SecurityRequirementsInterface } from '../security-requirements'; | ||
import type { SecurityRequirementInterface } from '../security-requirement'; | ||
|
||
export class SecurityRequirements extends Collection<SecurityRequirementInterface> implements SecurityRequirementsInterface { | ||
override get(id: string): SecurityRequirementInterface | undefined { | ||
return this.collections.find(securityRequirement => securityRequirement.meta().id === id); | ||
} | ||
|
||
override has(id: string): boolean { | ||
return this.collections.some(securityRequirement => securityRequirement.meta().id === id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ModelMetadata } from '../../../src/models/base'; | ||
import { SecuritySchemeInterface } from '../../../src/models/security-scheme'; | ||
import { SecurityRequirement } from '../../../src/models/v2/security-requirement' | ||
import { SecurityScheme } from '../../../src/models/v2/security-scheme'; | ||
|
||
describe('SecurityRequirement model', function() { | ||
describe('.scheme()', function() { | ||
it('should return scheme', function() { | ||
const doc = {}; | ||
const expectedScheme = new SecurityScheme({type: "oauth2"}, {id: "test"} as any); | ||
const d = new SecurityRequirement(doc, ({ id: "test", scheme: expectedScheme } as any)); // TODO Pointer | ||
|
||
expect(d.scheme()).toBeInstanceOf(SecurityScheme); | ||
expect(d.scheme()).toEqual(expectedScheme); | ||
}); | ||
|
||
}) | ||
describe('.scopes()', function() { | ||
it('should return scopes', function() { | ||
const doc = ["scope_one"]; | ||
const d = new SecurityRequirement(doc); // TODO Pointer | ||
|
||
expect(d.scopes()).toEqual(doc); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { SecurityRequirements } from '../../../src/models/v2/security-requirements'; | ||
import { Operation } from '../../../src/models/v2/operation'; | ||
import { SecurityRequirement } from '../../../src/models/v2/security-requirement'; | ||
|
||
const operation = { | ||
operationId: 'test', | ||
}; | ||
const operationItem = new Operation(operation, { asyncapi: {} as any, pointer: '', id: 'test', action: 'publish' }); | ||
|
||
const requirementItem = new SecurityRequirement({}, {id: "test"} as any); | ||
|
||
describe('SecurityRequirements model', function () { | ||
describe('.isEmpty()', function () { | ||
it('should return true if collection is empty', function () { | ||
const requirements = new SecurityRequirements([]); | ||
expect(requirements.isEmpty()).toEqual(true); | ||
}); | ||
|
||
it('should return false if collection is not empty', function () { | ||
const requirements = new SecurityRequirements([requirementItem]); | ||
expect(requirements.isEmpty()).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('.get(id)', function () { | ||
it('should return a specific SecurityRequirement if it is present', function () { | ||
const requirements = new SecurityRequirements([requirementItem]); | ||
expect(requirements.get('test')).toBeTruthy(); | ||
}); | ||
|
||
it('should return undefined if specific SecurityRequirement is missing', function () { | ||
const requirements = new SecurityRequirements([]); | ||
expect(requirements.get('test')).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('.has(id)', function () { | ||
it('should return true if the said id is available', function () { | ||
const requirements = new SecurityRequirements([requirementItem]); | ||
expect(requirements.has('test')).toEqual(true); | ||
}) | ||
|
||
it('should return false if the SecurityRequirement id is missing', function () { | ||
const requirements = new SecurityRequirements([requirementItem]); | ||
expect(requirements.has('anotherId')).toEqual(false); | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters