Skip to content

Commit

Permalink
Merge pull request #229 from Inist-CNRS/host_config
Browse files Browse the repository at this point in the history
[RFR] Rework URIs
  • Loading branch information
ThieryMichel committed Apr 3, 2017
2 parents b136330 + ffc5ec0 commit a859cf1
Show file tree
Hide file tree
Showing 44 changed files with 271 additions and 281 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ On EzMaster, you can edit the instance configuration:
- `exporters`: Required - an array of the allowed exporters

- `loader`: Required - an array of loaders (which import your data) with their options
- `host`: Optional - the public host which will be used to generate resources URIs. It will fallback on the EzMaster environment variable `EZMASTER_PUBLIC_URL`. Format is `http://[host]` (no ending slash)

- `mongo`: Optional - Allow to override the default mongo configuration given by ezMaster. You can override all or part of the config, available properties are :

Expand Down
1 change: 1 addition & 0 deletions config/development-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ module.exports = {
host: 'mongo:27017',
dbName: 'lodex',
},
host: 'http://localhost:3000',
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"react-motion": "0.4.7",
"react-motion-ui-pack": "0.10.2",
"react-redux": "5.0.2",
"react-router": "3.0.2",
"react-router": "3.0.3",
"react-router-redux": "4.0.7",
"react-share": "1.12.1",
"react-tap-event-plugin": "2.0.1",
Expand Down
24 changes: 13 additions & 11 deletions src/api/controller/api/ark.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@ const ARK_URI = new RegExp(/ark:\/(\d{5,})/, 'i');

