-
Notifications
You must be signed in to change notification settings - Fork 18
New endpoint to allow admins to get all access requests #2888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
cf949ae
wip, initial endpoint to fetch all access requests
JRB66955 7823542
update findAccessRequest function to allow admins to get all accessRe…
JRB66955 3013b70
flatten if/else block and add more meaningful error msg
JRB66955 68e2694
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 9b3bf36
add initial tests for findAccessRequest func
JRB66955 fa88707
add tests for new getAccessRequests file
JRB66955 80f61ff
add back accidentally removed snapshot
JRB66955 6aa51aa
fix api routing issue, remove temp url to something more suitable whi…
JRB66955 5f643e0
re-order import to fix class extends value undefined error with authe…
JRB66955 1945c77
add additional mock getEntities func for auth
JRB66955 a7e992f
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 2affc33
wip commit, updating wrt to PR feedback, making endpoint more akin to…
JRB66955 f283092
update with new tests to reflect updated endpoint
JRB66955 26351ea
update one of the tests to exercise the full function, with added req…
JRB66955 85564c4
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 d21d80a
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 4da7e94
update with PR feedback, only include relevant ARs in auth connector,…
JRB66955 6640081
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 4703d4c
Slight tweak to new condition, try not to break python tests this time
JRB66955 84e76a4
rework of accessRequests, a new model/AR aggregation to loop/filter t…
JRB66955 3bc4252
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 43a4118
update accessRequest spec tests
JRB66955 8c636a9
tweak base auth check to work as intended, verify success status prop…
JRB66955 60d43a8
update filter/mine implementation, simplify and actually make work
JRB66955 0fad292
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 430e986
big update to the findAccessRequest endpoint, a new catch-all aggrega…
JRB66955 7c9c5b6
rename findAccessRequest -> findAccessRequests, trivially initially g…
JRB66955 adebb03
pr feedback, split the full stage aggregation into two to make return…
JRB66955 be65897
update tests with some example dummy data, shuffle spec snapshots to …
JRB66955 58ac0d5
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 0665eda
update with pr feedback, add isNamed condition
JRB66955 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -287,6 +287,17 @@ export class BasicAuthorisationConnector { | |
| return { success: false, info: 'You cannot change an access request you do not own.', id: request.id } | ||
| } | ||
|
|
||
| if ( | ||
| !(await this.model(user, model, ModelAction.View)).success && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes sense to update this to deny a user if they are viewing and access request and either they aren't able to view a model or they aren't named on the access request |
||
| ([AccessRequestAction.View] as AccessRequestActionKeys[]).includes(action) | ||
| ) { | ||
| return { | ||
| success: false, | ||
| info: 'You cannot view an access request for a model you cannot view.', | ||
| id: request.id, | ||
| } | ||
| } | ||
|
|
||
| // Otherwise they either own the model, access request or this is a read-only action. | ||
| return { success: true, id: request.id } | ||
| }), | ||
|
|
||
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
backend/src/routes/v2/model/accessRequest/getAccessRequests.ts
JRB66955 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,57 @@ | ||
| import { Request, Response } from 'express' | ||
| import { z } from 'zod' | ||
|
|
||
| import { AuditInfo } from '../../../../connectors/audit/Base.js' | ||
| import audit from '../../../../connectors/audit/index.js' | ||
| import { AccessRequestInterface } from '../../../../models/AccessRequest.js' | ||
| import { findAccessRequests } from '../../../../services/accessRequest.js' | ||
| import { accessRequestInterfaceSchema, registerPath } from '../../../../services/specification.js' | ||
| import { coerceArray, parse, strictCoerceBoolean } from '../../../../utils/validate.js' | ||
|
|
||
| export const GetAccessRequestsSchema = z.object({ | ||
| query: z.object({ | ||
| modelId: coerceArray(z.array(z.string()).optional().default([])), | ||
| schemaId: z.string().optional().default(''), | ||
| mine: strictCoerceBoolean(z.boolean().optional().default(false)), | ||
| adminAccess: strictCoerceBoolean(z.boolean().optional().default(false)), | ||
| }), | ||
| }) | ||
|
|
||
| registerPath({ | ||
| method: 'get', | ||
| path: '/api/v2/access-requests/search', | ||
| tags: ['access-request'], | ||
| description: 'Get all access requests for all models.', | ||
| schema: GetAccessRequestsSchema, | ||
| responses: { | ||
| 200: { | ||
| description: 'An array of access request instances.', | ||
| content: { | ||
| 'application/json': { | ||
| schema: z.object({ | ||
| accessRequests: z.array(accessRequestInterfaceSchema), | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| interface GetAccessRequestsResponse { | ||
| accessRequests: Array<AccessRequestInterface> | ||
| } | ||
|
|
||
| export const getAccessRequests = [ | ||
| async (req: Request, res: Response<GetAccessRequestsResponse>): Promise<void> => { | ||
| req.audit = AuditInfo.ViewAccessRequests | ||
| const { | ||
| query: { modelId, schemaId, mine, adminAccess }, | ||
| } = parse(req, GetAccessRequestsSchema) | ||
|
|
||
| const accessRequests = await findAccessRequests(req.user, modelId, schemaId, mine, adminAccess) | ||
|
|
||
| await audit.onViewAccessRequests(req, accessRequests) | ||
|
|
||
| res.json({ accessRequests }) | ||
| }, | ||
| ] |
This file contains hidden or 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
25 changes: 25 additions & 0 deletions
25
backend/test/routes/model/accessRequest/__snapshots__/getAccessRequests.spec.ts.snap
This file contains hidden or 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,25 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`routes > accessRequest > getAccessRequests > 200 > ok 1`] = ` | ||
| { | ||
| "accessRequests": { | ||
| "_id": "664e1aa8bda1f88c28e1c0ce", | ||
| "deleted": false, | ||
| "deletedAt": "", | ||
| "deletedBy": "", | ||
| "id": "test-access-request-13623", | ||
| "modelId": "test-model-4342", | ||
| }, | ||
| } | ||
| `; | ||
|
|
||
| exports[`routes > accessRequest > getAccessRequests > audit > expected call 1`] = ` | ||
| { | ||
| "_id": "664e1aa8bda1f88c28e1c0ce", | ||
| "deleted": false, | ||
| "deletedAt": "", | ||
| "deletedBy": "", | ||
| "id": "test-access-request-13623", | ||
| "modelId": "test-model-4342", | ||
| } | ||
| `; |
51 changes: 51 additions & 0 deletions
51
backend/test/routes/model/accessRequest/getAccessRequests.spec.ts
This file contains hidden or 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,51 @@ | ||
| import qs from 'qs' | ||
| import { describe, expect, test, vi } from 'vitest' | ||
|
|
||
| import audit from '../../../../src/connectors/audit/__mocks__/index.js' | ||
| import AccessRequestModel from '../../../../src/models/AccessRequest.js' | ||
| import { GetAccessRequestsSchema } from '../../../../src/routes/v2/model/accessRequest/getAccessRequests.js' | ||
| import { createFixture, testGet } from '../../../testUtils/routes.js' | ||
| import { testAccessRequest } from '../../../testUtils/testModels.js' | ||
|
|
||
| vi.mock('../../../../src/connectors/audit/index.js') | ||
|
|
||
| const accessRequestsMock = vi.hoisted(() => { | ||
| return { | ||
| findAccessRequests: vi.fn(() => undefined as any), | ||
| } | ||
| }) | ||
| vi.mock('../../../../src/services/accessRequest.js', () => accessRequestsMock) | ||
|
|
||
| const responseMock = vi.hoisted(() => { | ||
| return { | ||
| findResponses: vi.fn(() => undefined as any), | ||
| } | ||
| }) | ||
| vi.mock('../../../../src/services/response.js', () => responseMock) | ||
|
|
||
| describe('routes > accessRequest > getAccessRequests', () => { | ||
| test('200 > ok', async () => { | ||
| const accessRequestDoc = new AccessRequestModel({ ...testAccessRequest }) | ||
| accessRequestsMock.findAccessRequests.mockResolvedValueOnce(accessRequestDoc) | ||
| responseMock.findResponses.mockResolvedValue([testAccessRequest]) | ||
|
|
||
| const fixture = createFixture(GetAccessRequestsSchema) | ||
| const res = await testGet(`/api/v2/access-requests/search?${qs.stringify(fixture.query)}`) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(res.body).matchSnapshot() | ||
| }) | ||
|
|
||
| test('audit > expected call', async () => { | ||
| const accessRequestDoc = new AccessRequestModel({ ...testAccessRequest }) | ||
| accessRequestsMock.findAccessRequests.mockResolvedValueOnce(accessRequestDoc) | ||
| responseMock.findResponses.mockResolvedValue([testAccessRequest]) | ||
|
|
||
| const fixture = createFixture(GetAccessRequestsSchema) | ||
| const res = await testGet(`/api/v2/access-requests/search?${qs.stringify(fixture.query)}`) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(audit.onViewAccessRequests).toBeCalled() | ||
| expect(audit.onViewAccessRequests.mock.calls.at(0)?.at(1)).toMatchSnapshot() | ||
| }) | ||
| }) |
28 changes: 28 additions & 0 deletions
28
backend/test/services/__snapshots__/accessRequest.spec.ts.snap
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This authorisation function doesn't seem to be getting run for the search for some reason...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
False alarm, a problem with my local instance