Skip to content

Commit c9f2fb0

Browse files
authored
fix: basic auth in integration tests (#18)
1 parent ac732c7 commit c9f2fb0

5 files changed

Lines changed: 30 additions & 18 deletions

File tree

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ export default {
1515
reporters: ['default'],
1616
modulePaths: [compilerOptions.baseUrl],
1717
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { useESM: true }),
18+
testMatch: ['<rootDir>/src/**/*.test.ts'],
1819
testTimeout: 30000,
1920
}

src/__tests__/server.integration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('Server Integration Tests', () => {
101101
expect(response.statusCode).toBe(401)
102102
})
103103

104-
it.skip('should return customers list with valid credentials', async () => {
104+
it('should return customers list with valid credentials', async () => {
105105
const response = await app.inject({
106106
method: 'GET',
107107
url: '/crm/v1/customers',
@@ -163,7 +163,7 @@ describe('Server Integration Tests', () => {
163163
expect(body).toHaveProperty('openResourceDiscovery')
164164
})
165165

166-
it.skip('should return tenant-aware, system-instance ORD document', async () => {
166+
it('should return tenant-aware, system-instance ORD document', async () => {
167167
const response = await app.inject({
168168
method: 'GET',
169169
url: '/open-resource-discovery/v1/documents/system-instance',

src/api/health/v2/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { healthCheckV2Config } from './config.js'
55
* This is a typical health check API for health probes
66
* as used by CloudFoundry or K8s
77
*/
8-
export async function healthCheckV2Api(fastify: FastifyInstance): Promise<void> {
8+
export function healthCheckV2Api(fastify: FastifyInstance): void {
99
fastify.log.info(`Registering ${healthCheckV2Config.apiName}...`)
10-
fastify.get('/', async (req: FastifyRequest) => {
10+
fastify.get('/', (req: FastifyRequest) => {
1111
req.log.debug('Health Check invoked')
1212
return {
1313
status: 'OK',

src/api/shared/validateUserAuthorization.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FastifyRequest } from 'fastify'
1+
import { FastifyRequest, FastifyReply } from 'fastify'
22
import _ from 'lodash'
33
import { TenantConfiguration, tenants } from '../../data/user/tenants.js'
44
import { apiUsersAndPasswords } from '../../data/user/users.js'
@@ -24,18 +24,29 @@ const localTenants = Object.values(globalTenantIdToLocalTenantIdMapping)
2424
*
2525
* @throws UnauthorizedError
2626
*/
27-
export function validateUserAuthorization(username: string, password: string, req: FastifyRequest): void {
28-
if (apiUsersAndPasswords[username] && apiUsersAndPasswords[username].password === password) {
29-
const tenantId = apiUsersAndPasswords[username].tenantId
30-
// Add user info to the request that we've validated
31-
req.user = {
32-
userName: username,
33-
tenantId,
34-
tenantConfiguration: tenants[tenantId],
27+
export function validateUserAuthorization(
28+
username: string,
29+
password: string,
30+
req: FastifyRequest,
31+
_reply: FastifyReply,
32+
done: (error?: Error) => void,
33+
): void {
34+
try {
35+
if (apiUsersAndPasswords[username] && apiUsersAndPasswords[username].password === password) {
36+
const tenantId = apiUsersAndPasswords[username].tenantId
37+
// Add user info to the request that we've validated
38+
req.user = {
39+
userName: username,
40+
tenantId,
41+
tenantConfiguration: tenants[tenantId],
42+
}
43+
req.log.info(`User "${username}" of tenant "${tenantId}" authenticated successfully.`)
44+
done()
45+
} else {
46+
done(new UnauthorizedError(`Unknown username "${username}" and password combination`))
3547
}
36-
req.log.info(`User "${username}" of tenant "${tenantId}" authenticated successfully.`)
37-
} else {
38-
throw new UnauthorizedError(`Unknown username "${username}" and password combination`)
48+
} catch (error) {
49+
done(error instanceof Error ? error : new Error('Authentication failed'))
3950
}
4051
}
4152

src/event/odm-finance-costobject/v1/eventCatalogDefinition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export const openApiResourceName = 'openapi'
1313
*
1414
* This will later be referenced through ORD.
1515
*/
16-
export async function sapEventCatalogDefinition(fastify: FastifyInstance): Promise<void> {
16+
export function sapEventCatalogDefinition(fastify: FastifyInstance): void {
1717
fastify.get('/odm-finance-costobject.asyncapi2.json', {}, getSapEventCatalogDefinitionHandler)
1818
}
1919

20-
async function getSapEventCatalogDefinitionHandler(req: CustomRequest): Promise<SapEventCatalog> {
20+
function getSapEventCatalogDefinitionHandler(req: CustomRequest): SapEventCatalog {
2121
const tenantIds = getTenantIdsFromHeader(req)
2222
if (tenantIds.localTenantId) {
2323
// This is the `sap.foo.bar:open-local-tenant-id:v1` access strategy

0 commit comments

Comments
 (0)