Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service and identifier cannot be empty #345

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/services/api/player-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntityManager } from '@mikro-orm/mysql'
import { Request, Response, Routes, Validate, HasPermission, ForwardTo, forwardRequest } from 'koa-clay'
import { Request, Response, Routes, Validate, HasPermission, ForwardTo, forwardRequest, ValidationCondition } from 'koa-clay'
import APIKey, { APIKeyScope } from '../../entities/api-key'
import Player from '../../entities/player'
import GameSave from '../../entities/game-save'
Expand Down Expand Up @@ -70,6 +70,15 @@ export async function createPlayerFromIdentifyRequest(
}
}

function validateIdentifyQueryParam(param: 'service' | 'identifier') {
return async (val?: string): Promise<ValidationCondition[]> => [
{
check: val.trim().length > 0,
error: `Invalid ${param}, must be a non-empty string`
}
]
}

@Routes([
{
method: 'GET',
Expand All @@ -90,7 +99,16 @@ export async function createPlayerFromIdentifyRequest(
])
export default class PlayerAPIService extends APIService {
@Validate({
query: ['service', 'identifier']
query: {
service: {
required: true,
validation: validateIdentifyQueryParam('service')
},
identifier: {
required: true,
validation: validateIdentifyQueryParam('identifier')
}
}
})
@HasPermission(PlayerAPIPolicy, 'identify')
@ForwardTo('games.players', 'post')
Expand Down
72 changes: 72 additions & 0 deletions tests/services/_api/player-api/identify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,76 @@ describe('Player API service - identify', () => {

expect(res.body).toStrictEqual({ message: 'Player not found: Talo aliases must be created using the /v1/players/auth API' })
})

it('should require the service to be set', async () => {
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYERS])
const player = await new PlayerFactory([apiKey.game]).one()
await (<EntityManager>global.em).persistAndFlush(player)

const res = await request(global.app)
.get('/v1/players/identify')
.query({ identifier: player.aliases[0].identifier })
.auth(token, { type: 'bearer' })
.expect(400)

expect(res.body).toStrictEqual({
errors: {
service: ['service is missing from the request query']
}
})
})

it('should require the service to be a non-empty string', async () => {
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYERS])
const player = await new PlayerFactory([apiKey.game]).one()
await (<EntityManager>global.em).persistAndFlush(player)

const res = await request(global.app)
.get('/v1/players/identify')
.query({ service: '', identifier: player.aliases[0].identifier })
.auth(token, { type: 'bearer' })
.expect(400)

expect(res.body).toStrictEqual({
errors: {
service: ['Invalid service, must be a non-empty string']
}
})
})

it('should require the identifier to be set', async () => {
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYERS])
const player = await new PlayerFactory([apiKey.game]).one()
await (<EntityManager>global.em).persistAndFlush(player)

const res = await request(global.app)
.get('/v1/players/identify')
.query({ service: player.aliases[0].service })
.auth(token, { type: 'bearer' })
.expect(400)

expect(res.body).toStrictEqual({
errors: {
identifier: ['identifier is missing from the request query']
}
})
})

it('should require the identifier to be a non-empty string', async () => {
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYERS])
const player = await new PlayerFactory([apiKey.game]).one()
await (<EntityManager>global.em).persistAndFlush(player)

const res = await request(global.app)
.get('/v1/players/identify')
.query({ service: player.aliases[0].service, identifier: '' })
.auth(token, { type: 'bearer' })
.expect(400)

expect(res.body).toStrictEqual({
errors: {
identifier: ['Invalid identifier, must be a non-empty string']
}
})
})
})