Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
emranemran committed Mar 23, 2024
1 parent 3dbccb4 commit 220bcc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
31 changes: 19 additions & 12 deletions packages/api/src/controllers/stream.test.ts
Expand Up @@ -1076,6 +1076,7 @@ describe("controllers/stream", () => {
describe("stream endpoint with api key", () => {
let newApiKey;
beforeEach(async () => {
// create streams without a projectId
for (let i = 0; i < 5; i += 1) {
const document = {
id: uuid(),
Expand All @@ -1098,17 +1099,18 @@ describe("controllers/stream", () => {
newApiKey = await createApiToken({
client: client,
projectId: projectId,
corsAccess: { allowedOrigins },
//corsAccess: { allowedOrigins },
jwtAuthToken: nonAdminToken,
});
expect(newApiKey).toMatchObject({
id: expect.any(String),
access: { cors: { allowedOrigins: ["http://localhost:3000"] } },
//access: { cors: { allowedOrigins: ["http://localhost:3000"] } },
});

client.jwtAuth = "";
client.apiKey = newApiKey.id;

// create streams with a projectId
for (let i = 0; i < 5; i += 1) {
const document = {
id: uuid(),
Expand All @@ -1120,26 +1122,31 @@ describe("controllers/stream", () => {
const res = await client.get(`/stream/${document.id}`);
expect(res.status).toBe(200);
}

client.jwtAuth = "";
});

it.only("should get own streams", async () => {
it("should get own streams", async () => {
client.apiKey = nonAdminApiKey;
let res = await client.get(`/stream/user/${nonAdminUser.id}`);
expect(res.status).toBe(200);
const streams = await res.json();
console.log("XXX: got nonAdminUser streams: ", streams);
expect(streams.length).toEqual(8);
expect(streams[0].userId).toEqual(nonAdminUser.id);

// ensure project associated with a new api-key is not enforced for this endpoint
client.apiKey = newApiKey.id;
console.log("XXX: about to call", client.apiKey, newApiKey.id);
let res2 = await client.get(`/stream/user/${nonAdminUser.id}`);
expect(res2.status).toBe(200);
streams = await res2.json();
console.log("XXX: got newApiKey streams: ", streams);
expect(streams.length).toEqual(3);
const streams2 = await res2.json();
expect(streams2.length).toEqual(8);
expect(streams2[0].userId).toEqual(nonAdminUser.id);
});

it("should get streams owned by project when using api-key for project", async () => {
client.apiKey = newApiKey.id;
let res = await client.get(`/stream/`);
expect(res.status).toBe(200);
const streams = await res.json();
expect(streams.length).toEqual(5);
expect(streams[0].userId).toEqual(nonAdminUser.id);
});

Expand All @@ -1148,7 +1155,7 @@ describe("controllers/stream", () => {
let res = await client.get(`/stream/user/${nonAdminUser.id}`);
expect(res.status).toBe(200);
const streams = await res.json();
expect(streams.length).toEqual(3);
expect(streams.length).toEqual(8);
expect(streams[0].userId).toEqual(nonAdminUser.id);
let dres = await client.delete(`/stream/${streams[0].id}`);
expect(dres.status).toBe(204);
Expand All @@ -1157,7 +1164,7 @@ describe("controllers/stream", () => {
let res2 = await client.get(`/stream/user/${nonAdminUser.id}`);
expect(res2.status).toBe(200);
const streams2 = await res2.json();
expect(streams2.length).toEqual(2);
expect(streams2.length).toEqual(7);
});

it("should not get others streams", async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/controllers/stream.ts
Expand Up @@ -662,7 +662,6 @@ app.get("/sessions/:parentId", authorizer({}), async (req, res) => {
});

app.get("/user/:userId", authorizer({}), async (req, res) => {
console.log("YYY: ", req.user.id, req.params.userId);
const { userId } = req.params;
let { limit, cursor, streamsonly, sessionsonly } = toStringValues(req.query);

Expand All @@ -672,7 +671,6 @@ app.get("/user/:userId", authorizer({}), async (req, res) => {
errors: ["user can only request information on their own streams"],
});
}
console.log("YYY im here");
const query = [
sql`data->>'deleted' IS NULL`,
sql`data->>'userId' = ${userId}`,
Expand Down Expand Up @@ -711,6 +709,7 @@ app.get("/:id", authorizer({}), async (req, res) => {
const raw = req.query.raw && req.user.admin;
const { forceUrl } = req.query;
let stream = await db.stream.get(req.params.id);
console.log("XXX: stream is", stream);
if (
!stream ||
((stream.userId !== req.user.id ||
Expand Down

0 comments on commit 220bcc4

Please sign in to comment.