All files / src testUtils.ts

100% Statements 28/28
100% Branches 0/0
100% Functions 7/7
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 693x   3x 3x       3x     3x   3x   3x 8x 8x   5x       3x         3x   3x         2x   2x 2x     2x 1x 1x       2x 1x     2x 3x     2x 2x 2x 2x                 2x    
import express from 'express';
import { Server } from 'http';
import jwtDecode from 'jwt-decode';
import { AuthClient, HttpConnector } from '.';
/**
 * AccessToken with no expiration date
 */
export const ACCESS_TOKEN =
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1SWQiOiJ1c2VyXzEyMyIsImNJZCI6ImNvbXBhbnlfMTIzIiwic2NvcGUiOiJhOnI6dyIsImlhdCI6MTUxOTA2MjY4MH0.FPnQLylqy7hfTLULsNDLNhaswFD3HI7zxRt6G-u3h9s';
 
export const AT_COOKIE = 'a_t';
 
export const RT_COOKIE = 'r_t';
 
export const decode = (at: string) => {
  try {
    return jwtDecode(at);
  } catch {
    return;
  }
};
 
export const fetchConnector = new HttpConnector({
  refreshAccessTokenUri: '',
  logoutUri: ''
});
 
export const basicAuth = new AuthClient({ decode });
 
export const createServer = (): {
  server: Server;
  authClient: AuthClient;
  url: string;
} => {
  const app = express();
 
  app.get('/accessToken', (_req, res) => {
    res.json({ accessToken: 'newToken' });
  });
 
  app.get('/accessToken/late', (_req, res) => {
    setTimeout(() => {
      res.json({ accessToken: Math.random().toString(36) });
    }, 500);
  });
 
  app.get('/logout', (_req, res) => {
    res.json({ done: true });
  });
 
  app.get('/error', (_req, res) => {
    res.status(400).json({ message: 'failed' });
  });
 
  const server: Server = app.listen(0);
  const { port } = server.address();
  const url = `http://localhost:${port}`;
  const authClient = new AuthClient({
    decode,
    refreshTokenCookie: 'r_t',
    fetchConnector: new HttpConnector({
      refreshAccessTokenUri: `${url}/accessToken`,
      logoutUri: `${url}/logout`
    })
  });
 
  return { server, authClient, url };
};