Skip to content

Commit

Permalink
a few more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Cassidy <steve.cassidy@mq.edu.au>
  • Loading branch information
stevecassidy committed Nov 2, 2023
1 parent 4f4e68f commit 088bfc6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"require": [
"ts-node/register"
],
"recursive": true
"recursive": true,
"timeout": 10000
}
2 changes: 1 addition & 1 deletion src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ api.get(
requireAuthenticationAPI,
async (req, res) => {
if (req.user && userHasPermission(req.user, req.params.id, 'read')) {
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Type', 'application/zip');
streamNotebookFilesAsZip(req.params.id, req.params.viewid, res);
} else {
res.json({error: 'notebook not found'});
Expand Down
5 changes: 4 additions & 1 deletion src/couchdb/notebooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ export const streamNotebookFilesAsZip = async (
res: NodeJS.WritableStream
) => {
let allFilesAdded = false;
let doneFinalize = false;
const iterator = await notebookRecordIterator(project_id, viewID);
const archive = archiver('zip', {zlib: {level: 9}});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
Expand All @@ -703,8 +704,10 @@ export const streamNotebookFilesAsZip = async (
// check on progress, if we've finished adding files and they are
// all processed then we can finalize the archive
archive.on('progress', (entries: any) => {
if (allFilesAdded && entries.total === entries.processed) {
if (!doneFinalize && allFilesAdded && entries.total === entries.processed) {
console.log('finalizing archive');
archive.finalize();
doneFinalize = true;
}
});

Expand Down
17 changes: 17 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,23 @@ describe('API tests', () => {
}
});

it('can download files as zip', async () => {
// pull in some test data
await restoreFromBackup('test/backup.jsonl');

const adminUser = await getUserFromEmailOrUsername('admin');
if (adminUser) {
const notebooks = await getNotebooks(adminUser);
expect(notebooks).to.have.lengthOf(2);

await request(app)
.get('/api/notebooks/1693291182736-campus-survey-demo/FORM2.zip')
.set('Authorization', `Bearer ${adminToken}`)
.expect(200)
.expect('Content-Type', 'application/zip');
}
});

if (DEVELOPER_MODE) {
it('create random record', async () => {
const nb1 = await createNotebook('NB1', uispec, {});
Expand Down
12 changes: 11 additions & 1 deletion test/backup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
*/
import PouchDB from 'pouchdb';
import {restoreFromBackup} from '../src/couchdb/backupRestore';
import {getNotebooks, notebookRecordIterator} from '../src/couchdb/notebooks';
import {
getNotebookRecords,
getNotebooks,
notebookRecordIterator,
} from '../src/couchdb/notebooks';
import {registerClient} from 'faims3-datamodel';
import {getUserFromEmailOrUsername} from '../src/couchdb/users';
PouchDB.plugin(require('pouchdb-adapter-memory')); // enable memory adapter for testing
Expand Down Expand Up @@ -58,6 +62,12 @@ describe('Backup and restore', () => {
({record, done} = await iterator.next());
}
expect(count).to.equal(17);

// throw in a test of getNotebookRecords while we're here
const records = await getNotebookRecords(
notebooks[0].non_unique_project_id
);
expect(records).to.have.lengthOf(28);
}
});
});
19 changes: 19 additions & 0 deletions test/couchdb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
getNotebookUISpec,
getRolesForNotebook,
updateNotebook,
validateNotebookID,
} from '../src/couchdb/notebooks';
import * as fs from 'fs';
import {
Expand Down Expand Up @@ -187,6 +188,24 @@ describe('notebook api', () => {
}
});

it('can validate a notebook id', async () => {
const jsonText = fs.readFileSync(
'./notebooks/sample_notebook.json',
'utf-8'
);
const {metadata, 'ui-specification': uiSpec} = JSON.parse(jsonText);
const name = 'Test Notebook';
const projectID = await createNotebook(name, uiSpec, metadata);
expect(projectID).not.to.equal(undefined);
if (projectID) {
const valid = await validateNotebookID(projectID);
expect(valid).to.be.true;

const invalid = await validateNotebookID('invalid');
expect(invalid).to.be.false;
}
});

it('getNotebookUISpec', async () => {
await initialiseDatabases();

Expand Down

0 comments on commit 088bfc6

Please sign in to comment.