-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(backend): add a case for finding preprint information from Crossref
This change adds a test case for resolving basic preprint information from the Crossref API. It uses Nock (https://github.com/nock/nock) to prevent actual HTTP requests from being made and returns a mock response for the Crossref API. Refs #388
- Loading branch information
1 parent
2c6e835
commit 1e0840e
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,49 @@ | ||
import faker from 'faker'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import nock from 'nock'; | ||
import request from 'supertest'; | ||
import { createServer } from '../setup'; | ||
import { fakeDoi, isoDateTime, uuid } from '../utils'; | ||
|
||
describe('resolve', () => { | ||
it('returns basic details from Crossref', async () => { | ||
const created = faker.date.past(); | ||
const doi = fakeDoi(); | ||
const title = faker.lorem.sentence(); | ||
|
||
nock('https://api.crossref.org/') | ||
.get(`/works/${doi}`) | ||
.reply(StatusCodes.OK, { | ||
status: 'ok', | ||
message: { | ||
DOI: doi, | ||
created: { | ||
'date-time': created.toISOString(), | ||
}, | ||
title: [title], | ||
}, | ||
}); | ||
|
||
const response = await request(await createServer()) | ||
.get('/api/v2/resolve') | ||
.query({ identifier: doi }); | ||
|
||
expect(response.status).toBe(StatusCodes.OK); | ||
expect(response.type).toBe('application/json'); | ||
expect(response.body).toStrictEqual({ | ||
authors: '', | ||
communities: [], | ||
createdAt: expect.stringMatching(isoDateTime), | ||
datePosted: created.toISOString(), | ||
fullReviews: [], | ||
handle: `doi:${doi}`, | ||
isPublished: false, | ||
rapidReviews: [], | ||
requests: [], | ||
tags: [], | ||
title: title, | ||
updatedAt: expect.stringMatching(isoDateTime), | ||
uuid: expect.stringMatching(uuid), | ||
}); | ||
}); | ||
}); |
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,19 @@ | ||
import faker from 'faker'; | ||
|
||
export const isoDateTime = | ||
/^[0-9]{4}-((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(0[469]|11)-(0[1-9]|[12][0-9]|30)|(02)-(0[1-9]|[12][0-9]))T(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])\.[0-9]{3}Z$/; | ||
|
||
export const uuid = | ||
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/; | ||
|
||
function fakeDoiPrefix(): string { | ||
return `10.${faker.datatype.number({ min: 1000, max: 999999999 })}`; | ||
} | ||
|
||
function fakeDoiSuffix(): string { | ||
return `${faker.lorem.words().replace(/\s/g, '.')}`; | ||
} | ||
|
||
export function fakeDoi(): string { | ||
return `${fakeDoiPrefix()}/${fakeDoiSuffix()}`; | ||
} |