Skip to content

Commit

Permalink
fix linting and tests
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 33341c2 commit 62532f0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/couchdb/backupRestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
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';

// 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
22 changes: 10 additions & 12 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 () => {
Expand Down
80 changes: 31 additions & 49 deletions test/authkeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
5 changes: 2 additions & 3 deletions test/backup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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);
Expand All @@ -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");
}
});
});
6 changes: 1 addition & 5 deletions test/devtools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 5 additions & 5 deletions test/invites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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');
}
});

Expand All @@ -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');
}
});
});
1 change: 1 addition & 0 deletions test/mocks.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
8 changes: 4 additions & 4 deletions test/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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, '');
Expand All @@ -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');
}
});

Expand Down Expand Up @@ -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');
}
});

Expand Down

0 comments on commit 62532f0

Please sign in to comment.