-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into UserRoleTesting
- Loading branch information
Showing
71 changed files
with
4,169 additions
and
118 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
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,64 @@ | ||
import Ajv from 'ajv'; | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { CritterbaseService, IBulkCreate } from '../../../services/critterbase-service'; | ||
import { getRequestHandlerMocks } from '../../../__mocks__/db'; | ||
import * as createCritter from './index'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('paths/critter-data/critters/post', () => { | ||
const ajv = new Ajv(); | ||
|
||
it('is valid openapi v3 schema', () => { | ||
expect(ajv.validateSchema((createCritter.POST.apiDoc as unknown) as object)).to.be.true; | ||
}); | ||
|
||
const payload: IBulkCreate = { | ||
critters: [], | ||
captures: [], | ||
mortalities: [], | ||
locations: [], | ||
markings: [], | ||
qualitative_measurements: [], | ||
quantitative_measurements: [], | ||
families: [], | ||
collections: [] | ||
}; | ||
|
||
describe('createCritter', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
it('should succeed', async () => { | ||
const mockCreateCritter = sinon.stub(CritterbaseService.prototype, 'createCritter').resolves({ count: 0 }); | ||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.body = payload; | ||
const requestHandler = createCritter.createCritter(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockCreateCritter).to.have.been.calledOnceWith(payload); | ||
//expect(mockCreateCritter).calledWith(payload); | ||
expect(mockRes.statusValue).to.equal(201); | ||
expect(mockRes.json.calledWith({ count: 0 })).to.be.true; | ||
}); | ||
it('should fail', async () => { | ||
const mockError = new Error('mock error'); | ||
const mockCreateCritter = sinon.stub(CritterbaseService.prototype, 'createCritter').rejects(mockError); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.body = payload; | ||
const requestHandler = createCritter.createCritter(); | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockCreateCritter).to.have.been.calledOnceWith(payload); | ||
} | ||
}); | ||
}); | ||
}); |
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,160 @@ | ||
import { RequestHandler } from 'express'; | ||
import { Operation } from 'express-openapi'; | ||
import { PROJECT_PERMISSION, SYSTEM_ROLE } from '../../../constants/roles'; | ||
import { authorizeRequestHandler } from '../../../request-handlers/security/authorization'; | ||
import { CritterbaseService, ICritterbaseUser } from '../../../services/critterbase-service'; | ||
import { getLogger } from '../../../utils/logger'; | ||
|
||
const defaultLog = getLogger('paths/critter-data/critters'); | ||
export const POST: Operation = [ | ||
authorizeRequestHandler((req) => { | ||
return { | ||
or: [ | ||
{ | ||
validProjectPermissions: [PROJECT_PERMISSION.COORDINATOR, PROJECT_PERMISSION.COLLABORATOR], | ||
projectId: Number(req.params.projectId), | ||
discriminator: 'ProjectPermission' | ||
}, | ||
{ | ||
validSystemRoles: [SYSTEM_ROLE.DATA_ADMINISTRATOR], | ||
discriminator: 'SystemRole' | ||
} | ||
] | ||
}; | ||
}), | ||
createCritter() | ||
]; | ||
|
||
POST.apiDoc = { | ||
description: | ||
'Creates a new critter in critterbase. Optionally make captures, markings, measurements, etc. along with it.', | ||
tags: ['critterbase'], | ||
security: [ | ||
{ | ||
Bearer: [] | ||
} | ||
], | ||
requestBody: { | ||
description: 'Critterbase bulk creation request object', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
title: 'Bulk post request object', | ||
type: 'object', | ||
properties: { | ||
critters: { | ||
title: 'critters', | ||
type: 'array', | ||
items: { | ||
title: 'critter', | ||
type: 'object' | ||
} | ||
}, | ||
captures: { | ||
title: 'captures', | ||
type: 'array', | ||
items: { | ||
title: 'capture', | ||
type: 'object' | ||
} | ||
}, | ||
collections: { | ||
title: 'collection units', | ||
type: 'array', | ||
items: { | ||
title: 'collection unit', | ||
type: 'object' | ||
} | ||
}, | ||
markings: { | ||
title: 'markings', | ||
type: 'array', | ||
items: { | ||
title: 'marking', | ||
type: 'object' | ||
} | ||
}, | ||
locations: { | ||
title: 'locations', | ||
type: 'array', | ||
items: { | ||
title: 'location', | ||
type: 'object' | ||
} | ||
}, | ||
mortalities: { | ||
title: 'locations', | ||
type: 'array', | ||
items: { | ||
title: 'location', | ||
type: 'object' | ||
} | ||
}, | ||
qualitative_measurements: { | ||
title: 'qualitative measurements', | ||
type: 'array', | ||
items: { | ||
title: 'qualitative measurement', | ||
type: 'object' | ||
} | ||
}, | ||
quantitative_measurements: { | ||
title: 'quantitative measurements', | ||
type: 'array', | ||
items: { | ||
title: 'quantitative measurement', | ||
type: 'object' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
201: { | ||
description: 'Responds with counts of objects created in critterbase.', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
title: 'Bulk creation response object', | ||
type: 'object' | ||
} | ||
} | ||
} | ||
}, | ||
400: { | ||
$ref: '#/components/responses/400' | ||
}, | ||
401: { | ||
$ref: '#/components/responses/401' | ||
}, | ||
403: { | ||
$ref: '#/components/responses/401' | ||
}, | ||
500: { | ||
$ref: '#/components/responses/500' | ||
}, | ||
default: { | ||
$ref: '#/components/responses/default' | ||
} | ||
} | ||
}; | ||
|
||
export function createCritter(): RequestHandler { | ||
return async (req, res) => { | ||
const user: ICritterbaseUser = { | ||
keycloak_guid: req['system_user']?.user_guid, | ||
username: req['system_user']?.user_identifier | ||
}; | ||
|
||
const cb = new CritterbaseService(user); | ||
try { | ||
const result = await cb.createCritter(req.body); | ||
return res.status(201).json(result); | ||
} catch (error) { | ||
defaultLog.error({ label: 'createCritter', message: 'error', error }); | ||
throw error; | ||
} | ||
}; | ||
} |
Oops, something went wrong.