{"id":25044963,"url":"https://github.com/sectorlabs/keycloak-mock","last_synced_at":"2025-04-14T02:00:30.006Z","repository":{"id":40686963,"uuid":"243252590","full_name":"SectorLabs/keycloak-mock","owner":"SectorLabs","description":"Node.js package for mocking a Keycloak server in unit tests.","archived":false,"fork":false,"pushed_at":"2023-01-05T10:59:06.000Z","size":498,"stargazers_count":23,"open_issues_count":15,"forks_count":17,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-04-11T07:17:40.524Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SectorLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-26T12:02:54.000Z","updated_at":"2024-11-15T02:11:12.000Z","dependencies_parsed_at":"2023-02-04T02:15:16.981Z","dependency_job_id":null,"html_url":"https://github.com/SectorLabs/keycloak-mock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SectorLabs%2Fkeycloak-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SectorLabs%2Fkeycloak-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SectorLabs%2Fkeycloak-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SectorLabs%2Fkeycloak-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SectorLabs","download_url":"https://codeload.github.com/SectorLabs/keycloak-mock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248809032,"owners_count":21164895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-06T05:20:02.539Z","updated_at":"2025-04-14T02:00:29.973Z","avatar_url":"https://github.com/SectorLabs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keycloak Mock\n\n[![License](https://img.shields.io/:license-mit-blue.svg)](http://doge.mit-license.org)\n[![NPM version](https://badge.fury.io/js/keycloak-mock.svg)](http://badge.fury.io/js/keycloak-mock)\n![test](https://github.com/SectorLabs/keycloak-mock/workflows/test/badge.svg)\n\nA minimal mock for a Keycloak server to be used in unit tests.\n\nThis mock is not complete and it definitely doesn't match Keycloak completely. Improvements are welcome.\n\n## About\nThis does not launch an actual HTTP server. It uses [`nock`](https://github.com/nock/nock) to patch Node.js HTTP client to intercept requests.\n\nTested with Node.js 8.x, 10.x, 12.x, 13.x\n\n### What works\n* `GET /[realm]/protocol/openid-connect/certs`\n* `GET /[realm]/protocol/openid-connect/userinfo`\n* `GET /admin/realms/[realm]/users`\n* `GET /admin/realms/[realm]/users?username=myusername`\n* `POST /[realm]/protocol/openid-connect/token`\n* `GET /admin/realms/[realm]/users/[userid]`\n* `DELETE /admin/realms/[realm]/users/[userid]`\n* `POST /admin/realms/[realm]/users`\n\n## Usage\n### Basic\n    import * as KeycloakMock from \"keycloak-node-mock\";\n\n    const keycloak = await KeycloakMock.createMockInstance({\n        authServerURL: \"https://myserver.com/auth\",\n        realm: \"myrealm\",\n        clientID: \"client-1\",\n        clientSecret: \"test\",\n    });\n\n    // all requests to `https://myserver.com/auth` will now be\n    // intercepted and replied to\n    const mock = KeycloakMock.activateMock(keycloak);\n\n    // create a user and a token for it\n    const user = keycloak.database.createUser({\n        name: \"test\",\n        email: \"hello@hello.com\", // username will be email\n        credentials: [{\n            value: \"mypassword\",\n        }],\n    });\n\n    console.log(user.profile, user.credentials);\n\n    const token = keycloak.createBearerToken(user.profile.id);\n\n    // get active mock without a reference\n    const sameMock = KeycloakMock.getMock(\"https://myserver.com/auth\");\n\n    // clear user database\n    mock.instance.database.clear();\n\n    // find user profile\n    const sameUser = mock.instance.database.findUserByID(user.profile.id);\n\n    // de-activate the mock\n    KeycloakMock.deactivateMock(sameMock);\n\n## Custom handlers\n\n    import * as KeycloakMock from \"keycloak-node-mock\";\n\n    const keycloak = await KeycloakMock.createMockInstance({\n        authServerURL: \"https://myserver.com/auth\",\n        realm: \"myrealm\",\n        clientID: \"client-1\",\n    });\n\n    keycloak.activateMock(keycloak, {\n       listCertificatesView: (instance, request) =\u003e {\n           return [500, \"\"];\n       },\n       getUser: (instance, request) =\u003e {\n           // might be null if not authorized\n           console.log(request.user);\n           return [500, \"\"];\n       },\n       deleteUser: (instance, request) =\u003e {\n           return [500, \"\"];\n       },\n       getUserInfoView: (instance, request) =\u003e {\n           return [500, \"\"];\n       },\n       listUsers: (instance, request) =\u003e {\n           return [500, \"\"];\n       },\n       createTokenView: (instance, request, body) =\u003e {\n           return [500, \"\"];\n       },\n       createUserView: (instance, request, body) =\u003e {\n           return [500, \"\"];\n       },\n    });\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsectorlabs%2Fkeycloak-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsectorlabs%2Fkeycloak-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsectorlabs%2Fkeycloak-mock/lists"}