Git Product home page Git Product logo

cypress-keycloak's Introduction

cypress-keycloak

Cypress commands for Keycloak


Installation

Using npm:

$ npm install cypress-keycloak -D

Using yarn:

$ yarn add cypress-keycloak -D

Then with a module bundler like webpack, add the following line to cypress/support/commands.js | .ts:

// Using ES6
import 'cypress-keycloak';
// using CommonJS
require('cypress-keycloak');

Usage

Four cy commands have been added:

  • cy.logout({ ... }):
    • root: string
    • realm: string
    • redirect_uri?: string, for Keycloak >= 18.0.0 use post_logout_redirect_uri instead
    • post_logout_redirect_uri?: string, for Keycloak < 18.0.0 use post_logout_redirect_uri instead
    • id_token_hint?: string
    • path_prefix?: string = "auth"
  • cy.login({ ... }):
    • root: string
    • realm: string
    • username: string
    • password: string
    • client_id: string
    • redirect_uri: string
    • path_prefix?: string = "auth"
    • code_challenge_method: string, to use PKCE set this to S256
  • cy.loginOTP({ ... }):
    • root: string
    • realm: string
    • username: string
    • password: string
    • client_id: string
    • redirect_uri: string
    • path_prefix?: string = "auth"
    • otp_secret: string
    • otp_credential_id?: string | null = null
  • cy.register({ ... }):
    • root: string
    • realm: string
    • client_id: string
    • path_prefix?: string = "auth"
    • redirect_uri: string
    • kc_idp_hint?: string
    • username?: string
    • email?: string
    • password?: string
    • passwordConfirm?: string
    • firstName?: string
    • lastName?: string
    • additionalAttributes?: { [key: string]: any }

Installation:

If you don't want to use login with OTPs you can skip this section.

For generation of OTPs you need to create a new task named generateOTP in your cypress/plugins/index.js | .ts, like discribed in README of Cypress OTP.

To get your OTP secret you need to use e.g. an App for configurating OTP which can display the secret like FreeOTP+. The OTP credential ID can be found in Keycloaks Account Management Console, but only if there are at least two Authenticators configurated. It is only needed for login if you have more than one Authenticator.

Another way is to get these two values is by using the endpoint GET /{realmName}/users/{userId}/credentials.

It is highly recommended to save your username, password, otp_secret and otp_credential_id in .env.*.local or another file within your gitignore.

Example:

describe('thing', () => {
  beforeEach(() => {
    cy.login({
      root: 'https://keycloak.babangsund.com',
      realm: 'stage',
      username: 'babangsund',
      password: 'bacon',
      client_id: 'frontend',
      redirect_uri: 'https://babangsund.com/',
    });

    // or login with OTP
    cy.loginOTP({
      root: 'https://keycloak.babangsund.com',
      realm: 'stage',
      username: 'babangsund',
      password: 'bacon',
      client_id: 'frontend',
      redirect_uri: 'https://babangsund.com/',
      otp_secret: 'OZLDC2HZKM3QUC...', // e.g. 32 chars
      otp_credential_id: '5e231f20-8ca7-35e1-20a694b60181ca9', // e.g. 36 chars
    });
  });

  afterEach(() => {
    cy.logout({
      root: 'https://keycloak.babangsund.com',
      realm: 'stage',
      // using Keycloak < 18.0.0:
      // redirect_uri: 'https://babangsund.com/',
      // using Keycloak >= 18.0.0:
      post_logout_redirect_uri: 'https://babangsund.com/',
    });
  });
});

In case you want to declare these commands only one time with the values it is possible to overwrite them in your cypress/support/commands.js | .ts like this:

Cypress.Commands.overwrite('login', (originalFn) => {
  originalFn({
    root: 'https://keycloak.babangsund.com',
    realm: 'stage',
    username: 'babangsund',
    password: 'bacon',
    client_id: 'frontend',
    redirect_uri: 'https://babangsund.com/',
  })
})

...

And use them like this:

describe('thing', () => {
  beforeEach(() => {
    cy.login();

    // or login with OTP
    cy.loginOTP();
  });

  afterEach(() => {
    cy.logout();
  });
});

Credits

Owner

cypress-keycloak is built and maintained by babangsund. @blog. @github. @twitter.

Contributors

  • m4xd
  • Pascal Küsgen
  • Martin Guethle
  • Med Amine Amara
  • Oskar Thornblad
  • Kai
  • Raymon Ohmori
  • Robert Wysocki
  • Hussein Al Abry

cypress-keycloak's People

Contributors

babangsund avatar d-koppenhagen avatar dependabot[bot] avatar formatlos avatar kaispencer avatar kino90 avatar m4x3d avatar medamineamara avatar neryams avatar pascalmh avatar prewk avatar rlucha avatar robert-wysocki-sparkbit avatar romainbou avatar zidhuss avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.