qa 0.0.15 → 0.0.17

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.
@@ -1,6 +1,6 @@
1
1
  import { intro, outro, spinner } from '@clack/prompts';
2
2
  import { getAccessToken } from '../auth.js';
3
- import { getPresignUrl, uploadSpecification, fetchProjectNames, waitForUploadFinishes, createSpecification, generateTests, validateAuthToken } from './utils/index.js';
3
+ import { fetchProjectNames, generateTests, validateAuthToken, createSpecification } from './utils/index.js';
4
4
  import { askTestingType, askSpecLocation, askProjectDetails, askSecurityParams, askServerParams, askForUserAuth } from './utils/prompts.js';
5
5
  import { CLI_EPILOGUE } from '../constants.js';
6
6
  export const command = 'init [option]';
@@ -24,7 +24,7 @@ export const handler = async (argv) => {
24
24
  const s = spinner();
25
25
  intro('Start new CodelessQA project');
26
26
  await askTestingType();
27
- const { specPath, specString, spec } = await askSpecLocation();
27
+ const { specPath, spec } = await askSpecLocation();
28
28
  s.start('Looking up user account...');
29
29
  let userAuth = await validateAuthToken(token);
30
30
  if (!userAuth.valid) {
@@ -33,32 +33,16 @@ export const handler = async (argv) => {
33
33
  token = await askForUserAuth();
34
34
  userAuth = await validateAuthToken(token);
35
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
36
  const projectNames = await fetchProjectNames(token);
45
37
  s.stop(`User ${userAuth.email} authenticated!`);
46
38
  const projectProps = await askProjectDetails(projectNames, spec);
47
- const securityParams = await askSecurityParams(spec);
48
- const serverParam = await askServerParams(spec);
39
+ const securityPayloads = await askSecurityParams(spec);
40
+ const serverObject = await askServerParams(spec);
49
41
  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);
42
+ const id = await createSpecification(token, projectProps, specPath, s);
59
43
  s.stop(`Project "${projectProps.name}" created!`);
60
44
  s.start('Creating API tests...');
61
- await generateTests(token, id, s, { securityParams, serverParam });
45
+ await generateTests(token, id, s, { securityPayloads, serverObject });
62
46
  s.stop('API tests created!');
63
47
  outro(`You're all set!\n\nNow run your first test with:\n> qa test "${projectProps.name}"`);
64
48
  };
@@ -1,44 +1,10 @@
1
- import type { SecurityObject, ServerObject } from '@codeless.qa/contracts';
1
+ import type { SpecificationRequest } from '@codeless.qa/contracts';
2
2
  import type { ProjectProps } from './prompts.js';
3
3
  type Spinner = {
4
4
  stop: () => void;
5
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
6
  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>;
7
+ export declare function generateTests(token: string, id: string, spinner: Spinner, body: SpecificationRequest['generate']['body']): Promise<void>;
42
8
  export declare function fetchProjectNames(token: string): Promise<string[]>;
43
9
  export declare function validateAuthToken(token: string): Promise<{
44
10
  valid: false;
@@ -1,80 +1,8 @@
1
- import path from 'node:path';
2
- import fs from 'node:fs/promises';
3
1
  import { outro } from '@clack/prompts';
4
2
  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
3
  export async function createSpecification(token, projectProps, file, spinner) {
76
4
  const client = getAuthenticatedClient(token);
77
- const resSpecCreation = await client.specifications.create({
5
+ const resSpecCreation = await client.specification.create({
78
6
  body: {
79
7
  ...projectProps,
80
8
  file
@@ -94,10 +22,7 @@ export async function generateTests(token, id, spinner, body) {
94
22
  const client = getAuthenticatedClient(token);
95
23
  const resTests = await client.specification.generate({
96
24
  params: { id },
97
- body: {
98
- securityObjects: body.securityParams,
99
- server: body.serverParam
100
- }
25
+ body
101
26
  });
102
27
  if (resTests.status !== 200 || 'error' in resTests.body) {
103
28
  spinner.stop();
@@ -1,10 +1,9 @@
1
- import { type SecurityObject, ServerObject } from '@codeless.qa/contracts';
1
+ import { type ServerObject, type SpecificationRequest } from '@codeless.qa/contracts';
2
2
  import { type OpenAPIV3 } from 'openapi-types';
3
3
  export declare function askTestingType(): Promise<string>;
4
4
  export declare function askSpecLocation(): Promise<{
5
5
  spec: OpenAPIV3.Document<{}>;
6
6
  specPath: string;
7
- specString: string;
8
7
  }>;
9
8
  export interface ProjectProps {
10
9
  name: string;
@@ -12,7 +11,9 @@ export interface ProjectProps {
12
11
  version: string;
13
12
  }
14
13
  export declare function askProjectDetails(existingProjectNames: string[], spec: OpenAPIV3.Document): Promise<ProjectProps>;
15
- export declare function askSecurityParams(spec: OpenAPIV3.Document): Promise<SecurityObject[]>;
14
+ type SecurityPayloads = SpecificationRequest['generate']['body']['securityPayloads'];
15
+ export declare function askSecurityParams(spec: OpenAPIV3.Document): Promise<SecurityPayloads>;
16
16
  export declare function askServerParams(spec: OpenAPIV3.Document): Promise<ServerObject>;
17
17
  export declare function askForUserAuth(): Promise<string>;
18
+ export {};
18
19
  //# sourceMappingURL=prompts.d.ts.map
@@ -2,7 +2,6 @@ import os from 'node:os';
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import fsp from 'node:fs/promises';
5
- import { TestParameterType } from '@codeless.qa/contracts';
6
5
  import { select, text, confirm, outro, isCancel } from '@clack/prompts';
7
6
  import { handler as loginFlow } from '../login.js';
8
7
  export async function askTestingType() {
@@ -66,7 +65,7 @@ export async function askSpecLocation() {
66
65
  }
67
66
  const specString = JSON.stringify(spec);
68
67
  await fsp.writeFile(filePath, specString, 'utf8');
69
- return { spec, specPath: filePath, specString };
68
+ return { spec, specPath: filePath };
70
69
  }
71
70
  catch (err) {
72
71
  const error = err instanceof Error ? err : new Error('Unknown error');
@@ -77,7 +76,7 @@ export async function askSpecLocation() {
77
76
  try {
78
77
  const specString = fs.readFileSync(specPath, 'utf8');
79
78
  const spec = JSON.parse(specString);
80
- return { spec, specPath, specString };
79
+ return { spec, specPath };
81
80
  }
82
81
  catch (err) {
83
82
  const error = err instanceof Error ? err : new Error('Unknown error');
@@ -100,7 +99,7 @@ export async function askProjectDetails(existingProjectNames, spec) {
100
99
  };
101
100
  if (key === 'name') {
102
101
  opts.validate = (value) => existingProjectNames.includes(value)
103
- ? `Project naem "${value}" already exists`
102
+ ? `Project name "${value}" already exists`
104
103
  : undefined;
105
104
  }
106
105
  const value = await text(opts);
@@ -117,11 +116,11 @@ export async function askSecurityParams(spec) {
117
116
  !spec.components.securitySchemes ||
118
117
  (Array.isArray(spec.components.securitySchemes) &&
119
118
  spec.components.securitySchemes.length === 0)) {
120
- return [];
119
+ return {};
121
120
  }
122
- const securitySchemes = new Set(Object.values(spec.components.securitySchemes).filter((schema) => 'type' in schema));
123
- const securityObjects = [];
124
- for (const secScheme of securitySchemes) {
121
+ const securitySchemes = new Set(Object.entries(spec.components.securitySchemes).filter(([, schema]) => 'type' in schema));
122
+ const securityObjects = {};
123
+ for (const [name, secScheme] of securitySchemes) {
125
124
  const isBasicAuth = secScheme.type === 'http' && secScheme.scheme === 'basic';
126
125
  const isJWTAuth = secScheme.type === 'http' && secScheme.scheme === 'bearer' && secScheme.bearerFormat === 'JWT';
127
126
  const message = isBasicAuth
@@ -150,17 +149,13 @@ export async function askSecurityParams(spec) {
150
149
  }
151
150
  const scope = secScheme.in.slice(0, 1).toUpperCase() +
152
151
  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
- });
152
+ securityObjects[name] = {
153
+ name: isBasicAuth || isJWTAuth
154
+ ? 'Authorization'
155
+ : secScheme.name,
156
+ value,
157
+ scope
158
+ };
164
159
  }
165
160
  return securityObjects;
166
161
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qa",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "author": "Christian Bromann <christian@codeless.qa>",
5
5
  "description": "Codeless QA CLI",
6
6
  "license": "Apache-2.0",
@@ -19,7 +19,7 @@
19
19
  "uuid": "^9.0.1",
20
20
  "ws": "^8.17.0",
21
21
  "yargs": "^17.7.2",
22
- "@codeless.qa/contracts": "0.0.5"
22
+ "@codeless.qa/contracts": "0.0.7"
23
23
  },
24
24
  "devDependencies": {
25
25
  "replace-in-files-cli": "^3.0.0",