Skip to content

Commit

Permalink
Add happy case test
Browse files Browse the repository at this point in the history
  • Loading branch information
darunrs committed Aug 10, 2024
1 parent 87012a0 commit efbb5c4
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ProvisioiningState correctly fetch metadata for source and schema 1`] = `
[
{
"delete_permissions": [
{
"permission": {},
"role": "account_id",
},
],
"insert_permissions": [
{
"permission": {},
"role": "account_id",
},
],
"select_permissions": [
{
"permission": {},
"role": "account_id",
},
],
"table": {
"name": "tableA",
"schema": "account_id_function_name",
},
"update_permissions": [
{
"permission": {},
"role": "account_id",
},
],
},
{
"delete_permissions": [
{
"permission": {},
"role": "account_id",
},
],
"insert_permissions": [
{
"permission": {},
"role": "account_id",
},
],
"select_permissions": [
{
"permission": {},
"role": "account_id",
},
],
"table": {
"name": "tableB",
"schema": "account_id_function_name",
},
"update_permissions": [
{
"permission": {},
"role": "account_id",
},
],
},
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('ProvisioiningState', () => {
);

it('can create state whether source exists or not', async () => {
const sourceWithoutUser = generateSourceWithTables(['some_schema'], ['tableA', 'tableB'], 'someAccount');
const mockExportMetadata = jest.fn().mockResolvedValue(sourceWithoutUser);
const metadataWithoutUser = generateHasuraMetadata(['some_schema'], ['tableA', 'tableB'], 'someAccount');
const mockExportMetadata = jest.fn().mockResolvedValue(metadataWithoutUser);
const mockGetTableNames = jest.fn().mockResolvedValue([]);
const mockHasuraClient = {
exportMetadata: mockExportMetadata,
Expand All @@ -26,9 +26,43 @@ describe('ProvisioiningState', () => {
expect(provisioningState.doesSchemaExist()).toBe(false);
expect(provisioningState.getCreatedTables()).toEqual([]);
});

it('state works with existing source', async () => {
const metadataWithUser = generateHasuraMetadata([provisioningConfig.schemaName(), 'some_schema'], ['tableA', 'tableB'], provisioningConfig.hasuraRoleName());
metadataWithUser.sources.push(generateSourceWithTables(['anotherSchema'], ['anotherTable'], 'anotherRole'));
const mockExportMetadata = jest.fn().mockResolvedValue(metadataWithUser);
const mockGetTableNames = jest.fn().mockResolvedValue(['tableA']);
const mockHasuraClient = {
exportMetadata: mockExportMetadata,
getTableNames: mockGetTableNames,
} as unknown as HasuraClient;

const provisioningState = await ProvisioningState.loadProvisioningState(mockHasuraClient, provisioningConfig);
expect(provisioningState.doesSourceExist()).toBe(true);
expect(provisioningState.doesSchemaExist()).toBe(true);
expect(provisioningState.getCreatedTables()).toEqual(['tableA']);
});

it('correctly fetch metadata for source and schema', async () => {
const metadataWithUser = generateHasuraMetadata([provisioningConfig.schemaName(), 'some_schema'], ['tableA', 'tableB'], provisioningConfig.hasuraRoleName());
metadataWithUser.sources.push(generateSourceWithTables(['anotherSchema'], ['anotherTable'], 'anotherRole'));
const mockExportMetadata = jest.fn().mockResolvedValue(metadataWithUser);
const mockGetTableNames = jest.fn().mockResolvedValue(['tableA']);
const mockHasuraClient = {
exportMetadata: mockExportMetadata,
getTableNames: mockGetTableNames,
} as unknown as HasuraClient;

const provisioningState = await ProvisioningState.loadProvisioningState(mockHasuraClient, provisioningConfig);
expect(provisioningState.getSourceMetadata().name).toBe(provisioningConfig.hasuraRoleName());
expect(provisioningState.getMetadataForTables().length).toBe(2);
expect(provisioningState.getMetadataForTables()).toMatchSnapshot();
expect(provisioningState.getTrackedTables()).toEqual(['tableA', 'tableB']);
expect(provisioningState.getTablesWithPermissions()).toEqual(['tableA', 'tableB']);
});
});

function generateSourceWithTables (schemaNames: string[], tableNames: string[], role: string): HasuraMetadata {
function generateHasuraMetadata (schemaNames: string[], tableNames: string[], role: string): HasuraMetadata {
const sources: HasuraSource[] = [];
// Insert default source which has different format than the rest
sources.push({
Expand All @@ -42,23 +76,27 @@ function generateSourceWithTables (schemaNames: string[], tableNames: string[],
}
});

sources.push(generateSourceWithTables(schemaNames, tableNames, role));

return {
version: 3,
sources
};
}

function generateSourceWithTables (schemaNames: string[], tableNames: string[], role: string): HasuraSource {
const tables: HasuraTableMetadata[] = [];
schemaNames.forEach((schemaName) => {
tableNames.forEach((tableName) => {
tables.push(generateTableConfig(schemaName, tableName, role));
});
});

sources.push({
return {
name: role,
kind: 'postgres',
tables,
configuration: generateHasuraConfiguration(role, 'password'),
});

return {
version: 3,
sources
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ export default class ProvisioningState {

private tableContainsAllPermissions (tableMetadata: HasuraTableMetadata): boolean {
const allPermissions: string[] = HASURA_PERMISSION_TYPES.map(permission => `${permission}_permissions`);
return allPermissions.every(permission => Object.keys(tableMetadata).includes(`${permission}_permissions`));
const metadataKeys = Object.keys(tableMetadata);
return allPermissions.every(permission => metadataKeys.includes(permission));
}

// Does not check for partial permissions
getTablesWithPermissions (): string[] {
const tableMetadataList = this.getMetadataForTables();
return tableMetadataList.filter(metadata => this.tableContainsAllPermissions(metadata)).map(metadata => metadata.table.name);
return tableMetadataList
.filter(metadata => this.tableContainsAllPermissions(metadata))
.map(metadata => metadata.table.name);
}
}

0 comments on commit efbb5c4

Please sign in to comment.