Skip to content

Commit

Permalink
#7, add mock revoke puzzle api
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Dec 19, 2023
1 parent d6b9f58 commit ab60d85
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/schema/puzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ export const puzzleItemStatSchema = z.object({

export type PuzzleStats = z.infer<typeof puzzleStatsSchema>
export const puzzleStatsSchema = z.array(puzzleItemStatSchema)

export const puzzleRevokeResponseSchema = z.object({
status: z.string().default('OK'),
})
16 changes: 16 additions & 0 deletions features/puzzle_revoke.feature
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions features/support/apiSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
1 change: 1 addition & 0 deletions worker/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './puzzleStatus'
export * from './puzzleDeliverer'
export * from './puzzleDelivery'
export * from './puzzleDashboard'
export * from './revokePuzzle'
29 changes: 29 additions & 0 deletions worker/controller/revokePuzzle.ts
Original file line number Diff line number Diff line change
@@ -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' })
}
}
10 changes: 10 additions & 0 deletions worker/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export function Post<T extends OpenAPIRouteConstructor>(path: string) {
}
}

export function Put<T extends OpenAPIRouteConstructor>(path: string) {
return function (handler: T) {
routes.push({
method: 'put',
path,
handler,
})
}
}

export const setup = (router: OpenAPIRouterType) => {
routes.forEach(({ method, path, handler }) => {
if (method) {
Expand Down

0 comments on commit ab60d85

Please sign in to comment.