diff --git a/server-api/src/core/generated/alkemio-schema.ts b/server-api/src/core/generated/alkemio-schema.ts index 178dee1b..6371325b 100644 --- a/server-api/src/core/generated/alkemio-schema.ts +++ b/server-api/src/core/generated/alkemio-schema.ts @@ -190765,3 +190765,21 @@ export type VirtualContributorQuery = { }; }; }; + +export type GetMyUserInfoQueryVariables = Exact<{ [key: string]: never }>; + +export type GetMyUserInfoQuery = { + me: { + user?: + | { + id: string; + nameID: string; + email: string; + profile: { id: string; displayName: string }; + agent: { id: string }; + account?: { id: string } | undefined; + } + | undefined; + }; + platform: { myRoles: Array }; +}; diff --git a/server-api/src/core/generated/graphql.ts b/server-api/src/core/generated/graphql.ts index 57ca4242..0036f086 100644 --- a/server-api/src/core/generated/graphql.ts +++ b/server-api/src/core/generated/graphql.ts @@ -202459,6 +202459,26 @@ export type VirtualContributorQuery = { }; }; +export type GetMyUserInfoQueryVariables = SchemaTypes.Exact<{ + [key: string]: never; +}>; + +export type GetMyUserInfoQuery = { + me: { + user?: + | { + id: string; + nameID: string; + email: string; + profile: { id: string; displayName: string }; + agent: { id: string }; + account?: { id: string } | undefined; + } + | undefined; + }; + platform: { myRoles: Array }; +}; + export const UserDataLightFragmentDoc = gql` fragment UserDataLight on User { id @@ -206379,6 +206399,30 @@ export const VirtualContributorDocument = gql` } ${TagsetDetailsFragmentDoc} `; +export const GetMyUserInfoDocument = gql` + query getMyUserInfo { + me { + user { + id + nameID + email + profile { + id + displayName + } + agent { + id + } + account { + id + } + } + } + platform { + myRoles + } + } +`; export type SdkFunctionWrapper = ( action: (requestHeaders?: Record) => Promise, @@ -206681,6 +206725,7 @@ const GetUserReferenceUriDocumentString = print(GetUserReferenceUriDocument); const GetUsersDataDocumentString = print(GetUsersDataDocument); const MeQueryDocumentString = print(MeQueryDocument); const VirtualContributorDocumentString = print(VirtualContributorDocument); +const GetMyUserInfoDocumentString = print(GetMyUserInfoDocument); export function getSdk( client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper @@ -210074,6 +210119,28 @@ export function getSdk( variables ); }, + getMyUserInfo( + variables?: SchemaTypes.GetMyUserInfoQueryVariables, + requestHeaders?: GraphQLClientRequestHeaders + ): Promise<{ + data: SchemaTypes.GetMyUserInfoQuery; + errors?: GraphQLError[]; + extensions?: any; + headers: Headers; + status: number; + }> { + return withWrapper( + wrappedRequestHeaders => + client.rawRequest( + GetMyUserInfoDocumentString, + variables, + { ...requestHeaders, ...wrappedRequestHeaders } + ), + 'getMyUserInfo', + 'query', + variables + ); + }, }; } export type Sdk = ReturnType; diff --git a/server-api/src/functional-api/contributor-management/organization/organization-settings.it-spec.ts b/server-api/src/functional-api/contributor-management/organization/organization-settings.it-spec.ts index 2d35ea13..f4b61d9a 100644 --- a/server-api/src/functional-api/contributor-management/organization/organization-settings.it-spec.ts +++ b/server-api/src/functional-api/contributor-management/organization/organization-settings.it-spec.ts @@ -1,13 +1,19 @@ -import { updateOrganization } from './organization.request.params'; +import { getOrganizationData, updateOrganization } from './organization.request.params'; import { updateOrganizationSettings } from './organization.request.params'; -import { TestUser } from '@alkemio/tests-lib'; +import { TestUser, UniqueIDGenerator } from '@alkemio/tests-lib'; import { assignUserAsOrganizationOwner } from './organization-authorization-mutation'; import { OrganizationWithSpaceModel } from '@src/scenario/models/OrganizationWithSpaceModel'; import { TestScenarioFactory } from '@src/scenario/TestScenarioFactory'; import { TestScenarioConfig } from '@src/scenario/config/test-scenario-config'; import { TestUserManager } from '@src/scenario/TestUserManager'; +import { deleteUser, registerVerifiedUser } from '../user/user.request.params'; +import { eventOnOrganizationVerification } from './organization-verification.events.request.params'; -const domain = 'alkem.io'; +const uniqueId = UniqueIDGenerator.getID(); +const firstName = `fn${uniqueId}`; +const lastName = `ln${uniqueId}`; +let userId: string; +const domain = `alkem${uniqueId}.io`; let baseScenario: OrganizationWithSpaceModel; // Note: no space, just the org to make this test suite much faster @@ -35,7 +41,7 @@ beforeAll(async () => { }); afterAll(async () => { - //await TestScenarioFactory.cleanUpBaseScenario(baseScenario); + await TestScenarioFactory.cleanUpBaseScenario(baseScenario); }); describe('Organization settings', () => { @@ -54,7 +60,7 @@ describe('Organization settings', () => { baseScenario.organization.id, { membership: { - allowUsersMatchingDomainToJoin: false, + allowUsersMatchingDomainToJoin: true, }, }, userRole @@ -69,226 +75,235 @@ describe('Organization settings', () => { ); }); - // describe('DDT user WITHOUT privileges to update organization settings', () => { - // // Arrange - // test.each` - // userRole | message - // ${TestUser.NON_SPACE_MEMBER} | ${"Authorization: unable to grant 'update' privilege: organization settings update:"} - // `( - // 'User: "$userRole" get message: "$message", when intend to update organization settings ', - // async ({ userRole, message }) => { - // // Act - // const res = await updateOrganizationSettings( - // baseScenario.organization.id, - // { - // membership: { - // allowUsersMatchingDomainToJoin: false, - // }, - // }, - // userRole - // ); - - // // Assert - // expect(res?.error?.errors[0].message).toContain(message); - // } - // ); - // }); - - // describe('Unverified organization - domain match', () => { - // afterEach(async () => { - // await deleteUser(userId); - // }); - // test("don't assign new user to organization,domain setting enabled", async () => { - // // Arrange - // await updateOrganizationSettings(baseScenario.organization.id, { - // membership: { - // allowUsersMatchingDomainToJoin: true, - // }, - // }); - - // // Act - // const email = `enm${uniqueId}@${domain}`; - // userId = await registerVerifiedUser(email, firstName, lastName); - - // const organizationData = await getOrganizationData( - // baseScenario.organization.id - // ); - // const organizationMembers = - // organizationData?.data?.organization.associates; - - // // Assert - // expect(organizationMembers).toHaveLength(1); - // expect(organizationMembers).not.toEqual( - // expect.arrayContaining([ - // expect.objectContaining({ - // email: email, - // }), - // ]) - // ); - // }); - - // test("don't assign new user to organization, domain setting disabled", async () => { - // // Arrange - // await updateOrganizationSettings(baseScenario.organization.id, { - // membership: { - // allowUsersMatchingDomainToJoin: false, - // }, - // }); - - // // Act - // const email = `dism${uniqueId}@${domain}`; - // userId = await registerVerifiedUser(email, firstName, lastName); - - // const organizationData = await getOrganizationData( - // baseScenario.organization.id - // ); - // const organizationMembers = - // organizationData?.data?.organization.associates; - - // // Assert - // expect(organizationMembers).toHaveLength(1); - // expect(organizationMembers).not.toEqual( - // expect.arrayContaining([ - // expect.objectContaining({ - // email: email, - // }), - // ]) - // ); - // }); - - // test("don't assign new user with different domain to organization,domain setting enabled", async () => { - // // Arrange - // await updateOrganizationSettings(baseScenario.organization.id, { - // membership: { - // allowUsersMatchingDomainToJoin: true, - // }, - // }); - - // // Act - // const email = `enms${uniqueId}@a${domain}`; - // userId = await registerVerifiedUser(email, firstName, lastName); - - // const organizationData = await getOrganizationData( - // baseScenario.organization.id - // ); - // const organizationMembers = - // organizationData?.data?.organization.associates; - - // // Assert - - // expect(organizationMembers).toHaveLength(1); - // expect(organizationMembers).not.toEqual( - // expect.arrayContaining([ - // expect.objectContaining({ - // email: email, - // }), - // ]) - // ); - // }); - // }); - - // describe('Verified organization - domain match', () => { - // beforeAll(async () => { - // await eventOnOrganizationVerification( - // baseScenario.organization.verificationId, - // 'VERIFICATION_REQUEST' - // ); - - // await eventOnOrganizationVerification( - // baseScenario.organization.verificationId, - // 'MANUALLY_VERIFY' - // ); - // }); - - // afterEach(async () => { - // await deleteUser(userId); - // }); - // test('assign new user to organization,domain setting enabled', async () => { - // // Arrange - // await updateOrganizationSettings(baseScenario.organization.id, { - // membership: { - // allowUsersMatchingDomainToJoin: true, - // }, - // }); - - // // Act - // const email = `en${uniqueId}@${domain}`; - // userId = await registerVerifiedUser(email, firstName, lastName); - - // const organizationData = await getOrganizationData( - // baseScenario.organization.id - // ); - // const organizationMembers = - // organizationData?.data?.organization.associates; - - // // Assert - // expect(organizationMembers).toHaveLength(2); - // expect(organizationMembers).toEqual( - // expect.arrayContaining([ - // expect.objectContaining({ - // email: email, - // }), - // ]) - // ); - // }); - - // test("don't assign new user to organization, domain setting disabled", async () => { - // // Arrange - // await updateOrganizationSettings(baseScenario.organization.id, { - // membership: { - // allowUsersMatchingDomainToJoin: false, - // }, - // }); - - // // Act - // const email = `dis${uniqueId}@${domain}`; - // userId = await registerVerifiedUser(email, firstName, lastName); - - // const organizationData = await getOrganizationData( - // baseScenario.organization.id - // ); - // const organizationMembers = - // organizationData?.data?.organization.associates; - - // // Assert - // expect(organizationMembers).toHaveLength(1); - // expect(organizationMembers).not.toEqual( - // expect.arrayContaining([ - // expect.objectContaining({ - // email: email, - // }), - // ]) - // ); - // }); - - // test("don't assign new user with different domain to organization,domain setting enabled", async () => { - // // Arrange - // await updateOrganizationSettings(baseScenario.organization.id, { - // membership: { - // allowUsersMatchingDomainToJoin: true, - // }, - // }); - - // // Act - // const email = `en${uniqueId}@a${domain}`; - // userId = await registerVerifiedUser(email, firstName, lastName); - - // const organizationData = await getOrganizationData( - // baseScenario.organization.id - // ); - // const organizationMembers = - // organizationData?.data?.organization.associates; - - // // Assert - - // expect(organizationMembers).toHaveLength(1); - // expect(organizationMembers).not.toEqual( - // expect.arrayContaining([ - // expect.objectContaining({ - // email: email, - // }), - // ]) - // ); - // }); - // }); + describe('DDT user WITHOUT privileges to update organization settings', () => { + // Arrange + test.each` + userRole | message + ${TestUser.NON_SPACE_MEMBER} | ${"Authorization: unable to grant 'update' privilege: organization settings update:"} + `( + 'User: "$userRole" get message: "$message", when intend to update organization settings ', + async ({ userRole, message }) => { + // Act + const res = await updateOrganizationSettings( + baseScenario.organization.id, + { + membership: { + allowUsersMatchingDomainToJoin: false, + }, + }, + userRole + ); + + // Assert + expect(res?.error?.errors[0].message).toContain(message); + } + ); + }); + + describe('Unverified organization - domain match', () => { + afterEach(async () => { + await deleteUser(userId); + }); + test("don't assign new user to organization,domain setting enabled", async () => { + // Arrange + await updateOrganizationSettings(baseScenario.organization.id, { + membership: { + allowUsersMatchingDomainToJoin: true, + }, + }); + + // Act + const email = `enm${uniqueId}@${domain}`; + userId = await registerVerifiedUser(email, firstName, lastName); + + const organizationData = await getOrganizationData( + baseScenario.organization.id + ); + const organizationMembers = + organizationData?.data?.organization.associates; + + // Assert + expect(organizationMembers).toHaveLength(1); + expect(organizationMembers).not.toEqual( + expect.arrayContaining([ + expect.objectContaining({ + email: email, + }), + ]) + ); + }); + + test("don't assign new user to organization, domain setting disabled", async () => { + // Arrange + await updateOrganizationSettings(baseScenario.organization.id, { + membership: { + allowUsersMatchingDomainToJoin: false, + }, + }); + + // Act + const email = `dism${uniqueId}@${domain}`; + userId = await registerVerifiedUser(email, firstName, lastName); + + const organizationData = await getOrganizationData( + baseScenario.organization.id + ); + const organizationMembers = + organizationData?.data?.organization.associates; + + // Assert + expect(organizationMembers).toHaveLength(1); + expect(organizationMembers).not.toEqual( + expect.arrayContaining([ + expect.objectContaining({ + email: email, + }), + ]) + ); + }); + + test("don't assign new user with different domain to organization,domain setting enabled", async () => { + // Arrange + await updateOrganizationSettings(baseScenario.organization.id, { + membership: { + allowUsersMatchingDomainToJoin: true, + }, + }); + + // Act + const email = `enms${uniqueId}@a${domain}`; + userId = await registerVerifiedUser(email, firstName, lastName); + + const organizationData = await getOrganizationData( + baseScenario.organization.id + ); + const organizationMembers = + organizationData?.data?.organization.associates; + + // Assert + + expect(organizationMembers).toHaveLength(1); + expect(organizationMembers).not.toEqual( + expect.arrayContaining([ + expect.objectContaining({ + email: email, + }), + ]) + ); + }); + }); + + describe('Verified organization - domain match', () => { + beforeAll(async () => { + await eventOnOrganizationVerification( + baseScenario.organization.verificationId, + 'VERIFICATION_REQUEST' + ); + + await eventOnOrganizationVerification( + baseScenario.organization.verificationId, + 'MANUALLY_VERIFY' + ); + }); + + afterEach(async () => { + await deleteUser(userId); + }); + test('assign new user to organization,domain setting enabled', async () => { + // Arrange + await updateOrganizationSettings(baseScenario.organization.id, { + membership: { + allowUsersMatchingDomainToJoin: true, + }, + }); + + const origOrgData = await getOrganizationData( + baseScenario.organization.id + ); + const origOrganizationMembers = + origOrgData?.data?.organization.associates; + const origMembersCount = origOrganizationMembers?.length ?? -999; + + // Act + const email = `en${uniqueId}@${domain}`; + userId = await registerVerifiedUser(email, firstName, lastName); + + const organizationData = await getOrganizationData( + baseScenario.organization.id + ); + const organizationMembers = + organizationData?.data?.organization.associates; + const associates = organizationMembers?.map((m) => m.email) || []; + console.log(`associates: ${associates}`); + + // Assert + expect(associates).toHaveLength(origMembersCount + 1); + expect(organizationMembers).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + email: email, + }), + ]) + ); + }); + + test("don't assign new user to organization, domain setting disabled", async () => { + // Arrange + await updateOrganizationSettings(baseScenario.organization.id, { + membership: { + allowUsersMatchingDomainToJoin: false, + }, + }); + + // Act + const email = `dis${uniqueId}@${domain}`; + userId = await registerVerifiedUser(email, firstName, lastName); + + const organizationData = await getOrganizationData( + baseScenario.organization.id + ); + const organizationMembers = + organizationData?.data?.organization.associates; + + // Assert + expect(organizationMembers).toHaveLength(1); + expect(organizationMembers).not.toEqual( + expect.arrayContaining([ + expect.objectContaining({ + email: email, + }), + ]) + ); + }); + + test("don't assign new user with different domain to organization,domain setting enabled", async () => { + // Arrange + await updateOrganizationSettings(baseScenario.organization.id, { + membership: { + allowUsersMatchingDomainToJoin: true, + }, + }); + + // Act + const email = `en${uniqueId}@a${domain}`; + userId = await registerVerifiedUser(email, firstName, lastName); + + const organizationData = await getOrganizationData( + baseScenario.organization.id + ); + const organizationMembers = + organizationData?.data?.organization.associates; + + // Assert + + expect(organizationMembers).toHaveLength(1); + expect(organizationMembers).not.toEqual( + expect.arrayContaining([ + expect.objectContaining({ + email: email, + }), + ]) + ); + }); + }); }); diff --git a/server-api/src/functional-api/entitlements/vc-functional-entitlements.it-spec.ts b/server-api/src/functional-api/entitlements/vc-functional-entitlements.it-spec.ts index e8ac90fb..1186e3ce 100644 --- a/server-api/src/functional-api/entitlements/vc-functional-entitlements.it-spec.ts +++ b/server-api/src/functional-api/entitlements/vc-functional-entitlements.it-spec.ts @@ -32,9 +32,9 @@ import { } from '@functional-api/contributor-management/virtual-contributor/vc.request.params'; import { getAccountMainEntities } from '../account/account.params.request'; import { PlatformRole } from '@generated/graphql'; -import { UniqueIDGenerator } from '@alkemio/tests-lib';; +import { UniqueIDGenerator } from '@alkemio/tests-lib';import { assignPlatformRoleToUser, removePlatformRoleFromUser } from '@functional-api/platform/authorization-platform-mutation'; +; const uniqueId = UniqueIDGenerator.getID(); -import { assignPlatformRoleToUser, removePlatformRoleFromUser } from '@functional-api/platform/authorization-platform-mutation'; const spaceName = `space-name-${uniqueId}`; const vcName = `vcname1-${uniqueId}`; diff --git a/server-api/src/functional-api/platform/authorization-platform-mutation.ts b/server-api/src/functional-api/platform/authorization-platform-mutation.ts index 9073da35..b80f9812 100644 --- a/server-api/src/functional-api/platform/authorization-platform-mutation.ts +++ b/server-api/src/functional-api/platform/authorization-platform-mutation.ts @@ -26,7 +26,6 @@ export const assignPlatformRoleToUser = async ( export const removePlatformRoleFromUser = async ( userID: string, platformRole: PlatformRole, - userRole: TestUser = TestUser.GLOBAL_ADMIN ) => { const graphqlClient = getGraphqlClient(); @@ -43,113 +42,6 @@ export const removePlatformRoleFromUser = async ( return graphqlErrorWrapper(callback, userRole); }; -export const assignUserAsGlobalCommunityAdmin = async ( - userID: string, - userRole: TestUser = TestUser.GLOBAL_ADMIN -) => { - const graphqlClient = getGraphqlClient(); - const callback = (authToken: string | undefined) => - graphqlClient.assignPlatformRoleToUser( - { - input: { userID, role: PlatformRole.CommunityReader }, - }, - { - authorization: `Bearer ${authToken}`, - } - ); - - return graphqlErrorWrapper(callback, userRole); -}; - -export const removeUserAsGlobalCommunityAdmin = async ( - userID: string, - userRole: TestUser = TestUser.GLOBAL_ADMIN -) => { - const graphqlClient = getGraphqlClient(); - const callback = (authToken: string | undefined) => - graphqlClient.removePlatformRoleFromUser( - { - input: { userID, role: PlatformRole.CommunityReader }, - }, - { - authorization: `Bearer ${authToken}`, - } - ); - - return graphqlErrorWrapper(callback, userRole); -}; - -export const assignUserAsGlobalSupport = async ( - userID: string, - userRole: TestUser = TestUser.GLOBAL_ADMIN -) => { - const graphqlClient = getGraphqlClient(); - const callback = (authToken: string | undefined) => - graphqlClient.assignPlatformRoleToUser( - { - input: { userID, role: PlatformRole.Support }, - }, - { - authorization: `Bearer ${authToken}`, - } - ); - - return graphqlErrorWrapper(callback, userRole); -}; - -export const removeUserAsGlobalSupport = async ( - userID: string, - userRole: TestUser = TestUser.GLOBAL_ADMIN -) => { - const graphqlClient = getGraphqlClient(); - const callback = (authToken: string | undefined) => - graphqlClient.removePlatformRoleFromUser( - { - input: { userID, role: PlatformRole.Support }, - }, - { - authorization: `Bearer ${authToken}`, - } - ); - - return graphqlErrorWrapper(callback, userRole); -}; - -export const assignUserAsGlobalAdmin = async ( - userID: string, - userRole: TestUser = TestUser.GLOBAL_ADMIN -) => { - const graphqlClient = getGraphqlClient(); - const callback = (authToken: string | undefined) => - graphqlClient.assignPlatformRoleToUser( - { - input: { userID, role: PlatformRole.GlobalAdmin }, - }, - { - authorization: `Bearer ${authToken}`, - } - ); - - return graphqlErrorWrapper(callback, userRole); -}; - -export const removeUserAsGlobalAdmin = async ( - userID: string, - userRole: TestUser = TestUser.GLOBAL_ADMIN -) => { - const graphqlClient = getGraphqlClient(); - const callback = (authToken: string | undefined) => - graphqlClient.removePlatformRoleFromUser( - { - input: { userID, role: PlatformRole.GlobalAdmin }, - }, - { - authorization: `Bearer ${authToken}`, - } - ); - - return graphqlErrorWrapper(callback, userRole); -}; export const authorizationPolicyResetOnPlatform = async ( userRole: TestUser = TestUser.GLOBAL_ADMIN diff --git a/server-api/src/scenario/TestScenarioFactory.ts b/server-api/src/scenario/TestScenarioFactory.ts index 1a703be5..1c87da74 100644 --- a/server-api/src/scenario/TestScenarioFactory.ts +++ b/server-api/src/scenario/TestScenarioFactory.ts @@ -24,18 +24,25 @@ import { TestScenarioConfig, TestScenarioSpaceConfig, } from './config/test-scenario-config'; -import { - assignPlatformRoleToUser, - assignUserAsGlobalCommunityAdmin, - assignUserAsGlobalSupport, -} from '@functional-api/platform/authorization-platform-mutation'; import { TestUserManager } from './TestUserManager'; +import { UserModel } from './models/UserModel'; +import { assignPlatformRoleToUser } from '@functional-api/platform/authorization-platform-mutation'; +import { logElapsedTime } from '@utils/profiling'; export class TestScenarioFactory { - public static async createBaseScenario( scenarioConfig: TestScenarioConfig + ): Promise { + const start = performance.now(); + const result = await this.createBaseScenarioPrivate(scenarioConfig); + logElapsedTime('createBaseScenario', start); + return result; + + } + + public static async createBaseScenarioPrivate( + scenarioConfig: TestScenarioConfig ): Promise { const scenarioName = scenarioConfig.name; await TestUserManager.populateUserModelMap(); @@ -92,21 +99,32 @@ export class TestScenarioFactory { } private static async populateGlobalRoles(): Promise { - // TODO: check the role already assigned to the user - await assignUserAsGlobalSupport( - TestUserManager.users.globalLicenseAdmin.id, - TestUser.GLOBAL_ADMIN + await this.checkAndAssignPlatformRoleToUser( + TestUserManager.users.globalLicenseAdmin, + PlatformRole.LicenseManager ); - await assignUserAsGlobalCommunityAdmin( - TestUserManager.users.globalSupportAdmin.id, - TestUser.GLOBAL_ADMIN + + await this.checkAndAssignPlatformRoleToUser( + TestUserManager.users.globalSupportAdmin, + PlatformRole.Support ); - await assignPlatformRoleToUser( - TestUserManager.users.betaTester.id, + + await this.checkAndAssignPlatformRoleToUser( + TestUserManager.users.betaTester, PlatformRole.BetaTester ); } + private static async checkAndAssignPlatformRoleToUser( + userModel: UserModel, + role: PlatformRole + ): Promise { + const alreadyHasRole = userModel.platformRoles.includes(role); + if (!alreadyHasRole) { + await assignPlatformRoleToUser(userModel.id, role); + } + } + public static async cleanUpBaseScenario( baseScenario: OrganizationWithSpaceModel ): Promise { diff --git a/server-api/src/scenario/TestUserManager.ts b/server-api/src/scenario/TestUserManager.ts index d17c52ba..fa4a8b0f 100644 --- a/server-api/src/scenario/TestUserManager.ts +++ b/server-api/src/scenario/TestUserManager.ts @@ -4,6 +4,7 @@ import { AlkemioClient } from '@alkemio/client-lib'; import { TestUser } from '@alkemio/tests-lib'; import { TestUserModels } from './models/TestUserModels'; import { getGraphqlClient } from '@utils/graphqlClient'; +import { logElapsedTime } from '@utils/profiling'; export class TestUserManager { private static userModelMapEmail: Map; @@ -12,6 +13,8 @@ export class TestUserManager { public static users: TestUserModels; public static async populateUserModelMap() { + const start = performance.now(); + this.userModelMapEmail = new Map(); this.userModelMapType = new Map(); @@ -32,6 +35,8 @@ export class TestUserManager { } // Finally ensure the exposed users field is populated this.populateUsers(); + + logElapsedTime('populateUserModels', start); } private static createUserModel(email: string, testUser: TestUser): UserModel { @@ -45,6 +50,7 @@ export class TestUserManager { accountId: '', authToken: '', type: testUser, + platformRoles: [], }; return result; } @@ -74,7 +80,7 @@ export class TestUserManager { ), qaUser: TestUserManager.getUserModelByEmail('qa.user@alkem.io'), notificationsAdmin: TestUserManager.getUserModelByEmail( - 'notifications@alkem.io' + 'subspace.member@alkem.io' // TODO: notifications seems to not be properly setup ), nonSpaceMember: TestUserManager.getUserModelByEmail('non.space@alkem.io'), betaTester: TestUserManager.getUserModelByEmail('beta.tester@alkem.io'), @@ -101,22 +107,24 @@ export class TestUserManager { userModel: UserModel ): Promise { const userData = await this.getUserData( - userModel.email, userModel.authToken ); - userModel.displayName = userData?.data?.user.profile.displayName || ''; - userModel.id = userData?.data?.user.id || ''; - userModel.profileId = userData?.data?.user.profile.id || ''; - userModel.nameId = userData?.data?.user.nameID || ''; - userModel.agentId = userData?.data?.user.agent.id || ''; - userModel.accountId = userData?.data?.user?.account?.id || ''; + const userInfo = userData?.data?.me.user; + userModel.displayName = userInfo?.profile.displayName || ''; + userModel.id = userInfo?.id || ''; + userModel.profileId = userInfo?.profile.id || ''; + userModel.nameId = userInfo?.nameID || ''; + userModel.agentId = userInfo?.agent.id || ''; + userModel.accountId = userInfo?.account?.id || ''; + + const platformRoles = userData?.data?.platform?.myRoles || []; + userModel.platformRoles = platformRoles; } - private static async getUserData(email: string, authToken: string) { + private static async getUserData(authToken: string) { const graphqlClient = getGraphqlClient(); - const result = graphqlClient.getUserData( + const result = graphqlClient.getMyUserInfo( { - userId: email, }, { authorization: `Bearer ${authToken}`, diff --git a/server-api/src/scenario/graphql/getMyUserInfo.graphql b/server-api/src/scenario/graphql/getMyUserInfo.graphql new file mode 100644 index 00000000..e4979c02 --- /dev/null +++ b/server-api/src/scenario/graphql/getMyUserInfo.graphql @@ -0,0 +1,22 @@ +query getMyUserInfo { + me { + user { + id + nameID + email + profile { + id + displayName + } + agent { + id + } + account { + id + } + } + } + platform { + myRoles + } +} diff --git a/server-api/src/scenario/models/UserModel.ts b/server-api/src/scenario/models/UserModel.ts index f92ac4fd..9ca780b2 100644 --- a/server-api/src/scenario/models/UserModel.ts +++ b/server-api/src/scenario/models/UserModel.ts @@ -10,4 +10,5 @@ export type UserModel = { accountId: string; authToken: string; type: TestUser; + platformRoles: string[]; }; \ No newline at end of file diff --git a/server-api/src/utils/profiling.ts b/server-api/src/utils/profiling.ts new file mode 100644 index 00000000..fe68e59f --- /dev/null +++ b/server-api/src/utils/profiling.ts @@ -0,0 +1,7 @@ +import { performance } from 'perf_hooks'; + +export const logElapsedTime = (prefix: string, start: number) => { + const elapsed = ((performance.now() - start) / 1000).toFixed(1); + const msg = `${prefix}: Execution time: ${elapsed} seconds`; + console.log(msg); +};