|
| 1 | +<?php |
| 2 | + |
| 3 | +use CleaniqueCoders\LaravelMediaSecure\Http\Middleware\ValidateMediaAccess; |
| 4 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Illuminate\Support\Facades\Artisan; |
| 7 | +use Illuminate\Support\Facades\Gate; |
| 8 | +use Illuminate\Support\Facades\Route; |
| 9 | +use Illuminate\Support\Facades\Schema; |
| 10 | +use Spatie\MediaLibrary\MediaCollections\Models\Media; |
| 11 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 12 | + |
| 13 | +uses(RefreshDatabase::class); |
| 14 | + |
| 15 | +// Set up a login route for redirects and migrations |
| 16 | +beforeEach(function () { |
| 17 | + // Set up migrations if they don't exist |
| 18 | + if (! Schema::hasTable('users')) { |
| 19 | + Artisan::call('migrate', [ |
| 20 | + '--path' => '../../../../tests/database/migrations/', |
| 21 | + ]); |
| 22 | + } |
| 23 | + |
| 24 | + Route::get('/login', fn () => 'login')->name('login'); |
| 25 | +}); |
| 26 | + |
| 27 | +it('validates access type in the middleware', function () { |
| 28 | + // Set up test route with middleware |
| 29 | + Route::get('/test-media/{type}/{uuid}', function (Request $request, $type, $uuid) { |
| 30 | + return 'passed'; |
| 31 | + })->middleware(ValidateMediaAccess::class); |
| 32 | + |
| 33 | + // Remove exception handling to see the actual exceptions |
| 34 | + $this->withoutExceptionHandling(); |
| 35 | + |
| 36 | + // Create a real user for testing |
| 37 | + $user = user(); |
| 38 | + login($user); |
| 39 | + |
| 40 | + // Test with invalid type - should throw an exception |
| 41 | + $this->expectException(HttpException::class); |
| 42 | + $this->expectExceptionMessage('Invalid request type of invalid-type'); |
| 43 | + |
| 44 | + $this->get('/test-media/invalid-type/123-uuid'); |
| 45 | +})->group('middleware'); |
| 46 | + |
| 47 | +it('checks authorization in the middleware', function () { |
| 48 | + // Set up test route with middleware |
| 49 | + Route::get('/test-media/{type}/{uuid}', function (Request $request, $type, $uuid) { |
| 50 | + return 'passed'; |
| 51 | + })->middleware(ValidateMediaAccess::class); |
| 52 | + |
| 53 | + // Remove exception handling |
| 54 | + $this->withoutExceptionHandling(); |
| 55 | + |
| 56 | + // Create a real user for testing |
| 57 | + $user = user(); |
| 58 | + login($user); |
| 59 | + |
| 60 | + // Create a fake media UUID that doesn't exist |
| 61 | + $fakeUuid = 'fake-uuid-123'; |
| 62 | + |
| 63 | + // Should throw model not found exception because media doesn't exist |
| 64 | + $this->expectException(\Illuminate\Database\Eloquent\ModelNotFoundException::class); |
| 65 | + |
| 66 | + $this->get('/test-media/view/'.$fakeUuid); |
| 67 | +})->group('middleware'); |
| 68 | + |
| 69 | +it('adds media to the request attributes', function () { |
| 70 | + // Create test route that checks for media in request attributes |
| 71 | + Route::get('/test-media-attributes/{type}/{uuid}', function (Request $request, $type, $uuid) { |
| 72 | + return $request->attributes->has('media') ? 'has media' : 'no media'; |
| 73 | + })->middleware(ValidateMediaAccess::class); |
| 74 | + |
| 75 | + // Create a real user for testing |
| 76 | + $user = user(); |
| 77 | + login($user); |
| 78 | + |
| 79 | + // Create a real media record in the database |
| 80 | + $media = new \Spatie\MediaLibrary\MediaCollections\Models\Media([ |
| 81 | + 'model_type' => get_class($user), |
| 82 | + 'model_id' => $user->id, |
| 83 | + 'uuid' => 'test-uuid-123', |
| 84 | + 'collection_name' => 'default', |
| 85 | + 'name' => 'test-file', |
| 86 | + 'file_name' => 'test.txt', |
| 87 | + 'mime_type' => 'text/plain', |
| 88 | + 'disk' => 'public', |
| 89 | + 'conversions_disk' => 'public', |
| 90 | + 'size' => 100, |
| 91 | + 'manipulations' => '[]', |
| 92 | + 'custom_properties' => '{}', |
| 93 | + 'generated_conversions' => '{}', |
| 94 | + 'responsive_images' => '{}', |
| 95 | + ]); |
| 96 | + $media->save(); |
| 97 | + |
| 98 | + // Mock the Gate to allow access |
| 99 | + Gate::shouldReceive('forUser')->andReturnSelf(); |
| 100 | + Gate::shouldReceive('check')->andReturn(true); |
| 101 | + |
| 102 | + // Test that media was added to the request attributes |
| 103 | + $this->get('/test-media-attributes/view/'.$media->uuid) |
| 104 | + ->assertSuccessful() |
| 105 | + ->assertSee('has media'); |
| 106 | +})->group('middleware'); |
| 107 | + |
| 108 | +// Test for each media type |
| 109 | +it('handles view media type correctly', function () { |
| 110 | + // Set up test route with middleware |
| 111 | + Route::get('/test-media/{type}/{uuid}', function (Request $request, $type, $uuid) { |
| 112 | + // Check that the type is correctly passed |
| 113 | + return 'Type: '.$type; |
| 114 | + })->middleware(ValidateMediaAccess::class); |
| 115 | + |
| 116 | + // Create a real user for testing |
| 117 | + $user = user(); |
| 118 | + login($user); |
| 119 | + |
| 120 | + // Create a real media record in the database |
| 121 | + $media = new \Spatie\MediaLibrary\MediaCollections\Models\Media([ |
| 122 | + 'model_type' => get_class($user), |
| 123 | + 'model_id' => $user->id, |
| 124 | + 'uuid' => 'test-uuid-view', |
| 125 | + 'collection_name' => 'default', |
| 126 | + 'name' => 'test-file', |
| 127 | + 'file_name' => 'test.txt', |
| 128 | + 'mime_type' => 'text/plain', |
| 129 | + 'disk' => 'public', |
| 130 | + 'conversions_disk' => 'public', |
| 131 | + 'size' => 100, |
| 132 | + 'manipulations' => '[]', |
| 133 | + 'custom_properties' => '{}', |
| 134 | + 'generated_conversions' => '{}', |
| 135 | + 'responsive_images' => '{}', |
| 136 | + ]); |
| 137 | + $media->save(); |
| 138 | + |
| 139 | + // Mock the Gate to allow access |
| 140 | + Gate::shouldReceive('forUser')->andReturnSelf(); |
| 141 | + Gate::shouldReceive('check')->andReturn(true); |
| 142 | + |
| 143 | + $this->get('/test-media/view/'.$media->uuid) |
| 144 | + ->assertSuccessful() |
| 145 | + ->assertSee('Type: view'); |
| 146 | +})->group('middleware'); |
| 147 | + |
| 148 | +it('handles download media type correctly', function () { |
| 149 | + // Set up test route with middleware |
| 150 | + Route::get('/test-media/{type}/{uuid}', function (Request $request, $type, $uuid) { |
| 151 | + // Check that the type is correctly passed |
| 152 | + return 'Type: '.$type; |
| 153 | + })->middleware(ValidateMediaAccess::class); |
| 154 | + |
| 155 | + // Create a real user for testing |
| 156 | + $user = user(); |
| 157 | + login($user); |
| 158 | + |
| 159 | + // Create a real media record in the database |
| 160 | + $media = new \Spatie\MediaLibrary\MediaCollections\Models\Media([ |
| 161 | + 'model_type' => get_class($user), |
| 162 | + 'model_id' => $user->id, |
| 163 | + 'uuid' => 'test-uuid-download', |
| 164 | + 'collection_name' => 'default', |
| 165 | + 'name' => 'test-file', |
| 166 | + 'file_name' => 'test.txt', |
| 167 | + 'mime_type' => 'text/plain', |
| 168 | + 'disk' => 'public', |
| 169 | + 'conversions_disk' => 'public', |
| 170 | + 'size' => 100, |
| 171 | + 'manipulations' => '[]', |
| 172 | + 'custom_properties' => '{}', |
| 173 | + 'generated_conversions' => '{}', |
| 174 | + 'responsive_images' => '{}', |
| 175 | + ]); |
| 176 | + $media->save(); |
| 177 | + |
| 178 | + // Mock the Gate to allow access |
| 179 | + Gate::shouldReceive('forUser')->andReturnSelf(); |
| 180 | + Gate::shouldReceive('check')->andReturn(true); |
| 181 | + |
| 182 | + $this->get('/test-media/download/'.$media->uuid) |
| 183 | + ->assertSuccessful() |
| 184 | + ->assertSee('Type: download'); |
| 185 | +})->group('middleware'); |
| 186 | + |
| 187 | +it('handles stream media type correctly', function () { |
| 188 | + // Set up test route with middleware |
| 189 | + Route::get('/test-media/{type}/{uuid}', function (Request $request, $type, $uuid) { |
| 190 | + // Check that the type is correctly passed |
| 191 | + return 'Type: '.$type; |
| 192 | + })->middleware(ValidateMediaAccess::class); |
| 193 | + |
| 194 | + // Create a real user for testing |
| 195 | + $user = user(); |
| 196 | + login($user); |
| 197 | + |
| 198 | + // Create a real media record in the database |
| 199 | + $media = new \Spatie\MediaLibrary\MediaCollections\Models\Media([ |
| 200 | + 'model_type' => get_class($user), |
| 201 | + 'model_id' => $user->id, |
| 202 | + 'uuid' => 'test-uuid-stream', |
| 203 | + 'collection_name' => 'default', |
| 204 | + 'name' => 'test-file', |
| 205 | + 'file_name' => 'test.txt', |
| 206 | + 'mime_type' => 'text/plain', |
| 207 | + 'disk' => 'public', |
| 208 | + 'conversions_disk' => 'public', |
| 209 | + 'size' => 100, |
| 210 | + 'manipulations' => '[]', |
| 211 | + 'custom_properties' => '{}', |
| 212 | + 'generated_conversions' => '{}', |
| 213 | + 'responsive_images' => '{}', |
| 214 | + ]); |
| 215 | + $media->save(); |
| 216 | + |
| 217 | + // Mock the Gate to allow access |
| 218 | + Gate::shouldReceive('forUser')->andReturnSelf(); |
| 219 | + Gate::shouldReceive('check')->andReturn(true); |
| 220 | + |
| 221 | + $this->get('/test-media/stream/'.$media->uuid) |
| 222 | + ->assertSuccessful() |
| 223 | + ->assertSee('Type: stream'); |
| 224 | +})->group('middleware'); |
0 commit comments