creating a mock JWT token for testing

1.9k views Asked by At

I'm trying to create a test that will test the following branch.

if (req.body.sessionToken) {
  let sessionToken = req.body.sessionToken;
  let tokenData = jwt.verify(sessionToken,config.secretSessionKey);
  sessionID = tokenData.sessionID;
}

I tried creating a mock JSON response containing a sessionToken key with a random value that will be sent but I'm getting a UnauthorizedError: jwt malformed error. I believe that I only have to provide the mock sessionToken in the right format to run the test since based on the test coverage, I'm getting the error on the

let sessionTokenData = jwt.verify(sessionToken,config.secretSessionKey);

but I have to idea how should it be written.

Here is a part of my test. I'm calling the /authenticate endpoint then sending the sessionToken JSON that will contain the mock sessionToken value.

 return request
  .post("/authenticate")
  .send(sessionToken)
  .then(response => {
    let body = JSON.parse(response.text); })
0

There are 0 answers