Skip to content

Commit

Permalink
test(backend): add a case for finding preprint information from Crossref
Browse files Browse the repository at this point in the history
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
thewilkybarkid committed Oct 28, 2021
1 parent 2c6e835 commit 1e0840e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"@parcel/transformer-image": "2.0.0-beta.2",
"@parcel/transformer-typescript-tsc": "2.0.0-beta.2",
"@parcel/validator-typescript": "2.0.0-beta.2",
"@types/faker": "^5.5.9",
"@types/jest": "^27.0.2",
"@types/koa-joi-router": "^8.0.1",
"@types/koa-log4": "^2.3.3",
Expand All @@ -180,8 +181,10 @@
"eslint-plugin-node": "9.0.1",
"eslint-plugin-promise": "4.1.1",
"eslint-plugin-react": "^7.18.0",
"faker": "^5.5.3",
"http-status-codes": "^2.1.4",
"jest": "^27.2.4",
"nock": "^13.1.4",
"nodemon": "^2.0.6",
"npm-watch": "^0.9.0",
"parcel": "2.0.0-beta.2",
Expand Down
49 changes: 49 additions & 0 deletions test/backend/resolve.test.ts
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),
});
});
});
7 changes: 7 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MikroORM } from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { RequestListener } from 'http';
import nock from 'nock';
import { IBackup, newDb } from 'pg-mem';
import dbConfig from '../src/backend/mikro-orm.config';
import configServer from '../src/backend/server';
Expand All @@ -9,6 +10,8 @@ let orm: MikroORM<PostgreSqlDriver>;
let backup: IBackup;

global.beforeAll(async () => {
nock.enableNetConnect('127.0.0.1');

const db = newDb();
orm = await db.adapters.createMikroOrm(dbConfig);
const generator = orm.getSchemaGenerator();
Expand All @@ -18,6 +21,10 @@ global.beforeAll(async () => {
backup = db.backup();
});

global.afterEach(() => {
nock.cleanAll();
});

global.afterAll(async () => {
await orm.close(true);
});
Expand Down
19 changes: 19 additions & 0 deletions test/utils.ts
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()}`;
}

0 comments on commit 1e0840e

Please sign in to comment.