Skip to content

Commit

Permalink
route 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 4146e31 commit e0a02f9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ import {api} from './api/routes';
export const app = express();
app.use(morgan('combined'));

// set up rate limiter: maximum of five requests per minute
const limiter = RateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 30,
});
app.use(limiter);
if (process.env.NODE_ENV !== 'test') {
// set up rate limiter: maximum of five requests per minute
const limiter = RateLimit({

Check warning on line 56 in src/core.ts

View check run for this annotation

Codecov / codecov/patch

src/core.ts#L56

Added line #L56 was not covered by tests
windowMs: 1 * 60 * 1000, // 1 minute
max: 30,
});
app.use(limiter);

Check warning on line 60 in src/core.ts

View check run for this annotation

Codecov / codecov/patch

src/core.ts#L60

Added line #L60 was not covered by tests
}

// Only parse query parameters into strings, not objects
app.set('query parser', 'simple');
Expand Down
2 changes: 1 addition & 1 deletion src/datamodel/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Data models related to users.
*/

import {NonUniqueProjectID} from './core';
import {NonUniqueProjectID} from 'faims3-datamodel';
/*
* This represents an arbitrary profile from an arbitrary service that where
* information must be extracted. Specific services should use this internally,
Expand Down
95 changes: 94 additions & 1 deletion test/conductor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,44 @@ PouchDB.plugin(require('pouchdb-find'));

import request from 'supertest';
import {app} from '../src/routes';
import {CONDUCTOR_AUTH_PROVIDERS} from '../src/buildconfig';
import {CONDUCTOR_AUTH_PROVIDERS, LOCAL_COUCHDB_AUTH} from '../src/buildconfig';
import {expect} from 'chai';
import {cleanDataDBS, resetDatabases} from './mocks';
import {
createUser,
getUserFromEmailOrUsername,
saveUser,
} from '../src/couchdb/users';
import {createNotebook} from '../src/couchdb/notebooks';
import fs from 'fs';
import {createAuthKey} from '../src/authkeys/create';
import {getSigningKey} from '../src/authkeys/signing_keys';

it('check is up', async () => {
const result = await request(app).get('/up');
expect(result.statusCode).to.equal(200);
});

let adminToken = '';
const username = 'bobalooba';
const adminPassword = LOCAL_COUCHDB_AUTH ? LOCAL_COUCHDB_AUTH.password : '';

describe('Auth', () => {
beforeEach(async () => {
await resetDatabases();
await cleanDataDBS();
const signing_key = await getSigningKey();
const adminUser = await getUserFromEmailOrUsername('admin');
if (adminUser) {
adminToken = await createAuthKey(adminUser, signing_key);

Check warning on line 56 in test/conductor.test.ts

View workflow job for this annotation

GitHub Actions / build

'adminToken' is assigned a value but never used
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [user, _error] = await createUser('', username);
if (user) {
await saveUser(user);
}
}
});

it('redirect to auth', done => {
request(app)
.get('/')
Expand Down Expand Up @@ -72,4 +101,68 @@ describe('Auth', () => {
done();
});
});

it('shows the notebooks page', async () => {
const filename = 'notebooks/sample_notebook.json';
const jsonText = fs.readFileSync(filename, 'utf-8');
const {metadata, 'ui-specification': uiSpec} = JSON.parse(jsonText);

await createNotebook('test-notebook', uiSpec, metadata);
const agent = request.agent(app);

await agent
.post('/auth/local/')
.send({username: 'admin', password: adminPassword})
.expect(302);

await agent
.get('/notebooks/')
.expect(200)
.then(response => {
expect(response.text).to.include('test-notebook');
});
});

it('shows page for one notebook', async () => {
const filename = 'notebooks/sample_notebook.json';
const jsonText = fs.readFileSync(filename, 'utf-8');
const {metadata, 'ui-specification': uiSpec} = JSON.parse(jsonText);

const project_id = await createNotebook('test-notebook', uiSpec, metadata);
const agent = request.agent(app);

await agent
.post('/auth/local/')
.send({username: 'admin', password: adminPassword})
.expect(302);

await agent
.get(`/notebooks/${project_id}/`)
.expect(200)
.then(response => {
expect(response.text).to.include('test-notebook');
});
});

it('get home page logged in', async () => {
const filename = 'notebooks/sample_notebook.json';
const jsonText = fs.readFileSync(filename, 'utf-8');
const {metadata, 'ui-specification': uiSpec} = JSON.parse(jsonText);

await createNotebook('test-notebook', uiSpec, metadata);
const agent = request.agent(app);

await agent
.post('/auth/local/')
.send({username: 'admin', password: adminPassword})
.expect(302);

await agent
.get('/')
.expect(200)
.then(response => {
expect(response.text).to.include('Admin User');
});
});

Check warning on line 167 in test/conductor.test.ts

View workflow job for this annotation

GitHub Actions / build

Delete `⏎`
});

0 comments on commit e0a02f9

Please sign in to comment.