From ab60d852d2d6f643d93287f9bb94f250968cc1ba Mon Sep 17 00:00:00 2001 From: Aotokitsuruya Date: Tue, 19 Dec 2023 21:57:10 +0800 Subject: [PATCH] #7, add mock revoke puzzle api --- api/schema/puzzle.ts | 4 ++++ features/puzzle_revoke.feature | 16 ++++++++++++++++ features/support/apiSteps.ts | 11 +++++++++++ worker/controller/index.ts | 1 + worker/controller/revokePuzzle.ts | 29 +++++++++++++++++++++++++++++ worker/router.ts | 10 ++++++++++ 6 files changed, 71 insertions(+) create mode 100644 features/puzzle_revoke.feature create mode 100644 worker/controller/revokePuzzle.ts diff --git a/api/schema/puzzle.ts b/api/schema/puzzle.ts index 761d5f2..13df1d2 100644 --- a/api/schema/puzzle.ts +++ b/api/schema/puzzle.ts @@ -29,3 +29,7 @@ export const puzzleItemStatSchema = z.object({ export type PuzzleStats = z.infer export const puzzleStatsSchema = z.array(puzzleItemStatSchema) + +export const puzzleRevokeResponseSchema = z.object({ + status: z.string().default('OK'), +}) diff --git a/features/puzzle_revoke.feature b/features/puzzle_revoke.feature new file mode 100644 index 0000000..372e588 --- /dev/null +++ b/features/puzzle_revoke.feature @@ -0,0 +1,16 @@ +Feature: Puzzle Revoke + Scenario: PUT /event/puzzle/revoke to revoke attendee puzzle + Given there have some attendees + | token | event_id | display_name | + | f185f505-d8c0-43ce-9e7b-bb9e8909072d | SITCON | Aotoki | + When I make a PUT request to "/event/puzzle/revoke?token=f185f505-d8c0-43ce-9e7b-bb9e8909072d": + """ + {} + """ + Then the response json should be: + """ + { + "status": "OK" + } + """ + And the response status should be 200 diff --git a/features/support/apiSteps.ts b/features/support/apiSteps.ts index a74d5ae..032f028 100644 --- a/features/support/apiSteps.ts +++ b/features/support/apiSteps.ts @@ -20,6 +20,17 @@ When( } ) +When( + 'I make a PUT request to {string}:', + async function (this: WorkerWorld, path: string, payload: string) { + this.apiResponse = await this.api.fetch(`https://ccip.opass.app${path}`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: payload, + }) + } +) + Then('the response status should be {int}', async function (statusCode) { expect(this.apiResponse?.status).toEqual(statusCode) }) diff --git a/worker/controller/index.ts b/worker/controller/index.ts index 1d568cf..46d191f 100644 --- a/worker/controller/index.ts +++ b/worker/controller/index.ts @@ -6,3 +6,4 @@ export * from './puzzleStatus' export * from './puzzleDeliverer' export * from './puzzleDelivery' export * from './puzzleDashboard' +export * from './revokePuzzle' diff --git a/worker/controller/revokePuzzle.ts b/worker/controller/revokePuzzle.ts new file mode 100644 index 0000000..41847bb --- /dev/null +++ b/worker/controller/revokePuzzle.ts @@ -0,0 +1,29 @@ +import { IRequest } from 'itty-router' +import { OpenAPIRoute, OpenAPIRouteSchema } from '@cloudflare/itty-router-openapi' +import { Put } from '@worker/router' +import { json } from '@worker/utils' +import * as schema from '@api/schema' + +export type RevokePuzzleRequest = IRequest + +@Put('/event/puzzle/revoke') +export class RevokePuzzle extends OpenAPIRoute { + static schema: OpenAPIRouteSchema = { + summary: "Revoke attendee's puzzle", + tags: ['Puzzle'], + requestBody: {}, + parameters: { + token: schema.OptionalAttendeeTokenQuery, + }, + responses: { + '200': { + description: 'Result of puzzle revocation', + schema: schema.puzzleRevokeResponseSchema, + }, + }, + } + + async handle(_request: RevokePuzzleRequest, _env: unknown, _context: unknown) { + return json({ status: 'OK' }) + } +} diff --git a/worker/router.ts b/worker/router.ts index 86b0ba7..6e86d73 100644 --- a/worker/router.ts +++ b/worker/router.ts @@ -30,6 +30,16 @@ export function Post(path: string) { } } +export function Put(path: string) { + return function (handler: T) { + routes.push({ + method: 'put', + path, + handler, + }) + } +} + export const setup = (router: OpenAPIRouterType) => { routes.forEach(({ method, path, handler }) => { if (method) {