Commit 7cd63dad authored by Alejandro De Maria Antolinos's avatar Alejandro De Maria Antolinos

Merge branch 'issue75' into 'master'

Issue75

Closes #75

See merge request !49
parents ef4acaa5 f3dc0cfc
var IDS =
{
//server: "https://ovm-icat-test.esrf.fr:8181",
server: "https://ids.esrf.fr/ids",
server: "https://ovm-icat-test.esrf.fr:8181",
//server: "https://ids.esrf.fr/ids",
connection : {
plugins :['esrf']
}
......
......@@ -5,6 +5,7 @@ import PropTypes from 'prop-types'
import EventList from '../components/Event/EventList'
import { getEventsByInvestigationId } from '../actions/Event/index.js'
import { getFileByEventIdHTTPRequest } from '../api/icat/icatPlus';
class EventContainer extends React.Component {
......@@ -18,6 +19,7 @@ class EventContainer extends React.Component {
this.refreshList = this.refreshList.bind(this);
this.onEventsReceptionSuccess = this.onEventsReceptionSuccess.bind(this);
this.onEventsReceptionFailure = this.onEventsReceptionFailure.bind(this);
this.replaceImgLink = this.replaceImgLink.bind(this);
}
/**
......@@ -39,6 +41,12 @@ class EventContainer extends React.Component {
}
onEventsReceptionSuccess(data) {
// update the image src property */
data.forEach(event => {
if (event.content) {
event.content = this.replaceImgLink(event.content)
}
});
this.setState({
eventList: data,
progress: "fetched"
......@@ -85,6 +93,27 @@ class EventContainer extends React.Component {
}
}
/**
* Replace the img src link so to use the actual icat+ api settings to access the image
* @param {*} content the event content to modify as provided by ICAT+
*/
replaceImgLink(content) {
let { investigationId, user } = this.props;
let flatContent = JSON.stringify(content);
let updatedFlatContent = flatContent;
//extract EventId which is inside the link
let eventIdRegExp = new RegExp(/src=\S+\/events\/(\S+)\/file\S+/, 'g');
let result = [];
while ((result = eventIdRegExp.exec(flatContent)) !== null) {
// an image html element which src points to icat+ was found
let eventId = result[1];
let url = getFileByEventIdHTTPRequest(investigationId, eventId, user.sessionId);
updatedFlatContent = updatedFlatContent.replace(result[0], 'src=\\"' + url + '\\"');
}
return JSON.parse(updatedFlatContent);
}
}
function mapStateToProps(state) {
......
import React from 'react'
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import EventContainer from '../src/containers/EventContainer';
import { createStore, applyMiddleware } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import { Provider } from 'react-redux'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import reducer from '../src/reducers'
import promise from "redux-promise-middleware"
import { LOGGED_IN } from '../src/constants/ActionTypes';
import ICATPLUS from "../src/config/icat/icatPlus.js";
beforeEach(() => {
Enzyme.configure({ adapter: new Adapter() });
})
describe("Tests on eventContainer", () => {
function getWrapper() {
const middleware = [thunk];
const persistConfig = {
key: 'root',
storage,
}
const persistedReducer = persistReducer(persistConfig, reducer)
const store = createStore(
persistedReducer,
{
user: {
type: LOGGED_IN,
username: 'username',
sessionId: 'testSessionId'
}
},
applyMiddleware(...middleware, logger, promise(), thunk)
)
return Enzyme.shallow(<EventContainer
investigationId="testInvestigationId"
store={store}
/>)
}
describe("replaceImgLink", () => {
ICATPLUS.server = "http://testServer:8000";
it("replace img src value using the local API setup.", () => {
let actualContent = [
{
"_id": "5bcee3e30908a72afcb62a19",
"format": "plainText",
"text": "test with an image\n\n\n"
},
{
"_id": "5bcee3e30908a72afcb62a18",
"format": "html",
"text": "<p>test with an image</p>\n<p><img src=\"http://linfalcon.esrf.fr:8000/investigations/77570462/events/5bcee3dc0908a72afcb62a16/file?sessionId=ce964cbd-96d4-41b6-a249-c645f523eb4a\" alt=\"\" width=\"500\" height=\"411\" /></p>"
}
];
let expectedContent = [
{
"_id": "5bcee3e30908a72afcb62a19",
"format": "plainText",
"text": "test with an image\n\n\n"
},
{
"_id": "5bcee3e30908a72afcb62a18",
"format": "html",
"text": "<p>test with an image</p>\n<p><img src=\"http://testServer:8000/investigations/testInvestigationId/events/5bcee3dc0908a72afcb62a16/file?sessionId=testSessionId\" alt=\"\" width=\"500\" height=\"411\" /></p>"
}
]
expect(getWrapper().dive().instance().replaceImgLink(actualContent)).toEqual(expectedContent);
})
it("replace img src value by the local API setup on an event with several images (not links).", () => {
let actualContent = [
{
"_id": "5bcee3e30908a72afcb62a19",
"format": "plainText",
"text": "test with an image\n\n\n"
},
{
"_id": "5bcee3e30908a72afcb62a18",
"format": "html",
"text": "<p>there are 2 images in this comment</p>\n<p><img src=\"http://linfalcon.esrf.fr:8000/investigations/77570462/events/5bcee3dc0908a72afcb62a16/file?sessionId=ce964cbd-96d4-41b6-a249-c645f523eb4a\" alt=\"\" width=\"500\" height=\"411\" /></p>\n<p><img src=\"http://linfalcon.esrf.fr:8000/investigations/77570462/events/5bcee3dc0908a72afcb62a20/file?sessionId=ce964cbd-96d4-41b6-a249-c645f523eb4a\" alt=\"\" width=\"500\" height=\"411\" /></p>"
}
];
let expectedContent = [
{
"_id": "5bcee3e30908a72afcb62a19",
"format": "plainText",
"text": "test with an image\n\n\n"
},
{
"_id": "5bcee3e30908a72afcb62a18",
"format": "html",
"text": "<p>there are 2 images in this comment</p>\n<p><img src=\"http://testServer:8000/investigations/testInvestigationId/events/5bcee3dc0908a72afcb62a16/file?sessionId=testSessionId\" alt=\"\" width=\"500\" height=\"411\" /></p>\n<p><img src=\"http://testServer:8000/investigations/testInvestigationId/events/5bcee3dc0908a72afcb62a20/file?sessionId=testSessionId\" alt=\"\" width=\"500\" height=\"411\" /></p>"
}
]
expect(getWrapper().dive().instance().replaceImgLink(actualContent)).toEqual(expectedContent);
})
it("does not replace img src value when event id is not found.", () => {
let actualContent = [
{
"_id": "5bcee3e30908a72afcb62a19",
"format": "plainText",
"text": "test with an image\n\n\n"
},
{
"_id": "5bcee3e30908a72afcb62a18",
"format": "html",
"text": "<p>test with an image</p>\n<p><img src=\"http://linfalcon.esrf.fr:8000/investigations/77570462/events//file?sessionId=ce964cbd-96d4-41b6-a249-c645f523eb4a\" alt=\"\" width=\"500\" height=\"411\" /></p>"
}
];
let expectedContent = [
{
"_id": "5bcee3e30908a72afcb62a19",
"format": "plainText",
"text": "test with an image\n\n\n"
},
{
"_id": "5bcee3e30908a72afcb62a18",
"format": "html",
"text": "<p>test with an image</p>\n<p><img src=\"http://linfalcon.esrf.fr:8000/investigations/77570462/events//file?sessionId=ce964cbd-96d4-41b6-a249-c645f523eb4a\" alt=\"\" width=\"500\" height=\"411\" /></p>"
}
]
expect(getWrapper().dive().instance().replaceImgLink(actualContent)).toEqual(expectedContent);
})
it("does not replace the src field of an image that comes from another website", () => {
let actualContent = [
{
"_id": "5bcee3e30908a72afcb62a19",
"format": "plainText",
"text": "test with an image\n\n\n"
},
{
"_id": "5bcee3e30908a72afcb62a18",
"format": "html",
"text": "<p>test with an image from wikipedia </p>\n<p><img src=\"https://en.wikipedia.org/static/images/project-logos/enwiki.png\" alt=\"\" width=\"500\" height=\"411\" /></p>"
}
];
let expectedContent = actualContent
expect(getWrapper().dive().instance().replaceImgLink(actualContent)).toEqual(expectedContent);
})
})
})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment