GetUsersForGroupApiTest.php 1.82 KB
Newer Older
1
2
3
4
5
6
7
<?php


namespace Tests\Feature\Api\Groups;

use Source\Entities\User;
use Source\Entities\Group;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Illuminate\Testing\TestResponse;
Jacob Priddy's avatar
Jacob Priddy committed
9
use Source\Gateways\Groups\GroupsRepository;
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
use Source\Gateways\GroupUser\GroupUserRepository;
use Tests\Feature\AuthenticatesWithApplicationTestCase;

class GetUsersForGroupApiTest extends AuthenticatesWithApplicationTestCase
{
    protected TestResponse $response;

    protected GroupsRepository $groups;

    protected GroupUserRepository $repository;

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

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

    public function handleTest(string $groupId): void
    {
        $this->response = $this->getJson("/groups/$groupId/users?api_token={$this->authToken}");
    }

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

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

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

        $this->handleTest('uid');

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

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

Jacob Priddy's avatar
Jacob Priddy committed
65
        $user = $this->usersRepository->create(new User(0, '', '', '', '', ''));
66
67
68
69
70
71
72
73
74
75
76
        $group = $this->groups->create(new Group(0, '', ''));

        $this->repository->addUserToGroup($user->getId(), $group->getId());

        $this->handleTest($group->getId());

        $this->response->assertStatus(200);

        $this->assertCount(1, $this->response->json('users'));
    }
}