From 62532f002f338a25d584f36901d72627a4c92641 Mon Sep 17 00:00:00 2001 From: Steve Cassidy Date: Wed, 1 Nov 2023 21:51:14 +1100 Subject: [PATCH] fix linting and tests Signed-off-by: Steve Cassidy --- src/couchdb/backupRestore.ts | 2 +- src/index.ts | 10 ++--- test/api.test.ts | 22 +++++----- test/authkeys.test.ts | 80 ++++++++++++++---------------------- test/backup.test.ts | 5 +-- test/devtools.test.ts | 6 +-- test/invites.test.ts | 10 ++--- test/mocks.ts | 1 + test/users.test.ts | 8 ++-- 9 files changed, 60 insertions(+), 84 deletions(-) diff --git a/src/couchdb/backupRestore.ts b/src/couchdb/backupRestore.ts index 1f57abb4..d6b5b6ac 100644 --- a/src/couchdb/backupRestore.ts +++ b/src/couchdb/backupRestore.ts @@ -69,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); } } } diff --git a/src/index.ts b/src/index.ts index 699dfebe..bee629c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,11 +30,11 @@ import {registerClient} from 'faims3-datamodel'; import {getProjectDataDB, getProjectMetaDB} from './couchdb'; // 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'); diff --git a/test/api.test.ts b/test/api.test.ts index 532cd367..d27d2d65 100644 --- a/test/api.test.ts +++ b/test/api.test.ts @@ -54,6 +54,7 @@ before(async () => { const adminUser = await getUserFromEmailOrUsername('admin'); if (adminUser) { adminToken = await createAuthKey(adminUser, signing_key); + // eslint-disable-next-line @typescript-eslint/no-unused-vars const [user, _error] = await createUser('', username); if (user) { await saveUser(user); @@ -183,18 +184,15 @@ it('get notebook', async () => { const project_id = await createNotebook('test-notebook', uiSpec, metadata); - if (project_id) { - return request(app) - .get('/api/notebooks/' + project_id) - .set('Authorization', `Bearer ${adminToken}`) - .set('Content-Type', 'application/json') - .expect(200) - .expect(response => { - expect(response.body.metadata.name).to.equal('test-notebook'); - }); - } else { - fail('unable to create test notebook'); - } + expect(project_id).not.to.be.undefined; + return request(app) + .get('/api/notebooks/' + project_id) + .set('Authorization', `Bearer ${adminToken}`) + .set('Content-Type', 'application/json') + .expect(200) + .expect(response => { + expect(response.body.metadata.name).to.equal('test-notebook'); + }); }); it('update admin user - no auth', async () => { diff --git a/test/authkeys.test.ts b/test/authkeys.test.ts index 6ee41a6f..33a0d55e 100644 --- a/test/authkeys.test.ts +++ b/test/authkeys.test.ts @@ -22,61 +22,43 @@ import PouchDB from 'pouchdb'; PouchDB.plugin(require('pouchdb-adapter-memory')); // enable memory adapter for testing PouchDB.plugin(require('pouchdb-find')); -import {testProp, fc} from 'jest-fast-check'; - import {createAuthKey} from '../src/authkeys/create'; import {validateToken} from '../src/authkeys/read'; -import type {CouchDBUsername, CouchDBUserRoles} from '../src/datamodel/users'; import {getSigningKey} from '../src/authkeys/signing_keys'; import {addOtherRoleToUser, createUser, saveUser} from '../src/couchdb/users'; import {expect} from 'chai'; -describe('fake test', () => { - expect(1).to.equal(1); -}); - -// describe('roundtrip creating and reading token', () => { -// testProp( -// 'token roundtrip', -// [ -// fc.fullUnicodeString(1, 100), // username -// fc.array(fc.fullUnicodeString()), // roles -// fc.fullUnicodeString(), // name -// ], -// async ( -// username: CouchDBUsername, -// roles: CouchDBUserRoles, -// name: string -// ) => { -// const signing_key = await getSigningKey(); +describe('roundtrip creating and reading token', () => { + it('create and read token', async () => { + const username = 'bobalooba'; + const name = 'Bob Bobalooba'; + const roles = ['admin', 'user']; + const signing_key = await getSigningKey(); -// // need to make a user with these details -// const [user, err] = await createUser(username, ''); + // need to make a user with these details + const [user, err] = await createUser(username, ''); -// if (user) { -// user.name = name; -// for (let i = 0; i < roles.length; i++) { -// addOtherRoleToUser(user, roles[i]); -// } -// await saveUser(user); + if (user) { + user.name = name; + for (let i = 0; i < roles.length; i++) { + addOtherRoleToUser(user, roles[i]); + } + await saveUser(user); -// return createAuthKey(user, signing_key) -// .then(token => { -// return validateToken(token); -// }) -// .then(valid_user => { -// expect(valid_user).not.toBe(undefined); -// if (valid_user) { -// expect(valid_user.user_id).toBe(user.user_id); -// expect(valid_user.roles).toStrictEqual(user.roles); -// expect(valid_user.name).toStrictEqual(user.name); -// // expect(CONDUCTOR_INSTANCE_NAME).toBe(token_props.instance_name); -// // expect(CONDUCTOR_KEY_ID).toBe(token_props.key_id); -// } -// }); -// } else { -// console.error(err); -// } -// } -// ); -// }); + return createAuthKey(user, signing_key) + .then(token => { + return validateToken(token); + }) + .then(valid_user => { + expect(valid_user).not.to.be.undefined; + if (valid_user) { + expect(valid_user.user_id).to.equal(user.user_id); + expect(valid_user.roles).to.deep.equal(user.roles); + expect(valid_user.name).to.equal(user.name); + } + }); + } else { + console.error(err); + } + }); +}); diff --git a/test/backup.test.ts b/test/backup.test.ts index 5b37f914..db401591 100644 --- a/test/backup.test.ts +++ b/test/backup.test.ts @@ -21,7 +21,7 @@ import PouchDB from 'pouchdb'; import {initialiseDatabases} from '../src/couchdb'; import {restoreFromBackup} from '../src/couchdb/backupRestore'; import {getNotebooks, notebookRecordIterator} from '../src/couchdb/notebooks'; -import {getDataDB, registerClient} from 'faims3-datamodel'; +import {registerClient} from 'faims3-datamodel'; import {getUserFromEmailOrUsername} from '../src/couchdb/users'; PouchDB.plugin(require('pouchdb-adapter-memory')); // enable memory adapter for testing PouchDB.plugin(require('pouchdb-find')); @@ -41,6 +41,7 @@ describe('Backup and restore', () => { // should now have the notebooks from the backup defined const user = await getUserFromEmailOrUsername('admin'); + expect(user).not.to.be.undefined; if (user) { const notebooks = await getNotebooks(user); expect(notebooks.length).to.equal(2); @@ -58,8 +59,6 @@ describe('Backup and restore', () => { ({record, done} = await iterator.next()); } expect(count).to.equal(17); - } else { - fail("can't find user admin"); } }); }); diff --git a/test/devtools.test.ts b/test/devtools.test.ts index c61b4860..6ea084a7 100644 --- a/test/devtools.test.ts +++ b/test/devtools.test.ts @@ -21,11 +21,7 @@ import PouchDB from 'pouchdb'; PouchDB.plugin(require('pouchdb-adapter-memory')); // enable memory adapter for testing PouchDB.plugin(require('pouchdb-find')); -import { - getProjectDataDB, - getProjectMetaDB, - initialiseDatabases, -} from '../src/couchdb'; +import {initialiseDatabases} from '../src/couchdb'; import {createNotebook} from '../src/couchdb/notebooks'; import * as fs from 'fs'; import {createRandomRecord} from '../src/couchdb/devtools'; diff --git a/test/invites.test.ts b/test/invites.test.ts index f9866bc0..2381e1e6 100644 --- a/test/invites.test.ts +++ b/test/invites.test.ts @@ -32,7 +32,7 @@ import {initialiseDatabases} from '../src/couchdb'; PouchDB.plugin(require('pouchdb-adapter-memory')); // enable memory adapter for testing PouchDB.plugin(require('pouchdb-find')); -import {expect} from 'chai'; +import {expect, assert} from 'chai'; const uispec: ProjectUIModel = { fields: [], @@ -68,10 +68,10 @@ describe('Invites', () => { const deleted = await deleteInvite(fetched); expect(deleted._deleted).to.be.true; } else { - fail('could not retrieve newly created invite'); + assert.fail('could not retrieve newly created invite'); } } else { - fail('could not get admin user'); + assert.fail('could not get admin user'); } }); @@ -91,10 +91,10 @@ describe('Invites', () => { expect(fetched.project_id).to.equal(project_id); expect(fetched.unlimited).to.be.true; } else { - fail('could not retrieve newly created invite'); + assert.fail('could not retrieve newly created invite'); } } else { - fail('could not get admin user'); + assert.fail('could not get admin user'); } }); }); diff --git a/test/mocks.ts b/test/mocks.ts index c4b2fe13..15976d32 100644 --- a/test/mocks.ts +++ b/test/mocks.ts @@ -1,4 +1,5 @@ import PouchDB from 'pouchdb'; +// eslint-disable-next-line node/no-unpublished-require PouchDB.plugin(require('pouchdb-adapter-memory')); // enable memory adapter for testing import {ProjectID, DBCallbackObject} from 'faims3-datamodel'; import {getProjectsDB, getUsersDB, initialiseDatabases} from '../src/couchdb'; diff --git a/test/users.test.ts b/test/users.test.ts index 1653d8ab..c53f8e91 100644 --- a/test/users.test.ts +++ b/test/users.test.ts @@ -37,7 +37,7 @@ import { } from '../src/couchdb/users'; PouchDB.plugin(require('pouchdb-adapter-memory')); // enable memory adapter for testing PouchDB.plugin(require('pouchdb-find')); -import {expect} from 'chai'; +import {expect, assert} from 'chai'; import * as fs from 'fs'; import {createNotebook} from '../src/couchdb/notebooks'; @@ -63,7 +63,7 @@ describe('user creation', () => { expect(newUserUsername.user_id).to.equal(username); expect(newUserUsername.emails.length).to.equal(0); } else { - fail('user is null after createUser with valid username'); + assert.fail('user is null after createUser with valid username'); } const [newUserEmail, errorEmail] = await createUser(email, ''); @@ -72,7 +72,7 @@ describe('user creation', () => { expect(newUserEmail.user_id).not.to.equal(''); expect(newUserEmail.emails).to.include(email.toLowerCase()); } else { - fail('user is null after createUser with valid email'); + assert.fail('user is null after createUser with valid email'); } }); @@ -256,7 +256,7 @@ describe('user creation', () => { } ); } else { - fail('user is null after createUser with valid username'); + assert.fail('user is null after createUser with valid username'); } });