|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { loadSchema } from '@zenstackhq/testtools'; |
| 3 | +import { createServer, RequestListener } from 'http'; |
| 4 | +import { apiResolver } from 'next/dist/server/api-utils/node'; |
| 5 | +import superjson from 'superjson'; |
| 6 | +import request from 'supertest'; |
| 7 | +import { requestHandler, RequestHandlerOptions } from '../src/'; |
| 8 | + |
| 9 | +function makeTestClient(apiPath: string, options: RequestHandlerOptions, queryArgs?: unknown) { |
| 10 | + const pathParts = apiPath.split('/').filter((p) => p); |
| 11 | + |
| 12 | + const query = { |
| 13 | + path: pathParts, |
| 14 | + ...(queryArgs ? { q: superjson.stringify(queryArgs) } : {}), |
| 15 | + }; |
| 16 | + |
| 17 | + const handler = requestHandler(options); |
| 18 | + |
| 19 | + const listener: RequestListener = (req, res) => { |
| 20 | + return apiResolver( |
| 21 | + req, |
| 22 | + res, |
| 23 | + query, |
| 24 | + handler, |
| 25 | + { |
| 26 | + previewModeEncryptionKey: '', |
| 27 | + previewModeId: '', |
| 28 | + previewModeSigningKey: '', |
| 29 | + }, |
| 30 | + false |
| 31 | + ); |
| 32 | + }; |
| 33 | + |
| 34 | + return request(createServer(listener)); |
| 35 | +} |
| 36 | + |
| 37 | +describe('request handler tests', () => { |
| 38 | + let origDir: string; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + origDir = process.cwd(); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + process.chdir(origDir); |
| 46 | + }); |
| 47 | + |
| 48 | + it('simple crud', async () => { |
| 49 | + const model = ` |
| 50 | +model M { |
| 51 | + id String @id @default(cuid()) |
| 52 | + value Int |
| 53 | +} |
| 54 | + `; |
| 55 | + |
| 56 | + const { prisma } = await loadSchema(model); |
| 57 | + |
| 58 | + await makeTestClient('/m/create', { getPrisma: () => prisma }) |
| 59 | + .post('/') |
| 60 | + .send({ data: { id: '1', value: 1 } }) |
| 61 | + .expect(201) |
| 62 | + .expect((resp) => { |
| 63 | + expect(resp.body.json.value).toBe(1); |
| 64 | + }); |
| 65 | + |
| 66 | + await makeTestClient('/m/findUnique', { getPrisma: () => prisma }, { where: { id: '1' } }) |
| 67 | + .get('/') |
| 68 | + .expect(200) |
| 69 | + .expect((resp) => { |
| 70 | + expect(resp.body.json.value).toBe(1); |
| 71 | + }); |
| 72 | + |
| 73 | + await makeTestClient('/m/findFirst', { getPrisma: () => prisma }, { where: { id: '1' } }) |
| 74 | + .get('/') |
| 75 | + .expect(200) |
| 76 | + .expect((resp) => { |
| 77 | + expect(resp.body.json.value).toBe(1); |
| 78 | + }); |
| 79 | + |
| 80 | + await makeTestClient('/m/findMany', { getPrisma: () => prisma }, {}) |
| 81 | + .get('/') |
| 82 | + .expect(200) |
| 83 | + .expect((resp) => { |
| 84 | + expect(resp.body.json).toHaveLength(1); |
| 85 | + }); |
| 86 | + |
| 87 | + await makeTestClient('/m/update', { getPrisma: () => prisma }) |
| 88 | + .put('/') |
| 89 | + .send({ where: { id: '1' }, data: { value: 2 } }) |
| 90 | + .expect(200) |
| 91 | + .expect((resp) => { |
| 92 | + expect(resp.body.json.value).toBe(2); |
| 93 | + }); |
| 94 | + |
| 95 | + await makeTestClient('/m/updateMany', { getPrisma: () => prisma }) |
| 96 | + .put('/') |
| 97 | + .send({ data: { value: 4 } }) |
| 98 | + .expect(200) |
| 99 | + .expect((resp) => { |
| 100 | + expect(resp.body.json.count).toBe(1); |
| 101 | + }); |
| 102 | + |
| 103 | + await makeTestClient('/m/upsert', { getPrisma: () => prisma }) |
| 104 | + .post('/') |
| 105 | + .send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } }) |
| 106 | + .expect(201) |
| 107 | + .expect((resp) => { |
| 108 | + expect(resp.body.json.value).toBe(2); |
| 109 | + }); |
| 110 | + |
| 111 | + await makeTestClient('/m/upsert', { getPrisma: () => prisma }) |
| 112 | + .post('/') |
| 113 | + .send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } }) |
| 114 | + .expect(201) |
| 115 | + .expect((resp) => { |
| 116 | + expect(resp.body.json.value).toBe(3); |
| 117 | + }); |
| 118 | + |
| 119 | + await makeTestClient('/m/count', { getPrisma: () => prisma }, { where: { id: '1' } }) |
| 120 | + .get('/') |
| 121 | + .expect(200) |
| 122 | + .expect((resp) => { |
| 123 | + expect(resp.body.json).toBe(1); |
| 124 | + }); |
| 125 | + |
| 126 | + await makeTestClient('/m/count', { getPrisma: () => prisma }, {}) |
| 127 | + .get('/') |
| 128 | + .expect(200) |
| 129 | + .expect((resp) => { |
| 130 | + expect(resp.body.json).toBe(2); |
| 131 | + }); |
| 132 | + |
| 133 | + await makeTestClient('/m/aggregate', { getPrisma: () => prisma }, { _sum: { value: true } }) |
| 134 | + .get('/') |
| 135 | + .expect(200) |
| 136 | + .expect((resp) => { |
| 137 | + expect(resp.body.json._sum.value).toBe(7); |
| 138 | + }); |
| 139 | + |
| 140 | + await makeTestClient('/m/groupBy', { getPrisma: () => prisma }, { by: ['id'], _sum: { value: true } }) |
| 141 | + .get('/') |
| 142 | + .expect(200) |
| 143 | + .expect((resp) => { |
| 144 | + const data = resp.body.json; |
| 145 | + expect(data).toHaveLength(2); |
| 146 | + expect(data.find((item: any) => item.id === '1')._sum.value).toBe(4); |
| 147 | + expect(data.find((item: any) => item.id === '2')._sum.value).toBe(3); |
| 148 | + }); |
| 149 | + |
| 150 | + await makeTestClient('/m/delete', { getPrisma: () => prisma }, { where: { id: '1' } }) |
| 151 | + .del('/') |
| 152 | + .expect(200); |
| 153 | + expect(await prisma.m.count()).toBe(1); |
| 154 | + |
| 155 | + await makeTestClient('/m/deleteMany', { getPrisma: () => prisma }, {}) |
| 156 | + .del('/') |
| 157 | + .expect(200) |
| 158 | + .expect((resp) => { |
| 159 | + expect(resp.body.json.count).toBe(1); |
| 160 | + }); |
| 161 | + expect(await prisma.m.count()).toBe(0); |
| 162 | + }); |
| 163 | + |
| 164 | + it('access policy crud', async () => { |
| 165 | + const model = ` |
| 166 | +model M { |
| 167 | + id String @id @default(cuid()) |
| 168 | + value Int |
| 169 | +
|
| 170 | + @@allow('create', true) |
| 171 | + @@allow('read', value > 0) |
| 172 | + @@allow('update', future().value > 1) |
| 173 | + @@allow('delete', value > 2) |
| 174 | +} |
| 175 | + `; |
| 176 | + |
| 177 | + const { withPresets } = await loadSchema(model); |
| 178 | + |
| 179 | + await makeTestClient('/m/create', { getPrisma: () => withPresets() }) |
| 180 | + .post('/m/create') |
| 181 | + .send({ data: { value: 0 } }) |
| 182 | + .expect(403) |
| 183 | + .expect((resp) => { |
| 184 | + expect(resp.body.reason).toBe('RESULT_NOT_READABLE'); |
| 185 | + }); |
| 186 | + |
| 187 | + await makeTestClient('/m/create', { getPrisma: () => withPresets() }) |
| 188 | + .post('/') |
| 189 | + .send({ data: { id: '1', value: 1 } }) |
| 190 | + .expect(201); |
| 191 | + |
| 192 | + await makeTestClient('/m/findMany', { getPrisma: () => withPresets() }) |
| 193 | + .get('/') |
| 194 | + .expect(200) |
| 195 | + .expect((resp) => { |
| 196 | + expect(resp.body.json).toHaveLength(1); |
| 197 | + }); |
| 198 | + |
| 199 | + await makeTestClient('/m/update', { getPrisma: () => withPresets() }) |
| 200 | + .put('/') |
| 201 | + .send({ where: { id: '1' }, data: { value: 0 } }) |
| 202 | + .expect(403); |
| 203 | + |
| 204 | + await makeTestClient('/m/update', { getPrisma: () => withPresets() }) |
| 205 | + .put('/') |
| 206 | + .send({ where: { id: '1' }, data: { value: 2 } }) |
| 207 | + .expect(200); |
| 208 | + |
| 209 | + await makeTestClient('/m/delete', { getPrisma: () => withPresets() }, { where: { id: '1' } }) |
| 210 | + .del('/') |
| 211 | + .expect(403); |
| 212 | + |
| 213 | + await makeTestClient('/m/update', { getPrisma: () => withPresets() }) |
| 214 | + .put('/') |
| 215 | + .send({ where: { id: '1' }, data: { value: 3 } }) |
| 216 | + .expect(200); |
| 217 | + |
| 218 | + await makeTestClient('/m/delete', { getPrisma: () => withPresets() }, { where: { id: '1' } }) |
| 219 | + .del('/') |
| 220 | + .expect(200); |
| 221 | + }); |
| 222 | +}); |
0 commit comments