0.0.6 • Published 1 year ago

@jc21/cypress-jwt-creation v0.0.6

Weekly downloads
261
License
MIT
Repository
github
Last release
1 year ago

Cypress JWT Creation

Create JWT tokens with ease

npm (scoped) npm (types) npm (licence)

Cypress Installation

yarn add @jc21/cypress-jwt-creation

Then in your cypress Plugins file:

const {JwtCreation} = require('@jc21/cypress-jwt-creation');

module.exports = (on, config) => {
    // ...
    on('task', JwtCreation(config));
    // ...
    return config;
};

Cypress Usage

describe('Hit an authenticated endpoint', () => {
    it('Should be able to get a response', async function () {
        cy.request('/users/me').then($response => {

            const token = await cy.task('generateToken', {
                privateKey: '/path/to/private.key',
                issuer:     'cypress-tester',
                algo:       'RS256',
                expires:    '1 day',
                claims:     {
                    capabilities: 'superuser'
                }
            });

            // use token in your requests
        });
    });
});

The Private Key

Due to the fact that this plugin runs on the Cypress Backend, the location of the private key file must be defined as either the full path on disk or relative path to the running of the cypress command. You can define the file location either with an environment variable which can apply to all tests:

config.env.jwtPrivateKey

or within each individial test using the options below. In addition, you can also define the JWT algorithm if different from the default RS256 with:

config.env.jwtAlgo

Options

OptionDescriptionOptionalDefault
privateKeyThe location of the private key filetrueconfig.env.jwtPrivateKey
issuerIssuer stringtrue"cypress.testing"
algoThe request method of the endpointtrueconfig.env.jwtAlgo or RS256
expiresEnglish interval of token expirytrue"1 day"
claimsAn object of extra claims you might want to settrue{}

Compiling Source

yarn install
yarn build
yarn test
0.0.5

1 year ago

0.0.6

1 year ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

5 years ago

0.0.1

5 years ago