Skip to content

Commit

Permalink
Merge pull request #63 from Gateway-DAO/feature/prot-350-create-data-…
Browse files Browse the repository at this point in the history
…asset-class

tests: modifed unit test for data assets
  • Loading branch information
Siddharth9890 authored Sep 13, 2024
2 parents 8b998b8 + e2792b5 commit 94b45a1
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 28 deletions.
46 changes: 44 additions & 2 deletions scripts/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,61 @@ import fs from 'fs';

const getJSONSchema = async () => {
try {
const data = await fetch('https://dev.api.gateway.tech/swagger/doc.json');
const data = await fetch('https://dev.api.gateway.tech/docs/swagger.json');

const body = await data.json();

const types = generateTypes(body.definitions);

const routes = generateRouteConstants(body.paths);

fs.writeFileSync('./src/common/types.ts', types);
fs.writeFileSync('./src/common/routes.ts', routes);
} catch (error) {
console.log(error);
}
};

type PathItem = Record<string, Operation>;

interface Operation {
summary: string;
}

function generateRouteConstants(paths: Record<string, PathItem>): string {
const constants: string[] = ['export const routes = {'];

for (const path in paths) {
const pathItem = paths[path];

for (const method in pathItem) {
const operation = pathItem[method];
const constantName = generateRouteConstantName(
method,
path,
operation.summary,
);
const parameterizedPath = path;
constants.push(` ${constantName}: "${parameterizedPath}",`);
}
}

constants.push('};');
return constants.join('\n');
}

function generateRouteConstantName(
method: string,
path: string,
summary: string,
): string {
let name = summary.trim();
if (name === '') {
name = path;
}
return toPascalCase(name);
}

function toPascalCase(str: string): string {
const cleanedStr = str.replace(/^model\./, '');
return cleanedStr
Expand Down Expand Up @@ -108,7 +151,6 @@ export interface OpenAPIClient<Paths extends {}, Media extends MediaType> {
`export type ${toPascalCase(typeName)} = ${processSchema(schema)};\n`,
);
});

return types.join('\n');
};

Expand Down
22 changes: 22 additions & 0 deletions src/common/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const routes = {
CreateAccount: '/accounts',
GetMyAccount: '/accounts/me',
AuthenticateAccount: '/auth',
GenerateSignMessage: '/auth/message',
RefreshToken: '/auth/refresh-token',
CreateANewDataAsset: '/data-assets',
GetMyDataAssets: '/data-assets/me',
GetDataAssetByID: '/data-assets/{id}',
UpdateDataAssetByID: '/data-assets/{id}',
DeleteDataAssetByID: '/data-assets/{id}',
UpdateACLItemsToDataAsset: '/data-assets/{id}/acl',
AssignACLItemsToDataAsset: '/data-assets/{id}/acl',
DeleteAssignedRoleByACL: '/data-assets/{id}/acl',
DownloadDataAssetByID: '/data-assets/{id}/download',
ShareDataAssetByID: '/data-assets/{id}/share',
GetDataModels: '/data-models',
CreateDataModel: '/data-models',
GetDataModelsByUser: '/data-models/me',
GetDataModelByID: '/data-models/{id}',
UpdateDataModel: '/data-models/{id}',
};
21 changes: 12 additions & 9 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
mockPost,
successMessage,
} from './stubs/common.stub';
import { routes } from '../src/common/routes';
jest.mock('openapi-fetch');

let auth: Auth;
Expand All @@ -30,15 +31,15 @@ describe('Auth Unit Test', () => {

const message = await auth.generateSignMessage();
expect(message).toBeDefined();
expect(mockGet).toHaveBeenCalled();
expect(mockGet).toHaveBeenCalledWith(routes.GenerateSignMessage);
});

it('should throw GTWError for generate sign message', async () => {
mockGet.mockResolvedValue(errorMessage());

await expect(auth.generateSignMessage()).rejects.toThrow(GTWError);
expect(mockGet).toHaveBeenCalledWith('/auth/message');
expect(mockGet).toHaveBeenCalled();
expect(mockGet).toHaveBeenCalledWith(routes.GenerateSignMessage);
});

