npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

keycloak-testcontainer

v0.0.7

Published

Run a Keycloak testcontainer with node.js

Downloads

226

Readme

keycloak-testcontainer

This package adds the ability to start keycloak as a testcontainer in node.js.

Install

npm install keycloak-testcontainer --save-dev

ES6 import

import KeycloakContainer from 'keycloak-testcontainer';

Common js import:

const KeycloakContainer = require('keycloak-testcontainer').default;

Keycloak Version Support

Currently, this package only supports the latest version of Keycloak. Older releases will work for older versions, but don't expect new releases to work for older Keycloak versions.

Node Version Support

This package supports node.js version 18 and higher. This package has also been build and tested for non-lts releases.

Example

You can start a keycloak container with a few lines of code:

import KeycloakContainer from 'keycloak-testcontainer';

describe('Keycloak Testcontainer Example', () => {

    it('should run against keycloak', async () => {
        const container = await new KeycloakContainer().start();

        // do something with the container

        await container.stop();
    });
});

Features

Currently, this package provides the following features:

Starting a container

import KeycloakContainer from 'keycloak-testcontainer';

const container = await new KeycloakContainer()
    .start();

By default, every container is always a Keycloak in development mode.

Starting a container with Keycloak commands

You can run this testcontainer with a bunch of different commands to obtain different Keycloak functionality. For a deeper explaination and up to date documentation have a look at the Keycloak guides.

With metrics

To enable Keycloaks metrics endpoint start the container with the following command:

const container = await new KeycloakContainer()
    .withMetrics()
    .start();

With features

Keycloak provides different additional or experimental features. A list of the supported features can be found here. To enable additional features start the container with the following command:

const container = await new KeycloakContainer()
    .withFeatures([
        'docker',
        'token-exchange'
    ])
    .start();

With disabled features

Keycloak allows to disable certain features. A list of the supported features can be found here. To disable certain features start the container with the following command:

const container = await new KeycloakContainer()
    .withDisabledFeatures([
        'impersonation',
    ])
    .start();

With custom admin user

To start the Keycloak container with a custom admin user start the container with the following command:

const container = await new KeycloakContainer()
    .withAdminUser({
        username: 'admin',
        password: 'password'
    })
    .start();

With database

Keycloak runs by default with a h2 database. To run Keycloak with a different database (for example postgres) you can start the container with the following command:

const container = await new KeycloakContainer()
    .withDatabase({
        vendor: 'postgres',
        url: 'your-jdbc-url-here',
        username: 'dbuser',
        password: 'dbpassword'
    });
    .start();

With realm import

To start the Keycloak container with a custom realm you can start the container with the following command:

const container = await new KeycloakContainer()
    .withRealmImport('/path/to/realm/data')
    .start();

With providers

To add custom providers start the container with the following command:

const container = await new KeycloakContainer()
    .withProviders('/path/to/providers')
    .start();

With health

To enable Keycloaks health endpoint start the container with the following command:

const container = await new KeycloakContainer()
    .withHealth()
    .start();

With custom hostname

To run Keycloak with a custom hostname start the container with the following command:

const container = await new KeycloakContainer()
    .withHostname('localhost')
    .start();

With custom hostname path

To run Keycloak with a custom hostname path (default: /) start the container with the following command:

const container = await new KeycloakContainer()
    .withHostnamePath('/auth')
    .start();

With management port

You can change the default management port (9000) with the following command:

const container = await new KeycloakContainer()
    .withManagementPort(9001)
    .start();

With management path

You can change the default path for the management interface (/) with the following command:

const container = await new KeycloakContainer()
    .withManagementPath('/admin')
    .start();

With theme caching disabled

To disable theme caching start the container with the following command:

const container = await new KeycloakContainer()
    .withThemeCacheDisabled()
    .start();

Stopping a container

import KeycloakContainer from 'keycloak-testcontainer';

const container = await new KeycloakContainer()
    .start();
await container.stop();

Restarting a container

import KeycloakContainer from 'keycloak-testcontainer';

const container = await new KeycloakContainer()
    .start();
await container.restart();

Admin Client

It is possible to obtain an admin client for the test container after starting the container. This admin client can be used to change configuration for different tests. You can obtain an admin client with the following command:

const container = await new KeycloakContainer()
    .start();

container.getAdminClient({
    baseUrl: 'http://localhost:8080',
    realmName: 'master',
    totp: '123456'
});

The admin client can be customized by using an options object. By default the admin client uses http://localhost:8080 as the base url and the default admin credentials. If you changed the admin credentials by using withAdminUser(username, password) the new credentials will be used by the admin client and don't need to be passed as an option.

Currently, the client accepts the following options (all of them are optional):

  • baseUrl: The url pointing to keycloak
  • realmName: The name of the realm (default: master)
  • totp: optional one-time password (if required)