Skip to content

Commit

Permalink
Test handle method returns false on invalid mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 28, 2023
1 parent 5ff4b35 commit 2ff6ae5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/Jobs/ImageUploadAndResizingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ImageUploadAndResizingJob implements ShouldQueue
'640x480' => [640, 480],
];

public array $allowedExtension = ['jpg', 'jpeg', 'png'];

private ?Filesystem $storage;

/**
Expand Down Expand Up @@ -55,6 +57,10 @@ public function storage()
public function handle()
{
$path = 'fake-image-name.jpg';

if (! $this->isAllowedExtension($this->getExtensionFromMimeType())) {
return false;
}

if (! $this->storage()->put($path, base64_decode($this->imageContent))) {
return false;
Expand All @@ -73,6 +79,21 @@ public function handle()
return $paths;
}

private function isAllowedExtension(string $extension)
{
return in_array($extension, $this->allowedExtension);
}

private function getExtensionFromMimeType()
{
if (! preg_match("/^[a-z]+\/[a-z0-9\.\+-]+$/", $this->mimeType))
return false;

[, $extension] = explode('/', $this->mimeType);

return $extension;
}

/**
* Modify an absolute path for renaming image name with resolution key
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,14 @@ public function handle_method_returns_false_on_upload_fail()

$this->assertFalse($job->handle());
}

/** @test */
public function handle_method_returns_false_on_invalid_mime_type()
{
$file = UploadedFile::fake()->create('file.txt', '10', 'text/plain');

$job = new ImageUploadAndResizingJob($file->getMimeType(), base64_encode($file->getContent()));

$this->assertFalse($job->handle());
}
}

0 comments on commit 2ff6ae5

Please sign in to comment.