export default async (ctx, next) => {
const uri = ctx.path;
const matches = ARK_URI.exec(uri);

if (!uri.startsWith('ark:/') && !ctx.query.uri) {
if (!uri.startsWith('ark:/') && !uri.startsWith('uid:/') && !ctx.query.uri) {
await next();
return;
}

const arkMatches = ARK_URI.exec(uri);

if (ctx.query.uri) {
ctx.body = await ctx.publishedDataset.findByUri(ctx.query.uri);
return;
}

if (!matches) {
if (!arkMatches) {
ctx.status = 404;
return;
}

const ark = new InistArk({
naan: matches[1],
});
if (arkMatches) {
const ark = new InistArk({
naan: arkMatches[1],
});

const validation = ark.validate(uri);
const validation = ark.validate(uri);

if (!validation.ark) {
ctx.status = 404;
return;
if (!validation.ark) {
ctx.status = 404;
return;
}
}

ctx.body = await ctx.publishedDataset.findByUri(uri);
Expand Down
10 changes: 9 additions & 1 deletion src/api/controller/front/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ const middleware = process.env.NODE_ENV === 'development' ? dev() : prod();
app.use(async (ctx, next) => {
const uri = ctx.path;

if (uri.startsWith('/ark:/') || uri.startsWith('/uid:/')) {
if (!uri.startsWith('/api')
&& uri !== '/admin'
&& uri.indexOf('.js') === -1
&& uri.indexOf('__webpack_hmr') === -1) {
if (uri.startsWith('/admin')) {
ctx.path = '/admin';
await next();
return;
}
// Override the path so that webpack serves the application correctly
ctx.path = '/';
}
Expand Down
11 changes: 10 additions & 1 deletion src/api/models/publishedDataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ObjectID } from 'mongodb';
import chunk from 'lodash.chunk';
import omit from 'lodash.omit';
import compose from 'lodash.compose';
import config from 'config';
import { getResourceUri } from '../../common/uris';

import { VALIDATED, PROPOSED } from '../../common/propositionStatus';

Expand Down Expand Up @@ -106,7 +108,14 @@ export default (db) => {
)({ removedAt: { $exists: false } });
const cursor = collection.getFindCursor(filters, sortBy, sortDir);

return cursor.stream();
return cursor
.map(resource => (
resource.uri.startsWith('http') ? resource : ({
...resource,
uri: getResourceUri(resource, config.host),
})
))
.stream();
};

collection.findById = async (id) => {
Expand Down
4 changes: 2 additions & 2 deletions src/app/custom/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Lodex</a>
<a class="navbar-brand" href="/">Lodex</a>
</div>
<ul class="nav navbar-nav">
<li><a class="btn-admin" href="/admin">Administration</a></li>
<li><a class="btn-sign-in" href="#/login">Connection</a></li>
<li><a class="btn-sign-in" href="/login">Connection</a></li>
</ul>
</div>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion src/app/e2e/addCharacteristic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('add characteristic', function homePublishedDataTests() {
before(async () => {
await clear();
await loadFixtures(fixtures);
await loginAsJulia('/', '/');
await loginAsJulia('/');
});

it('should display the dataset characteristics', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/e2e/admin/composedOf.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
],
"dataset": [
{
"id": 5,
"id": "1",
"firstname": "PEREGRIN",
"name": "TOOK",
"email": "peregrin.took@shire.net",
Expand Down
4 changes: 2 additions & 2 deletions src/app/e2e/admin/composedOf.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import driver from '../../../common/tests/chromeDriver';
import { clear, loadFixtures } from '../../../common/tests/fixtures';
import fixtures from './composedOf.json';
import { inputElementIsFocusable } from '../../../common/tests/conditions';
import loginAsJulia from '../loginAsJulia';
import loginAsJulia from './loginAsJulia';
import waitForPreviewComputing from './waitForPreviewComputing';

describe('Admin', () => {
Expand All @@ -27,7 +27,7 @@ describe('Admin', () => {
await driver.get('http://localhost:3100/admin');
await driver.executeScript('return localStorage.clear();');
await driver.executeScript('return sessionStorage.clear();');
await loginAsJulia('/admin', '/');
await loginAsJulia('/admin');
});
let fieldForm;

Expand Down
10 changes: 5 additions & 5 deletions src/app/e2e/admin/contributedResources.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
],
"publishedDataset": [
{
"uri" : "1",
"uri" : "uid:/1",
"versions": [{
"fullname" : "PEREGRIN.TOOK",
"email" : "peregrin.took@shire.net"
Expand Down Expand Up @@ -99,7 +99,7 @@
}
},
{
"uri" : "2",
"uri" : "uid:/2",
"versions": [{
"fullname" : "SAMSAGET.GAMGIE",
"email" : "samsaget.gamgie@shire.net"
Expand All @@ -121,7 +121,7 @@
}
},
{
"uri" : "3",
"uri" : "uid:/3",
"versions": [{
"fullname" : "BILBON.BAGGINS",
"email" : "bilbon.saquet@shire.net"
Expand All @@ -143,14 +143,14 @@
}
},
{
"uri" : "4",
"uri" : "uid:/4",
"versions": [{
"fullname" : "FRODO.BAGGINS",
"email" : "frodo.saquet@shire.net"
}]
},
{
"uri" : "5",
"uri" : "uid:/5",
"versions": [{
"fullname" : "MERIADOC.BRANDYBUCK",
"email" : "meriadoc.brandybuck@shire.net"
Expand Down
5 changes: 3 additions & 2 deletions src/app/e2e/admin/contributedResources.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { elementIsClicked } from 'selenium-smart-wait';
import driver from '../../../common/tests/chromeDriver';
import { clear, loadFixtures } from '../../../common/tests/fixtures';
import fixtures from './contributedResources.json';
import loginAsJulia from '../loginAsJulia';
import loginAsJulia from './loginAsJulia';

describe('Admin', () => {
describe('Contributed Resource management', function homePublishedDataTests() {
Expand All @@ -15,7 +15,7 @@ describe('Admin', () => {
before(async () => {
await clear(); // Had to ensure clear state for unknown reason
await loadFixtures(fixtures);
await loginAsJulia('/admin', '/');
await loginAsJulia('/admin');
});

it('should display the proposed contributed resources', async () => {
Expand Down Expand Up @@ -88,6 +88,7 @@ describe('Admin', () => {

it('should go to resource page when clicking on review', async () => {
const reviewButton = '.btn-review-resource';
await driver.sleep(10000);
await driver.wait(elementIsClicked(reviewButton), DEFAULT_WAIT_TIMEOUT);
await driver.wait(until.elementLocated(By.css('.resource')));
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/e2e/admin/import.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { elementIsClicked, elementsCountIs, elementTextIs, elementTextMatches }

import driver from '../../../common/tests/chromeDriver';
import { clear } from '../../../common/tests/fixtures';
import loginAsJulia from '../loginAsJulia';
import loginAsJulia from './loginAsJulia';

describe('Admin', () => {
describe('Import model', function homeTests() {
Expand All @@ -19,7 +19,7 @@ describe('Admin', () => {
await driver.executeScript('return localStorage.clear();');
await driver.executeScript('return sessionStorage.clear();');

await loginAsJulia('/admin', '/');
await loginAsJulia('/admin');
});

describe('Uploading dataset', () => {
Expand Down
22 changes: 22 additions & 0 deletions src/app/e2e/admin/loginAsJulia.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { until, By } from 'selenium-webdriver';

import driver from '../../../common/tests/chromeDriver';
import { inputElementIsFocusable } from '../../../common/tests/conditions';

const DEFAULT_WAIT_TIMEOUT = 9000; // A bit less than mocha's timeout to get explicit errors from selenium

export default async (nextpathname) => {
await driver.get(`http://localhost:3100${nextpathname}`);

await driver.wait(until.elementLocated(By.css('form')));
const form = await driver.findElement(By.css('form'));
const username = await driver.findElement(By.css('input[name=username]'));
const password = await driver.findElement(By.css('input[name=password]'));
await driver.wait(inputElementIsFocusable(username, true), DEFAULT_WAIT_TIMEOUT);
await driver.wait(inputElementIsFocusable(password, true), DEFAULT_WAIT_TIMEOUT);

await username.sendKeys('user');
await password.sendKeys('secret');
await form.submit();
await driver.wait(until.urlMatches(new RegExp(nextpathname)));
};
4 changes: 2 additions & 2 deletions src/app/e2e/admin/publication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import driver from '../../../common/tests/chromeDriver';
import { clear } from '../../../common/tests/fixtures';
import { inputElementIsFocusable } from '../../../common/tests/conditions';
import loginAsJulia from '../loginAsJulia';
import loginAsJulia from './loginAsJulia';
import waitForPreviewComputing from './waitForPreviewComputing';

describe('Admin', () => {
Expand All @@ -27,7 +27,7 @@ describe('Admin', () => {
await driver.get('http://localhost:3100/admin');
await driver.executeScript('return localStorage.clear();');
await driver.executeScript('return sessionStorage.clear();');
await loginAsJulia('/admin', '/');
await loginAsJulia('/admin');
});

describe('Uploading', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/app/e2e/admin/removedResources.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { elementIsClicked } from 'selenium-smart-wait';
import driver from '../../../common/tests/chromeDriver';
import { clear, loadFixtures } from '../../../common/tests/fixtures';
import fixtures from './removedResources.json';
import loginAsJulia from '../loginAsJulia';
import loginAsJulia from './loginAsJulia';

describe('Admin', () => {
describe('Removed Resource management', function homePublishedDataTests() {
Expand All @@ -20,7 +20,7 @@ describe('Admin', () => {
await driver.executeScript('return localStorage.clear();');
await driver.executeScript('return sessionStorage.clear();');

await loginAsJulia('/admin', '/');
await loginAsJulia('/admin');
});

it('should activate removed-resources tab', async () => {
Expand Down
18 changes: 9 additions & 9 deletions src/app/e2e/home_published.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,47 +177,47 @@
],
"publishedDataset": [
{
"uri" : "1",
"uri" : "uid:/1",
"versions": [{
"firstname" : "PEREGRIN",
"name" : "TOOK",
"email" : "peregrin.took@shire.net",
"best_friend_of": "5"
"best_friend_of": "uid:/5"
}]
},
{
"uri" : "2",
"uri" : "uid:/2",
"versions": [{
"firstname" : "SAMSAGET",
"name" : "GAMGIE",
"email" : "samsaget.gamgie@shire.net",
"best_friend_of": "4"
"best_friend_of": "uid:/4"
}]
},
{
"uri" : "3",
"uri" : "uid:/3",
"versions": [{
"firstname" : "BILBON",
"name" : "BAGGINS",
"email" : "bilbon.saquet@shire.net"
}]
},
{
"uri" : "4",
"uri" : "uid:/4",
"versions": [{
"firstname": "FRODO",
"name" : "BAGGINS",
"email" : "frodo.saquet@shire.net",
"best_friend_of": "2"
"best_friend_of": "uid:/2"
}]
},
{
"uri" : "5",
"uri" : "uid:/5",
"versions": [{
"firstname" : "MERIADOC",
"name" : "BRANDYBUCK",
"email" : "meriadoc.brandybuck@shire.net",
"best_friend_of": "1"
"best_friend_of": "uid:/1"
}]
}
],
Expand Down
Loading

0 comments on commit a859cf1

Please sign in to comment.