GetGroupApiTest.php 2.08 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
<?php


namespace Tests\Feature\Api\Groups;

use Source\Entities\Group;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Illuminate\Testing\TestResponse;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Gateways\Groups\GroupsRepository;
Jacob Priddy's avatar
Jacob Priddy committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use Source\Gateways\Groups\InMemoryGroupsRepository;
use Tests\Feature\AuthenticatesWithApplicationTestCase;

class GetGroupApiTest extends AuthenticatesWithApplicationTestCase
{
    /**
     * @var \Illuminate\Foundation\Testing\TestResponse
     */
    protected TestResponse $response;

    protected InMemoryGroupsRepository $groups;

    public function setUp(): void
    {
        parent::setUp();

        $this->groups = $this->app->make(GroupsRepository::class);
    }

    protected function handleTest(string $groupId): void
    {
        $this->response = $this->getJson('/groups/' . $groupId . '?api_token=' . $this->authToken);
    }

    /**
     * @test
     */
    public function it_denies_unauthorized(): void
    {
        $this->handleTest('asdf');

        $this->response->assertStatus(401);
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_protects_the_route(): void
    {
        $this->authorize(false);

        $this->handleTest('asdf');

        $this->response->assertStatus(403);
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_returns_404_on_non_existent_group(): void
    {
        $this->authenticate();

        $this->handleTest('asdfasdfasdfasd');

        $this->response->assertStatus(404);
        $this->response->assertExactJson(
            [
                'status'  => 'error',
                'code'    => 404,
                'message' => 'Entity not found',
            ]
        );
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_returns_a_group(): void
    {
        $this->authenticate();

        $group = new Group(69, '', '');
        $this->groups->create($group);

        $this->handleTest('69');

        $this->response->assertStatus(200);
        $this->response->assertJson(['status' => 'success']);
    }
}