|
| 1 | +import { createTestClient } from '@zenstackhq/testtools'; |
| 2 | +import fastify from 'fastify'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { ZenStackFastifyPlugin } from '../../src/adapter/fastify'; |
| 5 | +import { RestApiHandler, RPCApiHandler } from '../../src/api'; |
| 6 | +import { makeUrl, schema } from '../utils'; |
| 7 | + |
| 8 | +describe('Fastify adapter tests - rpc handler', () => { |
| 9 | + it('run plugin regular json', async () => { |
| 10 | + const client = await createTestClient(schema); |
| 11 | + |
| 12 | + const app = fastify(); |
| 13 | + app.register(ZenStackFastifyPlugin, { |
| 14 | + prefix: '/api', |
| 15 | + getClient: () => client, |
| 16 | + apiHandler: new RPCApiHandler({ schema: client.schema }) |
| 17 | + }); |
| 18 | + |
| 19 | + let r = await app.inject({ |
| 20 | + method: 'GET', |
| 21 | + url: makeUrl('/api/post/findMany', { where: { id: { equals: '1' } } }), |
| 22 | + }); |
| 23 | + expect(r.statusCode).toBe(200); |
| 24 | + expect(r.json().data).toHaveLength(0); |
| 25 | + |
| 26 | + r = await app.inject({ |
| 27 | + method: 'POST', |
| 28 | + url: '/api/user/create', |
| 29 | + payload: { |
| 30 | + include: { posts: true }, |
| 31 | + data: { |
| 32 | + id: 'user1', |
| 33 | + |
| 34 | + posts: { |
| 35 | + create: [ |
| 36 | + { title: 'post1', published: true, viewCount: 1 }, |
| 37 | + { title: 'post2', published: false, viewCount: 2 }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }); |
| 43 | + expect(r.statusCode).toBe(201); |
| 44 | + const data = r.json().data; |
| 45 | + expect(data).toEqual( |
| 46 | + expect.objectContaining({ |
| 47 | + |
| 48 | + posts: expect.arrayContaining([ |
| 49 | + expect.objectContaining({ title: 'post1' }), |
| 50 | + expect.objectContaining({ title: 'post2' }), |
| 51 | + ]), |
| 52 | + }) |
| 53 | + ); |
| 54 | + |
| 55 | + r = await app.inject({ |
| 56 | + method: 'GET', |
| 57 | + url: makeUrl('/api/post/findMany'), |
| 58 | + }); |
| 59 | + expect(r.statusCode).toBe(200); |
| 60 | + expect(r.json().data).toHaveLength(2); |
| 61 | + |
| 62 | + r = await app.inject({ |
| 63 | + method: 'GET', |
| 64 | + url: makeUrl('/api/post/findMany', { where: { viewCount: { gt: 1 } } }), |
| 65 | + }); |
| 66 | + expect(r.statusCode).toBe(200); |
| 67 | + expect(r.json().data).toHaveLength(1); |
| 68 | + |
| 69 | + r = await app.inject({ |
| 70 | + method: 'PUT', |
| 71 | + url: '/api/user/update', |
| 72 | + payload: { where: { id: 'user1' }, data: { email: '[email protected]' } }, |
| 73 | + }); |
| 74 | + expect(r.statusCode).toBe(200); |
| 75 | + expect(r.json().data.email).toBe('[email protected]'); |
| 76 | + |
| 77 | + r = await app.inject({ |
| 78 | + method: 'GET', |
| 79 | + url: makeUrl('/api/post/count', { where: { viewCount: { gt: 1 } } }), |
| 80 | + }); |
| 81 | + expect(r.statusCode).toBe(200); |
| 82 | + expect(r.json().data).toBe(1); |
| 83 | + |
| 84 | + r = await app.inject({ |
| 85 | + method: 'GET', |
| 86 | + url: makeUrl('/api/post/aggregate', { _sum: { viewCount: true } }), |
| 87 | + }); |
| 88 | + expect(r.statusCode).toBe(200); |
| 89 | + expect(r.json().data._sum.viewCount).toBe(3); |
| 90 | + |
| 91 | + r = await app.inject({ |
| 92 | + method: 'GET', |
| 93 | + url: makeUrl('/api/post/groupBy', { by: ['published'], _sum: { viewCount: true } }), |
| 94 | + }); |
| 95 | + expect(r.statusCode).toBe(200); |
| 96 | + expect(r.json().data).toEqual( |
| 97 | + expect.arrayContaining([ |
| 98 | + expect.objectContaining({ published: true, _sum: { viewCount: 1 } }), |
| 99 | + expect.objectContaining({ published: false, _sum: { viewCount: 2 } }), |
| 100 | + ]) |
| 101 | + ); |
| 102 | + |
| 103 | + r = await app.inject({ |
| 104 | + method: 'DELETE', |
| 105 | + url: makeUrl('/api/user/deleteMany', { where: { id: 'user1' } }), |
| 106 | + }); |
| 107 | + expect(r.statusCode).toBe(200); |
| 108 | + expect(r.json().data.count).toBe(1); |
| 109 | + }); |
| 110 | + |
| 111 | + it('invalid path or args', async () => { |
| 112 | + const client = await createTestClient(schema); |
| 113 | + |
| 114 | + const app = fastify(); |
| 115 | + app.register(ZenStackFastifyPlugin, { |
| 116 | + prefix: '/api', |
| 117 | + getClient: () => client, |
| 118 | + apiHandler: new RPCApiHandler({ schema: client.schema }), |
| 119 | + }); |
| 120 | + |
| 121 | + let r = await app.inject({ |
| 122 | + method: 'GET', |
| 123 | + url: '/api/post/', |
| 124 | + }); |
| 125 | + expect(r.statusCode).toBe(400); |
| 126 | + |
| 127 | + r = await app.inject({ |
| 128 | + method: 'GET', |
| 129 | + url: '/api/post/findMany/abc', |
| 130 | + }); |
| 131 | + expect(r.statusCode).toBe(400); |
| 132 | + |
| 133 | + r = await app.inject({ |
| 134 | + method: 'GET', |
| 135 | + url: '/api/post/findMany?q=abc', |
| 136 | + }); |
| 137 | + expect(r.statusCode).toBe(400); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +describe('Fastify adapter tests - rest handler', () => { |
| 142 | + it('run plugin regular json', async () => { |
| 143 | + const client = await createTestClient(schema); |
| 144 | + |
| 145 | + const app = fastify(); |
| 146 | + app.register(ZenStackFastifyPlugin, { |
| 147 | + prefix: '/api', |
| 148 | + getClient: () => client, |
| 149 | + apiHandler: new RestApiHandler({ schema: client.schema, endpoint: 'http://localhost/api' }), |
| 150 | + }); |
| 151 | + |
| 152 | + let r = await app.inject({ |
| 153 | + method: 'GET', |
| 154 | + url: '/api/post/1', |
| 155 | + }); |
| 156 | + expect(r.statusCode).toBe(404); |
| 157 | + |
| 158 | + r = await app.inject({ |
| 159 | + method: 'POST', |
| 160 | + url: '/api/user', |
| 161 | + payload: { |
| 162 | + data: { |
| 163 | + type: 'User', |
| 164 | + attributes: { |
| 165 | + id: 'user1', |
| 166 | + |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }); |
| 171 | + expect(r.statusCode).toBe(201); |
| 172 | + expect(r.json()).toMatchObject({ |
| 173 | + jsonapi: { version: '1.1' }, |
| 174 | + data: { type: 'User', id: 'user1', attributes: { email: '[email protected]' } }, |
| 175 | + }); |
| 176 | + |
| 177 | + r = await app.inject({ method: 'GET', url: '/api/user?filter[id]=user1' }); |
| 178 | + expect(r.json().data).toHaveLength(1); |
| 179 | + |
| 180 | + r = await app.inject({ method: 'GET', url: '/api/user?filter[id]=user2' }); |
| 181 | + expect(r.json().data).toHaveLength(0); |
| 182 | + |
| 183 | + r = await app.inject({ method: 'GET', url: '/api/user?filter[id]=user1&filter[email]=xyz' }); |
| 184 | + expect(r.json().data).toHaveLength(0); |
| 185 | + |
| 186 | + r = await app.inject({ |
| 187 | + method: 'PUT', |
| 188 | + url: '/api/user/user1', |
| 189 | + payload: { data: { type: 'User', attributes: { email: '[email protected]' } } }, |
| 190 | + }); |
| 191 | + expect(r.statusCode).toBe(200); |
| 192 | + expect(r.json().data.attributes.email).toBe('[email protected]'); |
| 193 | + |
| 194 | + r = await app.inject({ method: 'DELETE', url: '/api/user/user1' }); |
| 195 | + expect(r.statusCode).toBe(200); |
| 196 | + expect(await client.user.findMany()).toHaveLength(0); |
| 197 | + }); |
| 198 | +}); |
0 commit comments