Skip to content

Commit e14d249

Browse files
authored
Merge pull request #75 from SolidLabResearch/verify-source
Add source verification
2 parents bba51a5 + 2ea34b8 commit e14d249

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Source verification (#73).
13+
1214
### Changed
1315

1416
### Fixed

cypress/e2e/verify-source.cy.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe("Verify source", () => {
2+
it("Verification button exists and returns correct result", () => {
3+
cy.visit("/");
4+
5+
cy.contains("My favourite musicians").click();
6+
7+
cy.get('[aria-label="Sources info"]').click();
8+
9+
cy.get('[aria-label="Verify source"]').each(($button) => {
10+
cy.wrap($button).click();
11+
});
12+
13+
cy.get('[aria-label="Verification succeeded"]').should("exist");
14+
cy.get('[aria-label="Verification failed"]').should("exist")
15+
});
16+
});

src/components/ActionBar/ActionBar.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import InfoIcon from "@mui/icons-material/Info";
1919
import SourceAuthenticationIcon from "./SourceAuthenticationIcon/SourceAuthenticationIcon";
2020
import SourceFetchStatusIcon from "./SourceFetchStatusIcon/SourceFetchStatusIcon";
21+
import SourceVerificationIcon from "./SourceVerificationIcon/SourceVerificationIcon.jsx";
2122

2223
/**
2324
*
@@ -95,6 +96,7 @@ function ActionBar() {
9596
<TableCell>Source</TableCell>
9697
<TableCell>Authentication needed</TableCell>
9798
<TableCell>Fetch status</TableCell>
99+
<TableCell>Verified</TableCell>
98100
</TableRow>
99101
</TableHead>
100102
<TableBody>
@@ -107,6 +109,9 @@ function ActionBar() {
107109
<TableCell>
108110
<SourceFetchStatusIcon proxyUrl={config.httpProxy} context={context} source={source} />
109111
</TableCell>
112+
<TableCell>
113+
<SourceVerificationIcon proxyUrl={config.httpProxy} context={context} source={source} />
114+
</TableCell>
110115
</TableRow>
111116
))}
112117
</TableBody>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import CheckIcon from "@mui/icons-material/Check";
2+
import {CircularProgress, Tooltip} from "@mui/material";
3+
import {Component, useState} from "react";
4+
import QuestionMarkIcon from "@mui/icons-material/QuestionMark";
5+
import {Button} from "react-admin";
6+
import CancelIcon from "@mui/icons-material/Cancel";
7+
import PropTypes from "prop-types";
8+
9+
/**
10+
* @param {object} props - the props passed to the component
11+
* @param {object} props.context - the query context
12+
* @param {string} props.source - the source to check
13+
* @param {string} props.proxyUrl - the proxy url to use if the resource is accessed through a proxy
14+
* @returns {Component} an icon indicating whether the source was verified or not
15+
*/
16+
function SourceVerificationIcon({context, source, proxyUrl}) {
17+
let sourceUrl = source;
18+
if (context.useProxy) {
19+
sourceUrl = `${proxyUrl}${source}`;
20+
}
21+
22+
const [isLoading, setIsLoading] = useState(true);
23+
const [isVerified, setIsVerified] = useState(false);
24+
const [needsVerification, setNeedsVerification] = useState(false);
25+
26+
// This function should be replaced by the actual verification function
27+
const verifyFunction = async (source, fetchFunction) => {
28+
try {
29+
const response = await fetchFunction(source);
30+
return response.ok;
31+
} catch (error) {
32+
return false;
33+
}
34+
};
35+
36+
/**
37+
* Handle the request for source verification
38+
*/
39+
function verify() {
40+
setNeedsVerification(true);
41+
verifyFunction(sourceUrl, context.underlyingFetchFunction).then((result) => {
42+
setIsVerified(result);
43+
setIsLoading(false);
44+
})
45+
}
46+
47+
if (needsVerification) {
48+
if (isLoading) {
49+
return <CircularProgress size={20}/>;
50+
} else {
51+
if (isVerified) {
52+
return (
53+
<Tooltip title="Verification succeeded">
54+
<CheckIcon size="small"/>
55+
</Tooltip>
56+
);
57+
} else {
58+
return (
59+
<Tooltip title="Verification failed">
60+
<CancelIcon size="small"/>
61+
</Tooltip>
62+
);
63+
}
64+
}
65+
} else {
66+
return (
67+
<Tooltip title="Verify source">
68+
<Button onClick={verify}>
69+
<QuestionMarkIcon size="small"/>
70+
</Button>
71+
</Tooltip>
72+
);
73+
}
74+
}
75+
76+
SourceVerificationIcon.propTypes = {
77+
context: PropTypes.object.isRequired,
78+
source: PropTypes.string.isRequired,
79+
proxyUrl: PropTypes.string.isRequired,
80+
}
81+
82+
export default SourceVerificationIcon;

src/dataProvider/SparqlDataProvider.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,13 @@ function generateContext(context) {
213213
}
214214
}
215215

216-
let fetchFunction = fetch;
216+
let underlyingFetchFunction = fetch;
217217
if (getDefaultSession().info.isLoggedIn) {
218-
fetchFunction = authFetch;
218+
underlyingFetchFunction = authFetch;
219219
}
220220

221-
context.fetch = statusFetch(fetchFunction, context);
221+
context.underlyingFetchFunction = underlyingFetchFunction;
222+
context.fetch = statusFetch(underlyingFetchFunction, context);
222223

223224
if (context.useProxy) {
224225
context.httpProxyHandler = proxyHandler;
@@ -234,7 +235,7 @@ function generateContext(context) {
234235
* @returns {Function} a function that wraps the fetch function and sets the fetchSuccess flag in the context.
235236
*/
236237
function statusFetch(customFetch, context) {
237-
const fetchFunction = async (arg) => {
238+
const wrappedFetchFunction = async (arg) => {
238239
try{
239240
const response = await customFetch(arg);
240241
context.fetchSuccess[arg] = true;
@@ -245,7 +246,7 @@ function statusFetch(customFetch, context) {
245246
throw error;
246247
}
247248
}
248-
return fetchFunction;
249+
return wrappedFetchFunction;
249250
}
250251

251252
/**

0 commit comments

Comments
 (0)