Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe('DelimitedStyleDeserializer', () => {
const delimitedStyleDeserializer = new DelimitedStyleDeserializer('|', HttpParamStyles.PipeDelimited);
describe('supports()', () => {
describe('style is supported', () => {
it('returns true', () => {
expect(delimitedStyleDeserializer.supports(HttpParamStyles.PipeDelimited)).toBe(true);
});
});
describe('style is not supported', () => {
it('returns false', () => {
// Force compile to pass for purpose of test.
// @ts-ignore
expect(delimitedStyleDeserializer.supports('invalid')).toBe(false);
});
});
});
it('returns static example', () => {
jest.spyOn(helpers, 'negotiateOptionsForInvalidRequest');
jest.spyOn(helpers, 'negotiateOptionsForValidRequest');
mock({
config: { dynamic: false },
resource: mockResource,
input: Object.assign({}, mockInput, { validations: [{ severity: DiagnosticSeverity.Warning }] }),
})(logger);
expect(helpers.negotiateOptionsForValidRequest).toHaveBeenCalled();
expect(helpers.negotiateOptionsForInvalidRequest).not.toHaveBeenCalled();
});
});
describe('oas3-valid-schema-example ', () => {
const s = new Spectral();
s.registerFormat('oas3', () => true);
s.setRules({
'oas3-valid-schema-example ': Object.assign(ruleset.rules['oas3-valid-schema-example '], {
recommended: true,
severity: DiagnosticSeverity.Error,
type: RuleType[ruleset.rules['oas3-valid-schema-example ']!.type],
}),
});
test('will pass when simple example is valid', async () => {
const results = await s.run({
openapi: '3.0.2',
components: {
schemas: {
xoxo: {
type: 'string',
example: 'doggie',
},
},
},
});
it('returns static example', () => {
jest.spyOn(helpers, 'negotiateOptionsForValidRequest');
jest.spyOn(helpers, 'negotiateOptionsForInvalidRequest');
mock({
config: { dynamic: false },
resource: mockResource,
input: Object.assign({}, mockInput, { validations: [{ severity: DiagnosticSeverity.Error }] }),
})(logger);
expect(helpers.negotiateOptionsForValidRequest).not.toHaveBeenCalled();
expect(helpers.negotiateOptionsForInvalidRequest).toHaveBeenCalled();
});
});
test('will fail when simple example is invalid', async () => {
const results = await s.run({
openapi: '3.0.2',
[path]: {
xoxo: {
schema: {
type: 'string',
example: 123,
},
},
},
});
expect(results).toEqual([
expect.objectContaining({
severity: DiagnosticSeverity.Error,
code: ruleName,
message: '"schema.example" property type should be string',
}),
]);
});
it('logs error', () => {
violationLogger(logger)({ severity: DiagnosticSeverity.Error, message: 'Test' });
expect(logger.error).toHaveBeenCalledWith({ name: 'VALIDATOR' }, 'Violation: Test');
expect(logger.warn).not.toHaveBeenCalled();
expect(logger.info).not.toHaveBeenCalled();
});
});
it('returns validation error', () => {
assertLeft(
httpPathValidator.validate({}, [{ name: 'aParam', style: HttpParamStyles.Simple, required: true }]),
error =>
expect(error).toEqual([
{
code: 'required',
message: "should have required property 'aparam'",
path: ['path'],
severity: 0,
},
])
);
});
});
it('returns false', () => {
expect(labelStyleDeserializer.supports(HttpParamStyles.Simple)).toBe(false);
});
});
it('generates simple style', () => {
assertRight(
createExamplePath({
id: '123',
path: '/path/{p}',
method: 'get',
request: { path: [{ name: 'p', style: HttpParamStyles.Simple, examples: [{ key: 'foo', value: 'test' }] }] },
responses: [{ code: '200' }],
}),
r => expect(r).toEqual('/path/test')
);
});
it('omits schema validation', () => {
jest.spyOn(registry, 'get').mockReturnValueOnce(undefined);
const param: IHttpPathParam = {
name: 'param',
style: HttpParamStyles.Simple,
schema: { type: 'number' },
};
assertRight(httpPathValidator.validate({ param: 'abc' }, [param]));
expect(validateAgainstSchemaModule.validateAgainstSchema).toReturnWith(O.none);
});
});