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

SIMSBIOHUB-308: System API User Add Bug #1127

Merged
merged 7 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
119 changes: 115 additions & 4 deletions api/src/database/db-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { expect } from 'chai';
import { QueryResult } from 'pg';
import sinon from 'sinon';
import { z } from 'zod';
import { getZodQueryResult } from './db-utils';
import { SYSTEM_IDENTITY_SOURCE } from '../constants/database';
import {
BceidBasicUserInformation,
BceidBusinessUserInformation,
DatabaseUserInformation,
IdirUserInformation
} from '../utils/keycloak-utils';
import { getGenericizedKeycloakUserInformation, getZodQueryResult } from './db-utils';

/**
* Enforces that a zod schema satisfies an existing type definition.
Expand Down Expand Up @@ -28,7 +37,6 @@ function zodImplements<Model = never>() {
? z.ZodNullableType<z.ZodType<Model[key]>>
: z.ZodType<Model[key]>;
};

return {
with: <
Schema extends ZodImplements<Model> &
Expand All @@ -40,14 +48,117 @@ function zodImplements<Model = never>() {
) => z.object(schema)
};
}

describe('getZodQueryResult', () => {
it('defines a zod schema that conforms to the real pg `QueryResult` type', () => {
const zodQueryResultRow = z.object({});

const zodQueryResult = getZodQueryResult(zodQueryResultRow);

// Not a traditional test: will just cause a compile error if the zod schema doesn't satisfy the `QueryResult` type
zodImplements<QueryResult>().with(zodQueryResult.shape);

// Dummy assertion to satisfy linter
expect(true).to.be.true;
});
});

describe('getGenericizedKeycloakUserInformation', () => {
afterEach(() => {
sinon.restore();
});

it('identifies a database user information object and returns null', () => {
const keycloakUserInformation: DatabaseUserInformation = {
database_user_guid: '123456789',
identity_provider: 'database',
username: 'biohub_dapi_v1'
};

const result = getGenericizedKeycloakUserInformation(keycloakUserInformation);

expect(result).to.be.null;
});

it('identifies an idir user information object and returns a genericized object', () => {
const keycloakUserInformation: IdirUserInformation = {
idir_user_guid: '123456789',
identity_provider: 'idir',
idir_username: 'testuser',
email_verified: false,
name: 'test user',
preferred_username: 'testguid@idir',
display_name: 'test user',
given_name: 'test',
family_name: 'user',
email: '[email protected]'
};

const result = getGenericizedKeycloakUserInformation(keycloakUserInformation);

expect(result).to.eql({
user_guid: keycloakUserInformation.idir_user_guid,
user_identifier: keycloakUserInformation.idir_username,
user_identity_source: SYSTEM_IDENTITY_SOURCE.IDIR,
display_name: keycloakUserInformation.display_name,
email: keycloakUserInformation.email,
given_name: keycloakUserInformation.given_name,
family_name: keycloakUserInformation.family_name
});
});

it('identifies a bceid business user information object and returns a genericized object', () => {
const keycloakUserInformation: BceidBusinessUserInformation = {
bceid_business_guid: '1122334455',
bceid_business_name: 'Business Name',
bceid_user_guid: '123456789',
identity_provider: 'bceidbusiness',
bceid_username: 'tname',
name: 'Test Name',
preferred_username: '123456789@bceidbusiness',
display_name: 'Test Name',
email: '[email protected]',
email_verified: false,
given_name: 'Test',
family_name: ''
};

const result = getGenericizedKeycloakUserInformation(keycloakUserInformation);

expect(result).to.eql({
user_guid: keycloakUserInformation.bceid_user_guid,
user_identifier: keycloakUserInformation.bceid_username,
user_identity_source: SYSTEM_IDENTITY_SOURCE.BCEID_BUSINESS,
display_name: keycloakUserInformation.display_name,
email: keycloakUserInformation.email,
given_name: keycloakUserInformation.given_name,
family_name: keycloakUserInformation.family_name,
agency: keycloakUserInformation.bceid_business_name
});
});

it('identifies a bceid basic user information object and returns a genericized object', () => {
const keycloakUserInformation: BceidBasicUserInformation = {
bceid_user_guid: '123456789',
identity_provider: 'bceidbasic',
bceid_username: 'tname',
name: 'Test Name',
preferred_username: '123456789@bceidbasic',
display_name: 'Test Name',
email: '[email protected]',
email_verified: false,
given_name: 'Test',
family_name: ''
};

const result = getGenericizedKeycloakUserInformation(keycloakUserInformation);

expect(result).to.eql({
user_guid: keycloakUserInformation.bceid_user_guid,
user_identifier: keycloakUserInformation.bceid_username,
user_identity_source: SYSTEM_IDENTITY_SOURCE.BCEID_BASIC,
display_name: keycloakUserInformation.display_name,
email: keycloakUserInformation.email,
given_name: keycloakUserInformation.given_name,
family_name: keycloakUserInformation.family_name
});
});
});
77 changes: 77 additions & 0 deletions api/src/database/db-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { z } from 'zod';
import { SYSTEM_IDENTITY_SOURCE } from '../constants/database';
import { ApiExecuteSQLError } from '../errors/api-error';
import {
isBceidBusinessUserInformation,
isDatabaseUserInformation,
isIdirUserInformation,
KeycloakUserInformation
} from '../utils/keycloak-utils';

/**
* A type for a set of generic keycloak user information properties.
*/
type GenericizedKeycloakUserInformation = {
user_guid: string;
user_identifier: string;
user_identity_source: SYSTEM_IDENTITY_SOURCE;
display_name: string;
email: string;
given_name: string;
family_name: string;
agency?: string;
};

