111 lines
3.2 KiB
PHP
111 lines
3.2 KiB
PHP
<?php
|
|
|
|
use App\Events\User\UpdatedByAdmin;
|
|
use App\Models\User;
|
|
use Facades\Tests\Setup\UserFactory;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Tests\Feature\ApiTestCase;
|
|
|
|
test('upload user avatar unauthenticated', function () {
|
|
$user = User::factory()->create();
|
|
|
|
$this->post("/api/users/{$user->id}/avatar")
|
|
->assertStatus(401);
|
|
});
|
|
|
|
test('upload avatar without permission', function () {
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAs($user, ApiTestCase::API_GUARD)
|
|
->post("/api/users/{$user->id}/avatar")
|
|
->assertForbidden();
|
|
});
|
|
|
|
test('upload avatar image', function () {
|
|
Event::fake([UpdatedByAdmin::class]);
|
|
|
|
Storage::fake('public');
|
|
|
|
$user = UserFactory::withPermissions('users.manage')->create();
|
|
|
|
$file = UploadedFile::fake()->image('avatar.png', 500, 500);
|
|
|
|
$response = $this->actingAs($user, ApiTestCase::API_GUARD)
|
|
->post("/api/users/{$user->id}/avatar", ['file' => $file])
|
|
->assertOk();
|
|
|
|
$avatar = $response->json('data.avatar');
|
|
$this->assertNotNull($avatar);
|
|
|
|
$uploadedFile = str_replace(url(''), '', $avatar);
|
|
$uploadedFile = ltrim($uploadedFile, '/storage/');
|
|
|
|
Storage::disk('public')->assertExists($uploadedFile);
|
|
|
|
[$width, $height] = getimagesizefromstring(
|
|
Storage::disk('public')->get($uploadedFile)
|
|
);
|
|
|
|
$this->assertEquals(160, $width);
|
|
$this->assertEquals(160, $height);
|
|
|
|
Event::assertDispatched(UpdatedByAdmin::class);
|
|
});
|
|
|
|
test('upload invalid image', function () {
|
|
$user = UserFactory::withPermissions('users.manage')->create();
|
|
|
|
Storage::fake('public');
|
|
|
|
$file = UploadedFile::fake()->create('avatar.txt', 500);
|
|
|
|
$this->actingAs($user, ApiTestCase::API_GUARD)
|
|
->post("/api/users/{$user->id}/avatar", ['file' => $file])
|
|
->assertStatus(422)
|
|
->assertJsonFragment([
|
|
'file' => [
|
|
trans('validation.image', ['attribute' => 'file']),
|
|
],
|
|
]);
|
|
});
|
|
|
|
test('update avatar from external source', function () {
|
|
Event::fake([UpdatedByAdmin::class]);
|
|
|
|
$user = UserFactory::withPermissions('users.manage')->create();
|
|
|
|
$url = 'http://google.com';
|
|
|
|
$this->actingAs($user, ApiTestCase::API_GUARD)
|
|
->putJson("/api/users/{$user->id}/avatar/external", ['url' => $url])
|
|
->assertOk()
|
|
->assertJsonFragment(['avatar' => $url]);
|
|
|
|
Event::assertDispatched(UpdatedByAdmin::class);
|
|
});
|
|
|
|
test('update avatar with invalid external source', function () {
|
|
$user = UserFactory::withPermissions('users.manage')->create();
|
|
|
|
$this->actingAs($user, ApiTestCase::API_GUARD)
|
|
->putJson("/api/users/{$user->id}/avatar/external", ['url' => 'foo'])
|
|
->assertStatus(422);
|
|
});
|
|
|
|
test('delete user avatar', function () {
|
|
Event::fake([UpdatedByAdmin::class]);
|
|
|
|
$user = UserFactory::withPermissions('users.manage')->create();
|
|
|
|
$user->forceFill(['avatar' => 'http://google.com'])->save();
|
|
|
|
$this->actingAs($user, ApiTestCase::API_GUARD)
|
|
->deleteJson("api/users/{$user->id}/avatar")
|
|
->assertOk()
|
|
->assertJsonFragment([
|
|
'avatar' => url('assets/img/profile.png'), // default profile image
|
|
]);
|
|
|
|
Event::assertDispatched(UpdatedByAdmin::class);
|
|
});
|