-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1007 from gchq/feature/entity-lookup
Initial work on entity lookup
- Loading branch information
Showing
7 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import bodyParser from 'body-parser' | ||
import { Request, Response } from 'express' | ||
import { z } from 'zod' | ||
|
||
import { UserInformation } from '../../../connectors/v2/authentication/Base.js' | ||
import authentication from '../../../connectors/v2/authentication/index.js' | ||
import { registerPath, UserInformationSchema } from '../../../services/v2/specification.js' | ||
import { toEntity } from '../../../utils/v2/entity.js' | ||
import { parse } from '../../../utils/v2/validate.js' | ||
|
||
export const getEntityLookupSchema = z.object({ | ||
params: z.object({ | ||
dn: z.string(), | ||
}), | ||
}) | ||
|
||
registerPath({ | ||
method: 'get', | ||
path: '/api/v2/entity/{dn}/lookup', | ||
tags: ['user'], | ||
description: 'Get information about an entity', | ||
schema: getEntityLookupSchema, | ||
responses: { | ||
200: { | ||
description: 'Information about the provided entity.', | ||
content: { | ||
'application/json': { | ||
schema: z.object({ entity: UserInformationSchema }), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
interface GetEntityLookup { | ||
entity: UserInformation | ||
} | ||
|
||
export const getEntityLookup = [ | ||
bodyParser.json(), | ||
async (req: Request, res: Response<GetEntityLookup>) => { | ||
const { | ||
params: { dn }, | ||
} = parse(req, getEntityLookupSchema) | ||
|
||
const information = await authentication.getUserInformation(toEntity('user', dn)) | ||
|
||
return res.json({ entity: information }) | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,3 +221,9 @@ export const webhookInterfaceSchema = z.object({ | |
createdAt: z.string().openapi({ example: new Date().toISOString() }), | ||
updatedAt: z.string().openapi({ example: new Date().toISOString() }), | ||
}) | ||
|
||
export const UserInformationSchema = z.object({ | ||
email: z.string().optional().openapi({ example: '[email protected]' }), | ||
name: z.string().optional().openapi({ example: 'Joe Bloggs' }), | ||
organisation: z.string().optional().openapi({ example: 'Acme Corp' }), | ||
}) |
19 changes: 19 additions & 0 deletions
19
backend/test/routes/entities/__snapshots__/getEntityLookup.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`routes > entities > getEntityLookup > 200 > ok 1`] = ` | ||
[ | ||
[ | ||
"user:userdn", | ||
], | ||
] | ||
`; | ||
|
||
exports[`routes > entities > getEntityLookup > 200 > ok 2`] = ` | ||
{ | ||
"entity": { | ||
"email": "[email protected]", | ||
"name": "Joe Bloggs", | ||
"organisation": "Acme Corp", | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { describe, expect, test, vi } from 'vitest' | ||
|
||
import { testGet } from '../../testUtils/routes.js' | ||
|
||
vi.mock('../../../src/utils/config.js') | ||
vi.mock('../../../src/utils/user.js') | ||
vi.mock('../../../src/utils/v2/config.js') | ||
|
||
const authenticationMocks = vi.hoisted(() => ({ | ||
getUserFromReq: vi.fn(() => ({ | ||
dn: 'user', | ||
})), | ||
getUserInformation: vi.fn(() => ({ | ||
email: `[email protected]`, | ||
name: 'Joe Bloggs', | ||
organisation: 'Acme Corp', | ||
})), | ||
})) | ||
vi.mock('../../../src/connectors/v2/authentication/index.js', async () => ({ | ||
default: authenticationMocks, | ||
})) | ||
|
||
vi.mock('../../../src/utils/v2/entity.js', async () => ({ | ||
toEntity: vi.fn(() => 'user:userdn'), | ||
})) | ||
|
||
describe('routes > entities > getEntityLookup', () => { | ||
test('200 > ok', async () => { | ||
vi.mock('../../../src/services/v2/model.js', () => ({ | ||
getModelById: vi.fn(() => ({ _id: 'test' })), | ||
})) | ||
const res = await testGet(`/api/v2/entity/userdn/lookup`) | ||
|
||
expect(res.statusCode).toBe(200) | ||
expect(authenticationMocks.getUserInformation.mock.calls).matchSnapshot() | ||
expect(res.body).matchSnapshot() | ||
}) | ||
}) |