Added drawing endpoint tests

Also refactored ImageTests away from BrowserKit
Also added image upload type validation.
This commit is contained in:
Dan Brown 2018-01-28 13:18:28 +00:00
parent 9bbef3a3dd
commit 88d09a2a3b
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
7 changed files with 119 additions and 28 deletions

View File

@ -120,7 +120,10 @@ class ImageController extends Controller
$this->validate($request, [
'file' => 'is_image'
]);
// TODO - Restrict & validate types
if (!$this->imageRepo->isValidType($type)) {
return $this->jsonError(trans('errors.image_upload_type_error'));
}
$imageUpload = $request->file('file');

View File

@ -1,12 +1,9 @@
<?php namespace BookStack\Repos;
use BookStack\Image;
use BookStack\Page;
use BookStack\Services\ImageService;
use BookStack\Services\PermissionService;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Setting;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class ImageRepo
@ -247,5 +244,15 @@ class ImageRepo
}
}
/**
* Check if the provided image type is valid.
* @param $type
* @return bool
*/
public function isValidType($type)
{
$validTypes = ['drawing', 'gallery', 'cover', 'system', 'user'];
return in_array($type, $validTypes);
}
}

View File

@ -36,6 +36,7 @@ return [
'cannot_create_thumbs' => 'The server cannot create thumbnails. Please check you have the GD PHP extension installed.',
'server_upload_limit' => 'The server does not allow uploads of this size. Please try a smaller file size.',
'image_upload_error' => 'An error occurred uploading the image',
'image_upload_type_error' => 'The image type being uploaded is invalid',
// Attachments
'attachment_page_mismatch' => 'Page mismatch during attachment update',

View File

@ -13,13 +13,6 @@ abstract class BrowserKitTest extends TestCase
use DatabaseTransactions;
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';
// Local user instances
private $admin;
private $editor;

View File

@ -1,8 +1,20 @@
<?php namespace Tests;
class ImageTest extends BrowserKitTest
use BookStack\Image;
use BookStack\Page;
class ImageTest extends TestCase
{
/**
* Get the path to our basic test image.
* @return string
*/
protected function getTestImageFilePath()
{
return base_path('tests/test-data/test-image.png');
}
/**
* Get a test image that can be uploaded
* @param $fileName
@ -10,7 +22,7 @@ class ImageTest extends BrowserKitTest
*/
protected function getTestImage($fileName)
{
return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-image.jpg'), $fileName, 'image/jpeg', 5238);
return new \Illuminate\Http\UploadedFile($this->getTestImageFilePath(), $fileName, 'image/jpeg', 5238);
}
/**
@ -28,13 +40,12 @@ class ImageTest extends BrowserKitTest
* Uploads an image with the given name.
* @param $name
* @param int $uploadedTo
* @return string
* @return \Illuminate\Foundation\Testing\TestResponse
*/
protected function uploadImage($name, $uploadedTo = 0)
{
$file = $this->getTestImage($name);
$this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
return $this->getTestImagePath('gallery', $name);
return $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
}
/**
@ -49,19 +60,20 @@ class ImageTest extends BrowserKitTest
public function test_image_upload()
{
$page = \BookStack\Page::first();
$page = Page::first();
$this->asAdmin();
$admin = $this->getAdmin();
$imageName = 'first-image.jpg';
$imageName = 'first-image.png';
$relPath = $this->uploadImage($imageName, $page->id);
$this->assertResponseOk();
$upload = $this->uploadImage($imageName, $page->id);
$upload->assertStatus(200);
$relPath = $this->getTestImagePath('gallery', $imageName);
$this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
$this->deleteImage($relPath);
$this->seeInDatabase('images', [
$this->assertDatabaseHas('images', [
'url' => $this->baseUrl . $relPath,
'type' => 'gallery',
'uploaded_to' => $page->id,
@ -75,17 +87,18 @@ class ImageTest extends BrowserKitTest
public function test_image_delete()
{
$page = \BookStack\Page::first();
$page = Page::first();
$this->asAdmin();
$imageName = 'first-image.jpg';
$imageName = 'first-image.png';
$relPath = $this->uploadImage($imageName, $page->id);
$image = \BookStack\Image::first();
$this->uploadImage($imageName, $page->id);
$image = Image::first();
$relPath = $this->getTestImagePath('gallery', $imageName);
$this->call('DELETE', '/images/' . $image->id);
$this->assertResponseOk();
$delete = $this->delete( '/images/' . $image->id);
$delete->assertStatus(200);
$this->dontSeeInDatabase('images', [
$this->assertDatabaseMissing('images', [
'url' => $this->baseUrl . $relPath,
'type' => 'gallery'
]);
@ -93,4 +106,78 @@ class ImageTest extends BrowserKitTest
$this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
}
public function testBase64Get()
{
$page = Page::first();
$this->asAdmin();
$imageName = 'first-image.png';
$this->uploadImage($imageName, $page->id);
$image = Image::first();
$imageGet = $this->getJson("/images/base64/{$image->id}");
$imageGet->assertJson([
'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
]);
}
public function test_drawing_base64_upload()
{
$page = Page::first();
$editor = $this->getEditor();
$this->actingAs($editor);
$upload = $this->postJson('images/drawing/upload', [
'uploaded_to' => $page->id,
'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
]);
$upload->assertStatus(200);
$upload->assertJson([
'type' => 'drawio',
'uploaded_to' => $page->id,
'created_by' => $editor->id,
'updated_by' => $editor->id,
]);
$image = Image::where('type', '=', 'drawio')->first();
$this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: '. public_path($image->path));
$testImageData = file_get_contents($this->getTestImageFilePath());
$uploadedImageData = file_get_contents(public_path($image->path));
$this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
}
public function test_drawing_replacing()
{
$page = Page::first();
$editor = $this->getEditor();
$this->actingAs($editor);
$this->postJson('images/drawing/upload', [
'uploaded_to' => $page->id,
'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDQ4S1RUeKwAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12NctNWSAQkwMaACUvkAfCkBmjyhGl4AAAAASUVORK5CYII='
]);
$image = Image::where('type', '=', 'drawio')->first();
$replace = $this->putJson("images/drawing/upload/{$image->id}", [
'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
]);
$replace->assertStatus(200);
$replace->assertJson([
'type' => 'drawio',
'uploaded_to' => $page->id,
'created_by' => $editor->id,
'updated_by' => $editor->id,
]);
$this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: '. public_path($image->path));
$testImageData = file_get_contents($this->getTestImageFilePath());
$uploadedImageData = file_get_contents(public_path($image->path));
$this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 B