-
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.
feat: add StaticTeamProvider to default config with some sample teams
- Loading branch information
Showing
12 changed files
with
167 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import request from "supertest"; | ||
import { createTestApp } from "../../../../test-util/app.js"; | ||
import { sampleTeam } from "../../../../test-util/sampleTeam.js"; | ||
import { sampleUserToken } from "../../../../test-util/sampleTokens.js"; | ||
|
||
const token = await sampleUserToken(); | ||
|
||
describe("team.get", () => { | ||
let app: any; | ||
|
||
beforeAll(async () => { | ||
({ app } = await createTestApp()); | ||
}); | ||
|
||
it("should return 401 on un-authenticated request", async () => { | ||
const res = await request(app).get("/api/v1/teams/123"); | ||
expect(res.status).toBe(401); | ||
}); | ||
|
||
it("should return 404 on invalid team id", async () => { | ||
const res = await request(app) | ||
.get("/api/v1/teams/234") | ||
.set("Authorization", token); | ||
|
||
expect(res.status).toBe(404); | ||
}); | ||
|
||
it("should return 200 with dummy data", async () => { | ||
const res = await request(app) | ||
.get("/api/v1/teams/" + sampleTeam.id) | ||
.set("Authorization", token); | ||
|
||
expect(res.status).toBe(200); | ||
expect(res.body.team).toEqual(sampleTeam); | ||
}); | ||
}); |
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,16 @@ | ||
import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; | ||
import { Request, Response } from "express"; | ||
|
||
export function getTeam(dal: DataAccessLayer) { | ||
return async (req: Request, res: Response) => { | ||
const id = req.params.id; | ||
|
||
const team = await dal.teamHandler.getTeam({ currentRequest: req }, id); | ||
|
||
if (team === null) { | ||
return res.sendStatus(404); | ||
} | ||
|
||
return res.json({ team }); | ||
}; | ||
} |
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,32 @@ | ||
import { TeamProvider } from "@gram/core/dist/auth/TeamProvider.js"; | ||
import { Team } from "@gram/core/dist/auth/models/Team.js"; | ||
import { RequestContext } from "@gram/core/dist/data/providers/RequestContext.js"; | ||
import { | ||
sampleAdmin, | ||
sampleOtherUser, | ||
sampleReviewer, | ||
sampleUser, | ||
} from "./sampleUser.js"; | ||
import { sampleTeam, sampleOtherTeam, teams } from "./sampleTeam.js"; | ||
|
||
export class TestTeamProvider implements TeamProvider { | ||
private teamMap: Map<string, string[]>; | ||
|
||
constructor() { | ||
this.teamMap = new Map([ | ||
[sampleUser.sub, [sampleTeam.id]], | ||
[sampleOtherUser.sub, [sampleOtherTeam.id]], | ||
[sampleReviewer.sub, [sampleOtherTeam.id]], | ||
[sampleAdmin.sub, [sampleTeam.id, sampleOtherTeam.id]], | ||
]); | ||
} | ||
async lookup(ctx: RequestContext, teamIds: string[]): Promise<Team[]> { | ||
return teamIds | ||
.map((tid) => teams.find((team) => team.id === tid)) | ||
.filter((t) => t) as Team[]; | ||
} | ||
async getTeamsForUser(ctx: RequestContext, userId: string): Promise<Team[]> { | ||
return this.lookup(ctx, this.teamMap.get(userId) || []); | ||
} | ||
key: string = "test"; | ||
} |
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ export const api = createApi({ | |
"Suggestions", | ||
"System", | ||
"Templates", | ||
"Team", | ||
"Threats", | ||
"User", | ||
], | ||
|
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,14 @@ | ||
import { api } from "./api"; | ||
|
||
const teamApi = api.injectEndpoints({ | ||
endpoints: (build) => ({ | ||
getTeam: build.query({ | ||
query: ({ teamId }) => ({ | ||
url: `/teams/${teamId}`, | ||
}), | ||
transformResponse: (response, meta, arg) => response.team, | ||
}), | ||
}), | ||
}); | ||
|
||
export const { useGetTeamQuery } = teamApi; |
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
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 { TeamProvider } from "@gram/core/dist/auth/TeamProvider.js"; | ||
import { Team } from "@gram/core/dist/auth/models/Team.js"; | ||
|
||
import { RequestContext } from "@gram/core/dist/data/providers/RequestContext.js"; | ||
|
||
export class StaticTeamProvider implements TeamProvider { | ||
constructor( | ||
public teams: Team[], | ||
public UserIdToTeamIds: Map<string, string[]> | ||
) {} | ||
|
||
async lookup(ctx: RequestContext, teamIds: string[]): Promise<Team[]> { | ||
return teamIds | ||
.map((tid) => this.teams.find((team) => team.id === tid)) | ||
.filter((t) => t) as Team[]; | ||
} | ||
|
||
async getTeamsForUser(ctx: RequestContext, userId: string): Promise<Team[]> { | ||
const teamIds = this.UserIdToTeamIds.get(userId) || []; | ||
return this.lookup(ctx, teamIds); | ||
} | ||
|
||
key: string = "static"; | ||
} |