mares-swagger-spec-maker
Version:
mares framework ���조에 맞춰서openapi spec을 combine 해주는 모듈입니다.
187 lines (148 loc) • 5.88 kB
JavaScript
const should = require('should')
const utils = require('./utils')
suite('mares swagger spec maker', () => {
suiteSetup(async () => {
this.root = require('./mocks/.root-api-spec')
this.bc = require('./mocks/.context-api-spec')
this.paths = require('./mocks/.spec')
this.version = require('./mocks/.spec')
})
test('convertDomainToSchema and updateRequiredType methods test', async () => {
let Aggregates = require('./domain-mock')
let schemas = {}
for (let k in Aggregates) {
let domains = Aggregates[k]
for (let d in domains) {
let schema = utils.convertDomainToSchema(domains[d])
let props = schema.properties
let schemaName = d
schemaName.should.exactly('Root')
schema.should.have.property('type', 'object')
schema.should.have.property('properties')
schema.properties.should.have.properties(['id', 'authorId', 'templateId', 'createdAt', 'bool', 'enum', 'float', 'payload', 'receivers', 'author'])
props.id.should.have.property('type', 'integer')
props.authorId.should.have.property('type', '?integer')
props.templateId.should.have.property('type', 'integer')
props.memo.should.have.property('type', 'string')
props.createdAt.should.have.property('type', 'string')
props.createdAt.should.have.property('format', 'date')
props.bool.should.have.property('type', 'boolean')
props.enum.should.have.property('type', '?string')
props.enum.should.have.property('enum')
props.enum.enum.should.containEql('a')
props.enum.enum.should.containEql('b')
props.float.should.have.property('type', 'number')
props.payload.should.have.property('type', 'object')
props.payload.should.have.property('additionalProperties', true)
props.receivers.should.have.property('type', '?array')
props.receivers.should.have.property('items')
props.receivers.items.should.have.property('type', 'object')
props.receivers.items.should.have.property('properties')
props.receivers.items.properties.should.have.property('phoneNum')
props.receivers.items.properties.should.have.property('body')
props.receivers.items.properties.should.have.property('status')
props.receivers.items.properties.status.should.have.property('type', '?string')
props.receivers.items.properties.should.have.property('createdAt')
props.receivers.items.properties.should.have.property('updatedAt')
schema = utils.updateRequiredType(schema)
props.authorId.should.have.property('type', 'integer')
props.receivers.should.have.property('type', 'array')
schema.required.should.instanceOf(Array)
schema.required.should.not.containEql('authorId')
schema.required.should.not.containEql('receivers')
schema.required.should.containEql('templateId')
schema.required.should.containEql('id')
props.receivers.items.required.should.containEql('phoneNum')
props.receivers.items.required.should.containEql('body')
props.receivers.items.required.should.containEql('createdAt')
props.receivers.items.required.should.containEql('updatedAt')
schemas[schemaName] = schema
}
}
this.schemas = schemas
})
test('addSchemaToComponents method test', async () => {
let schemas = this.root.components.schemas
this.root.components = utils.addSchemasToComponents(this.root.components, this.schemas)
schemas.should.have.property('SingleError')
schemas.SingleError.should.have.property('type', 'object')
schemas.Root.should.have.property('type', 'object')
})
test('addSecurityToPath method test', async () => {
let paths = utils.addSecurityToPath(this.paths, this.bc)
for (let key in paths) {
let path = paths[key]
for (let methodKey in path) {
let method = path[methodKey]
should.exist(method.security)
method.security.should.instanceOf(Array)
}
}
})
test('addVersionToPath method test', async () => {
let paths = utils.addVersionToPath(this.paths, this.version)
for (let key in paths) {
let path = paths[key]
for (let methodKey in path) {
path[methodKey].parameters.should.containEql(this.version)
}
}
})
test('mergeRootAndBoundedContext method test', async () => {
let root = utils.mergeRootAndBoundedContext(this.root, this.bc)
should.exist(root.components.schemas.Test)
root.components.schemas.Test.should.containEql({
type: 'object',
properties: {
id: {
type: 'integer'
},
name: {
type: 'string'
}
}
})
root.components.schemas.SingleError.should.containEql({
type: 'object',
description: '단일 에러일 경우 공통 형태',
properties: {
code: {
type: 'string',
description: '에러코드, 영문 숫자 혼합',
example: 'invalidId'
},
msg: {
type: 'string',
description: '에러코드의 국가별 번역 메세지, 헤더의 언어코드를 읽고 해당 언어코드의 해당하는 메세지로 번역. 만일 지원하지 않는 언어코드라면 기본 언어값으로 설정된다.',
example: '잘못된 아이디 입니다.'
}
}
})
})
test('mergeRootAndPath method test', async () => {
let root = utils.mergeRootAndPath(this.root, this.paths)
root.paths.should.have.property('/{version}/templates')
root.paths['/{version}/templates'].should.have.property('get')
root.paths['/{version}/templates'].should.not.have.property('delete')
})
test('addResourcePrefixInPaths method test', async () => {
const prefix = '/apis'
let root = {
paths: {
'/root/test': {
get: {}
},
'/root/test/:id': {
post: {}
}
}
}
root = utils.addResourcePrefixInPaths(root.paths, prefix)
root.should.have.property(prefix + '/root/test')
root.should.have.property(prefix + '/root/test/:id')
root.should.not.have.property('/root/test')
root.should.not.have.property('/root/test/:id')
})
suiteTeardown(() => {
})
})