diff --git a/src/core.ts b/src/core.ts index ac355ac6..a900d4d8 100644 --- a/src/core.ts +++ b/src/core.ts @@ -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({ + windowMs: 1 * 60 * 1000, // 1 minute + max: 30, + }); + app.use(limiter); +} // Only parse query parameters into strings, not objects app.set('query parser', 'simple'); diff --git a/src/datamodel/users.ts b/src/datamodel/users.ts index f7e8dd3e..c4207405 100644 --- a/src/datamodel/users.ts +++ b/src/datamodel/users.ts @@ -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, diff --git a/test/conductor.test.ts b/test/conductor.test.ts index e8bba2de..6e477995 100644 --- a/test/conductor.test.ts +++ b/test/conductor.test.ts @@ -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); + // 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('/') @@ -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'); + }); + }); + });