test('should generate refresh token', async () => {
Expand All @@ -47,15 +48,14 @@ describe('Auth Unit Test', () => {
const message = await auth.generateRefreshToken();

expect(message).toBeDefined();
expect(mockGet).toHaveBeenCalled();
expect(mockGet).toHaveBeenCalledWith(routes.RefreshToken);
});

it('should throw GTWError for generate refresh token', async () => {
mockGet.mockResolvedValue(errorMessage());

await expect(auth.generateRefreshToken()).rejects.toThrow(GTWError);
expect(mockGet).toHaveBeenCalledWith('/auth/refresh-token');
expect(mockGet).toHaveBeenCalled();
expect(mockGet).toHaveBeenCalledWith(routes.RefreshToken);
});

test('should login using etherum wallet', async () => {
Expand All @@ -64,7 +64,9 @@ describe('Auth Unit Test', () => {
const message = await auth.login(authDetails());

expect(message).toBeDefined();
expect(mockPost).toHaveBeenCalled();
expect(mockPost).toHaveBeenCalledWith(routes.AuthenticateAccount, {
body: authDetails(),
});
});

test('should login using solana wallet', async () => {
Expand All @@ -73,16 +75,17 @@ describe('Auth Unit Test', () => {
const message = await auth.login(authSolanaDetails());

expect(message).toBeDefined();
expect(mockPost).toHaveBeenCalled();
expect(mockPost).toHaveBeenCalledWith(routes.AuthenticateAccount, {
body: authDetails(),
});
});

it('should throw GTWError for login', async () => {
mockPost.mockResolvedValue(errorMessage());

await expect(auth.login(authDetails())).rejects.toThrow(GTWError);
expect(mockPost).toHaveBeenCalledWith('/auth', {
expect(mockPost).toHaveBeenCalledWith(routes.AuthenticateAccount, {
body: authDetails(),
});
expect(mockPost).toHaveBeenCalled();
});
});
77 changes: 61 additions & 16 deletions test/data-asset.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createClient from 'openapi-fetch';
import {
bodyStub,
errorMessage,
ID,
linksStub,
Expand All @@ -9,6 +10,7 @@ import {
mockGet,
mockPost,
mockPut,
paramsStub,
successMessage,
} from './stubs/common.stub';
import { DataAsset } from '../src/modules/data-asset/data-asset';
Expand All @@ -20,6 +22,7 @@ import {
blobStub,
dataAssetStub,
} from './stubs/data-asset.stub';
import { routes } from '../src/common/routes';

jest.mock('openapi-fetch');

Expand All @@ -43,7 +46,10 @@ describe('Data Assets Test', () => {
const pdaId = await dataAsset.createClaimBasedDataAsset();

expect(pdaId).toBeDefined();
expect(mockPost).toHaveBeenCalled();
expect(mockPost).toHaveBeenCalledWith(
routes.CreateANewDataAsset,
bodyStub(),
);
});

it('should throw GTWError for create claim based data asset', async () => {
Expand All @@ -52,7 +58,10 @@ describe('Data Assets Test', () => {
await expect(dataAsset.createClaimBasedDataAsset()).rejects.toThrow(
GTWError,
);
expect(mockPost).toHaveBeenCalled();
expect(mockPost).toHaveBeenCalledWith(
routes.CreateANewDataAsset,
bodyStub(),
);
});

it('should get my data assets', async () => {
Expand All @@ -66,15 +75,15 @@ describe('Data Assets Test', () => {

expect(data).toBeDefined();
expect(data.data.length).toBeGreaterThan(0);
expect(mockGet).toHaveBeenCalled();
expect(mockGet).toHaveBeenCalledWith(routes.GetMyDataAssets, paramsStub());
});

it('should throw GTWError for get my data assets', async () => {
mockGet.mockResolvedValue(errorMessage());

await expect(dataAsset.getMyDataAssets()).rejects.toThrow(GTWError);

expect(mockGet).toHaveBeenCalled();
expect(mockGet).toHaveBeenCalledWith(routes.GetMyDataAssets, paramsStub());
});

it('should get data asset by id', async () => {
Expand All @@ -87,15 +96,21 @@ describe('Data Assets Test', () => {
const data = await dataAsset.getDataAssetById(ID);

expect(data).toBeDefined();
expect(mockGet).toHaveBeenCalled();
expect(mockGet).toHaveBeenCalledWith(
routes.GetDataAssetByID,
paramsStub({ params: { path: { id: 1 } } }),
);
});

it('should throw GTWError for get data asset by id', async () => {
mockGet.mockResolvedValue(errorMessage());

await expect(dataAsset.getDataAssetById(ID)).rejects.toThrow(GTWError);

expect(mockGet).toHaveBeenCalled();
expect(mockGet).toHaveBeenCalledWith(
routes.GetDataAssetByID,
paramsStub({ params: { path: { id: 1 } } }),
);
});

it('should update data asset', async () => {
Expand All @@ -104,14 +119,20 @@ describe('Data Assets Test', () => {
const updatedDataAsset = await dataAsset.updateDataAsset(ID);

expect(updatedDataAsset).toBeDefined();
expect(mockPut).toHaveBeenCalled();
expect(mockPut).toHaveBeenCalledWith(routes.UpdateDataAssetByID, {
body: {},
params: paramsStub({ params: { path: { id: 1 } } }).params,
});
});

it('should throw GTWError for update data asset', async () => {
mockPut.mockResolvedValue(errorMessage());

await expect(dataAsset.updateDataAsset(ID)).rejects.toThrow(GTWError);
expect(mockPut).toHaveBeenCalled();
expect(mockPut).toHaveBeenCalledWith(routes.UpdateDataAssetByID, {
body: {},
params: paramsStub({ params: { path: { id: 1 } } }).params,
});
});

it('should delete data asset by id', async () => {
Expand All @@ -120,14 +141,20 @@ describe('Data Assets Test', () => {
const deleteDataAsset = await dataAsset.deleteDataAsset(ID);

expect(deleteDataAsset).toBeDefined();
expect(mockDelete).toHaveBeenCalled();
expect(mockDelete).toHaveBeenCalledWith(
routes.DeleteDataAssetByID,
paramsStub({ params: { path: { id: 1 } } }),
);
});

it('should throw GTWError for delete data asset by id', async () => {
mockDelete.mockResolvedValue(errorMessage());

await expect(dataAsset.deleteDataAsset(ID)).rejects.toThrow(GTWError);
expect(mockDelete).toHaveBeenCalled();
expect(mockDelete).toHaveBeenCalledWith(
routes.DeleteDataAssetByID,
paramsStub({ params: { path: { id: 1 } } }),
);
});

it('should update acl', async () => {
Expand All @@ -136,7 +163,10 @@ describe('Data Assets Test', () => {
const aclList = await dataAsset.updateACL(ID, [aclListStub()]);

expect(aclList).toBeDefined();
expect(mockPut).toHaveBeenCalled();
expect(mockPut).toHaveBeenCalledWith(routes.UpdateACLItemsToDataAsset, {
body: bodyStub({ body: [aclListStub()] }).body,
params: paramsStub({ params: { path: { id: 1 } } }).params,
});
});

it('should throw GTWError for updating acl', async () => {
Expand All @@ -145,7 +175,10 @@ describe('Data Assets Test', () => {
await expect(dataAsset.updateACL(ID, [aclListStub()])).rejects.toThrow(
GTWError,
);
expect(mockPut).toHaveBeenCalled();
expect(mockPut).toHaveBeenCalledWith(routes.UpdateACLItemsToDataAsset, {
body: bodyStub({ body: [aclListStub()] }).body,
params: paramsStub({ params: { path: { id: 1 } } }).params,
});
});

it('should override acl', async () => {
Expand All @@ -154,7 +187,10 @@ describe('Data Assets Test', () => {
const aclList = await dataAsset.overrideACL(ID, [aclListStub()]);

expect(aclList).toBeDefined();
expect(mockPost).toHaveBeenCalled();
expect(mockPost).toHaveBeenCalledWith(routes.AssignACLItemsToDataAsset, {
body: bodyStub({ body: [aclListStub()] }).body,
params: paramsStub({ params: { path: { id: 1 } } }).params,
});
});

it('should throw GTWError for override acl', async () => {
Expand All @@ -163,7 +199,10 @@ describe('Data Assets Test', () => {
await expect(dataAsset.overrideACL(ID, [aclListStub()])).rejects.toThrow(
GTWError,
);
expect(mockPost).toHaveBeenCalled();
expect(mockPost).toHaveBeenCalledWith(routes.AssignACLItemsToDataAsset, {
body: bodyStub({ body: [aclListStub()] }).body,
params: paramsStub({ params: { path: { id: 1 } } }).params,
});
});

it('should delete acl', async () => {
Expand All @@ -172,7 +211,10 @@ describe('Data Assets Test', () => {
const message = await dataAsset.deleteACL(ID, [aclListStub()]);

expect(message).toBeDefined();
expect(mockDelete).toHaveBeenCalled();
expect(mockDelete).toHaveBeenCalledWith(routes.DeleteAssignedRoleByACL, {
body: bodyStub({ body: [aclListStub()] }).body,
params: paramsStub({ params: { path: { id: 1 } } }).params,
});
});

it('should throw GTWError for updating acl', async () => {
Expand All @@ -181,7 +223,10 @@ describe('Data Assets Test', () => {
await expect(dataAsset.deleteACL(ID, [aclListStub()])).rejects.toThrow(
GTWError,
);
expect(mockDelete).toHaveBeenCalled();
expect(mockDelete).toHaveBeenCalledWith(routes.DeleteAssignedRoleByACL, {
body: bodyStub({ body: [aclListStub()] }).body,
params: paramsStub({ params: { path: { id: 1 } } }).params,
});
});

it('should share data asset', async () => {
Expand Down
Loading

0 comments on commit 94b45a1

Please sign in to comment.