Skip to content

Commit

Permalink
feat: add test for ping controller
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Nov 14, 2024
1 parent bcb3d7f commit 2513db0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
node_modules
node_modules
coverage
36 changes: 36 additions & 0 deletions src/controllers/auth-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IHttpServerComponent } from "@well-known-components/interfaces";

Check failure on line 1 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Replace `"@well-known-components/interfaces";` with `'@well-known-components/interfaces'`

Check failure on line 1 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Strings must use singlequote
import { Context, StatusCode } from "../types";

Check failure on line 2 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Replace `"../types";` with `'../types'`

Check failure on line 2 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Strings must use singlequote

async function withAuthTokenValidation(
context: IHttpServerComponent.DefaultContext<Context<string>>,
next: () => Promise<IHttpServerComponent.IResponse>
): Promise<IHttpServerComponent.IResponse> {
// Validate Authorization header
const authHeader = context.request.headers.get("Authorization");

Check failure on line 9 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Replace `"Authorization");` with `'Authorization')`

Check failure on line 9 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Strings must use singlequote
if (!authHeader || !authHeader.startsWith("Bearer ")) {

Check failure on line 10 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Replace `"Bearer·"` with `'Bearer·'`

Check failure on line 10 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Strings must use singlequote
return {
status: StatusCode.UNAUTHORIZED,
body: {
ok: false,
message: "Missing or invalid Authorization header",

Check failure on line 15 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Replace `"Missing·or·invalid·Authorization·header",` with `'Missing·or·invalid·Authorization·header'`

Check failure on line 15 in src/controllers/auth-middleware.ts

View workflow job for this annotation

GitHub Actions / install

Strings must use singlequote
},
};
}

const token = authHeader.slice(7); // Extract token after 'Bearer '
const expectedToken = process.env.AUTH_TOKEN;

if (token !== expectedToken) {
return {
status: StatusCode.UNAUTHORIZED,
body: {
ok: false,
message: "Invalid authorization token",
},
};
}

return next();
}

export { withAuthTokenValidation };
2 changes: 1 addition & 1 deletion src/controllers/handlers/ping-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function pingHandler(context: Pick<HandlerContextWithPath<'metrics'
components: { metrics }
} = context

metrics.increment('wkc_logger_logs_total', {
metrics.increment('test_ping_counter', {
pathname: url.pathname
})

Expand Down
2 changes: 1 addition & 1 deletion src/ports/squids/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function createSubsquidComponent({
const servicesResponse = await client.send(listServicesCommand)

const serviceArns = servicesResponse.serviceArns || []
const squidServices = serviceArns.filter(arn => arn.includes('squid-server'))
const squidServices = serviceArns.filter(arn => arn.includes('-squid-server'))

// Step 2: Get tasks for each service and fetch task IPs
const results: Squid[] = []
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
IBaseComponent,
IMetricsComponent
} from '@well-known-components/interfaces'
import { metricDeclarations } from '@well-known-components/logger/dist/metrics'
import { IPgComponent } from '@well-known-components/pg-component'
import { metricDeclarations } from './metrics'
import { ISquidComponent } from './ports/squids/types'

export type GlobalContext = {
Expand Down
30 changes: 30 additions & 0 deletions test/unit/ping-controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createTestMetricsComponent } from '@well-known-components/metrics'
import { pingHandler } from '../../src/controllers/handlers/ping-handler'
import { metricDeclarations } from '../../src/metrics'

describe('ping-controller-unit', () => {
it('must return the pathname of a URL', async () => {
const url = new URL('https://github.com/well-known-components')
const metrics = createTestMetricsComponent(metricDeclarations)
expect((await metrics.getValue('test_ping_counter')).values).toEqual([])
expect(await pingHandler({ url, components: { metrics } })).toEqual({ body: url.pathname })
expect((await metrics.getValue('test_ping_counter')).values).toEqual([{ labels: { pathname: '/well-known-components' }, value: 1 }])
})

it('metrics should create a brand new registry', async () => {
const url = new URL('https://github.com/well-known-components')
const metrics = createTestMetricsComponent(metricDeclarations)
expect((await metrics.getValue('test_ping_counter')).values).toEqual([])
expect(await pingHandler({ url, components: { metrics } })).toEqual({ body: url.pathname })
expect((await metrics.getValue('test_ping_counter')).values).toEqual([{ labels: { pathname: '/well-known-components' }, value: 1 }])
})

it('calling twice should increment twice the metrics', async () => {
const url = new URL('https://github.com/well-known-components')
const metrics = createTestMetricsComponent(metricDeclarations)
expect((await metrics.getValue('test_ping_counter')).values).toEqual([])
expect(await pingHandler({ url, components: { metrics } })).toEqual({ body: url.pathname })
expect(await pingHandler({ url, components: { metrics } })).toEqual({ body: url.pathname })
expect((await metrics.getValue('test_ping_counter')).values).toEqual([{ labels: { pathname: '/well-known-components' }, value: 2 }])
})
})

0 comments on commit 2513db0

Please sign in to comment.