Chai-JWT
JSON Web tokens helpers for Chai
Description
A set of helpers for chai that allow you to make assertions on json web tokens.
Installation
npm i chai-jwt
Usage
const chai = ;const chaiJWT = ; chai;
Assertions
.jwt
Asserts that the provided string has a valid format that can be parsed as JWT. Does not check signature or the provided fields. If succeed, it changes the context to the decoded token.
Example
const validJWT = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6InVwZGF0ZTpzb21ldGhpbmciLCJleHAiOjE0NzA3NjE3ODEsImlhdCI6MTQ3MDc2MDc5M30.1b4RC22Kpx4X4GWXU-Wgsk4IbeRGVD7tNW-tM-LzkVE'; const invalidJWT = 'eyJ0eXAiOiJK22V1QiLCJhbGciOiJIUzI1NiJ9..1b4RC22Kpx4X4GWXU-Wgsk4IbeRGVD7tNW-tM-LzkVE'; tobeajwt; // Doesn't fail tobeajwt; // fails tobeajwtand;
.signedWith(secret)
Asserts that the provided token is signed with the provided secret.
Example
const validJWT = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6InVwZGF0ZTpzb21ldGhpbmciLCJleHAiOjE0NzA3NjE3ODEsImlhdCI6MTQ3MDc2MDc5M30.1b4RC22Kpx4X4GWXU-Wgsk4IbeRGVD7tNW-tM-LzkVE'; tobe; // Doesn't fail tobe; // fails
.claim(key, [value])
Alias of .property
for JWTs. Asserts that the provided token has a given claim. If value is provided
it will also assert the claim value using strict equality.
Example
const validJWT = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6InVwZGF0ZTpzb21ldGhpbmciLCJleHAiOjE0NzA3NjE3ODEsImlhdCI6MTQ3MDc2MDc5M30.1b4RC22Kpx4X4GWXU-Wgsk4IbeRGVD7tNW-tM-LzkVE'; tobehave; // Doesn't fail tobehave; // fails