All files / blong-int-adapter/keycloak/test/test testKeycloakRealmCrud.ts

100% Statements 139/139
92.3% Branches 12/13
100% Functions 9/9
100% Lines 139/139

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 1401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x  
import {handler, type IAssert, type IMeta} from '@feasibleone/blong';
 
import {testRealm, updatedRealm} from '../fixtures/realm.ts';
 
type RealmResult = {realm?: string; displayName?: string; enabled?: boolean};
type StepMeta = {$meta: IMeta};
 
/**
 * testKeycloakRealmCrud — integration test covering Keycloak realm operations:
 * find, add, get, edit, remove.
 *
 * Creates a dedicated `blong-test-realm`, verifies it, updates it, and removes it.
 * The `blong-integration` realm (created by the init job) is used only for listing.
 */
export default handler(
    ({
        lib: {group, checkpoint},
        handler: {authRealmFind, authRealmAdd, authRealmGet, authRealmEdit, authRealmRemove},
    }) => ({
        testKeycloakRealmCrud: ({name = 'keycloak realm CRUD'}: {name?: string}) =>
            // `id` — top-level realm UUID; `defaultRole.id` and `defaultRole.containerId`
            // are UUIDs for the auto-created default role — all change each test run.
            group(name, {mask: ['id', 'defaultRole.id', 'defaultRole.containerId']})([
                // ── 1. Clean up leftover test realm if present ─────────────
                async function ensureClean(assert: IAssert, {$meta}: StepMeta) {
                    try {
                        await authRealmRemove({realm: testRealm.realm}, $meta);
                    } catch {
                        // ignore – realm may not exist
                    }
                    assert.ok(true, 'pre-test cleanup completed');
                    return {cleaned: true};
                },
 
                // ── 2. find — list all realms (master should always be present)
                async function findRealms(
                    assert: IAssert,
                    {$meta, ensureClean}: StepMeta & {ensureClean: Promise<unknown>},
                ) {
                    await ensureClean;
                    const result = await authRealmFind({}, $meta);
                    assert.ok(Array.isArray(result), 'find returned an array');
                    assert.ok(
                        (result as RealmResult[]).length > 0,
                        'find returned at least one realm',
                    );
                    const master = (result as RealmResult[]).find(r => r.realm === 'master');
                    assert.ok(master, 'master realm is present');
                    return result as RealmResult[];
                },
 
                // ── 3. add — create the test realm ────────────────────────
                async function addRealm(
                    assert: IAssert,
                    {$meta, ensureClean}: StepMeta & {ensureClean: Promise<unknown>},
                ) {
                    await ensureClean;
                    const result = await authRealmAdd({...testRealm}, $meta);
                    assert.ok(result !== undefined, 'add realm returned a result');
                    return {realm: testRealm.realm};
                },
 
                // ── 4. get — fetch the newly created realm ─────────────────
                async function getRealm(
                    assert: IAssert,
                    {$meta, addRealm}: StepMeta & {addRealm: Promise<{realm: string}>},
                ) {
                    // Snapshot captures realm name, displayName, and enabled in one entry.
                    // Chain-level mask handles the Keycloak `id` UUID.
                    assert.snapshot();
                    return (await authRealmGet(
                        {realm: (await addRealm).realm},
                        $meta,
                    )) as RealmResult;
                },
 
                // ── 5. edit — update the realm display name ────────────────
                async function editRealm(
                    assert: IAssert,
                    {$meta, getRealm}: StepMeta & {getRealm: Promise<RealmResult>},
                ) {
                    await getRealm;
                    await authRealmEdit({realm: testRealm.realm, ...updatedRealm}, $meta);
                    assert.ok(true, 'edit realm completed without error');
                    return {realm: testRealm.realm};
                },
 
                // ── 6. verify edit — re-fetch to confirm the update ────────
                async function verifyEdit(
                    assert: IAssert,
                    {$meta, editRealm}: StepMeta & {editRealm: Promise<{realm: string}>},
                ) {
                    // Snapshot captures the updated displayName and all other fields.
                    assert.snapshot();
                    return (await authRealmGet(
                        {realm: (await editRealm).realm},
                        $meta,
                    )) as RealmResult;
                },
 
                // Phase checkpoint: snapshot both read-back results together
                checkpoint('realm-read-snapshots', 'getRealm', 'verifyEdit'),
 
                // ── 7. remove — delete the test realm ─────────────────────
                async function removeRealm(
                    assert: IAssert,
                    {
                        $meta,
                        verifyEdit,
                        findRealms,
                    }: StepMeta & {
                        verifyEdit: Promise<unknown>;
                        findRealms: Promise<unknown>;
                    },
                ) {
                    await verifyEdit;
                    await findRealms;
                    await authRealmRemove({realm: testRealm.realm}, $meta);
                    assert.ok(true, 'remove realm completed without error');
                    return {removed: true};
                },
 
                // ── 8. find — verify the test realm is gone ───────────────
                async function verifyRemoval(
                    assert: IAssert,
                    {$meta, removeRealm}: StepMeta & {removeRealm: Promise<unknown>},
                ) {
                    await removeRealm;
                    const result = await authRealmFind({}, $meta);
                    assert.ok(Array.isArray(result), 'find after removal returned an array');
                    const stillPresent = (result as RealmResult[]).find(
                        r => r.realm === testRealm.realm,
                    );
                    assert.ok(!stillPresent, 'test realm is no longer in the list');
                    return result as RealmResult[];
                },
            ]),
    }),
);