AddUserToGroupApiTest.php 1.95 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
<?php


namespace Tests\Feature\Api\Users;

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;
Jacob Priddy's avatar
Jacob Priddy committed
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
use Source\Gateways\GroupUser\GroupUserRepository;
use Tests\Feature\AuthenticatesWithApplicationTestCase;

class AddUserToGroupApiTest 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 $userId, string $groupId): void
    {
        $this->response = $this->postJson(
            "/users/$userId/group/$groupId",
            ['api_token' => $this->authToken]
        );
    }

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

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

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

        $this->handleTest('uid', 'gid');

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

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

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

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

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

        $this->response->assertJson(['message' => 'Success']);
        $this->assertEquals($group->getId(), $this->repository->getGroupsForUser($user->getId())[0]->getId());
    }
}