qa 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth.js +6 -2
- package/dist/commands/index.js +2 -1
- package/dist/commands/init.d.ts +19 -0
- package/dist/commands/init.js +64 -0
- package/dist/commands/login.d.ts +1 -1
- package/dist/commands/login.js +10 -3
- package/dist/commands/test.js +0 -1
- package/dist/commands/utils/index.d.ts +50 -0
- package/dist/commands/utils/index.js +134 -0
- package/dist/commands/utils/prompts.d.ts +18 -0
- package/dist/commands/utils/prompts.js +241 -0
- package/dist/constants.js +1 -1
- package/package.json +3 -2
package/dist/auth.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
|
+
import { intro } from '@clack/prompts';
|
|
3
|
+
import { askForUserAuth } from './commands/utils/prompts.js';
|
|
2
4
|
import { TOKEN_PATH } from './constants.js';
|
|
3
5
|
export async function getAccessToken(tokenParam) {
|
|
4
6
|
if (tokenParam) {
|
|
@@ -10,12 +12,14 @@ export async function getAccessToken(tokenParam) {
|
|
|
10
12
|
}
|
|
11
13
|
const hasAccess = await fs.access(TOKEN_PATH).then(() => true).catch(() => false);
|
|
12
14
|
if (!hasAccess) {
|
|
13
|
-
|
|
15
|
+
intro('Log into CodelessQA');
|
|
16
|
+
return askForUserAuth();
|
|
14
17
|
}
|
|
15
18
|
const rc = await fs.readFile(TOKEN_PATH, 'utf-8');
|
|
16
19
|
const token = rc.split('\n').filter(Boolean).find((line) => line.startsWith('token='))?.split('=')[1];
|
|
17
20
|
if (!token) {
|
|
18
|
-
|
|
21
|
+
intro('Log into CodelessQA');
|
|
22
|
+
return askForUserAuth();
|
|
19
23
|
}
|
|
20
24
|
return token;
|
|
21
25
|
}
|
package/dist/commands/index.js
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Argv } from 'yargs';
|
|
2
|
+
export declare const command = "init [option]";
|
|
3
|
+
export declare const desc = "Create a new CodelessQA project";
|
|
4
|
+
export declare const cmdArgs: {
|
|
5
|
+
readonly token: {
|
|
6
|
+
readonly alias: "t";
|
|
7
|
+
readonly desc: "Provide an access token to authenticate with the platform";
|
|
8
|
+
readonly type: "string";
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare const builder: (yargs: Argv) => Argv<import("yargs").Omit<{}, "token"> & import("yargs").InferredOptionTypes<{
|
|
12
|
+
readonly token: {
|
|
13
|
+
readonly alias: "t";
|
|
14
|
+
readonly desc: "Provide an access token to authenticate with the platform";
|
|
15
|
+
readonly type: "string";
|
|
16
|
+
};
|
|
17
|
+
}>>;
|
|
18
|
+
export declare const handler: (argv: typeof cmdArgs) => Promise<void>;
|
|
19
|
+
//# sourceMappingURL=init.d.ts.map
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { intro, outro, spinner } from '@clack/prompts';
|
|
2
|
+
import { getAccessToken } from '../auth.js';
|
|
3
|
+
import { getPresignUrl, uploadSpecification, fetchProjectNames, waitForUploadFinishes, createSpecification, generateTests, validateAuthToken } from './utils/index.js';
|
|
4
|
+
import { askTestingType, askSpecLocation, askProjectDetails, askSecurityParams, askServerParams, askForUserAuth } from './utils/prompts.js';
|
|
5
|
+
import { CLI_EPILOGUE } from '../constants.js';
|
|
6
|
+
export const command = 'init [option]';
|
|
7
|
+
export const desc = 'Create a new CodelessQA project';
|
|
8
|
+
export const cmdArgs = {
|
|
9
|
+
token: {
|
|
10
|
+
alias: 't',
|
|
11
|
+
desc: 'Provide an access token to authenticate with the platform',
|
|
12
|
+
type: 'string',
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
export const builder = (yargs) => {
|
|
16
|
+
return yargs
|
|
17
|
+
.options(cmdArgs)
|
|
18
|
+
.example('qa init', 'Creates a new CodelessQA project')
|
|
19
|
+
.epilogue(CLI_EPILOGUE)
|
|
20
|
+
.help();
|
|
21
|
+
};
|
|
22
|
+
export const handler = async (argv) => {
|
|
23
|
+
let token = await getAccessToken(argv.token);
|
|
24
|
+
const s = spinner();
|
|
25
|
+
intro('Start new CodelessQA project');
|
|
26
|
+
await askTestingType();
|
|
27
|
+
const { specPath, specString, spec } = await askSpecLocation();
|
|
28
|
+
s.start('Looking up user account...');
|
|
29
|
+
let userAuth = await validateAuthToken(token);
|
|
30
|
+
if (!userAuth.valid) {
|
|
31
|
+
s.stop('Couldn\'t authenticate with the platform.');
|
|
32
|
+
console.log('\n');
|
|
33
|
+
token = await askForUserAuth();
|
|
34
|
+
userAuth = await validateAuthToken(token);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* start uploading the specification while continue to have the user answer questions
|
|
38
|
+
*/
|
|
39
|
+
const uploadProcess = (async () => {
|
|
40
|
+
const presignedUrl = await getPresignUrl(token, specPath);
|
|
41
|
+
await uploadSpecification(presignedUrl, specString);
|
|
42
|
+
return waitForUploadFinishes(presignedUrl);
|
|
43
|
+
})();
|
|
44
|
+
const projectNames = await fetchProjectNames(token);
|
|
45
|
+
s.stop(`User ${userAuth.email} authenticated!`);
|
|
46
|
+
const projectProps = await askProjectDetails(projectNames, spec);
|
|
47
|
+
const securityParams = await askSecurityParams(spec);
|
|
48
|
+
const serverParam = await askServerParams(spec);
|
|
49
|
+
s.start('Uploading specification...');
|
|
50
|
+
const fileOrError = await uploadProcess.catch((err) => err);
|
|
51
|
+
if (fileOrError instanceof Error) {
|
|
52
|
+
s.stop();
|
|
53
|
+
outro(fileOrError.message);
|
|
54
|
+
process.exit(0);
|
|
55
|
+
}
|
|
56
|
+
s.stop(`Specification ${spec.info.title} (${spec.info.version}) uploaded!`);
|
|
57
|
+
s.start('Creating project...');
|
|
58
|
+
const id = await createSpecification(token, projectProps, fileOrError, s);
|
|
59
|
+
s.stop(`Project "${projectProps.name}" created!`);
|
|
60
|
+
s.start('Creating API tests...');
|
|
61
|
+
await generateTests(token, id, s, { securityParams, serverParam });
|
|
62
|
+
s.stop('API tests created!');
|
|
63
|
+
outro(`You're all set!\n\nNow run your first test with:\n> qa test "${projectProps.name}"`);
|
|
64
|
+
};
|
package/dist/commands/login.d.ts
CHANGED
|
@@ -15,5 +15,5 @@ export declare const builder: (yargs: Argv) => Argv<import("yargs").Omit<{}, "to
|
|
|
15
15
|
readonly type: "string";
|
|
16
16
|
};
|
|
17
17
|
}>>;
|
|
18
|
-
export declare const handler: (argv
|
|
18
|
+
export declare const handler: (argv?: Record<keyof typeof cmdArgs, string>) => Promise<string>;
|
|
19
19
|
//# sourceMappingURL=login.d.ts.map
|
package/dist/commands/login.js
CHANGED
|
@@ -23,12 +23,18 @@ export const builder = (yargs) => {
|
|
|
23
23
|
.help();
|
|
24
24
|
};
|
|
25
25
|
export const handler = async (argv) => {
|
|
26
|
-
if (argv
|
|
27
|
-
|
|
26
|
+
if (argv?.token) {
|
|
27
|
+
console.log('Logging in via access token', argv.token);
|
|
28
|
+
return argv.token;
|
|
28
29
|
}
|
|
29
30
|
const ticket = uuidv4();
|
|
30
31
|
const loginUrl = `${PLATFORM_URL}/login/${ticket}`;
|
|
31
|
-
console.log(
|
|
32
|
+
console.log([
|
|
33
|
+
'🦄 Logging in via UI 🙌',
|
|
34
|
+
`👉 Visit the following URL to log in: ${loginUrl}`,
|
|
35
|
+
'',
|
|
36
|
+
'...waiting for user to be authenticated...'
|
|
37
|
+
].join('\n'));
|
|
32
38
|
execSync(`open ${loginUrl}`);
|
|
33
39
|
try {
|
|
34
40
|
const token = await new Promise((resolve, reject) => {
|
|
@@ -48,6 +54,7 @@ export const handler = async (argv) => {
|
|
|
48
54
|
});
|
|
49
55
|
await fs.writeFile(TOKEN_PATH, `token=${token}\n`);
|
|
50
56
|
console.log('🎉 Successfully logged in!');
|
|
57
|
+
return token;
|
|
51
58
|
}
|
|
52
59
|
catch (err) {
|
|
53
60
|
console.log(`🚨 Failed to log-in: ${err.message}`);
|
package/dist/commands/test.js
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { SecurityObject, ServerObject } from '@codeless.qa/contracts';
|
|
2
|
+
import type { ProjectProps } from './prompts.js';
|
|
3
|
+
type Spinner = {
|
|
4
|
+
stop: () => void;
|
|
5
|
+
};
|
|
6
|
+
export declare function getPresignUrl(token: string, specPath: string): Promise<{
|
|
7
|
+
url: string;
|
|
8
|
+
key: string;
|
|
9
|
+
fileName: string;
|
|
10
|
+
fileType: string;
|
|
11
|
+
fileUrl: string;
|
|
12
|
+
pollingJwt: string;
|
|
13
|
+
pollingUrl: string;
|
|
14
|
+
contentDisposition: string;
|
|
15
|
+
fields: {
|
|
16
|
+
key: string;
|
|
17
|
+
acl: string;
|
|
18
|
+
"Content-Type": string;
|
|
19
|
+
"Content-Disposition": string;
|
|
20
|
+
bucket: string;
|
|
21
|
+
"X-Amz-Algorithm": string;
|
|
22
|
+
"X-Amz-Credential": string;
|
|
23
|
+
"X-Amz-Date": string;
|
|
24
|
+
Policy: string;
|
|
25
|
+
"X-Amz-Signature": string;
|
|
26
|
+
};
|
|
27
|
+
customId?: any;
|
|
28
|
+
}>;
|
|
29
|
+
export declare function uploadSpecification(presignedUrl: {
|
|
30
|
+
url: string;
|
|
31
|
+
fields: Record<string, string>;
|
|
32
|
+
}, specString: string): Promise<void>;
|
|
33
|
+
export declare function waitForUploadFinishes(presignedUrl: {
|
|
34
|
+
pollingUrl: string;
|
|
35
|
+
pollingJwt: string;
|
|
36
|
+
}): Promise<string>;
|
|
37
|
+
export declare function createSpecification(token: string, projectProps: ProjectProps, file: string, spinner: Spinner): Promise<string>;
|
|
38
|
+
export declare function generateTests(token: string, id: string, spinner: Spinner, body: {
|
|
39
|
+
securityParams: SecurityObject[];
|
|
40
|
+
serverParam: ServerObject;
|
|
41
|
+
}): Promise<void>;
|
|
42
|
+
export declare function fetchProjectNames(token: string): Promise<string[]>;
|
|
43
|
+
export declare function validateAuthToken(token: string): Promise<{
|
|
44
|
+
valid: false;
|
|
45
|
+
} | {
|
|
46
|
+
valid: true;
|
|
47
|
+
email: string;
|
|
48
|
+
}>;
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import { outro } from '@clack/prompts';
|
|
4
|
+
import { getAuthenticatedClient } from '@codeless.qa/contracts/client';
|
|
5
|
+
export async function getPresignUrl(token, specPath) {
|
|
6
|
+
const client = getAuthenticatedClient(token);
|
|
7
|
+
const stat = await fs.stat(specPath);
|
|
8
|
+
const presigned = await client.specification.upload({
|
|
9
|
+
query: { actionType: 'upload', slug: 'openAPISpecUploader' },
|
|
10
|
+
body: {
|
|
11
|
+
files: [{
|
|
12
|
+
name: path.basename(specPath),
|
|
13
|
+
size: stat.size,
|
|
14
|
+
type: 'application/json'
|
|
15
|
+
}]
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
if (presigned.status !== 200) {
|
|
19
|
+
const error = typeof presigned.body === 'object' && presigned.body && 'error' in presigned.body && presigned.body.error instanceof Error
|
|
20
|
+
? presigned.body.error
|
|
21
|
+
: new Error('Unknown error');
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
return presigned.body[0];
|
|
25
|
+
}
|
|
26
|
+
export async function uploadSpecification(presignedUrl, specString) {
|
|
27
|
+
const formData = new FormData();
|
|
28
|
+
for (const [key, value] of Object.entries(presignedUrl.fields)) {
|
|
29
|
+
formData.append(key, value);
|
|
30
|
+
}
|
|
31
|
+
formData.append('file', Buffer.from(specString));
|
|
32
|
+
const res = await fetch(presignedUrl.url, {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
body: formData,
|
|
35
|
+
});
|
|
36
|
+
if (res.status !== 204) {
|
|
37
|
+
const error = typeof res.body === 'object' && res.body && 'error' in res.body && res.body.error instanceof Error
|
|
38
|
+
? res.body.error
|
|
39
|
+
: new Error('Unknown error');
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const UPLOAD_TIMEOUT = 1000 * 60;
|
|
44
|
+
export async function waitForUploadFinishes(presignedUrl) {
|
|
45
|
+
const start = Date.now();
|
|
46
|
+
let finalKey;
|
|
47
|
+
while (true) {
|
|
48
|
+
const waitRes = await fetch(presignedUrl.pollingUrl, {
|
|
49
|
+
headers: {
|
|
50
|
+
authorization: presignedUrl.pollingJwt
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
if (waitRes.status !== 200) {
|
|
54
|
+
const error = typeof waitRes.body === 'object' && waitRes.body && 'error' in waitRes.body && waitRes.body.error instanceof Error
|
|
55
|
+
? waitRes.body.error
|
|
56
|
+
: new Error('Unknown error');
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
const body = await waitRes.json();
|
|
60
|
+
if (body.status === 'done') {
|
|
61
|
+
finalKey = body.callbackData.key;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
if (Date.now() - start > UPLOAD_TIMEOUT) {
|
|
65
|
+
outro(`Timeout after ${UPLOAD_TIMEOUT / 1000} seconds!`);
|
|
66
|
+
process.exit(0);
|
|
67
|
+
}
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
69
|
+
}
|
|
70
|
+
if (!finalKey) {
|
|
71
|
+
throw new Error('Upload failed!');
|
|
72
|
+
}
|
|
73
|
+
return finalKey;
|
|
74
|
+
}
|
|
75
|
+
export async function createSpecification(token, projectProps, file, spinner) {
|
|
76
|
+
const client = getAuthenticatedClient(token);
|
|
77
|
+
const resSpecCreation = await client.specifications.create({
|
|
78
|
+
body: {
|
|
79
|
+
...projectProps,
|
|
80
|
+
file
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
if (resSpecCreation.status !== 201) {
|
|
84
|
+
spinner.stop();
|
|
85
|
+
const error = typeof resSpecCreation.body === 'object' && resSpecCreation.body && 'error' in resSpecCreation.body && resSpecCreation.body.error instanceof Error
|
|
86
|
+
? resSpecCreation.body.error
|
|
87
|
+
: new Error('Unknown error');
|
|
88
|
+
outro(error.message);
|
|
89
|
+
process.exit(0);
|
|
90
|
+
}
|
|
91
|
+
return resSpecCreation.body.data.id;
|
|
92
|
+
}
|
|
93
|
+
export async function generateTests(token, id, spinner, body) {
|
|
94
|
+
const client = getAuthenticatedClient(token);
|
|
95
|
+
const resTests = await client.specification.generate({
|
|
96
|
+
params: { id },
|
|
97
|
+
body: {
|
|
98
|
+
securityObjects: body.securityParams,
|
|
99
|
+
server: body.serverParam
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
if (resTests.status !== 200 || 'error' in resTests.body) {
|
|
103
|
+
spinner.stop();
|
|
104
|
+
const error = typeof resTests.body === 'object' && resTests.body && 'error' in resTests.body
|
|
105
|
+
? resTests.body.error instanceof Error
|
|
106
|
+
? resTests.body.error
|
|
107
|
+
: new Error(resTests.body.error)
|
|
108
|
+
: new Error('Unknown error');
|
|
109
|
+
outro(error.message);
|
|
110
|
+
process.exit(0);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export async function fetchProjectNames(token) {
|
|
114
|
+
const client = getAuthenticatedClient(token);
|
|
115
|
+
const projectNames = [];
|
|
116
|
+
try {
|
|
117
|
+
const res = await client.specifications.get();
|
|
118
|
+
if (res.status === 200) {
|
|
119
|
+
projectNames.push(...res.body.data.map((spec) => spec.name));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// noop
|
|
124
|
+
}
|
|
125
|
+
return projectNames;
|
|
126
|
+
}
|
|
127
|
+
export async function validateAuthToken(token) {
|
|
128
|
+
const client = getAuthenticatedClient(token);
|
|
129
|
+
const res = await client.auth.validatingAccessToken();
|
|
130
|
+
if (res.status !== 200) {
|
|
131
|
+
return { valid: false };
|
|
132
|
+
}
|
|
133
|
+
return res.body;
|
|
134
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type SecurityObject, ServerObject } from '@codeless.qa/contracts';
|
|
2
|
+
import { type OpenAPIV3 } from 'openapi-types';
|
|
3
|
+
export declare function askTestingType(): Promise<string>;
|
|
4
|
+
export declare function askSpecLocation(): Promise<{
|
|
5
|
+
spec: OpenAPIV3.Document<{}>;
|
|
6
|
+
specPath: string;
|
|
7
|
+
specString: string;
|
|
8
|
+
}>;
|
|
9
|
+
export interface ProjectProps {
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
version: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function askProjectDetails(existingProjectNames: string[], spec: OpenAPIV3.Document): Promise<ProjectProps>;
|
|
15
|
+
export declare function askSecurityParams(spec: OpenAPIV3.Document): Promise<SecurityObject[]>;
|
|
16
|
+
export declare function askServerParams(spec: OpenAPIV3.Document): Promise<ServerObject>;
|
|
17
|
+
export declare function askForUserAuth(): Promise<string>;
|
|
18
|
+
//# sourceMappingURL=prompts.d.ts.map
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import fsp from 'node:fs/promises';
|
|
5
|
+
import { TestParameterType } from '@codeless.qa/contracts';
|
|
6
|
+
import { select, text, confirm, outro, isCancel } from '@clack/prompts';
|
|
7
|
+
import { handler as loginFlow } from '../login.js';
|
|
8
|
+
export async function askTestingType() {
|
|
9
|
+
const answerTestingType = await select({
|
|
10
|
+
message: 'What type of testing you would like to do?',
|
|
11
|
+
options: [
|
|
12
|
+
{ value: 'api', label: 'API Testing' },
|
|
13
|
+
{ value: 'ui', label: 'Function UI Testing in a web browser', hint: 'not yet supported' },
|
|
14
|
+
]
|
|
15
|
+
});
|
|
16
|
+
if (isCancel(answerTestingType)) {
|
|
17
|
+
outro('Bye!');
|
|
18
|
+
process.exit(0);
|
|
19
|
+
}
|
|
20
|
+
return answerTestingType;
|
|
21
|
+
}
|
|
22
|
+
export async function askSpecLocation() {
|
|
23
|
+
const specPath = await text({
|
|
24
|
+
message: 'Please provide a file path or URL to your OpenAPI specification or Postman collection!',
|
|
25
|
+
validate: (path) => {
|
|
26
|
+
try {
|
|
27
|
+
if (path.startsWith('https://') || path.startsWith('http://')) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
fs.accessSync(path);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return 'Path does not exist';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
if (isCancel(specPath) || typeof specPath !== 'string') {
|
|
38
|
+
outro('Bye!');
|
|
39
|
+
process.exit(0);
|
|
40
|
+
}
|
|
41
|
+
if (specPath.startsWith('https://') || specPath.startsWith('http://')) {
|
|
42
|
+
try {
|
|
43
|
+
const origSpecString = await (await fetch(specPath)).text();
|
|
44
|
+
let filePath = `${os.tmpdir()}/codelessqa-${specPath.split('/').pop()}`;
|
|
45
|
+
/**
|
|
46
|
+
* make sure it is a json file
|
|
47
|
+
*/
|
|
48
|
+
if (!path.extname(filePath)) {
|
|
49
|
+
filePath += '.json';
|
|
50
|
+
}
|
|
51
|
+
const spec = JSON.parse(origSpecString);
|
|
52
|
+
/**
|
|
53
|
+
* Some OpenAPI specification may leave out the host name in the server URL
|
|
54
|
+
* and expect to take it from the spec origin source. In these cases we need
|
|
55
|
+
* to add it to the server object.
|
|
56
|
+
*/
|
|
57
|
+
if (spec.servers && spec.servers.some((server) => server.url.startsWith('/'))) {
|
|
58
|
+
const source = new URL(specPath);
|
|
59
|
+
spec.servers = spec.servers.map((server) => {
|
|
60
|
+
if (!server.url.startsWith('/')) {
|
|
61
|
+
return server;
|
|
62
|
+
}
|
|
63
|
+
server.url = new URL(server.url, source.origin).href;
|
|
64
|
+
return server;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const specString = JSON.stringify(spec);
|
|
68
|
+
await fsp.writeFile(filePath, specString, 'utf8');
|
|
69
|
+
return { spec, specPath: filePath, specString };
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
const error = err instanceof Error ? err : new Error('Unknown error');
|
|
73
|
+
outro(`Error: ${error.message}`);
|
|
74
|
+
process.exit(0);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const specString = fs.readFileSync(specPath, 'utf8');
|
|
79
|
+
const spec = JSON.parse(specString);
|
|
80
|
+
return { spec, specPath, specString };
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
const error = err instanceof Error ? err : new Error('Unknown error');
|
|
84
|
+
outro(`Error: ${error.message}`);
|
|
85
|
+
process.exit(0);
|
|
86
|
+
}
|
|
87
|
+
throw new Error('I should never reach here');
|
|
88
|
+
}
|
|
89
|
+
const PROJECT_PROPS = {
|
|
90
|
+
name: 'What is the name of your project?',
|
|
91
|
+
description: 'What is the description of your project?',
|
|
92
|
+
version: 'What is the version of your API specification?',
|
|
93
|
+
};
|
|
94
|
+
export async function askProjectDetails(existingProjectNames, spec) {
|
|
95
|
+
const projectProps = {};
|
|
96
|
+
for (const [key, message] of Object.entries(PROJECT_PROPS)) {
|
|
97
|
+
const opts = {
|
|
98
|
+
message,
|
|
99
|
+
initialValue: spec.info[key] || spec.info.title,
|
|
100
|
+
};
|
|
101
|
+
if (key === 'name') {
|
|
102
|
+
opts.validate = (value) => existingProjectNames.includes(value)
|
|
103
|
+
? `Project naem "${value}" already exists`
|
|
104
|
+
: undefined;
|
|
105
|
+
}
|
|
106
|
+
const value = await text(opts);
|
|
107
|
+
if (isCancel(value)) {
|
|
108
|
+
outro('Bye!');
|
|
109
|
+
process.exit(0);
|
|
110
|
+
}
|
|
111
|
+
projectProps[key] = value;
|
|
112
|
+
}
|
|
113
|
+
return projectProps;
|
|
114
|
+
}
|
|
115
|
+
export async function askSecurityParams(spec) {
|
|
116
|
+
if (!spec.components ||
|
|
117
|
+
!spec.components.securitySchemes ||
|
|
118
|
+
(Array.isArray(spec.components.securitySchemes) &&
|
|
119
|
+
spec.components.securitySchemes.length === 0)) {
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
const securitySchemes = new Set(Object.values(spec.components.securitySchemes).filter((schema) => 'type' in schema));
|
|
123
|
+
const securityObjects = [];
|
|
124
|
+
for (const secScheme of securitySchemes) {
|
|
125
|
+
const isBasicAuth = secScheme.type === 'http' && secScheme.scheme === 'basic';
|
|
126
|
+
const isJWTAuth = secScheme.type === 'http' && secScheme.scheme === 'bearer' && secScheme.bearerFormat === 'JWT';
|
|
127
|
+
const message = isBasicAuth
|
|
128
|
+
? 'Can you provide a username and password to access the API?'
|
|
129
|
+
: isJWTAuth
|
|
130
|
+
? 'Can you provide an example JWT token to access the API?'
|
|
131
|
+
: secScheme.type === 'apiKey'
|
|
132
|
+
? `Can you provide an example value for the "${secScheme.name}" ${secScheme.in} parameter?`
|
|
133
|
+
: undefined;
|
|
134
|
+
if (!message || 'in' in secScheme && secScheme.in === 'query') {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const value = await text({
|
|
138
|
+
message,
|
|
139
|
+
placeholder: '(leave empty to skip or press "s" to cancel and skip all)',
|
|
140
|
+
});
|
|
141
|
+
if (isCancel(value)) {
|
|
142
|
+
outro('Bye!');
|
|
143
|
+
process.exit(0);
|
|
144
|
+
}
|
|
145
|
+
if (value === 's') {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
if (!value) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const scope = secScheme.in.slice(0, 1).toUpperCase() +
|
|
152
|
+
secScheme.in.slice(1);
|
|
153
|
+
securityObjects.push({
|
|
154
|
+
id: Math.random().toString(36).slice(2).toLowerCase(),
|
|
155
|
+
type: TestParameterType.Security,
|
|
156
|
+
payload: {
|
|
157
|
+
name: isBasicAuth || isJWTAuth
|
|
158
|
+
? 'Authorization'
|
|
159
|
+
: secScheme.name,
|
|
160
|
+
value,
|
|
161
|
+
scope
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return securityObjects;
|
|
166
|
+
}
|
|
167
|
+
async function askCustomServer() {
|
|
168
|
+
const customServerPrompt = await text({
|
|
169
|
+
message: 'What is the server url?',
|
|
170
|
+
placeholder: 'https://api.gigantic-server.com/v1',
|
|
171
|
+
});
|
|
172
|
+
if (isCancel(customServerPrompt)) {
|
|
173
|
+
outro('Bye!');
|
|
174
|
+
process.exit(0);
|
|
175
|
+
}
|
|
176
|
+
return customServerPrompt;
|
|
177
|
+
}
|
|
178
|
+
export async function askServerParams(spec) {
|
|
179
|
+
const CUSTOM_SERVER = 'custom';
|
|
180
|
+
if (!spec.servers || spec.servers.length === 0) {
|
|
181
|
+
const customServer = await askCustomServer();
|
|
182
|
+
return { url: customServer };
|
|
183
|
+
}
|
|
184
|
+
const server = await select({
|
|
185
|
+
message: 'Which server do you want to use?',
|
|
186
|
+
options: [
|
|
187
|
+
...spec.servers.map((server, value) => ({
|
|
188
|
+
label: server.description || server.url,
|
|
189
|
+
value,
|
|
190
|
+
hint: server.description ? server.url : undefined
|
|
191
|
+
})),
|
|
192
|
+
{
|
|
193
|
+
label: 'Custom server url',
|
|
194
|
+
value: CUSTOM_SERVER
|
|
195
|
+
}
|
|
196
|
+
]
|
|
197
|
+
});
|
|
198
|
+
if (isCancel(server)) {
|
|
199
|
+
outro('Bye!');
|
|
200
|
+
process.exit(0);
|
|
201
|
+
}
|
|
202
|
+
const serverVars = {};
|
|
203
|
+
if (typeof server === 'number' && spec.servers[server].variables) {
|
|
204
|
+
for (const [name, val] of Object.entries(spec.servers[server].variables)) {
|
|
205
|
+
const varPrompt = await text({
|
|
206
|
+
message: `What is the value for the "${name}" variable?`,
|
|
207
|
+
initialValue: val.default,
|
|
208
|
+
});
|
|
209
|
+
if (isCancel(varPrompt)) {
|
|
210
|
+
outro('Bye!');
|
|
211
|
+
process.exit(0);
|
|
212
|
+
}
|
|
213
|
+
serverVars[name] = varPrompt;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
let customServerUrl;
|
|
217
|
+
if (typeof server === 'string' && server === CUSTOM_SERVER) {
|
|
218
|
+
customServerUrl = await askCustomServer();
|
|
219
|
+
}
|
|
220
|
+
const serverObject = {
|
|
221
|
+
url: typeof server === 'number'
|
|
222
|
+
? spec.servers[server].url
|
|
223
|
+
: customServerUrl,
|
|
224
|
+
variables: serverVars
|
|
225
|
+
};
|
|
226
|
+
return serverObject;
|
|
227
|
+
}
|
|
228
|
+
export async function askForUserAuth() {
|
|
229
|
+
const runLoginWorkflow = await confirm({
|
|
230
|
+
message: 'It seems like you are not logged in. Would you like to login now?',
|
|
231
|
+
initialValue: true
|
|
232
|
+
});
|
|
233
|
+
if (isCancel(runLoginWorkflow) || !runLoginWorkflow) {
|
|
234
|
+
outro('Bye!');
|
|
235
|
+
process.exit(0);
|
|
236
|
+
}
|
|
237
|
+
console.log('');
|
|
238
|
+
const token = await loginFlow();
|
|
239
|
+
console.log('');
|
|
240
|
+
return token;
|
|
241
|
+
}
|
package/dist/constants.js
CHANGED
|
@@ -8,5 +8,5 @@ qa (v${pkg.version})
|
|
|
8
8
|
© 2024 CodelessQA, Inc.
|
|
9
9
|
`;
|
|
10
10
|
export const PLATFORM_URL = 'https://codeless.qa' || 'http://localhost:4000';
|
|
11
|
-
export const WORKER_URL = 'wss://worker.codeless.qa' || 'localhost:8080';
|
|
11
|
+
export const WORKER_URL = 'wss://worker.codeless.qa' || 'ws://localhost:8080';
|
|
12
12
|
export const TOKEN_PATH = path.join(homedir(), '/.codeless-qa');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qa",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"author": "Christian Bromann <christian@codeless.qa>",
|
|
5
5
|
"description": "Codeless QA CLI",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"keywords": [],
|
|
12
12
|
"dependencies": {
|
|
13
|
+
"@clack/prompts": "^0.7.0",
|
|
13
14
|
"@types/uuid": "^9.0.8",
|
|
14
15
|
"@types/ws": "^8.5.10",
|
|
15
16
|
"@types/yargs": "^17.0.32",
|
|
@@ -18,7 +19,7 @@
|
|
|
18
19
|
"uuid": "^9.0.1",
|
|
19
20
|
"ws": "^8.17.0",
|
|
20
21
|
"yargs": "^17.7.2",
|
|
21
|
-
"@codeless.qa/contracts": "0.0.
|
|
22
|
+
"@codeless.qa/contracts": "0.0.3"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
25
|
"replace-in-files-cli": "^3.0.0",
|