Skip to content

Commit

Permalink
Create unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mavbraz committed Sep 12, 2023
1 parent d300a2f commit 09605e0
Show file tree
Hide file tree
Showing 9 changed files with 703 additions and 2 deletions.
34 changes: 34 additions & 0 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@typescript-eslint/eslint-plugin": "6.1.0",
"@typescript-eslint/parser": "6.1.0",
"chai": "4.3.7",
"chai-exclude": "^2.1.0",
"eslint": "8.45.0",
"eslint-config-oclif": "4.0.0",
"eslint-config-oclif-typescript": "1.0.3",
Expand Down
15 changes: 13 additions & 2 deletions packages/cli/src/commands/export.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Environment } from '@mockoon/commons';
import { OpenAPIConverter } from '@mockoon/commons-server';
import { Command, Flags } from '@oclif/core';
import { promises as fs } from 'fs';
import { parseDataFiles } from '../libs/data';
import { CLIMessages } from '../constants/cli-messages.constants';
import { parseDataFiles, prepareEnvironment } from '../libs/data';

export default class Export extends Command {
public static description =
Expand Down Expand Up @@ -35,9 +37,18 @@ export default class Export extends Command {

try {
const parsedEnvironments = await parseDataFiles([userFlags.input]);

if (parsedEnvironments.length !== 1) {
this.error(CLIMessages.ONLY_ONE_ENVIRONMENT_ALLOWED);
}

const environment: Environment = await prepareEnvironment({
environment: parsedEnvironments[0].environment,
userOptions: {}
});
const openApiConverter = new OpenAPIConverter();
const data: string = await openApiConverter.convertToOpenAPIV3(
parsedEnvironments[0].environment,
environment,
userFlags.prettify
);

Expand Down
178 changes: 178 additions & 0 deletions packages/cli/test/data/envs/petstore-imported.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"uuid": "2721eb99-7031-40ae-8725-0c37edc3976a",
"lastMigration": 28,
"name": "Swagger Petstore",
"endpointPrefix": "v1",
"latency": 0,
"port": 3000,
"hostname": "",
"folders": [],
"routes": [
{
"uuid": "b054b387-eb2d-4ae4-8891-fde9c7e3e4c9",
"type": "http",
"documentation": "List all pets",
"method": "get",
"endpoint": "pets",
"responses": [
{
"uuid": "a1224237-384c-43cb-b345-177ace428faa",
"body": "[\n {\n \"id\": {{faker 'datatype.number'}},\n \"name\": \"\",\n \"tag\": \"\"\n }\n]",
"latency": 0,
"statusCode": 200,
"label": "A paged array of pets",
"headers": [
{ "key": "Content-Type", "value": "application/json" },
{ "key": "x-next", "value": "" }
],
"bodyType": "INLINE",
"filePath": "",
"databucketID": "",
"sendFileAsBody": false,
"rules": [],
"rulesOperator": "OR",
"disableTemplating": false,
"fallbackTo404": false,
"default": true,
"crudKey": "id"
},
{
"uuid": "02027f69-e78e-4a92-938c-058bbaed76d0",
"body": "{\n \"code\": {{faker 'datatype.number'}},\n \"message\": \"\"\n}",
"latency": 0,
"statusCode": 200,
"label": "unexpected error",
"headers": [{ "key": "Content-Type", "value": "application/json" }],
"bodyType": "INLINE",
"filePath": "",
"databucketID": "",
"sendFileAsBody": false,
"rules": [],
"rulesOperator": "OR",
"disableTemplating": false,
"fallbackTo404": false,
"default": false,
"crudKey": "id"
}
],
"enabled": true,
"responseMode": null
},
{
"uuid": "094031a7-93fc-4659-82b4-58c8c9de10b0",
"type": "http",
"documentation": "Create a pet",
"method": "post",
"endpoint": "pets",
"responses": [
{
"uuid": "2aa26b10-4d66-48d7-86d3-50d260032752",
"body": "",
"latency": 0,
"statusCode": 201,
"label": "Null response",
"headers": [{ "key": "Content-Type", "value": "application/json" }],
"bodyType": "INLINE",
"filePath": "",
"databucketID": "",
"sendFileAsBody": false,
"rules": [],
"rulesOperator": "OR",
"disableTemplating": false,
"fallbackTo404": false,
"default": true,
"crudKey": "id"
},
{
"uuid": "fc1bce62-8926-456d-916c-e8cf6601bf14",
"body": "{\n \"code\": {{faker 'datatype.number'}},\n \"message\": \"\"\n}",
"latency": 0,
"statusCode": 200,
"label": "unexpected error",
"headers": [{ "key": "Content-Type", "value": "application/json" }],
"bodyType": "INLINE",
"filePath": "",
"databucketID": "",
"sendFileAsBody": false,
"rules": [],
"rulesOperator": "OR",
"disableTemplating": false,
"fallbackTo404": false,
"default": false,
"crudKey": "id"
}
],
"enabled": true,
"responseMode": null
},
{
"uuid": "4b9356a9-d656-4ea8-acf3-c254fb9d6616",
"type": "http",
"documentation": "Info for a specific pet",
"method": "get",
"endpoint": "pets/:petId",
"responses": [
{
"uuid": "73ec0850-ca23-4488-ac97-18a39257e0b5",
"body": "{\n \"id\": {{faker 'datatype.number'}},\n \"name\": \"\",\n \"tag\": \"\"\n}",
"latency": 0,
"statusCode": 200,
"label": "Expected response to a valid request",
"headers": [{ "key": "Content-Type", "value": "application/json" }],
"bodyType": "INLINE",
"filePath": "",
"databucketID": "",
"sendFileAsBody": false,
"rules": [],
"rulesOperator": "OR",
"disableTemplating": false,
"fallbackTo404": false,
"default": true,
"crudKey": "id"
},
{
"uuid": "34b404ae-2a7c-4d74-9647-1f62d380f699",
"body": "{\n \"code\": {{faker 'datatype.number'}},\n \"message\": \"\"\n}",
"latency": 0,
"statusCode": 200,
"label": "unexpected error",
"headers": [{ "key": "Content-Type", "value": "application/json" }],
"bodyType": "INLINE",
"filePath": "",
"databucketID": "",
"sendFileAsBody": false,
"rules": [],
"rulesOperator": "OR",
"disableTemplating": false,
"fallbackTo404": false,
"default": false,
"crudKey": "id"
}
],
"enabled": true,
"responseMode": null
}
],
"rootChildren": [
{ "type": "route", "uuid": "b054b387-eb2d-4ae4-8891-fde9c7e3e4c9" },
{ "type": "route", "uuid": "094031a7-93fc-4659-82b4-58c8c9de10b0" },
{ "type": "route", "uuid": "4b9356a9-d656-4ea8-acf3-c254fb9d6616" }
],
"proxyMode": false,
"proxyHost": "",
"proxyRemovePrefix": false,
"tlsOptions": {
"enabled": false,
"type": "CERT",
"pfxPath": "",
"certPath": "",
"keyPath": "",
"caPath": "",
"passphrase": ""
},
"cors": true,
"headers": [],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": []
}
37 changes: 37 additions & 0 deletions packages/cli/test/data/openapi/legacy-exported.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"openapi": "3.0.0",
"info": {
"title": "export-mock",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost:3000/api"
}
],
"paths": {
"/test": {
"get": {
"description": "",
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"example": {}
}
},
"headers": {
"": {
"schema": {
"type": "string"
},
"example": ""
}
}
}
}
}
}
}
}
37 changes: 37 additions & 0 deletions packages/cli/test/data/openapi/mock1-exported-prettified.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"openapi": "3.0.0",
"info": {
"title": "mock1",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost:3000/api"
}
],
"paths": {
"/test": {
"get": {
"description": "",
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"example": {}
}
},
"headers": {
"": {
"schema": {
"type": "string"
},
"example": ""
}
}
}
}
}
}
}
}
1 change: 1 addition & 0 deletions packages/cli/test/data/openapi/mock1-exported.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"openapi":"3.0.0","info":{"title":"mock1","version":"1.0.0"},"servers":[{"url":"http://localhost:3000/api"}],"paths":{"/test":{"get":{"description":"","responses":{"200":{"description":"","content":{"application/json":{"example":{}}},"headers":{"":{"schema":{"type":"string"},"example":""}}}}}}}}
Loading

0 comments on commit 09605e0

Please sign in to comment.