Skip to content

Commit

Permalink
adding some authorization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lm-sec committed Jun 8, 2024
1 parent 47b15bf commit 2025bea
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import {
TestingData,
checkAuthorizations,
checkAuthorizationsCronApiKey,
createProject,
deleteReq,
getReq,
Expand Down Expand Up @@ -105,6 +106,10 @@ describe('Job Controller (e2e)', () => {
expect(r.statusCode).toBe(HttpStatus.OK);
});

// ####################################
// ########## Authorizations ##########
// ####################################

it('Should have proper authorizations (GET /jobs)', async () => {
const success = await checkAuthorizations(
testData,
Expand Down Expand Up @@ -156,6 +161,23 @@ describe('Job Controller (e2e)', () => {
expect(success).toBe(true);
});

it('Should have proper authorizations (POST /jobs/cleanup)', async () => {
const success = await checkAuthorizationsCronApiKey(
testData,
async (givenToken: string, headers, authenticate) => {
return await postReq(
app,
givenToken,
`/jobs/cleanup`,
undefined,
headers,
authenticate,
);
},
);
expect(success).toBe(true);
});

// The delete all jobs path cannot be called using this method
// because it breaks the tests as they are run in parallel
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import {
TestingData,
checkAuthorizations,
checkAuthorizationsCronApiKey,
getReq,
initTesting,
postReq,
} from '../../test/e2e.utils';
import { AppModule } from '../app.module';
import { Role } from '../auth/constants';

describe('Findings Controller (e2e)', () => {
let app: INestApplication;
let testData: TestingData;
let jobId: string;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
}),
);
await app.init();
testData = await initTesting(app);
});

afterAll(async () => {
await app.close();
});

// ####################################
// ########## Authorizations ##########
// ####################################

it('Should have proper authorizations (GET /findings/)', async () => {
const success = await checkAuthorizations(
testData,
Role.ReadOnly,
async (givenToken: string) => {
return await getReq(app, givenToken, `/findings/`);
},
);
expect(success).toBe(true);
});

it('Should have proper authorizations (POST /findings/cleanup)', async () => {
const success = await checkAuthorizationsCronApiKey(
testData,
async (givenToken: string, headers, authenticate) => {
return await postReq(
app,
givenToken,
`/findings/cleanup`,
undefined,
headers,
authenticate,
);
},
);
expect(success).toBe(true);
});
});
8 changes: 8 additions & 0 deletions packages/backend/jobs-manager/service/src/test/e2e.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ export async function checkAuthorizations(
return true;
}

/**
* Automatically runs a test with the different valid authorization levels and
* returns true if the authorizations were properly checked. Used to check authorizations
* on a controller endpoint that uses the `CronApiTokenGuard`
* @param data The test data that includes valid user tokens.
* @param call The function to call to test the endpoint. Takes a bearer token as parameter, headers and if it should be authenticated. Returns a supertest Response
* @returns true if authorization is valid only for the cron API token, false otherwise
*/
export async function checkAuthorizationsCronApiKey(
data: TestingData,
call: (
Expand Down

0 comments on commit 2025bea

Please sign in to comment.