-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: nathaelbonnal <[email protected]>
- Loading branch information
Showing
4 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |