aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/ConsultationsBookingCreateTest.php
blob: d0af9dad82341e45d709c11d691f4a23626dad9a (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
use JsonApi\Routes\Consultations\BookingsCreate;
use JsonApi\Schemas\ConsultationBooking as Schema;
use JsonApi\Schemas\User as UserSchema;
use JsonApi\Schemas\ConsultationSlot as SlotSchema;
use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse;

require_once __DIR__ . '/ConsultationHelper.php';

// TODO: Test locked blocks
class ConsultationsBookingCreateTest extends Codeception\Test\Unit
{
    use ConsultationHelper;

    public function testAutorMayCreateBooking(): void
    {
        $credentials = $this->tester->getCredentialsForTestDozent();
        $range = User::find($credentials['id']);

        $block = $this->createBlockWithSlotsForRange($range);
        $slot = $this->getSlotFromBlock($block);

        $this->createBooking(
            $credentials,
            $slot,
            $this->tester->getCredentialsForTestAutor()['id'],
            [201]
        );
    }

    public function testAutorMayNotCreateBookingDueToLock(): void
    {
        $credentials = $this->tester->getCredentialsForTestDozent();
        $range = User::find($credentials['id']);

        $block = $this->createBlockWithSlotsForRange($range, true);
        $slot = $this->getSlotFromBlock($block);

        $response = $this->createBooking(
            $credentials,
            $slot,
            $this->tester->getCredentialsForTestAutor()['id'],
            null
        );

        $this->assertEquals(409, $response->getStatusCode());
    }

    public function testSlotIsOccupied(): void
    {
        $credentials = $this->tester->getCredentialsForTestDozent();
        $range = User::find($credentials['id']);

        $block = $this->createBlockWithSlotsForRange($range);
        $slot = $this->getSlotFromBlock($block);

        $this->createBooking(
            $credentials,
            $slot,
            $this->tester->getCredentialsForTestAutor()['id'],
            [201]
        );

        $response = $this->createBooking(
            $credentials,
            $slot,
            $this->tester->getCredentialsForTestAutor()['id'],
            null
        );

        $this->assertEquals(409, $response->getStatusCode());
    }

    public function testRootMayNotCreateBooking(): void
    {
        $credentials = $this->tester->getCredentialsForTestDozent();
        $range = User::find($credentials['id']);

        $block = $this->createBlockWithSlotsForRange($range);
        $slot = $this->getSlotFromBlock($block);

        $response = $this->createBooking(
            $credentials,
            $slot,
            $this->tester->getCredentialsForRoot()['id'],
            null
        );

        $this->assertEquals(403, $response->getStatusCode());
    }

    private function createBooking(array $credentials, ConsultationSlot $slot, string $user_id, ?array $considered_succssfull): JsonApiResponse
    {
        return $this->sendMockRequest(
            '/consultation-bookings',
            BookingsCreate::class,
            $credentials,
            [],
            [
                'considered_successful' => $considered_succssfull,
                'method' => 'POST',
                'json_body' => [
                    'data' => [
                        'type' => Schema::TYPE,
                        'attributes' => [
                            'reason' => self::$BOOKING_DATA['reason'],
                        ],
                        'relationships' => [
                            Schema::REL_SLOT => [
                                'data' => [
                                    'type' => SlotSchema::TYPE,
                                    'id' => $slot->id,
                                ],
                            ],
                            Schema::REL_USER => [
                                'data' => [
                                    'type' => UserSchema::TYPE,
                                    'id' => $user_id,
                                ],
                            ],
                        ]
                    ]
                ]
            ]
        );
    }
}