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

@simulacrum/auth0-cypress

v0.6.10

Published

Cypress simulacrum commands

Downloads

157

Readme

@simulacrum/auth0-cypress

Cypress Auth0 addon that simulates an auth0 server running on localhost so you don't have to create fake accounts while developing or running tests that require authentication.

Contents

Installation

Step 1: Install the addon

npm install @simulacrum/auth0-cypress --dev

Step 2: Choose which sdk or javascript sdk (default @auth0/auth0-react)

This plugin supports the following javascript sdks that interface with auth0:

If you want to use nextjs-auth0 then you need to set the AUTH0_SDK environment variables by any of the usual cypress environmental varaibles options.

e.g. in cypress.env.json

{
  "AUTH0_SDK": "nextjs-auth0",
}

or as a cypress env var:

export CYPRESS_AUTH0_SDK=nextjs-auth0

Step 3: Import the commands

// cypress/support/index.js

import '@simulacrum/auth0-cypress';

Step 4: Register the encrypt task

We need to register an encrypt cypress task.

ESM

// cypress/plugins/index.js

import { encrypt } from '@simulacrum/auth0-cypress/encrypt';

export default (on) => {
  on('task', { encrypt });
};

commonjs

// cypress/plugins/index.js

const { encrypt } = require('@simulacrum/auth0-cypress/encrypt');

module.exports = (on, config) => {
  on('task', { encrypt });
}

Step 5: Configure Auth0

An example cypress environment file is in the root of this repo. You can change the configuration to your auth0 values.

// cypress.env.json
{
  "audience": "https://thefrontside.auth0.com/api/v1/",
  "domain": "localhost:4400",
  "clientID": "YOUR_AUTH0_CLIENT_ID",
  "connection": "Username-Password-Authentication",
  "scope": "openid profile email offline_access"
}

Usage

Start the simulator

PORT=4000 npx @simulacrum/auth0-simulator

Usage with start-server-and-test

Cypress recommends using start-server-and-test to ensure the test process exits and any servers are shut down.

npx start-server-and-test 'npm run start:server' http://localhost:3000 \
                      'npm run start:auth0' http://localhost:4000 \
                      cypress:run

The following commands are now available in your test suite:

createSimulation

createSimulation creates the fake auth0 server with your configuration

import auth0Config from "../../cypress.env.json";

describe('tests requiring auth')
  it('should access restricted resource', () => {
    cy
      .createSimulation(auth0Config)

given

given creates a fake user that can be used to log into the fake auth0 server.

create random user

describe('tests requiring auth')
  it('should access restricted resource', () => {
    cy
      .createSimulation(auth0Config)
      .visit("/")
      .contains("Log out").should('not.exist')
      .given() // with no arguments a random user is created
      .login()

supply fixed fields

describe('tests requiring auth')
  it('should access restricted resource', () => {
    cy
      .createSimulation(auth0Config)
      .visit("/")
      .contains("Log out").should('not.exist')
      .given({ email: '[email protected]' })  // fixed fields
      .login()

login()

Call login and logout in your test. For example:

import { Auth0ClientOptions } from '@auth0/auth0-spa-js';
import type { Client, Scenario, Simulation } from '@simulacrum/client';
import { createClient } from '@simulacrum/client';
import config from '../../cypress.env.json';

describe('log in', () => {
  it('should get token without signing in', () => {
    cy
      .createSimulation(auth0Config)
      .visit("/")
      .contains("Log out").should('not.exist')
      .given()
      .login()
      .visit("/")
      .contains("Log out")
      .logout();
  });
});

logout()

cy.logout();

cy.logout will destroy the simulation and do any clean up between tests like removing an cookies.

debugging

It is possible to hook up express middleware to log each endpoint that is called and any associated querystring or POST data by simply adding the debug: true option when calling createSimulation:

e.g.

it("should log in and log out", () => {
  cy
    .createSimulation({ ...auth0Config, debug: true })

debug console output