Testing

For testing purposes you can mock TokenServiceAuthStrategy with TokenAuthStrategyMock. Mock class represents both strategy and authenticator.

Example:

import passport from 'passport';
import { TokenAuthStrategyMock } from '@titanhouse/global/lib/auth/testing/token-auth-strategy-mock';

export const testAuthenticator = new TokenAuthStrategyMock('service-id');


describe('Http test', () => {
  before(() => {
    // IMPORTANT you shouldn't use original TokenServiceAuthStrategy or use it BEFORE mock one
    // basicly you just need to initialize express app before mocking
    passport.use(testAuthenticator.getStrategy());
  })

  it('Ok', async () => {
    const token = 'aaaaaa';
    testAuthenticator.authorize(token, {
      id: 'id',
      type: UserTypes.Client,
      roles: [],
      name: 'name',
      email: 'email',
    });

    const resp = await request()
      .get('/api/authorized-route')
      .set('Authorization', `Bearer ${token}`);
    expect(resp.status).to.eq(200);
  });
});

results matching ""

    No results matching ""