-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: headers, env vars and secrets in expressions (#1544)
- Loading branch information
Showing
9 changed files
with
154 additions
and
32 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 |
---|---|---|
@@ -1,10 +1,29 @@ | ||
import { actions, resetDatabase } from "@teamkeel/testing"; | ||
import { actions, resetDatabase, models } from "@teamkeel/testing"; | ||
import { beforeEach, expect, test } from "vitest"; | ||
|
||
beforeEach(resetDatabase); | ||
|
||
test("create person with env var name", async () => { | ||
test("set with env var", async () => { | ||
const person = await actions.createPerson({}); | ||
|
||
expect(person.name).toEqual("Pedro"); | ||
}); | ||
|
||
test("permissions with env var", async () => { | ||
const person = await actions.createPerson({}); | ||
|
||
await expect( | ||
actions.getPedro({ id: person.id }) | ||
).not.toHaveAuthorizationError(); | ||
|
||
await expect(actions.getBob({ id: person.id })).toHaveAuthorizationError(); | ||
}); | ||
|
||
test("where with env var", async () => { | ||
await models.person.create({ name: "Pedro" }); | ||
await models.person.create({ name: "Bob" }); | ||
|
||
const pedros = await actions.listPedros(); | ||
expect(pedros.results.length).toEqual(1); | ||
expect(pedros.results[0].name).toEqual("Pedro"); | ||
}); |
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,22 @@ | ||
model Person { | ||
fields { | ||
name Text | ||
} | ||
|
||
actions { | ||
create createPerson() { | ||
@set(person.name = ctx.headers.PERSON_NAME) | ||
@permission(expression: true) | ||
} | ||
get getBob(id) { | ||
@permission(expression: ctx.headers.PERSON_NAME == "Bob") | ||
} | ||
get getPedro(id) { | ||
@permission(expression: ctx.headers.PERSON_NAME == "Pedro") | ||
} | ||
list listPedros() { | ||
@where(person.name == ctx.headers.PERSON_NAME) | ||
@permission(expression: true) | ||
} | ||
} | ||
} |
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,90 @@ | ||
import { actions, resetDatabase, models } from "@teamkeel/testing"; | ||
import { beforeEach, expect, test } from "vitest"; | ||
|
||
beforeEach(resetDatabase); | ||
|
||
test("set with http header", async () => { | ||
const response = await fetch( | ||
process.env.KEEL_TESTING_ACTIONS_API_URL + "/createPerson", | ||
{ | ||
body: "{}", | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
PERSON_NAME: "Pedro", | ||
}, | ||
} | ||
); | ||
|
||
expect(response.status).toEqual(200); | ||
const data = await response.json(); | ||
expect(data?.name).toEqual("Pedro"); | ||
}); | ||
|
||
test("permissions with http header", async () => { | ||
const response = await fetch( | ||
process.env.KEEL_TESTING_ACTIONS_API_URL + "/createPerson", | ||
{ | ||
body: "{}", | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
PERSON_NAME: "Pedro", | ||
}, | ||
} | ||
); | ||
|
||
expect(response.status).toEqual(200); | ||
const person = await response.json(); | ||
|
||
const getPedro = await fetch( | ||
process.env.KEEL_TESTING_ACTIONS_API_URL + "/getPedro", | ||
{ | ||
body: `{ "id": "${person.id}"}`, | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
PERSON_NAME: "Pedro", | ||
}, | ||
} | ||
); | ||
|
||
expect(getPedro.status).toEqual(200); | ||
|
||
const getBob = await fetch( | ||
process.env.KEEL_TESTING_ACTIONS_API_URL + "/getBob", | ||
{ | ||
body: `{ "id": "${person.id}"}`, | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
PERSON_NAME: "Pedro", | ||
}, | ||
} | ||
); | ||
|
||
expect(getBob.status).toEqual(403); | ||
}); | ||
|
||
test("where with http header", async () => { | ||
await models.person.create({ name: "Pedro" }); | ||
await models.person.create({ name: "Bob" }); | ||
|
||
const listPedros = await fetch( | ||
process.env.KEEL_TESTING_ACTIONS_API_URL + "/listPedros", | ||
{ | ||
body: "{}", | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
PERSON_NAME: "Pedro", | ||
}, | ||
} | ||
); | ||
|
||
expect(listPedros.status).toEqual(200); | ||
|
||
const pedros = await listPedros.json(); | ||
expect(pedros.results.length).toEqual(1); | ||
expect(pedros.results[0].name).toEqual("Pedro"); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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