Skip to content

Commit

Permalink
Added unit tests and JSDoc comments for public properties
Browse files Browse the repository at this point in the history
  • Loading branch information
JonWallsten committed Oct 4, 2024
1 parent 1613fe0 commit e16afd9
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
* Authorize HTTP API Routes with IAM
*/
export class HttpIamAuthorizer implements IHttpRouteAuthorizer {
/**
* The authorizationType used for IAM Authorizer
*/
public readonly authorizationType = HttpAuthorizerType.IAM;
public bind(_options: HttpRouteAuthorizerBindOptions): HttpRouteAuthorizerConfig {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export interface HttpJwtAuthorizerProps {
*/
export class HttpJwtAuthorizer implements IHttpRouteAuthorizer {
private authorizer?: HttpAuthorizer;
/**
* The authorizationType used for JWT Authorizer
*/
public readonly authorizationType = 'JWT';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ export interface HttpLambdaAuthorizerProps {
export class HttpLambdaAuthorizer implements IHttpRouteAuthorizer {
private authorizer?: HttpAuthorizer;
private httpApi?: IHttpApi;

/**
* The authorizationType used for Lambda Authorizer
*/
public readonly authorizationType = 'CUSTOM';

/**
* Initialize a lambda authorizer to be bound with HTTP route.
* @param id The id of the underlying construct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export interface HttpUserPoolAuthorizerProps {
*/
export class HttpUserPoolAuthorizer implements IHttpRouteAuthorizer {
private authorizer?: HttpAuthorizer;
/**
* The authorizationType used for UserPool Authorizer
*/
public readonly authorizationType = 'JWT';
/**
* Initialize a Cognito user pool authorizer to be bound with HTTP route.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,37 @@ describe('HttpJwtAuthorizer', () => {
// THEN
Template.fromStack(stack).resourceCountIs('AWS::ApiGatewayV2::Authorizer', 1);
});

test('should expose authorizer id after authorizer has been bound to route', () => {
// GIVEN
const stack = new Stack();
const api = new HttpApi(stack, 'HttpApi');
const authorizer = new HttpJwtAuthorizer('BooksAuthorizer', 'https://test.us.auth0.com', {
jwtAudience: ['3131231'],
});

// WHEN
api.addRoutes({
integration: new DummyRouteIntegration(),
path: '/books',
authorizer,
});

// THEN
expect(authorizer.authorizerId).toBeDefined();
});

test('should throw error when acessing authorizer before it been bound to route', () => {
// GIVEN
const stack = new Stack();
const t = () => {
const authorizer = new HttpJwtAuthorizer('BooksAuthorizer', 'https://test.us.auth0.com', {
jwtAudience: ['3131231'],
});
const authorizerId = authorizer.authorizerId;
};

// THEN
expect(t).toThrow(Error);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ describe('HttpLambdaAuthorizer', () => {
});

const t = () => {
new HttpLambdaAuthorizer('BooksAuthorizer', handler);
const authorizer = new HttpLambdaAuthorizer('BooksAuthorizer', handler);
const authorizerId = authorizer.authorizerId;
};

// THEN
expect(t).toThrow(TypeError);
expect(t).toThrow(Error);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,44 @@ describe('HttpUserPoolAuthorizer', () => {
},
});
});

test('should expose authorizer id after authorizer has been bound to route', () => {
// GIVEN
const stack = new Stack();
const api = new HttpApi(stack, 'HttpApi');
const userPool = new UserPool(stack, 'UserPool');
const userPoolClient1 = userPool.addClient('UserPoolClient1');
const userPoolClient2 = userPool.addClient('UserPoolClient2');
const authorizer = new HttpUserPoolAuthorizer('BooksAuthorizer', userPool, {
userPoolClients: [userPoolClient1, userPoolClient2],
});

// WHEN
api.addRoutes({
integration: new DummyRouteIntegration(),
path: '/books',
authorizer,
});

// THEN
expect(authorizer.authorizerId).toBeDefined();
});

test('should throw error when acessing authorizer before it been bound to route', () => {
// GIVEN
const stack = new Stack();
const userPool = new UserPool(stack, 'UserPool');
const userPoolClient1 = userPool.addClient('UserPoolClient1');
const userPoolClient2 = userPool.addClient('UserPoolClient2');

const t = () => {
const authorizer = new HttpUserPoolAuthorizer('BooksAuthorizer', userPool, {
userPoolClients: [userPoolClient1, userPoolClient2],
});
const authorizerId = authorizer.authorizerId;
};

// THEN
expect(t).toThrow(Error);
});
});

0 comments on commit e16afd9

Please sign in to comment.