|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Flarum. |
| 5 | + * |
| 6 | + * For detailed copyright and license information, please view the |
| 7 | + * LICENSE file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Flarum\Suspend\Tests\integration\api\users; |
| 11 | + |
| 12 | +use Carbon\Carbon; |
| 13 | +use Flarum\Testing\integration\RetrievesAuthorizedUsers; |
| 14 | +use Flarum\Testing\integration\TestCase; |
| 15 | +use Psr\Http\Message\ResponseInterface; |
| 16 | + |
| 17 | +class RemoveAvatarTest extends TestCase |
| 18 | +{ |
| 19 | + use RetrievesAuthorizedUsers; |
| 20 | + |
| 21 | + public function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $this->extension('flarum-suspend'); |
| 26 | + |
| 27 | + $this->prepareDatabase([ |
| 28 | + 'users' => [ |
| 29 | + ['id' => 1, 'username' => 'Muralf', 'email' => 'muralf@machine.local', 'is_email_confirmed' => 1], |
| 30 | + $this->normalUser(), |
| 31 | + ['id' => 3, 'username' => 'acme', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1, 'suspended_until' => Carbon::now()->addDay(), 'suspend_message' => 'You have been suspended.', 'suspend_reason' => 'Suspended for acme reasons.'], |
| 32 | + ['id' => 4, 'username' => 'acme4', 'email' => 'acme4@machine.local', 'is_email_confirmed' => 1], |
| 33 | + ['id' => 5, 'username' => 'acme5', 'email' => 'acme5@machine.local', 'is_email_confirmed' => 1, 'suspended_until' => Carbon::now()->subDay(), 'suspend_message' => 'You have been suspended.', 'suspend_reason' => 'Suspended for acme reasons.'], |
| 34 | + ], |
| 35 | + 'groups' => [ |
| 36 | + ['id' => 5, 'name_singular' => 'can_edit_users', 'name_plural' => 'can_edit_users', 'is_hidden' => 0] |
| 37 | + ], |
| 38 | + 'group_user' => [ |
| 39 | + ['user_id' => 2, 'group_id' => 5] |
| 40 | + ], |
| 41 | + 'group_permission' => [ |
| 42 | + ['permission' => 'user.edit', 'group_id' => 5], |
| 43 | + ] |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @test |
| 49 | + * @dataProvider allowedToRemoveAvatar |
| 50 | + */ |
| 51 | + public function can_remove_avatar_if_allowed(?int $authenticatedAs, int $targetUserId) |
| 52 | + { |
| 53 | + $response = $this->sendRemoveAvatarRequest($authenticatedAs, $targetUserId); |
| 54 | + |
| 55 | + $this->assertEquals(200, $response->getStatusCode(), $response->getBody()->getContents()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @test |
| 60 | + * @dataProvider notAllowedToRemoveAvatar |
| 61 | + */ |
| 62 | + public function cannot_remove_avatar_if_not_allowed(?int $authenticatedAs, int $targetUserId) |
| 63 | + { |
| 64 | + $response = $this->sendRemoveAvatarRequest($authenticatedAs, $targetUserId); |
| 65 | + |
| 66 | + $this->assertEquals(403, $response->getStatusCode(), $response->getBody()->getContents()); |
| 67 | + } |
| 68 | + |
| 69 | + public function allowedToRemoveAvatar(): array |
| 70 | + { |
| 71 | + return [ |
| 72 | + [1, 4, 'Admin can remove avatar of normal user'], |
| 73 | + [4, 4, 'Normal user can remove their own avatar'], |
| 74 | + [1, 3, 'Admin can remove avatar of suspended user'], |
| 75 | + [2, 3, 'Normal user with permission can remove avatar of suspended user'], |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + public function notAllowedToRemoveAvatar(): array |
| 80 | + { |
| 81 | + return [ |
| 82 | + [4, 2, 'Normal user cannot remove avatar of another user'], |
| 83 | + [4, 3, 'Normal user cannot remove avatar of suspended user'], |
| 84 | + [3, 3, 'Suspended user cannot remove their own avatar'], |
| 85 | + ]; |
| 86 | + } |
| 87 | + |
| 88 | + protected function sendRemoveAvatarRequest(?int $authenticatedAs, int $targetUserId): ResponseInterface |
| 89 | + { |
| 90 | + return $this->send( |
| 91 | + $this->request('DELETE', "/api/users/$targetUserId/avatar", [ |
| 92 | + 'authenticatedAs' => $authenticatedAs, |
| 93 | + ]) |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments