forked from zenstackhq/zenstack
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue with dealing with result read-back for nextjs and react hooks
- Loading branch information
Showing
49 changed files
with
1,520 additions
and
369 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ dist | |
.vscode/settings.json | ||
.env.local | ||
.npmcache | ||
coverage |
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,29 @@ | ||
/* | ||
* For a detailed explanation regarding each configuration property and type check, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
export default { | ||
// Automatically clear mock calls, instances, contexts and results before every test | ||
clearMocks: true, | ||
|
||
// Indicates whether the coverage information should be collected while executing the test | ||
collectCoverage: true, | ||
|
||
// The directory where Jest should output its coverage files | ||
coverageDirectory: 'tests/coverage', | ||
|
||
// An array of regexp pattern strings used to skip coverage collection | ||
coveragePathIgnorePatterns: ['/node_modules/', '/tests/'], | ||
|
||
// Indicates which provider should be used to instrument code for coverage | ||
coverageProvider: 'v8', | ||
|
||
// A list of reporter names that Jest uses when writing coverage reports | ||
coverageReporters: ['json', 'text', 'lcov', 'clover'], | ||
|
||
// A map from regular expressions to paths to transformers | ||
transform: { '^.+\\.tsx?$': 'ts-jest' }, | ||
|
||
testTimeout: 300000, | ||
}; |
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,222 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { loadSchema } from '@zenstackhq/testtools'; | ||
import { createServer, RequestListener } from 'http'; | ||
import { apiResolver } from 'next/dist/server/api-utils/node'; | ||
import superjson from 'superjson'; | ||
import request from 'supertest'; | ||
import { requestHandler, RequestHandlerOptions } from '../src/'; | ||
|
||
function makeTestClient(apiPath: string, options: RequestHandlerOptions, queryArgs?: unknown) { | ||
const pathParts = apiPath.split('/').filter((p) => p); | ||
|
||
const query = { | ||
path: pathParts, | ||
...(queryArgs ? { q: superjson.stringify(queryArgs) } : {}), | ||
}; | ||
|
||
const handler = requestHandler(options); | ||
|
||
const listener: RequestListener = (req, res) => { | ||
return apiResolver( | ||
req, | ||
res, | ||
query, | ||
handler, | ||
{ | ||
previewModeEncryptionKey: '', | ||
previewModeId: '', | ||
previewModeSigningKey: '', | ||
}, | ||
false | ||
); | ||
}; | ||
|
||
return request(createServer(listener)); | ||
} | ||
|
||
describe('request handler tests', () => { | ||
let origDir: string; | ||
|
||
beforeEach(() => { | ||
origDir = process.cwd(); | ||
}); | ||
|
||
afterEach(() => { | ||
process.chdir(origDir); | ||
}); | ||
|
||
it('simple crud', async () => { | ||
const model = ` | ||
model M { | ||
id String @id @default(cuid()) | ||
value Int | ||
} | ||
`; | ||
|
||
const { prisma } = await loadSchema(model); | ||
|
||
await makeTestClient('/m/create', { getPrisma: () => prisma }) | ||
.post('/') | ||
.send({ data: { id: '1', value: 1 } }) | ||
.expect(201) | ||
.expect((resp) => { | ||
expect(resp.body.json.value).toBe(1); | ||
}); | ||
|
||
await makeTestClient('/m/findUnique', { getPrisma: () => prisma }, { where: { id: '1' } }) | ||
.get('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json.value).toBe(1); | ||
}); | ||
|
||
await makeTestClient('/m/findFirst', { getPrisma: () => prisma }, { where: { id: '1' } }) | ||
.get('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json.value).toBe(1); | ||
}); | ||
|
||
await makeTestClient('/m/findMany', { getPrisma: () => prisma }, {}) | ||
.get('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json).toHaveLength(1); | ||
}); | ||
|
||
await makeTestClient('/m/update', { getPrisma: () => prisma }) | ||
.put('/') | ||
.send({ where: { id: '1' }, data: { value: 2 } }) | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json.value).toBe(2); | ||
}); | ||
|
||
await makeTestClient('/m/updateMany', { getPrisma: () => prisma }) | ||
.put('/') | ||
.send({ data: { value: 4 } }) | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json.count).toBe(1); | ||
}); | ||
|
||
await makeTestClient('/m/upsert', { getPrisma: () => prisma }) | ||
.post('/') | ||
.send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } }) | ||
.expect(201) | ||
.expect((resp) => { | ||
expect(resp.body.json.value).toBe(2); | ||
}); | ||
|
||
await makeTestClient('/m/upsert', { getPrisma: () => prisma }) | ||
.post('/') | ||
.send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } }) | ||
.expect(201) | ||
.expect((resp) => { | ||
expect(resp.body.json.value).toBe(3); | ||
}); | ||
|
||
await makeTestClient('/m/count', { getPrisma: () => prisma }, { where: { id: '1' } }) | ||
.get('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json).toBe(1); | ||
}); | ||
|
||
await makeTestClient('/m/count', { getPrisma: () => prisma }, {}) | ||
.get('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json).toBe(2); | ||
}); | ||
|
||
await makeTestClient('/m/aggregate', { getPrisma: () => prisma }, { _sum: { value: true } }) | ||
.get('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json._sum.value).toBe(7); | ||
}); | ||
|
||
await makeTestClient('/m/groupBy', { getPrisma: () => prisma }, { by: ['id'], _sum: { value: true } }) | ||
.get('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
const data = resp.body.json; | ||
expect(data).toHaveLength(2); | ||
expect(data.find((item: any) => item.id === '1')._sum.value).toBe(4); | ||
expect(data.find((item: any) => item.id === '2')._sum.value).toBe(3); | ||
}); | ||
|
||
await makeTestClient('/m/delete', { getPrisma: () => prisma }, { where: { id: '1' } }) | ||
.del('/') | ||
.expect(200); | ||
expect(await prisma.m.count()).toBe(1); | ||
|
||
await makeTestClient('/m/deleteMany', { getPrisma: () => prisma }, {}) | ||
.del('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json.count).toBe(1); | ||
}); | ||
expect(await prisma.m.count()).toBe(0); | ||
}); | ||
|
||
it('access policy crud', async () => { | ||
const model = ` | ||
model M { | ||
id String @id @default(cuid()) | ||
value Int | ||
@@allow('create', true) | ||
@@allow('read', value > 0) | ||
@@allow('update', future().value > 1) | ||
@@allow('delete', value > 2) | ||
} | ||
`; | ||
|
||
const { withPresets } = await loadSchema(model); | ||
|
||
await makeTestClient('/m/create', { getPrisma: () => withPresets() }) | ||
.post('/m/create') | ||
.send({ data: { value: 0 } }) | ||
.expect(403) | ||
.expect((resp) => { | ||
expect(resp.body.reason).toBe('RESULT_NOT_READABLE'); | ||
}); | ||
|
||
await makeTestClient('/m/create', { getPrisma: () => withPresets() }) | ||
.post('/') | ||
.send({ data: { id: '1', value: 1 } }) | ||
.expect(201); | ||
|
||
await makeTestClient('/m/findMany', { getPrisma: () => withPresets() }) | ||
.get('/') | ||
.expect(200) | ||
.expect((resp) => { | ||
expect(resp.body.json).toHaveLength(1); | ||
}); | ||
|
||
await makeTestClient('/m/update', { getPrisma: () => withPresets() }) | ||
.put('/') | ||
.send({ where: { id: '1' }, data: { value: 0 } }) | ||
.expect(403); | ||
|
||
await makeTestClient('/m/update', { getPrisma: () => withPresets() }) | ||
.put('/') | ||
.send({ where: { id: '1' }, data: { value: 2 } }) | ||
.expect(200); | ||
|
||
await makeTestClient('/m/delete', { getPrisma: () => withPresets() }, { where: { id: '1' } }) | ||
.del('/') | ||
.expect(403); | ||
|
||
await makeTestClient('/m/update', { getPrisma: () => withPresets() }) | ||
.put('/') | ||
.send({ where: { id: '1' }, data: { value: 3 } }) | ||
.expect(200); | ||
|
||
await makeTestClient('/m/delete', { getPrisma: () => withPresets() }, { where: { id: '1' } }) | ||
.del('/') | ||
.expect(200); | ||
}); | ||
}); |
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
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
Oops, something went wrong.