Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: workspace and project api [TESTENG-46] #9731

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions webui/react/src/e2e/fixtures/api.project.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import streamConsumers from 'stream/consumers';

import _ from 'lodash';

import { expect } from 'e2e/fixtures/global-fixtures';
import { safeName } from 'e2e/utils/naming';
import { ProjectsApi, V1PostProjectRequest, V1PostProjectResponse } from 'services/api-ts-sdk/api';

import { ApiAuthFixture } from './api.auth.fixture';

export class ApiProjectFixture {
readonly apiAuth: ApiAuthFixture;
constructor(apiAuth: ApiAuthFixture) {
this.apiAuth = apiAuth;
}

new({ projectProps = {}, projectPrefix = 'test-project' } = {}): V1PostProjectRequest {
const defaults = {
name: safeName(projectPrefix),
workspaceId: 0,
};
return {
...defaults,
...projectProps,
};
}

private static normalizeUrl(url: string): string {
if (url.endsWith('/')) {
return url.substring(0, url.length - 1);
}
return url;
}

private async startProjectRequest(): Promise<ProjectsApi> {
return new ProjectsApi(
{ apiKey: await this.apiAuth.getBearerToken() },
ApiProjectFixture.normalizeUrl(this.apiAuth.baseURL),
fetch,
);
}

/**
* Creates a project with the given parameters via the API.
* @param {number} workspaceId workspace id to create the project in.
* @param {V1PostProjectRequest} req the project request with the config for the new project.
* See apiProject.newRandom() for the default config.
* @returns {Promise<V1PostProjectRequest>} Representation of the created project. The request is returned since the
* password is not stored on the V1Project object and it is not returned in the response. However the Request is a
* strict superset of the Response, so no info is lost.
*/
async createProject(
workspaceId: number,
req: V1PostProjectRequest,
): Promise<V1PostProjectResponse> {
const projectResp = await (await this.startProjectRequest())
.postProject(workspaceId, req, {})
.catch(async function (error) {
const respBody = await streamConsumers.text(error.body);
throw new Error(
`Create Project Request failed. Status: ${error.status} Request: ${JSON.stringify(
req,
)} Response: ${respBody}`,
);
});
return _.merge(req, projectResp);
}

/**
*
* @summary Delete a project.
* @param {number} id The id of the project.
*/
async deleteProject(id: number): Promise<void> {
await expect
.poll(
async () => {
const projectResp = await (await this.startProjectRequest())
.deleteProject(id)
.catch(async function (error) {
const respBody = await streamConsumers.text(error.body);
if (error.status === 404) {
return { completed: true };
}
throw new Error(
`Delete Project Request failed. Status: ${error.status} Request: ${JSON.stringify(
id,
)} Response: ${respBody}`,
);
});
return projectResp.completed;
},
{
message: `Delete Project Request failed ${JSON.stringify(id)}`,
timeout: 15_000,
},
)
.toBe(true);
}
}
10 changes: 8 additions & 2 deletions webui/react/src/e2e/fixtures/api.user.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import streamConsumers from 'stream/consumers';
import _ from 'lodash';

import { randIdAlphanumeric, safeName } from 'e2e/utils/naming';
import { UsersApi, V1PatchUser, V1PostUserRequest, V1User } from 'services/api-ts-sdk/api';
import {
UsersApi,
V1PatchUser,
V1PostUserRequest,
V1PostUserResponse,
V1User,
} from 'services/api-ts-sdk/api';

import { ApiAuthFixture } from './api.auth.fixture';

Expand Down Expand Up @@ -52,7 +58,7 @@ export class ApiUserFixture {
* password is not stored on the V1User object and it is not returned in the response. However the Request is a
* strict superset of the Response, so no info is lost.
*/
async createUser(req: V1PostUserRequest): Promise<V1PostUserRequest> {
async createUser(req: V1PostUserRequest): Promise<V1PostUserResponse> {
const userResp = await (await this.startUserRequest())
.postUser(req, {})
.catch(async function (error) {
Expand Down
99 changes: 99 additions & 0 deletions webui/react/src/e2e/fixtures/api.workspace.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import streamConsumers from 'stream/consumers';

import _ from 'lodash';

import { expect } from 'e2e/fixtures/global-fixtures';
import { safeName } from 'e2e/utils/naming';
import {
V1PostWorkspaceRequest,
V1PostWorkspaceResponse,
WorkspacesApi,
} from 'services/api-ts-sdk/api';

import { ApiAuthFixture } from './api.auth.fixture';

export class ApiWorkspaceFixture {
readonly apiAuth: ApiAuthFixture;
constructor(apiAuth: ApiAuthFixture) {
this.apiAuth = apiAuth;
}

new({ workspaceProps = {}, workspacePrefix = 'test-workspace' } = {}): V1PostWorkspaceRequest {
const defaults = {
name: safeName(workspacePrefix),
};
return {
...defaults,
...workspaceProps,
};
}

private static normalizeUrl(url: string): string {
if (url.endsWith('/')) {
return url.substring(0, url.length - 1);
}
return url;
}

private async startWorkspaceRequest(): Promise<WorkspacesApi> {
return new WorkspacesApi(
{ apiKey: await this.apiAuth.getBearerToken() },
ApiWorkspaceFixture.normalizeUrl(this.apiAuth.baseURL),
fetch,
);
}

/**
* Creates a workspace with the given parameters via the API.
* @param {V1PostWorkspaceRequest} req the workspace request with the config for the new workspace.
* See apiWorkspace.newRandom() for the default config.
* @returns {Promise<V1PostWorkspaceRequest>} Representation of the created workspace. The request is returned since the
* password is not stored on the V1Workspace object and it is not returned in the response. However the Request is a
* strict superset of the Response, so no info is lost.
*/
async createWorkspace(req: V1PostWorkspaceRequest): Promise<V1PostWorkspaceResponse> {
const workspaceResp = await (await this.startWorkspaceRequest())
.postWorkspace(req, {})
.catch(async function (error) {
const respBody = await streamConsumers.text(error.body);
throw new Error(
`Create Workspace Request failed. Status: ${error.status} Request: ${JSON.stringify(
req,
)} Response: ${respBody}`,
);
});
return _.merge(req, workspaceResp);
}

/**
*
* @summary Delete a workspace.
* @param {number} id The id of the workspace.
*/
async deleteWorkspace(id: number): Promise<void> {
await expect
.poll(
async () => {
const workspaceResp = await (await this.startWorkspaceRequest())
.deleteWorkspace(id)
.catch(async function (error) {
const respBody = await streamConsumers.text(error.body);
if (error.status === 404) {
return { completed: true };
}
throw new Error(
`Delete Workspace Request failed. Status: ${error.status} Request: ${JSON.stringify(
id,
)} Response: ${respBody}`,
);
});
return workspaceResp.completed;
},
{
message: `Delete Project Request failed ${JSON.stringify(id)}`,
timeout: 15_000,
},
)
.toBe(true);
}
}
Loading
Loading