-
Notifications
You must be signed in to change notification settings - Fork 15
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
Check for approved access request #1069
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,9 @@ export async function getFilesByModel(user: UserDoc, modelId: string) { | |
|
||
export async function getFilesByIds(user: UserDoc, modelId: string, fileIds: string[]) { | ||
const model = await getModelById(user, modelId) | ||
if (fileIds.length === 0) { | ||
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. If you're going to add edge case checks like this, they should probably be at the top of the function. Did you put it down here so that we're still checking model authorisation? 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. Yes. |
||
return [] | ||
} | ||
const files = await FileModel.find({ _id: { $in: fileIds } }) | ||
|
||
if (files.length !== fileIds.length) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { describe, expect, test, vi } from 'vitest' | ||
|
||
import { BasicAuthorisationConnector } from '../../../src/connectors/v2/authorisation/base.js' | ||
import { ModelDoc } from '../../../src/models/v2/Model.js' | ||
import { UserDoc } from '../../../src/models/v2/User.js' | ||
|
||
const mockAccessRequestService = vi.hoisted(() => ({ | ||
getModelAccessRequestsForUser: vi.fn(), | ||
})) | ||
vi.mock('../../../src/services/v2/accessRequest.js', () => mockAccessRequestService) | ||
|
||
const mockModelService = vi.hoisted(() => ({})) | ||
vi.mock('../../../src/services/v2/model.js', () => mockModelService) | ||
|
||
const mockReviewService = vi.hoisted(() => ({ | ||
checkAccessRequestsApproved: vi.fn(), | ||
})) | ||
vi.mock('../../../src/services/v2/review.js', () => mockReviewService) | ||
|
||
describe('connectors > authorisation > base', () => { | ||
const user = { dn: 'testUser' } as UserDoc | ||
const model = { id: 'testModel' } as ModelDoc | ||
|
||
test('hasApprovedAccessRequest > no access requests for model', async () => { | ||
const connector = new BasicAuthorisationConnector() | ||
mockAccessRequestService.getModelAccessRequestsForUser.mockReturnValueOnce([]) | ||
|
||
const result = await connector.hasApprovedAccessRequest(user, model) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
|
||
test('hasApprovedAccessRequest > return access request check', async () => { | ||
const connector = new BasicAuthorisationConnector() | ||
mockAccessRequestService.getModelAccessRequestsForUser.mockReturnValueOnce([{ id: 'accessRequest' }]) | ||
const approvedAccessRequest = true | ||
mockReviewService.checkAccessRequestsApproved.mockReturnValueOnce(approvedAccessRequest) | ||
|
||
const result = await connector.hasApprovedAccessRequest(user, model) | ||
|
||
expect(result).toBe(approvedAccessRequest) | ||
}) | ||
}) |
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.
I would expect this to be a
service
function, not in a connector.Also, the implementation is a little odd. I wouldn't tend to handle the edge case of no access requests. The speed increase likely isn't worth the complexity?
If you did want to keep the check, I'd expect something like:
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.
"I would expect this to be a service function, not in a connector." - This was mainly done because the previous implementation made the same decision to make this function in the connector rather than a service. Additionally, it's unclear to me which service this function would belong to as uses both access requests and reviews to make the decision.
"The speed increase likely isn't worth the complexity?" - The complexity to me seems very minimal considering it's a single if statement to avoid an unnecessary database query, I'd happy to reverse the if statement.