/**
* An asynchronous wrapper function that will catch any exceptions thrown by the wrapped function
Expand Down Expand Up @@ -81,3 +102,59 @@ export const getZodQueryResult = <T extends z.Schema>(zodQueryResultRow: T) =>
})
)
});

/**
* Converts a type specific keycloak user information object with type specific properties into a new object with
* generic properties.
*
* @param {KeycloakUserInformation} keycloakUserInformation
* @return {*} {(GenericizedKeycloakUserInformation | null)}
*/
export const getGenericizedKeycloakUserInformation = (
keycloakUserInformation: KeycloakUserInformation
): GenericizedKeycloakUserInformation | null => {
let data: GenericizedKeycloakUserInformation | null;

if (isDatabaseUserInformation(keycloakUserInformation)) {
// Don't patch internal database user records
return null;
}

// We don't yet know at this point what kind of token was used (idir vs bceid basic, etc).
// Determine which type it is, and parse the information into a generic structure that is supported by the
// database patch function
if (isIdirUserInformation(keycloakUserInformation)) {
data = {
user_guid: keycloakUserInformation.idir_user_guid,
user_identifier: keycloakUserInformation.idir_username,
user_identity_source: SYSTEM_IDENTITY_SOURCE.IDIR,
display_name: keycloakUserInformation.display_name,
email: keycloakUserInformation.email,
given_name: keycloakUserInformation.given_name,
family_name: keycloakUserInformation.family_name
};
} else if (isBceidBusinessUserInformation(keycloakUserInformation)) {
data = {
user_guid: keycloakUserInformation.bceid_user_guid,
user_identifier: keycloakUserInformation.bceid_username,
user_identity_source: SYSTEM_IDENTITY_SOURCE.BCEID_BUSINESS,
display_name: keycloakUserInformation.display_name,
email: keycloakUserInformation.email,
given_name: keycloakUserInformation.given_name,
family_name: keycloakUserInformation.family_name,
agency: keycloakUserInformation.bceid_business_name
};
} else {
curtisupshall marked this conversation as resolved.
Show resolved Hide resolved
data = {
user_guid: keycloakUserInformation.bceid_user_guid,
user_identifier: keycloakUserInformation.bceid_username,
user_identity_source: SYSTEM_IDENTITY_SOURCE.BCEID_BASIC,
display_name: keycloakUserInformation.display_name,
email: keycloakUserInformation.email,
given_name: keycloakUserInformation.given_name,
family_name: keycloakUserInformation.family_name
};
}

return data;
};
Loading
Loading