Skip to content

Commit

Permalink
test: write new tests
Browse files Browse the repository at this point in the history
Signed-off-by: nathaelbonnal <[email protected]>
  • Loading branch information
NathaelB committed Dec 9, 2024
1 parent 8fa9589 commit a6a9ab0
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
5 changes: 4 additions & 1 deletion tests/functional/players/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ test.group('Players index', () => {
assert.equal(response.body().status, 401)
})

test('should return 403 if the user was connected but not authorized', async ({ assert, client }) => {
test('should return 403 if the user was connected but not authorized', async ({
assert,
client,
}) => {
const user = await UserFactory.make()

const response = await client.get('/v1/players').loginAs(user, [])
Expand Down
5 changes: 4 additions & 1 deletion tests/functional/players/show.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ test.group('Players show', () => {
expect(response.body()).toEqual(player.toJSON())
}).tags(['player'])

test('should return 403 if the user was connected but not authorized', async ({ assert, client }) => {
test('should return 403 if the user was connected but not authorized', async ({
assert,
client,
}) => {
const user = await UserFactory.make()
const player = await PlayerFactory.make()

Expand Down
61 changes: 61 additions & 0 deletions tests/functional/players/store.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { test } from '@japa/runner'
import { UserFactory } from '#database/factories/user_factory'
import { randomUUID } from 'node:crypto'

test.group('Players store', () => {
test('should return 401 if the user was not connected', async ({ assert, client }) => {
const response = await client.post('/v1/players')

response.assertStatus(401)
assert.properties(response.body(), ['code', 'status', 'message'])
assert.equal(response.body().code, 'E_UNAUTHORIZED_ACCESS')
assert.equal(response.body().status, 401)
})

test('should return 403 if the user was connected but not authorized', async ({
assert,
client,
}) => {
const user = await UserFactory.make()

const response = await client.post('/v1/players').loginAs(user, [])

response.assertStatus(403)
assert.properties(response.body(), ['code', 'status', 'message'])
assert.equal(response.body().code, 'E_AUTHORIZATION_FAILURE')
assert.equal(response.body().status, 403)
})

test('should return 201 if the user was connected and authorized', async ({ assert, client }) => {
const user = await UserFactory.make()

const response = await client
.post('/v1/players')
.json({
playerUuid: randomUUID(),
level: 1,
coins: 500,
})
.loginAs(user, ['create-player'])

response.assertStatus(201)
assert.properties(response.body(), ['id', 'playerUuid', 'coins', 'level'])
})

test('should return 422 if the playerUuid is missing', async ({ assert, client }) => {
const user = await UserFactory.make()

const response = await client
.post('/v1/players')
.json({
level: 1,
coins: 500,
})
.loginAs(user, ['create-player'])

response.assertStatus(422)

assert.properties(response.body(), ['errors'])
assert.equal(response.body().errors[0].field, 'playerUuid')
})
})
60 changes: 60 additions & 0 deletions tests/functional/players/update.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { test } from '@japa/runner'
import { UserFactory } from '#database/factories/user_factory'
import { PlayerFactory } from '#database/factories/player_factory'

test.group('Players update', () => {
test('should return 401 if the user was not connected', async ({ assert, client }) => {
const response = await client.put('/v1/players/1')

response.assertStatus(401)
assert.properties(response.body(), ['code', 'status', 'message'])
assert.equal(response.body().code, 'E_UNAUTHORIZED_ACCESS')
assert.equal(response.body().status, 401)
})

test('should return 403 if the user was connected but not authorized', async ({
assert,
client,
}) => {
const user = await UserFactory.make()
const player = await PlayerFactory.make()

const response = await client.put(`/v1/players/${player.id}`).loginAs(user, [])

response.assertStatus(403)
assert.properties(response.body(), ['code', 'status', 'message'])
assert.equal(response.body().code, 'E_AUTHORIZATION_FAILURE')
assert.equal(response.body().status, 403)
})

test('should return 404 if the player was not found', async ({ assert, client }) => {
const user = await UserFactory.make()

const response = await client.put(`/v1/players/1`).loginAs(user, ['update-player'])

assert.properties(response.body(), ['code', 'status', 'message'])
assert.equal(response.body().code, 'E_ROW_NOT_FOUND')
assert.equal(response.body().status, 404)

response.assertStatus(404)
})

test('should return 200 if the user was connected and authorized', async ({ assert, client }) => {
const user = await UserFactory.make()
const player = await PlayerFactory.make()

const response = await client
.put(`/v1/players/${player.id}`)
.json({
level: 2,
coins: 1000,
})
.loginAs(user, ['update-player'])

response.assertStatus(200)

assert.properties(response.body(), ['id', 'coins', 'level', 'playerUuid'])
assert.equal(response.body().level, 2)
assert.equal(response.body().coins, 1000)
})
})

0 comments on commit a6a9ab0

Please sign in to comment.