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


namespace Tests\Feature\Api\Users;

use Source\Entities\User;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Source\Entities\Doorcode;
8
use Source\Gateways\Users\UsersRepository;
Jacob Priddy's avatar
Jacob Priddy committed
9
use Source\Exceptions\EntityNotFoundException;
10
use Tests\Doubles\InMemoryUsersRepositoryStub;
Jacob Priddy's avatar
Jacob Priddy committed
11
use Illuminate\Foundation\Testing\TestResponse;
Jacob Priddy's avatar
Jacob Priddy committed
12
use Tests\Feature\AuthenticatesWithApplicationTestCase;
Jacob Priddy's avatar
Jacob Priddy committed
13

Jacob Priddy's avatar
Jacob Priddy committed
14
15
class UpdateUserApiTest extends AuthenticatesWithApplicationTestCase
{
Jacob Priddy's avatar
Jacob Priddy committed
16
17
18
19
20
21
    protected TestResponse $response;

    /**
     * @param string $userId
     * @param array  $data
     */
Jacob Priddy's avatar
Jacob Priddy committed
22
23
    protected function handleTest(string $userId, array $data): void
    {
24
        $this->response = $this->patchJson(
Jacob Priddy's avatar
Jacob Priddy committed
25
26
27
            '/users/' . $userId,
            array_merge(['api_token' => $this->authToken], $data)
        );
Jacob Priddy's avatar
Jacob Priddy committed
28
29
    }

Jacob Priddy's avatar
Jacob Priddy committed
30
31
32
    /**
     * @test
     */
Jacob Priddy's avatar
Jacob Priddy committed
33
34
    public function it_denies_unauthorized(): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
35
36
37
38
        $this->handleTest('radlfj', []);

        $this->response->assertStatus(401);
    }
Jacob Priddy's avatar
Jacob Priddy committed
39

Jacob Priddy's avatar
Jacob Priddy committed
40
41
42
43
44
45
46
47
48
49
50
51
52
    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_protects_the_route(): void
    {
        $this->authorize(false);

        $this->handleTest('user', []);

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

Jacob Priddy's avatar
Jacob Priddy committed
53
54
    public function invalidUserProvider(): array
    {
Jacob Priddy's avatar
Jacob Priddy committed
55
56
57
58
        return [
            // emplid is too small
            [
                [
59
                    'emplid' => 'small',
Jacob Priddy's avatar
Jacob Priddy committed
60
61
62
63
64
65
66
                ],
                ['emplid' => ['The emplid must be at least 6 characters.']],
            ],

            // emplid is too large
            [
                [
67
                    'emplid' => 'waaaaaaaaaaay to large',
Jacob Priddy's avatar
Jacob Priddy committed
68
69
70
71
72
73
74
                ],
                ['emplid' => ['The emplid may not be greater than 7 characters.']],
            ],

            // expires at isnt a date
            [
                [
75
                    'expires_at' => 'bad date',
Jacob Priddy's avatar
Jacob Priddy committed
76
77
78
79
80
81
82
                ],
                ['expires_at' => ['The expires at is not a valid date.']],
            ],

            // doorcode isnt a number
            [
                [
83
                    'doorcode' => 'not a number',
Jacob Priddy's avatar
Jacob Priddy committed
84
85
86
87
88
89
90
                ],
                ['doorcode' => ['The doorcode must be a number.', 'The doorcode must be between 4 and 255 digits.']],
            ],

            // doorcode is too small
            [
                [
91
                    'doorcode' => '000',
Jacob Priddy's avatar
Jacob Priddy committed
92
93
94
95
96
97
98
                ],
                ['doorcode' => ['The doorcode must be between 4 and 255 digits.']],
            ],

            // email invalid
            [
                [
99
                    'email' => 'not an email',
Jacob Priddy's avatar
Jacob Priddy committed
100
101
102
103
104
105
106
107
108
109
110
                ],
                ['email' => ['The email must be a valid email address.']],
            ],
        ];
    }

    /**
     * @test
     * @param array $data
     * @param array $message
     * @dataProvider invalidUserProvider
Jacob Priddy's avatar
Jacob Priddy committed
111
     * @throws EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
112
     */
Jacob Priddy's avatar
Jacob Priddy committed
113
114
    public function it_tests_the_validation_rules(array $data, array $message): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
115
        $this->authenticate();
Jacob Priddy's avatar
Jacob Priddy committed
116
117
118
119
        $this->handleTest('69', $data);
        $this->response->assertJsonFragment($message);
    }

Jacob Priddy's avatar
Jacob Priddy committed
120
121
122
123
    /**
     * @test
     * @throws EntityNotFoundException
     */
Jacob Priddy's avatar
Jacob Priddy committed
124
125
    public function it_updates_a_user(): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
126
        $user = new User(69, '', '', '', '');
Jacob Priddy's avatar
Jacob Priddy committed
127
128
        $this->usersRepository->create($user);

Jacob Priddy's avatar
Jacob Priddy committed
129
        $this->authenticate();
Jacob Priddy's avatar
Jacob Priddy committed
130

Jacob Priddy's avatar
Jacob Priddy committed
131
132
133
        $this->handleTest(
            '69',
            [
134
135
                'first_name' => 'first name',
                'last_name' => 'last name',
Jacob Priddy's avatar
Jacob Priddy committed
136
                'display_name' => 'display name',
137
138
                'email' => 'email@imsad.dumb',
                'doorcode' => '1234',
Jacob Priddy's avatar
Jacob Priddy committed
139
140
141
142
143
144
            ]
        );

        $this->response->assertStatus(200);
        $this->response->assertJsonFragment(
            [
145
146
                'first_name' => 'first name',
                'last_name' => 'last name',
Jacob Priddy's avatar
Jacob Priddy committed
147
                'display_name' => 'display name',
148
149
                'emplid' => null,
                'email' => 'email@imsad.dumb',
Jacob Priddy's avatar
Jacob Priddy committed
150
151
152
153
154
            ]
        );
        $this->assertEquals($this->usersRepository->get('69')->getFirstName(), 'first name');
    }

Jacob Priddy's avatar
Jacob Priddy committed
155
156
157
158
    /**
     * @test
     * @throws EntityNotFoundException
     */
159
    public function it_cannot_update_non_existent_user(): void
Jacob Priddy's avatar
Jacob Priddy committed
160
    {
Jacob Priddy's avatar
Jacob Priddy committed
161
        $this->authenticate();
162
163
164
165
166
167
168
        $this->handleTest('asdf', [
            'first_name' => 'first name',
            'last_name' => 'last name',
            'display_name' => 'display name',
            'email' => 'email@imsad.dumb',
            'doorcode' => '1234',
        ]);
Jacob Priddy's avatar
Jacob Priddy committed
169
170
171

        $this->response->assertStatus(404);
    }
172
173
174
175
176
177
178
179

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_presents_error(): void
    {
        $users = new InMemoryUsersRepositoryStub();
Jacob Priddy's avatar
Jacob Priddy committed
180
        $users->setUserToReturnOnGet(new User(4, '', '', '', ''));
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        $this->app->bind(UsersRepository::class, static function () use (&$users) {
            return $users;
        });

        $this->authenticate();

        $this->handleTest('asdf', [
            'first_name' => 'first name',
            'last_name' => 'last name',
            'display_name' => 'display name',
            'email' => 'email@imsad.dumb',
            'doorcode' => '1234',
        ]);

        $this->response->assertStatus(200);
        $this->response->assertJson(['status' => 'error', 'message' => 'Unable to update user.']);
    }
Jacob Priddy's avatar
Jacob Priddy committed
198
}