I am trying this with nestjs application in e2e tests but no luck !! any help is much appreciated
tkskumar opened this issue · 2 comments
tkskumar commented
chai.use(spies);
describe('Auth (e2e)', () => {
let app: INestApplication;
const configService = new ConfigService();
console.log(configService.get().auth.tokenIssuer);
const jwks = createJWKSMock(configService.get().auth.tokenIssuer);
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
jwks.start();
});
afterEach(async () => {
await app.close();
jwks.stop();
});
describe('/api/v1/suppliers (GET)', async () => {
it('should return bad request if auth headers are not set', async () => {
await request(app.getHttpServer())
.get('/api/v1/TEST')
.expect(400);
});
it('should return 401 if auth token is not Bearer token', async () => {
await request(app.getHttpServer())
.get('/api/v1/TEST')
.set({ Authorization: AUTH_FIXTURES.nonBearerAuthToken })
.expect(401);
});
it('should return 401 if auth token is invalid', async () => {
await request(app.getHttpServer())
.get('/api/v1/TEST')
.set({ Authorization: AUTH_FIXTURES.invalidBearerAuthToken })
.expect(401);
});
it('should return 401 if auth token is expired', async () => {
const mockToken = jwks.token({ exp: 0 });
const expiredAuthToken = `Bearer ${mockToken}`;
await request(app.getHttpServer())
.get('/api/v1/TEST')
.set({ Authorization: expiredAuthToken })
.expect(401);
});
it('should return 200 if token is valid', async () => {
const mockToken = jwks.token(AUTH_FIXTURES.mockValidToken);
const validAuthToken = `Bearer ${mockToken}`;
await request(app.getHttpServer())
.get('/api/v1/TEST')
.set({ Authorization: validAuthToken })
.expect(200);
});
});
});
tkskumar commented
chai.use(spies);
describe('Auth (e2e)', () => {
let app: INestApplication;
const configService = new ConfigService();
console.log(configService.get().auth.tokenIssuer);
const jwks = createJWKSMock(configService.get().auth.tokenIssuer);
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
jwks.start();
});
afterEach(async () => {
await app.close();
jwks.stop();
});
describe('/api/v1/suppliers (GET)', async () => {
it('should return bad request if auth headers are not set', async () => {
await request(app.getHttpServer())
.get('/api/v1/TEST')
.expect(400);
});
it('should return 401 if auth token is not Bearer token', async () => {
await request(app.getHttpServer())
.get('/api/v1/TEST')
.set({ Authorization: AUTH_FIXTURES.nonBearerAuthToken })
.expect(401);
});
it('should return 401 if auth token is invalid', async () => {
await request(app.getHttpServer())
.get('/api/v1/TEST')
.set({ Authorization: AUTH_FIXTURES.invalidBearerAuthToken })
.expect(401);
});
it('should return 401 if auth token is expired', async () => {
const mockToken = jwks.token({ exp: 0 });
const expiredAuthToken = `Bearer ${mockToken}`;
await request(app.getHttpServer())
.get('/api/v1/TEST')
.set({ Authorization: expiredAuthToken })
.expect(401);
});
it('should return 200 if token is valid', async () => {
const mockToken = jwks.token(AUTH_FIXTURES.mockValidToken);
const validAuthToken = `Bearer ${mockToken}`;
await request(app.getHttpServer())
.get('/api/v1/TEST')
.set({ Authorization: validAuthToken })
.expect(200);
});
});
});
levino commented
I do not understand what your configService
does etc. This repo contains tests and they are pretty complete and work. Just check it out and then adjust the code while keep it working. You will then understand what the problem is. I do not have the resources to reverse engineer or debug your code, I am sorry. As I said: There is a working example here in the codebase and you can take this as a starting point for your own project.