Skip to content

Commit 74799cd

Browse files
committed
service and identifier cannot be empty
1 parent 7b39c39 commit 74799cd

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/services/api/player-api.service.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EntityManager } from '@mikro-orm/mysql'
2-
import { Request, Response, Routes, Validate, HasPermission, ForwardTo, forwardRequest } from 'koa-clay'
2+
import { Request, Response, Routes, Validate, HasPermission, ForwardTo, forwardRequest, ValidationCondition } from 'koa-clay'
33
import APIKey, { APIKeyScope } from '../../entities/api-key'
44
import Player from '../../entities/player'
55
import GameSave from '../../entities/game-save'
@@ -70,6 +70,15 @@ export async function createPlayerFromIdentifyRequest(
7070
}
7171
}
7272

73+
function validateIdentifyQueryParam(param: 'service' | 'identifier') {
74+
return async (val?: string): Promise<ValidationCondition[]> => [
75+
{
76+
check: (val ?? '').trim().length > 0,
77+
error: `Invalid ${param}, must be a non-empty string`
78+
}
79+
]
80+
}
81+
7382
@Routes([
7483
{
7584
method: 'GET',
@@ -90,7 +99,16 @@ export async function createPlayerFromIdentifyRequest(
9099
])
91100
export default class PlayerAPIService extends APIService {
92101
@Validate({
93-
query: ['service', 'identifier']
102+
query: {
103+
service: {
104+
required: true,
105+
validation: validateIdentifyQueryParam('service')
106+
},
107+
identifier: {
108+
required: true,
109+
validation: validateIdentifyQueryParam('identifier')
110+
}
111+
}
94112
})
95113
@HasPermission(PlayerAPIPolicy, 'identify')
96114
@ForwardTo('games.players', 'post')

tests/services/_api/player-api/identify.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,40 @@ describe('Player API service - identify', () => {
124124

125125
expect(res.body).toStrictEqual({ message: 'Player not found: Talo aliases must be created using the /v1/players/auth API' })
126126
})
127+
128+
it('should require the service to be a non-empty string', async () => {
129+
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYERS])
130+
const player = await new PlayerFactory([apiKey.game]).one()
131+
await (<EntityManager>global.em).persistAndFlush(player)
132+
133+
const res = await request(global.app)
134+
.get('/v1/players/identify')
135+
.query({ service: '', identifier: player.aliases[0].identifier })
136+
.auth(token, { type: 'bearer' })
137+
.expect(400)
138+
139+
expect(res.body).toStrictEqual({
140+
errors: {
141+
service: ['Invalid service, must be a non-empty string']
142+
}
143+
})
144+
})
145+
146+
it('should require the identifier to be a non-empty string', async () => {
147+
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYERS])
148+
const player = await new PlayerFactory([apiKey.game]).one()
149+
await (<EntityManager>global.em).persistAndFlush(player)
150+
151+
const res = await request(global.app)
152+
.get('/v1/players/identify')
153+
.query({ service: player.aliases[0].service, identifier: '' })
154+
.auth(token, { type: 'bearer' })
155+
.expect(400)
156+
157+
expect(res.body).toStrictEqual({
158+
errors: {
159+
identifier: ['Invalid identifier, must be a non-empty string']
160+
}
161+
})
162+
})
127163
})

0 commit comments

Comments
 (0)