Skip to content

Commit

Permalink
test(Events): add coverage for API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
quetzyg committed Apr 23, 2019
1 parent dc91472 commit 9289b08
Show file tree
Hide file tree
Showing 6 changed files with 1,159 additions and 0 deletions.
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<directory suffix="Test.php">./tests/Integration/Controllers/DistrictController</directory>
</testsuite>

<testsuite name="EventController Integration Tests">
<directory suffix="Test.php">./tests/Integration/Controllers/EventController</directory>
</testsuite>

<testsuite name="ParishController Integration Tests">
<directory suffix="Test.php">./tests/Integration/Controllers/ParishController</directory>
</testsuite>
Expand Down
287 changes: 287 additions & 0 deletions tests/Integration/Controllers/EventController/CreateEndpointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
<?php

declare(strict_types=1);

namespace VOSTPT\Tests\Integration\Controllers\EventController;

use Illuminate\Foundation\Testing\RefreshDatabase;
use VOSTPT\Models\EventType;
use VOSTPT\Models\Parish;
use VOSTPT\Models\Role;
use VOSTPT\Models\User;
use VOSTPT\Tests\Integration\TestCase;

class CreateEndpointTest extends TestCase
{
use RefreshDatabase;

/**
* @test
*/
public function itFailsToCreateEventDueToInvalidContentTypeHeader(): void
{
$response = $this->json('POST', route('events::create'));

$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertStatus(415);
$response->assertJson([
'errors' => [
[
'status' => 415,
'detail' => 'Wrong media type',
],
],
]);
}

/**
* @test
*/
public function itFailsTCreateEventDueToMissingJwtToken(): void
{
$response = $this->json('POST', route('events::create'), [], [
'Content-Type' => 'application/vnd.api+json',
]);

$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertStatus(401);
$response->assertJson([
'errors' => [
[
'status' => 401,
'detail' => 'Token not provided',
],
],
]);
}

/**
* @test
*/
public function itFailsToCreateEventDueToValidation(): void
{
$user = factory(User::class)->create()->assign(Role::ADMINISTRATOR);

$token = auth()->login($user);

$response = $this->json('POST', route('events::create'), [
'type' => 1,
'parish' => 1,
'name' => \str_repeat('Incêndio florestal de Pedrógão Grande', 10),
'latitude' => '39 north',
'longitude' => '8 west',
'started_at' => '2017-06-24 18:54:00',
'ended_at' => '2017-06-17 16:51:00',
], [
'Content-Type' => 'application/vnd.api+json',
'Authorization' => \sprintf('Bearer %s', $token),
]);

$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertStatus(422);
$response->assertJson([
'errors' => [
[
'detail' => 'The name may not be greater than 255 characters.',
'meta' => [
'field' => 'name',
],
],
[
'detail' => 'The description field is required.',
'meta' => [
'field' => 'description',
],
],
[
'detail' => 'The latitude must be a number.',
'meta' => [
'field' => 'latitude',
],
],
[
'detail' => 'The longitude must be a number.',
'meta' => [
'field' => 'longitude',
],
],
[
'detail' => 'The started at must be a date before or equal to ended at.',
'meta' => [
'field' => 'started_at',
],
],
[
'detail' => 'The ended at must be a date after or equal to started at.',
'meta' => [
'field' => 'ended_at',
],
],
[
'detail' => 'The selected type is invalid.',
'meta' => [
'field' => 'type',
],
],
[
'detail' => 'The selected parish is invalid.',
'meta' => [
'field' => 'parish',
],
],
],
]);
}

/**
* @test
*/
public function itSuccessfullyCreatesEvent(): void
{
$user = factory(User::class)->create()->assign(Role::ADMINISTRATOR);

$eventType = factory(EventType::class)->create();
$parish = factory(Parish::class)->create();

$this->assertDatabaseMissing('events', [
'type_id' => $eventType->getKey(),
'parish_id' => $parish->getKey(),
'name' => 'Incêndio florestal de Pedrógão Grande',
'description' => 'O incêndio florestal de Pedrógão Grande, deflagrou a 17 de junho de 2017 no concelho de Pedrógão Grande, no distrito de Leiria.',
'latitude' => 39.954211,
'longitude' => -8.233333,
'started_at' => '2017-06-17 16:51:00',
'ended_at' => '2017-06-24 18:54:00',
]);

$token = auth()->login($user);

$response = $this->json('POST', route('events::create'), [
'type' => $eventType->getKey(),
'parish' => $parish->getKey(),
'name' => 'Incêndio florestal de Pedrógão Grande',
'description' => 'O incêndio florestal de Pedrógão Grande, deflagrou a 17 de junho de 2017 no concelho de Pedrógão Grande, no distrito de Leiria.',
'latitude' => 39.954211,
'longitude' => -8.233333,
'started_at' => '2017-06-17 16:51:00',
'ended_at' => '2017-06-24 18:54:00',
], [
'Content-Type' => 'application/vnd.api+json',
'Authorization' => \sprintf('Bearer %s', $token),
]);

$this->assertDatabaseHas('events', [
'type_id' => $eventType->getKey(),
'parish_id' => $parish->getKey(),
'name' => 'Incêndio florestal de Pedrógão Grande',
'description' => 'O incêndio florestal de Pedrógão Grande, deflagrou a 17 de junho de 2017 no concelho de Pedrógão Grande, no distrito de Leiria.',
'latitude' => 39.954211,
'longitude' => -8.233333,
'started_at' => '2017-06-17 16:51:00',
'ended_at' => '2017-06-24 18:54:00',
]);

$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertStatus(201);
$response->assertJsonStructure([
'data' => [
'type',
'id',
'attributes' => [
'name',
'description',
'latitude',
'longitude',
'started_at',
'ended_at',
'created_at',
'updated_at',
],
'relationships' => [
'type' => [
'data' => [
'type',
'id',
],
],
'parish' => [
'data' => [
'type',
'id',
],
],
],
'links' => [
'self',
],
],
'included' => [
[
'type',
'id',
'attributes' => [
'name',
'created_at',
'updated_at',
],
],
[
'type',
'id',
'attributes' => [
'code',
'name',
'created_at',
'updated_at',
],
'links' => [
'self',
],
],
],
]);
}

/**
* @test
* @dataProvider createDataProvider
*
* @param string $role
* @param int $status
*/
public function itVerifiesRoleAccessPermissionsToCreateEvent(string $role, int $status): void
{
$user = factory(User::class)->create()->assign($role);

$token = auth()->login($user);

$response = $this->json('POST', route('events::create'), [], [
'Content-Type' => 'application/vnd.api+json',
'Authorization' => \sprintf('Bearer %s', $token),
]);

$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertStatus($status);
}

/**
* @return array
*/
public function createDataProvider(): array
{
return [
'Administrator' => [
Role::ADMINISTRATOR,
422,
],
'Moderator' => [
Role::MODERATOR,
403,
],
'Contributor' => [
Role::CONTRIBUTOR,
403,
],
];
}
}
Loading

0 comments on commit 9289b08

Please sign in to comment.