GetAllGroupsApiTest.php 2.07 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
8
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
<?php


namespace Tests\Feature\Api\Groups;

use Source\Entities\Group;
use Source\Gateways\Groups\GroupsRepository;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Foundation\Testing\TestResponse;
use Tests\Feature\AuthenticatesWithApplicationTestCase;

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

    /**
     * @var mixed|\Source\Gateways\Groups\GroupsRepository
     */
    protected GroupsRepository $groups;

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

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

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

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

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

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

        $this->handleTest();

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

    /**
     * @test
     * @throws EntityNotFoundException
     */
    public function it_returns_empty_when_no_groups_are_added(): void
    {
        $this->authenticate();
        $this->handleTest();

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

    /**
     * @test
     * @throws EntityNotFoundException
     */
    public function it_returns_groups_in_the_repository(): void
    {
        $this->authenticate();
        $this->groups->create(new Group(69, '', ''));
        $this->groups->create(new Group(420, '', ''));

        $this->handleTest();

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