Skip to content

Commit

Permalink
Merge pull request #1695 from gchq/bugfix/fix-issue-with-putmodelcard…
Browse files Browse the repository at this point in the history
…-audit

updated putmodelcard audit to pass in model card revision object
  • Loading branch information
ARADDCC012 authored Jan 6, 2025
2 parents 7b4b784 + 52f33ac commit efa4d18
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/src/routes/v2/model/modelcard/putModelCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const putModelCard = [

const modelCard = await updateModelCard(req.user, modelId, metadata)

await audit.onUpdateModelCard(req, modelId, modelCard)
await audit.onUpdateModelCard(req, modelId, modelCard.toObject())

return res.json({
card: modelCard,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`routes > model > postModel > 200 > ok 1`] = `
exports[`routes > model > putModelCard > 200 > ok 1`] = `
{
"card": {
"_id": "test",
"_id": "6776901b879d08e34b599d7e",
"id": "6776901b879d08e34b599d7e",
"modelId": "test",
},
}
`;

exports[`routes > model > postModel > audit > expected call 1`] = `"vespillo"`;
exports[`routes > model > putModelCard > audit > expected call 1`] = `"vespillo"`;

exports[`routes > model > postModel > audit > expected call 2`] = `
exports[`routes > model > putModelCard > audit > expected call 2`] = `
{
"_id": "test",
"_id": "6776901b879d08e34b599d7e",
"modelId": "test",
}
`;
27 changes: 20 additions & 7 deletions backend/test/routes/model/modelcard/putModelCard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
import { ObjectId } from 'mongodb'
import { describe, expect, test, vi } from 'vitest'

import audit from '../../../../src/connectors/audit/__mocks__/index.js'
import ModelCardRevisionModel from '../../../../src/models/ModelCardRevision.js'
import { putModelCardSchema } from '../../../../src/routes/v2/model/modelcard/putModelCard.js'
import { createFixture, testPut } from '../../../testUtils/routes.js'

vi.mock('../../../../src/connectors/authorisation/index.js')
vi.mock('../../../../src/utils/user.js')
vi.mock('../../../../src/connectors/audit/index.js')

vi.mock('../../../../src/services/model.js', async () => {
const actual = (await vi.importActual('../../../../src/services/model.js')) as object
const modelServiceMock = vi.hoisted(() => {
return {
...actual,
updateModelCard: vi.fn(() => ({ _id: 'test' })),
updateModelCard: vi.fn(() => undefined as any),
}
})
vi.mock('../../../../src/services/model.js', () => modelServiceMock)

describe('routes > model > postModel', () => {
describe('routes > model > putModelCard', () => {
test('200 > ok', async () => {
const fixture = createFixture(putModelCardSchema)
modelServiceMock.updateModelCard.mockResolvedValueOnce(
new ModelCardRevisionModel({
_id: new ObjectId('6776901b879d08e34b599d7e'),
id: new ObjectId('6776901b879d08e34b599d7e'),
modelId: 'test',
}),
)
const res = await testPut(`/api/v2/model/${fixture.params.modelId}/model-cards`, fixture)

expect(res.statusCode).toBe(200)
expect(res.body).matchSnapshot()
})

test('audit > expected call', async () => {
const fixture = createFixture(putModelCardSchema)
modelServiceMock.updateModelCard.mockResolvedValueOnce(
new ModelCardRevisionModel({
_id: new ObjectId('6776901b879d08e34b599d7e'),
id: new ObjectId('6776901b879d08e34b599d7e'),
modelId: 'test',
}),
)
const res = await testPut(`/api/v2/model/${fixture.params.modelId}/model-cards`, fixture)

expect(res.statusCode).toBe(200)
expect(audit.onUpdateModelCard).toBeCalled()
expect(audit.onUpdateModelCard.mock.calls.at(0)?.at(1)).toMatchSnapshot()
Expand Down

0 comments on commit efa4d18

Please sign in to comment.