Skip to content

Commit

Permalink
test: workspace and project api
Browse files Browse the repository at this point in the history
  • Loading branch information
JComins000 committed Jul 25, 2024
1 parent 7260f04 commit 779694e
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 36 deletions.
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
108 changes: 108 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,108 @@
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),
// agentWorkspaceGroup: {
// agentGid: 0,
// agentGroup: null,
// agentUid: 0,
// agentWorkspace: null
// },
// checkpointStorageConfig: {},
// defaultComputePool: "string",
// defaultAuxPool: "string"
};
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

0 comments on commit 779694e

Please sign in to comment.