Skip to content

Commit

Permalink
port tests to mocha to avoid bug with jest and File
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Cassidy <[email protected]>
  • Loading branch information
stevecassidy committed Nov 1, 2023
1 parent 9490503 commit 33341c2
Show file tree
Hide file tree
Showing 21 changed files with 3,473 additions and 651 deletions.
8 changes: 8 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extension": ["ts"],
"spec": "test/*.test.ts",
"require": [
"ts-node/register"
],
"recursive": true
}
5 changes: 0 additions & 5 deletions jest.config.cjs

This file was deleted.

3,130 changes: 2,920 additions & 210 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 15 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"prestart": "npm run-script compile",
"start": "node .",
"watch": "nodemon --ignore build --ext ts --exec 'tsc --incremental && node .'",
"test": "env-cmd jest --silent=false test/*.test.ts",
"test:watch": "env-cmd jest --watch",
"coverage": "env-cmd jest --coverage test/*.test.ts",
"initdb": "env-cmd node scripts/initialise.js",
"load-notebooks": "env-cmd node scripts/loadNotebook.js ./notebooks/*.json"
"load-notebooks": "env-cmd node scripts/loadNotebook.js ./notebooks/*.json",
"test": "env-cmd cross-env NODE_ENV=test TS_NODE_PROJECT='./tsconfig.json' mocha",
"test:watch": "env-cmd cross-env NODE_ENV=test TS_NODE_PROJECT='./tsconfig.json' mocha -w",
"coverage": "nyc env-cmd cross-env NODE_ENV=test TS_NODE_PROJECT='./tsconfig.json' mocha"
},
"dependencies": {
"archiver": "^6.0.1",
Expand All @@ -31,10 +31,12 @@
"fast-check": "2.25.0",
"gts": "3.1.1",
"handlebars": "4.7.7.",
"istanbul": "^0.4.5",
"jose": "4.15.2",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.5",
"nyc": "^15.1.0",
"oauth": "0.10.0",
"passport": "0.5.3",
"passport-google-oauth20": "2.0.0",
Expand All @@ -45,21 +47,19 @@
"pouchdb-security-helper": "^2.1.2",
"qrcode": "^1.5.3",
"req-flash": "^0.0.3",
"supertest": "6.3.3",
"swagger-ui-dist": "^5.1.0",
"ts-jest": "27.1.4",
"ts-node": "10.9.1",
"typescript": "4.9.5",
"uuid": "9.0.1"
},
"devDependencies": {
"@types/archiver": "^5.3.4",
"@types/chai": "^4.3.9",
"@types/cookie-session": "2.0.45",
"@types/cors": "2.8.13",
"@types/express": "4.17.18",
"@types/express-handlebars": "5.3.1",
"@types/express-session": "1.17.5",
"@types/jest": "27.5.0",
"@types/mocha": "^10.0.3",
"@types/morgan": "^1.9.5",
"@types/multer": "^1.4.7",
"@types/node": "18.14.2",
Expand All @@ -70,17 +70,22 @@
"@types/passport-oauth2": "1.4.13",
"@types/pouchdb": "6.4.0",
"@types/qrcode": "^1.5.2",
"@types/sinon": "^10.0.20",
"@types/supertest": "2.0.12",
"@types/swagger-ui-dist": "^3.30.1",
"@types/uuid": "9.0.4",
"chai": "^4.3.10",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
"env-cmd": "^10.1.0",
"jest": "^27.5.1",
"jest-fast-check": "1.0.2",
"mocha": "^10.2.0",
"nano": "^10.1.2",
"node-fetch": "^3.3.2",
"nodemon": "^2.0.20",
"pouchdb-adapter-memory": "^8.0.1",
"supertest": "^6.3.3"
"sinon": "^17.0.0",
"supertest": "^6.3.3",
"ts-node": "10.9.1"
}
}
3 changes: 1 addition & 2 deletions src/authkeys/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {getSigningKey} from './signing_keys';
*/
export const validateToken = async (token: string) => {
const signingKey = await getSigningKey();

//console.log(`verifying token: '${token}'`);
try {
const {payload} = await jwtVerify(token, signingKey.public_key, {
Expand All @@ -39,7 +38,7 @@ export const validateToken = async (token: string) => {
//console.log('Token Payload', payload);

if (payload.sub) {
const user = getUserFromEmailOrUsername(payload.sub);
const user = await getUserFromEmailOrUsername(payload.sub);
return user;
} else {
return undefined;
Expand Down
9 changes: 5 additions & 4 deletions src/couchdb/backupRestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
* Functions to backup and restore databases
*/
import {open} from 'node:fs/promises';
import {getProjectDataDB, getProjectMetaDB, getProjectsDB} from '.';
import {getProjectsDB} from '.';
import {addDesignDocsForNotebook} from './notebooks';
import {getDataDB, getProjectDB} from 'faims3-datamodel';

/**
* restoreFromBackup - restore databases from a JSONL backup file
Expand All @@ -45,10 +46,10 @@ export const restoreFromBackup = async (filename: string) => {
db = await getProjectsDB();
} else if (dbName.startsWith('metadata')) {
const projectName = dbName.split('||')[1];
db = await getProjectMetaDB(projectName);
db = await getProjectDB(projectName);
} else if (dbName.startsWith('data')) {
const projectName = dbName.split('||')[1];
db = await getProjectDataDB(projectName);
db = await getDataDB(projectName);
if (db) {
addDesignDocsForNotebook(db);
// TODO: set up permissions for the databases
Expand All @@ -68,7 +69,7 @@ export const restoreFromBackup = async (filename: string) => {
try {
await db.put(doc.doc);
} catch (error) {
console.log('Error restoring document', doc.id, error);
console.log('Error restoring document', doc.id)//, error);

Check warning on line 72 in src/couchdb/backupRestore.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;·`
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/couchdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const getPublicUserDbURL = (): string => {
export const getUsersDB = (): PouchDB.Database | undefined => {
if (!_usersDB) {
const pouch_options = pouchOptions();

const dbName = COUCHDB_INTERNAL_URL + PEOPLE_DB_NAME;
_usersDB = new PouchDB(dbName, pouch_options);
}
Expand Down
1 change: 0 additions & 1 deletion src/couchdb/initialise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export const initialiseUserDB = async (db: PouchDB.Database | undefined) => {
security.members.roles.removeAll();
await security.save();
}

const [user, error] = await registerLocalUser(
'admin',
'', // no email address
Expand Down
4 changes: 2 additions & 2 deletions src/couchdb/notebooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const getNotebooks = async (user: Express.User): Promise<any[]> => {
projects.push(e.doc as unknown as ProjectObject);
}
});

for (const project of projects) {
const project_id = project._id;
const full_project_id = resolve_project_id(listing_id, project_id);
Expand Down Expand Up @@ -548,15 +549,14 @@ export const notebookRecordIterator = async (
) => {
let records = await getRecordsWithRegex(project_id, '.*', true);
let index = 0;

// select just those in this view
records = records.filter((record: any) => {
return record.type === viewid;
});

const recordIterator = {
async next() {
if (index < records.length - 1) {
if (index < records.length) {
const data = await getFullRecordData(
project_id,
records[index].record_id,
Expand Down
4 changes: 3 additions & 1 deletion src/couchdb/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ async function getUserFromUsername(
const users_db = getUsersDB();
if (users_db) {
try {
return (await users_db.get(username)) as Express.User;
const user = (await users_db.get(username)) as Express.User;
return user;
//return (await users_db.get(username)) as Express.User;
} catch (err) {
return null;
}
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import {registerClient} from 'faims3-datamodel';
import {getProjectDataDB, getProjectMetaDB} from './couchdb';

Check warning on line 30 in src/index.ts

View workflow job for this annotation

GitHub Actions / build

'getProjectDataDB' is defined but never used

Check warning on line 30 in src/index.ts

View workflow job for this annotation

GitHub Actions / build

'getProjectMetaDB' is defined but never used

// set up the database module faims3-datamodel with our callbacks to get databases
registerClient({
getDataDB: getProjectDataDB,
getProjectDB: getProjectMetaDB,
shouldDisplayRecord: () => true,
});
// registerClient({
// getDataDB: getProjectDataDB,
// getProjectDB: getProjectMetaDB,
// shouldDisplayRecord: () => true,
// });

process.on('unhandledRejection', error => {
console.error('unhandledRejection');
Expand Down
55 changes: 27 additions & 28 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import request from 'supertest';
import {app} from '../src/routes';
import {initialiseDatabases} from '../src/couchdb';
import {
createUser,
getUserFromEmailOrUsername,
createUser,
saveUser,
} from '../src/couchdb/users';
import {createAuthKey} from '../src/authkeys/create';
Expand All @@ -36,6 +36,7 @@ import fs from 'fs';
import {createNotebook} from '../src/couchdb/notebooks';
import {ProjectUIModel} from 'faims3-datamodel';
import {DEVELOPER_MODE} from '../src/buildconfig';
import {expect} from 'chai';

const uispec: ProjectUIModel = {
fields: [],
Expand All @@ -47,45 +48,43 @@ const uispec: ProjectUIModel = {
let adminToken = '';
const username = 'bobalooba';

beforeAll(async () => {
before(async () => {
await initialiseDatabases();
const signing_key = await getSigningKey();
const adminUser = await getUserFromEmailOrUsername('admin');
if (adminUser) {
adminToken = await createAuthKey(adminUser, signing_key);

const [user, error] = await createUser('', username);
const [user, _error] = await createUser('', username);

Check warning on line 57 in test/api.test.ts

View workflow job for this annotation

GitHub Actions / build

'_error' is assigned a value but never used
if (user) {
await saveUser(user);
} else {
throw new Error(error);
}
}
});

test('check is up - not authenticated', async () => {
it('check is up - not authenticated', async () => {
const result = await request(app).get('/api/hello');
expect(result.statusCode).toBe(401);
expect(result.statusCode).to.equal(401);
});

test('check is up - authenticated', async () => {
it('check is up - authenticated', async () => {
console.log('check is up - authenticated');
const result = await request(app)
.get('/api/hello')
.set('Authorization', `Bearer ${adminToken}`);
expect(result.statusCode).toBe(200);
expect(result.statusCode).to.equal(200);
});

test('get notebooks', () => {
it('get notebooks', () => {
return request(app)
.get('/api/notebooks')
.set('Authorization', `Bearer ${adminToken}`)
.expect(200)
.expect(response => {
expect(response.body).toEqual([]);
expect(response.body).to.be.empty;
});
});

test('create notebook', () => {
it('create notebook', () => {
const filename = 'notebooks/sample_notebook.json';
const jsonText = fs.readFileSync(filename, 'utf-8');
const {metadata, 'ui-specification': uiSpec} = JSON.parse(jsonText);
Expand All @@ -101,11 +100,11 @@ test('create notebook', () => {
.set('Content-Type', 'application/json')
.expect(200)
.expect(response => {
expect(response.body.notebook).toMatch('-test-notebook');
expect(response.body.notebook).to.include('-test-notebook');
});
});

test('update notebook', () => {
it('update notebook', () => {
const filename = 'notebooks/sample_notebook.json';
const jsonText = fs.readFileSync(filename, 'utf-8');
const {metadata, 'ui-specification': uiSpec} = JSON.parse(jsonText);
Expand Down Expand Up @@ -172,12 +171,12 @@ test('update notebook', () => {
.set('Content-Type', 'application/json')
.expect(200)
.expect(response => {
expect(response.body.notebook).toMatch('-test-notebook');
expect(response.body.notebook).to.include('-test-notebook');
});
});
});

test('get notebook', async () => {
it('get notebook', async () => {
const filename = 'notebooks/sample_notebook.json';
const jsonText = fs.readFileSync(filename, 'utf-8');
const {metadata, 'ui-specification': uiSpec} = JSON.parse(jsonText);
Expand All @@ -191,23 +190,23 @@ test('get notebook', async () => {
.set('Content-Type', 'application/json')
.expect(200)
.expect(response => {
expect(response.body.metadata.name).toEqual('test-notebook');
expect(response.body.metadata.name).to.equal('test-notebook');
});
} else {
fail('unable to create test notebook');
}
});

test('update admin user - no auth', async () => {
return request(app)
it('update admin user - no auth', async () => {
return await request(app)
.post(`/api/users/${username}/admin`)
.send({addrole: true})
.set('Content-Type', 'application/json')
.expect(401);
});

test('update admin user - add role', () => {
return request(app)
it('update admin user - add role', async () => {
return await request(app)
.post(`/api/users/${username}/admin`)
.set('Authorization', `Bearer ${adminToken}`)
.set('Content-Type', 'application/json')
Expand All @@ -216,7 +215,7 @@ test('update admin user - add role', () => {
.expect({status: 'success'});
});

test('update admin user - remove role', () => {
it('update admin user - remove role', () => {
return request(app)
.post(`/api/users/${username}/admin`)
.set('Authorization', `Bearer ${adminToken}`)
Expand All @@ -226,7 +225,7 @@ test('update admin user - remove role', () => {
.expect({status: 'success'});
});

test('get notebook users', async () => {
it('get notebook users', async () => {
const filename = 'notebooks/sample_notebook.json';
const jsonText = fs.readFileSync(filename, 'utf-8');
const {metadata, 'ui-specification': uiSpec} = JSON.parse(jsonText);
Expand All @@ -239,17 +238,17 @@ test('get notebook users', async () => {
.set('Content-Type', 'application/json')
.expect(200)
.then(response => {
expect(response.body.roles).toEqual([
expect(response.body.roles).to.deep.equal([
'admin',
'moderator',
'team',
'user',
]);
expect(response.body.users.length).toEqual(1);
expect(response.body.users.length).to.equal(1);
});
});

test('update notebook roles', async () => {
it('update notebook roles', async () => {
// make some notebooks
const nb1 = await createNotebook('NB1', uispec, {});

Expand All @@ -271,7 +270,7 @@ test('update notebook roles', async () => {
});

if (DEVELOPER_MODE) {
test('create random record', async () => {
it('create random record', async () => {
const nb1 = await createNotebook('NB1', uispec, {});

if (nb1) {
Expand Down
Loading

0 comments on commit 33341c2

Please sign in to comment.