-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pauline Didier
committed
Dec 5, 2024
1 parent
97ff55c
commit 7a29f1c
Showing
17 changed files
with
543 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; | ||
import { createTestApp } from "../../../../test-util/app.js"; | ||
import { sampleUserToken } from "../../../../test-util/sampleTokens.js"; | ||
import { testResourceProvider } from "../../../../test-util/testResourceHandler.js"; | ||
import request from "supertest"; | ||
import { createSampleModel } from "../../../../test-util/model.js"; | ||
describe("getResources", () => { | ||
let app: any; | ||
let token: string; | ||
let dal: DataAccessLayer; | ||
let validModelId: string; | ||
beforeAll(async () => { | ||
({ app, dal } = await createTestApp()); | ||
token = await sampleUserToken(); | ||
validModelId = await createSampleModel(dal); | ||
//dal.resourceHandler.register(testResourceProvider); | ||
}); | ||
|
||
it("should return 401 on un-authenticated request", async () => { | ||
const res = await request(app).get("/api/v1/resources/12323"); | ||
expect(res.status).toBe(401); | ||
}); | ||
it("should return 401 when using invalid user token", async () => { | ||
const res = await request(app) | ||
.get("/api/v1/resources/12323") | ||
.set("Authorization", "invalidtoken"); | ||
expect(res.status).toBe(401); | ||
}); | ||
|
||
it("should return 200 on successful get resources", async () => { | ||
const res = await request(app) | ||
.get("/api/v1/resources/" + validModelId) | ||
.set("Authorization", token); | ||
expect(res.status).toBe(200); | ||
}); | ||
|
||
it("should return an empty list if no resource provider is registered ", async () => { | ||
const res = await request(app) | ||
.get("/api/v1/resources/" + validModelId) | ||
.set("Authorization", token); | ||
expect(Array.isArray(res.body)).toBeTruthy(); | ||
expect(res.body.length).toEqual(0); | ||
}); | ||
|
||
it("should return the resources from the registered resource providers", async () => { | ||
dal.resourceHandler.register(testResourceProvider); | ||
const res = await request(app) | ||
.get("/api/v1/resources/" + validModelId) | ||
.set("Authorization", token); | ||
const result = res.body[0]; | ||
expect(result.id).toEqual("test-resource-id"); | ||
expect(result.displayName).toEqual("Test Resource"); | ||
expect(result.type).toEqual("external entity"); | ||
expect(result.systemId).toEqual("another-mocked-system-id"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Request, Response } from "express"; | ||
import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; | ||
|
||
export function getResources(dal: DataAccessLayer) { | ||
return async (req: Request, res: Response) => { | ||
const modelId = req.params.id; | ||
const model = await dal.modelService.getById(modelId); | ||
if (model && model.systemId) { | ||
const resources = await dal.resourceHandler.getResources(model.systemId); | ||
return res.json(resources); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; | ||
import express from "express"; | ||
import { errorWrap } from "../../../../util/errorHandler.js"; | ||
import { getResources } from "./resource.js"; | ||
|
||
export function resourceRouter(dal: DataAccessLayer): express.Router { | ||
const router = express.Router(); | ||
|
||
router.get("/:id", errorWrap(getResources(dal))); | ||
return router; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
Resource, | ||
ResourceProvider, | ||
} from "@gram/core/dist/resources/ResourceHandler.js"; | ||
|
||
class TestResourceProvider implements ResourceProvider { | ||
key = "testProvider"; | ||
async listResources(systemId: string): Promise<Resource[]> { | ||
if (systemId === "mocked-system-id") { | ||
return [ | ||
{ | ||
id: "test-resource-id", | ||
displayName: "Test Resource", | ||
type: "external entity", | ||
systemId: "another-mocked-system-id", | ||
attributes: {}, | ||
}, | ||
]; | ||
} | ||
return []; | ||
} | ||
} | ||
|
||
export const testResourceProvider = new TestResourceProvider(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { api } from "./api"; | ||
|
||
const resourceApi = api.injectEndpoints({ | ||
endpoints: (build) => ({ | ||
getResources: build.query({ | ||
query: (modelId) => `/resources/${modelId}`, | ||
transformResponse: (response) => { | ||
return response; | ||
}, | ||
providesTags: ["Resources"], | ||
}), | ||
}), | ||
}); | ||
|
||
export const { useGetResourcesQuery } = resourceApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
Box, | ||
Typography, | ||
Chip, | ||
TextField, | ||
InputAdornment, | ||
} from "@mui/material"; | ||
import CloseIcon from "@mui/icons-material/Close"; | ||
import { GROUP_BY } from "./ResourceTab"; | ||
function toggleGroupBy(groupBy, setGroupBy, value) { | ||
if (groupBy === value) { | ||
console.log("should reset to null"); | ||
setGroupBy(null); | ||
} else { | ||
setGroupBy(value); | ||
} | ||
} | ||
|
||
export function ResourceFilter({ | ||
groupBy, | ||
setGroupBy, | ||
isLoading, | ||
searchInput, | ||
setSearchInput, | ||
}) { | ||
if (isLoading) { | ||
return null; | ||
} | ||
return ( | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
gap: "1em", | ||
marginY: "1em", | ||
}} | ||
> | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
gap: "1em", | ||
alignItems: "center", | ||
}} | ||
> | ||
<Typography>Group by:</Typography> | ||
<Box sx={{ display: "flex", gap: "0.5em" }}> | ||
{Object.values(GROUP_BY).map((gb) => { | ||
if (groupBy === gb.value) { | ||
return ( | ||
<Chip | ||
key={gb.value} | ||
label={gb.label} | ||
onClick={() => toggleGroupBy(groupBy, setGroupBy, gb.value)} | ||
color="primary" | ||
/> | ||
); | ||
} | ||
return ( | ||
<Chip | ||
key={gb.value} | ||
label={gb.label} | ||
onClick={() => toggleGroupBy(groupBy, setGroupBy, gb.value)} | ||
variant="outlined" | ||
/> | ||
); | ||
})} | ||
</Box> | ||
</Box> | ||
|
||
<TextField | ||
fullWidth | ||
id="standard-basic" | ||
placeholder="Search by id, name or system id" | ||
variant="standard" | ||
value={searchInput} | ||
onChange={(event) => { | ||
setSearchInput(event.target.value); | ||
}} | ||
slotProps={{ | ||
input: { | ||
endAdornment: ( | ||
<InputAdornment position="end" onClick={() => setSearchInput("")}> | ||
<CloseIcon /> | ||
</InputAdornment> | ||
), | ||
}, | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} |
Oops, something went wrong.