UNPKG

swagger-tests

Version:

A typescript framework for testing an api against a swagger 3.0 json file.

75 lines 2.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const underscore_1 = require("underscore"); const constants_1 = require("../constants"); const underscore_string_1 = require("underscore.string"); const faker_1 = require("faker"); class AttributeSupport { constructor(apiSupport) { this.apiSupport = apiSupport; } async valid() { return underscore_1.pick(this.faker(), this.required()); } async invalid() { let body = this.faker(); body['invalid_parameter'] = faker_1.lorem.words(10); return body; } schema() { const schema = constants_1.API.components.schemas[this.apiSupport.modelName]; return schema || this.sti_schema(); } sti_schema() { const schemaName = underscore_1.last(constants_1.API.paths[`/${underscore_string_1.underscored(this.apiSupport.modelName)}s/{id}/`].get.responses['200'].content['application/json; charset=utf-8'].schema['oneOf'][0]['$ref'].split('/')); return constants_1.API.components.schemas[schemaName]; } required() { return this.schema().required; } all() { return this.schema().properties; } faker() { let fake = {}; underscore_1.map(this.all(), (schema, attribute) => { if (underscore_string_1.endsWith(attribute, '_id') && schema.type == 'string') { fake[attribute] = faker_1.random.uuid(); } else if (attribute == 'first_name') { fake[attribute] = faker_1.name.firstName(); } else if (attribute == 'last_name') { fake[attribute] = faker_1.name.lastName(); } else if (attribute == 'email') { fake[attribute] = faker_1.internet.exampleEmail(); } else if (schema.type == 'string' && (schema.format === 'string' || schema.format === undefined)) { fake[attribute] = faker_1.lorem.words(10); } else if (schema.type == 'integer') { fake[attribute] = faker_1.random.number({ min: 1, max: 999999 }); } else if (schema.type == 'integer') { fake[attribute] = faker_1.random.number({ min: 1, max: 999999 }); } else if (schema.type == 'number') { fake[attribute] = faker_1.finance.amount(1, 999999, 3); } else if (schema.format == 'date-time') { fake[attribute] = new Date(); } else if (schema.format == 'date') { fake[attribute] = faker_1.date.recent(10); } else { fake[attribute] = faker_1.lorem.words(10); } return fake[attribute]; }); return fake; } } exports.AttributeSupport = AttributeSupport; //# sourceMappingURL=attribute.support.js.map