From 2894d1e2ce1ae409a966942d8a167de58653f1d2 Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 7 May 2025 18:46:12 +0800 Subject: [PATCH] core: fix api tests --- framework/framework/api.ts | 14 +++++++++++++- packages/hydrooj/src/handler/user.ts | 2 +- test/main.ts | 8 ++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/framework/framework/api.ts b/framework/framework/api.ts index 7bd6beade..6d0f89d1d 100644 --- a/framework/framework/api.ts +++ b/framework/framework/api.ts @@ -150,8 +150,20 @@ export class ApiHandler extends Handler { await this.ctx.parallel('handler/api/before', this); // @ts-ignore await this.ctx.parallel(`handler/api/before/${op}`, this); + try { + if (typeof this.args.args === 'string') { + this.args.args = JSON.parse(this.args.args); + } + if (typeof this.args.projection === 'string') { + this.args.projection = '{['.includes(this.args.projection[0]) + ? JSON.parse(this.args.projection) + : this.args.projection.split(',').map((i) => i.trim()).filter((i) => i); + } + } catch (e) { + throw new BadRequestError('Invalid arguments'); + } const result = await this.ctx.api.execute( - this, op, { domainId: this.args.domainId, ...(this.args.args || {}) }, + this, op, { domainId: this.args.domainId, ...this.args, ...(this.args.args || {}) }, (m, args) => (this.ctx.parallel as any)(m, args), this.args.projection, ); if (BinaryResponse.check(result)) { diff --git a/packages/hydrooj/src/handler/user.ts b/packages/hydrooj/src/handler/user.ts index 96f5d67f4..b91c154e9 100644 --- a/packages/hydrooj/src/handler/user.ts +++ b/packages/hydrooj/src/handler/user.ts @@ -546,7 +546,7 @@ const UserApi = { if (arg.id) return user.getById(arg.domainId, arg.id); if (arg.mail) return user.getByEmail(arg.domainId, arg.mail); if (arg.uname) return user.getByUname(arg.domainId, arg.uname); - return c.user; + return user.getById(arg.domainId, c.user._id); }), users: Query(Schema.object({ ids: Schema.array(Schema.number().step(1)), diff --git a/test/main.ts b/test/main.ts index c4d499191..97cbd5e73 100644 --- a/test/main.ts +++ b/test/main.ts @@ -29,15 +29,15 @@ describe('App', () => { console.log('Application inited in %d ms', Date.now() - init); }, { timeout: 30000 }); - const routes = ['/', '/api', '/p', '/contest', '/homework', '/user/1', '/training']; + const routes = ['/', '/p', '/contest', '/homework', '/user/1', '/training']; for (const route of routes) { // eslint-disable-next-line @typescript-eslint/no-loop-func it(`GET ${route}`, () => agent.get(route).expect(200)); } it('API user', async () => { - await agent.get('/api?{user(id:1){uname}}').expect({ data: { user: { uname: 'Hydro' } } }); - await agent.get('/api?{user(id:2){uname}}').expect({ data: { user: null } }); + await agent.get('/api/user?args={"id":1}&projection=uname').expect({ uname: 'Hydro' }); + await agent.get('/api/user?args={"id":2}&projection=uname').expect(null); }); it('Create User', async () => { @@ -59,7 +59,7 @@ describe('App', () => { }); it('API registered user', async () => { - await agent.get('/api?{user(id:2){uname}}').expect({ data: { user: { uname: 'root' } } }); + await agent.get('/api/user?args={"id":2}&projection=uname').expect({ uname: 'root' }); }); // TODO add more tests