mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-22 08:48:26 +02:00
Simple tests for Postman and Yaak importers
This commit is contained in:
150
plugins/importer-postman/tests/fixtures/nested.json
vendored
150
plugins/importer-postman/tests/fixtures/nested.json
vendored
@@ -7,158 +7,32 @@
|
|||||||
},
|
},
|
||||||
"item": [
|
"item": [
|
||||||
{
|
{
|
||||||
"name": "New Folder",
|
"name": "Top Folder",
|
||||||
"item": [
|
"item": [
|
||||||
{
|
{
|
||||||
"name": "FOlder 2",
|
"name": "Nested Folder",
|
||||||
"item": [
|
"item": [
|
||||||
{
|
{
|
||||||
"name": "Basic Auth",
|
"name": "Request 1",
|
||||||
"request": {
|
"request": {
|
||||||
"auth": {
|
"method": "GET"
|
||||||
"type": "basic",
|
}
|
||||||
"basic": {
|
|
||||||
"password": "pass",
|
|
||||||
"username": "user"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"method": "GET",
|
|
||||||
"header": [],
|
|
||||||
"url": "https://schier.co"
|
|
||||||
},
|
|
||||||
"response": []
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Form Data",
|
"name": "Request 2",
|
||||||
"request": {
|
"request": {
|
||||||
"method": "POST",
|
"method": "GET"
|
||||||
"header": [
|
}
|
||||||
{
|
|
||||||
"key": "X-foo",
|
|
||||||
"value": "bar",
|
|
||||||
"description": "description",
|
|
||||||
"type": "text"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "Disabled",
|
|
||||||
"value": "tnroant",
|
|
||||||
"description": "ntisorantosra",
|
|
||||||
"type": "text",
|
|
||||||
"disabled": true
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"body": {
|
|
||||||
"mode": "formdata",
|
|
||||||
"formdata": [
|
|
||||||
{
|
|
||||||
"key": "Form",
|
|
||||||
"value": "Value",
|
|
||||||
"description": "descirption",
|
|
||||||
"type": "text"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "Disabled",
|
|
||||||
"value": "foo",
|
|
||||||
"description": "bar",
|
|
||||||
"type": "text",
|
|
||||||
"disabled": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "file",
|
|
||||||
"type": "file",
|
|
||||||
"src": "/Users/gschier/Desktop/foo.json"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "Rendered",
|
|
||||||
"value": "{{Foo}}",
|
|
||||||
"type": "text"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"url": {
|
|
||||||
"raw": "schier.co?firstkey=firstvalue&hi=there",
|
|
||||||
"host": [
|
|
||||||
"schier",
|
|
||||||
"co"
|
|
||||||
],
|
|
||||||
"query": [
|
|
||||||
{
|
|
||||||
"key": "disabled",
|
|
||||||
"value": "secondvalue",
|
|
||||||
"description": "this is disabled",
|
|
||||||
"disabled": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "firstkey",
|
|
||||||
"value": "firstvalue"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "hi",
|
|
||||||
"value": "there"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"response": []
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Form URL",
|
"name": "Request 3",
|
||||||
"request": {
|
"request": {
|
||||||
"method": "POST",
|
"method": "GET"
|
||||||
"header": [
|
}
|
||||||
{
|
|
||||||
"key": "X-foo",
|
|
||||||
"value": "bar",
|
|
||||||
"description": "description",
|
|
||||||
"type": "text"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "Disabled",
|
|
||||||
"value": "tnroant",
|
|
||||||
"description": "ntisorantosra",
|
|
||||||
"type": "text",
|
|
||||||
"disabled": true
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"body": {
|
|
||||||
"mode": "urlencoded",
|
|
||||||
"urlencoded": [
|
|
||||||
{
|
|
||||||
"key": "foo",
|
|
||||||
"value": "bar",
|
|
||||||
"type": "text"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"url": {
|
|
||||||
"raw": "schier.co?firstkey=firstvalue&hi=there",
|
|
||||||
"host": [
|
|
||||||
"schier",
|
|
||||||
"co"
|
|
||||||
],
|
|
||||||
"query": [
|
|
||||||
{
|
|
||||||
"key": "disabled",
|
|
||||||
"value": "secondvalue",
|
|
||||||
"description": "this is disabled",
|
|
||||||
"disabled": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "firstkey",
|
|
||||||
"value": "firstvalue"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "hi",
|
|
||||||
"value": "there"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"response": []
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import * as path from 'node:path';
|
import * as path from 'node:path';
|
||||||
import { expect, test, describe, beforeEach, afterEach, vi } from 'vitest';
|
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
||||||
import { pluginHookImport } from '../src';
|
import { pluginHookImport } from '../src';
|
||||||
|
|
||||||
let originalRandom = Math.random;
|
let originalRandom = Math.random;
|
||||||
@@ -8,8 +8,8 @@ let originalRandom = Math.random;
|
|||||||
describe('importer-postman', () => {
|
describe('importer-postman', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
const mocked = vi.fn(() => ((i++ * 1000) % 133) / 100);
|
// Psuedo-random number generator to ensure consistent ID generation
|
||||||
Math.random = mocked;
|
Math.random = vi.fn(() => ((i++ * 1000) % 133) / 100);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -19,16 +19,45 @@ describe('importer-postman', () => {
|
|||||||
const p = path.join(__dirname, 'fixtures');
|
const p = path.join(__dirname, 'fixtures');
|
||||||
const fixtures = fs.readdirSync(p);
|
const fixtures = fs.readdirSync(p);
|
||||||
|
|
||||||
console.log('FIXTURES', fixtures);
|
|
||||||
for (const fixture of fixtures) {
|
for (const fixture of fixtures) {
|
||||||
test('Imports ' + fixture, () => {
|
test('Imports ' + fixture, () => {
|
||||||
const contents = fs.readFileSync(path.join(p, fixture), 'utf-8');
|
const contents = fs.readFileSync(path.join(p, fixture), 'utf-8');
|
||||||
const imported = pluginHookImport(contents);
|
const imported = pluginHookImport(contents);
|
||||||
expect(imported).toEqual({
|
expect(imported).toEqual({
|
||||||
resources: {
|
resources: expect.objectContaining({
|
||||||
environments: [],
|
folders: expect.arrayContaining([
|
||||||
requests: [],
|
expect.objectContaining({
|
||||||
},
|
name: 'Top Folder',
|
||||||
|
workspaceId: 'wk_0G3J6M9QcT',
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
name: 'Nested Folder',
|
||||||
|
workspaceId: 'wk_0G3J6M9QcT',
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
httpRequests: expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
name: 'Request 1',
|
||||||
|
workspaceId: 'wk_0G3J6M9QcT',
|
||||||
|
folderId: 'fl_vundefinedyundefinedBundefinedE0H3',
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
name: 'Request 2',
|
||||||
|
workspaceId: 'wk_0G3J6M9QcT',
|
||||||
|
folderId: 'fl_fWiZlundefinedoundefinedrundefined',
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
name: 'Request 3',
|
||||||
|
workspaceId: 'wk_0G3J6M9QcT',
|
||||||
|
folderId: null,
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
workspaces: [
|
||||||
|
expect.objectContaining({
|
||||||
|
name: 'New Collection',
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export function pluginHookImport(contents) {
|
export function pluginHookImport(contents: string) {
|
||||||
let parsed;
|
let parsed;
|
||||||
try {
|
try {
|
||||||
parsed = JSON.parse(contents);
|
parsed = JSON.parse(contents);
|
||||||
@@ -10,23 +10,20 @@ export function pluginHookImport(contents) {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!('yaakSchema' in parsed)) {
|
const isYaakExport = 'yaakSchema' in parsed;
|
||||||
|
if (!isYaakExport) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Migrate v1 to v2 -- changes requests to httpRequests
|
// Migrate v1 to v2 -- changes requests to httpRequests
|
||||||
if (parsed.yaakSchema === 1) {
|
if ('requests' in parsed.resources) {
|
||||||
parsed.resources.httpRequests = parsed.resources.requests;
|
parsed.resources.httpRequests = parsed.resources.requests;
|
||||||
parsed.yaakSchema = 2;
|
delete parsed.resources.requests;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parsed.yaakSchema === 2) {
|
return { resources: parsed.resources }; // Should already be in the correct format
|
||||||
return { resources: parsed.resources }; // Should already be in the correct format
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isJSObject(obj) {
|
export function isJSObject(obj: any) {
|
||||||
return Object.prototype.toString.call(obj) === '[object Object]';
|
return Object.prototype.toString.call(obj) === '[object Object]';
|
||||||
}
|
}
|
||||||
29
plugins/importer-yaak/tests/index.test.ts
Normal file
29
plugins/importer-yaak/tests/index.test.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { describe, expect, test } from 'vitest';
|
||||||
|
import { pluginHookImport } from '../src';
|
||||||
|
|
||||||
|
describe('importer-yaak', () => {
|
||||||
|
test('Skips invalid imports', () => {
|
||||||
|
expect(pluginHookImport('not JSON')).toBeUndefined();
|
||||||
|
expect(pluginHookImport('[]')).toBeUndefined();
|
||||||
|
expect(pluginHookImport(JSON.stringify({ resources: {} }))).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('converts schema 1 to 2', () => {
|
||||||
|
const imported = pluginHookImport(
|
||||||
|
JSON.stringify({
|
||||||
|
yaakSchema: 1,
|
||||||
|
resources: {
|
||||||
|
requests: [],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(imported).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
resources: {
|
||||||
|
httpRequests: [],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -4,7 +4,7 @@ import { defineConfig } from 'vite';
|
|||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
build: {
|
build: {
|
||||||
lib: {
|
lib: {
|
||||||
entry: resolve(__dirname, 'src/index.js'),
|
entry: resolve(__dirname, 'src/index.ts'),
|
||||||
fileName: 'index',
|
fileName: 'index',
|
||||||
formats: ['es'],
|
formats: ['es'],
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user