import { createMockedHost, type MockedHost } from '@open-iga/connector-sdk/test'; import { awsConnector } from '../src/aws-connector.ts'; import { checkTargetExists } from '../src/utils/iam.ts'; import { GetLoginProfileCommand, IAMClient } from '@aws-sdk/client-iam'; const tenant = { id: '000000000000' }; const endpoint = () => process.env.AWS_ENDPOINT_URL ?? ''; const credentials = { accessKeyId: 'test', secretAccessKey: 'test' }; const checkLoginProfile = async (userName: string): Promise => { const iamClient = new IAMClient({ region: 'us-east-1', endpoint: endpoint(), credentials }); return checkTargetExists(iamClient.send(new GetLoginProfileCommand({ UserName: userName }))); }; describe('account-actions.lifecycle', () => { let host: MockedHost; beforeAll(async () => { host = await createMockedHost({ plugin: awsConnector, config: { AWS_REGION: 'us-east-1', AWS_ACCESS_KEY_ID: 'test', AWS_SECRET_ACCESS_KEY: 'test', AWS_USER_CREATION_ROLE: 'arn:aws:iam::000000000000:role/openiga-user-creation', AWS_USER_MANAGEMENT_ROLE: 'arn:aws:iam::000000000000:role/openiga-user-management', }, mockUpstream: endpoint(), }); }); afterAll(async () => { await host.close(); }); it('should create, disable, enable and delete an IAM user', async () => { const email = `lifecycle.user@openiga.dev`; // The IAM username is derived from the email's local part (see the create action). const userName = 'lifecycle.user'; // Create — user exists with a login profile. const created = await host.callAccountActions('iam-user', 'create', { tenant, email, firstname: 'life', lastname: 'cycle', }); expect(created.ok).toBe(true); if (!created.ok) return; const { accountId } = created.output; expect(await checkLoginProfile(userName)).toBe(true); // Read — exists and enabled. const afterCreate = await host.callAccountActions('iam-user', 'read', { tenant, accountId }); expect(afterCreate.ok).toBe(true); if (afterCreate.ok) expect(afterCreate.output).toEqual({ exists: true, enabled: true }); // Disable — console access revoked. const disabled = await host.callAccountActions('iam-user', 'disable', { tenant, accountId }); expect(disabled.ok).toBe(true); if (disabled.ok) expect(disabled.output.disabled).toBe(true); expect(await checkLoginProfile(userName)).toBe(false); // Read — still exists, now disabled. const afterDisable = await host.callAccountActions('iam-user', 'read', { tenant, accountId }); expect(afterDisable.ok).toBe(true); if (afterDisable.ok) expect(afterDisable.output).toEqual({ exists: true, enabled: false }); // Enable — console access restored and credentials re-sent via the email capability. const enabled = await host.callAccountActions('iam-user', 'enable', { tenant, accountId }); expect(enabled.ok).toBe(true); if (enabled.ok) expect(enabled.output.enabled).toBe(true); expect(await checkLoginProfile(userName)).toBe(true); expect(host.hostFunctions.sentEmails.at(-1)?.reason).toBe('reenabled'); // Delete — user removed entirely. const deleted = await host.callAccountActions('iam-user', 'delete', { tenant, accountId }); expect(deleted.ok).toBe(true); if (deleted.ok) expect(deleted.output.deleted).toBe(true); expect(await checkLoginProfile(userName)).toBe(false); // Read — no longer exists. const afterDelete = await host.callAccountActions('iam-user', 'read', { tenant, accountId }); expect(afterDelete.ok).toBe(true); if (afterDelete.ok) expect(afterDelete.output).toEqual({ exists: false, enabled: false }); }); });