Skip to content

fix: wrong query guard generated when context user doesn't have an id #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export const getServerSideProps: GetServerSideProps = async () => {

### [Database hosting considerations](/docs/ref/database-hosting-considerations.md)

### [Setup logging](/docs/ref/setup-logging.md)
### [Setting up logging](/docs/ref/setup-logging.md)

## Reach out to us for issues, feedback and ideas!

Expand Down
31 changes: 24 additions & 7 deletions docs/ref/setup-logging.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Setup Logging
# Setting Up Logging

ZenStack uses the following levels to control server-side logging:

1. error
- error

Error level logging

1. warn
- warn

Warning level logging

1. info
- info

Info level logging

1. verbose
- verbose

Verbose level logging

1. query
- query

Detailed database query logging

Expand All @@ -28,7 +28,24 @@ You can turn log levels on and off in `zenstack.config.json`:

```json
{
"log": ["verbose", "info", "warn"]
"log": ["verbose", "info"]
}
```

The settings shown above is an shorthand for:

```json
{
"log": [
{
"level": "verbose",
"emit": "stdout"
},
{
"level": "info",
"emit": "stdout"
}
]
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "0.2.9",
"version": "0.2.10",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/internal",
"version": "0.2.9",
"version": "0.2.10",
"displayName": "ZenStack Internal Library",
"description": "ZenStack internal runtime library. This package is for supporting runtime functionality of ZenStack and not supposed to be used directly.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "0.2.9",
"version": "0.2.10",
"description": "This package contains runtime library for consuming client and server side code generated by ZenStack.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "ZenStack is a toolkit that simplifies full-stack development",
"version": "0.2.9",
"version": "0.2.10",
"author": {
"name": "ZenStack Team"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ export default class QueryGuardGenerator {
.addBody();

func.addStatements(
`const user = context.user ?? { id: '${UNKNOWN_USER_ID}' };`
// make suer user id is always available
`const user = context.user?.id ? context.user : { ...context.user, id: '${UNKNOWN_USER_ID}' };`
);

// r = <guard object>;
Expand Down
50 changes: 25 additions & 25 deletions samples/todo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions samples/todo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "todo",
"version": "0.2.9",
"version": "0.2.10",
"private": true,
"scripts": {
"dev": "next dev",
Expand All @@ -17,8 +17,8 @@
"dependencies": {
"@heroicons/react": "^2.0.12",
"@prisma/client": "^4.4.0",
"@zenstackhq/internal": "^0.2.9",
"@zenstackhq/runtime": "^0.2.9",
"@zenstackhq/internal": "^0.2.10",
"@zenstackhq/runtime": "^0.2.10",
"bcryptjs": "^2.4.3",
"daisyui": "^2.31.0",
"moment": "^2.29.4",
Expand All @@ -42,6 +42,6 @@
"postcss": "^8.4.16",
"tailwindcss": "^3.1.8",
"typescript": "^4.6.2",
"zenstack": "^0.2.9"
"zenstack": "^0.2.10"
}
}
20 changes: 20 additions & 0 deletions tests/integration/tests/todo-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@ describe('Todo E2E Tests', () => {
await list1CredClientUser1.get('/').expect(404);
});

it('todo list with empty user id', async () => {
await createSpaceAndUsers();

await makeClient('/api/data/List', user1.id)
.post('/')
.send({
data: {
id: 'list1',
title: 'List 1',
owner: { connect: { id: user1.id } },
space: { connect: { id: space1.id } },
},
})
.expect(201);

await makeClient('/api/data/List', '')
.get('/')
.expect((resp) => expect(resp.body).toHaveLength(0));
});

it('todo', async () => {
await createSpaceAndUsers();

Expand Down
8 changes: 6 additions & 2 deletions tests/integration/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export async function setup(schemaFile: string) {

const options: RequestHandlerOptions = {
async getServerUser(req: NextApiRequest, res: NextApiResponse) {
if (req.cookies.userId) {
if (req.cookies.userId === '') {
// simulate "undefined" user id
return {} as { id: string};
} else if (req.cookies.userId) {
return { id: req.cookies.userId };
} else {
return undefined;
Expand Down Expand Up @@ -108,7 +111,7 @@ export function makeClient(apiPath: string, userId?: string, queryArgs?: any) {
prop: string | symbol,
receiver: any
) {
if (!userId) {
if (userId === undefined) {
return Reflect.get(target, prop, receiver);
}

Expand All @@ -119,6 +122,7 @@ export function makeClient(apiPath: string, userId?: string, queryArgs?: any) {
case 'del':
case 'delete':
return (url: string) => {
// use userId cookie to simulate a logged in user
return target[prop](url).set('Cookie', [
`userId=${userId}`,
]);
Expand Down