mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-23 20:35:03 +02:00
Allow to restrict the file extensions for a attachment type.
This commit is contained in:
parent
2187f5eac2
commit
fdfb099cb5
25 changed files with 714 additions and 4 deletions
|
@ -38,6 +38,10 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
abstract class AbstractAdminControllerTest extends WebTestCase
|
||||
{
|
||||
protected static $base_path = 'not_valid';
|
||||
|
@ -62,6 +66,7 @@ abstract class AbstractAdminControllerTest extends WebTestCase
|
|||
|
||||
/**
|
||||
* @dataProvider readDataProvider
|
||||
* @group slow
|
||||
* Tests if you can access the /new part which is used to list all entities. Checks if permissions are working
|
||||
*/
|
||||
public function testListEntries(string $user, bool $read)
|
||||
|
@ -81,6 +86,7 @@ abstract class AbstractAdminControllerTest extends WebTestCase
|
|||
|
||||
/**
|
||||
* @dataProvider readDataProvider
|
||||
* @group slow
|
||||
* Tests if it possible to access an specific entity. Checks if permissions are working.
|
||||
*/
|
||||
public function testReadEntity(string $user, bool $read)
|
||||
|
@ -110,6 +116,7 @@ abstract class AbstractAdminControllerTest extends WebTestCase
|
|||
|
||||
/**
|
||||
* Tests if deleting an entity is working.
|
||||
* @group slow
|
||||
* @dataProvider deleteDataProvider
|
||||
*/
|
||||
public function testDeleteEntity(string $user, bool $delete)
|
||||
|
|
|
@ -34,6 +34,10 @@ namespace App\Tests\Controller\AdminPages;
|
|||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class AttachmentTypeControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/attachment_type';
|
||||
|
|
|
@ -33,6 +33,10 @@ namespace App\Tests\Controller\AdminPages;
|
|||
|
||||
use App\Entity\Parts\Category;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class CategoryControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/category';
|
||||
|
|
|
@ -35,6 +35,10 @@ namespace App\Tests\Controller\AdminPages;
|
|||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Devices\Device;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class DeviceControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/device';
|
||||
|
|
|
@ -35,6 +35,10 @@ namespace App\Tests\Controller\AdminPages;
|
|||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Parts\Footprint;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class FootprintControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/footprint';
|
||||
|
|
|
@ -36,6 +36,10 @@ use App\Entity\Attachments\AttachmentType;
|
|||
use App\Entity\Devices\Device;
|
||||
use App\Entity\Parts\Manufacturer;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class ManufacturerControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/manufacturer';
|
||||
|
|
|
@ -37,6 +37,10 @@ use App\Entity\Devices\Device;
|
|||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\MeasurementUnit;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class MeasurementUnitControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/measurement_unit';
|
||||
|
|
|
@ -38,6 +38,10 @@ use App\Entity\Parts\Manufacturer;
|
|||
use App\Entity\Parts\Storelocation;
|
||||
use Symfony\Component\HttpKernel\HttpCache\Store;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class StorelocationControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/store_location';
|
||||
|
|
|
@ -37,6 +37,10 @@ use App\Entity\Devices\Device;
|
|||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\Supplier;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class SupplierControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/supplier';
|
||||
|
|
|
@ -36,6 +36,10 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
use Proxies\__CG__\App\Entity\UserSystem\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller
|
||||
*/
|
||||
class RedirectControllerTest extends WebTestCase
|
||||
{
|
||||
protected $em;
|
||||
|
@ -65,6 +69,7 @@ class RedirectControllerTest extends WebTestCase
|
|||
/**
|
||||
* Test if a certain request to an url will be redirected.
|
||||
* @dataProvider urlMatchDataProvider
|
||||
* @group slow
|
||||
*/
|
||||
public function testUrlMatch($url, $expect_redirect)
|
||||
{
|
||||
|
@ -95,6 +100,7 @@ class RedirectControllerTest extends WebTestCase
|
|||
/**
|
||||
* Test if the user is redirected to the localized version of a page, based on his settings.
|
||||
* @dataProvider urlAddLocaleDataProvider
|
||||
* @group slow
|
||||
* @depends testUrlMatch
|
||||
* @param $user_locale
|
||||
* @param $input_path
|
||||
|
@ -124,6 +130,7 @@ class RedirectControllerTest extends WebTestCase
|
|||
/**
|
||||
* Test if the user is redirected to password change page if he should do that
|
||||
* @depends testAddLocale
|
||||
* @group slow
|
||||
* @testWith ["de"]
|
||||
* ["en"]
|
||||
*/
|
||||
|
|
127
tests/Services/Attachments/FileTypeFilterToolsTest.php
Normal file
127
tests/Services/Attachments/FileTypeFilterToolsTest.php
Normal file
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* part-db version 0.1
|
||||
* Copyright (C) 2005 Christoph Lechner
|
||||
* http://www.cl-projects.de/
|
||||
*
|
||||
* part-db version 0.2+
|
||||
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
||||
* http://code.google.com/p/part-db/
|
||||
*
|
||||
* Part-DB Version 0.4+
|
||||
* Copyright (C) 2016 - 2019 Jan Böhmer
|
||||
* https://github.com/jbtronics
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Tests\Services\Attachments;
|
||||
|
||||
use App\Services\Attachments\FileTypeFilterTools;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class FileTypeFilterToolsTest extends WebTestCase
|
||||
{
|
||||
protected static $service;
|
||||
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
self::bootKernel();
|
||||
self::$service = self::$container->get(FileTypeFilterTools::class);
|
||||
}
|
||||
|
||||
public function validateDataProvider() : array
|
||||
{
|
||||
return [
|
||||
['', true], //Empty string is valid
|
||||
['.jpeg,.png, .gif', true], //Only extensions are valid
|
||||
['image/*, video/*, .mp4, video/x-msvideo, application/vnd.amazon.ebook', true],
|
||||
['application/vnd.amazon.ebook, audio/opus', true],
|
||||
|
||||
['*.notvalid, .png', false], //No stars in extension
|
||||
['test.png', false], //No full filename
|
||||
['application/*', false], //Only certain placeholders are allowed
|
||||
['.png;.png,.jpg', false], //Wrong separator
|
||||
['.png .jpg .gif', false]
|
||||
];
|
||||
}
|
||||
|
||||
public function normalizeDataProvider() : array
|
||||
{
|
||||
return [
|
||||
['', ''],
|
||||
['.jpeg,.png,.gif', '.jpeg,.png,.gif'],
|
||||
['.jpeg, .png, .gif,', '.jpeg,.png,.gif'],
|
||||
['jpg, *.gif', '.jpg,.gif'],
|
||||
['video, image/', 'video/*,image/*'],
|
||||
['video/*', 'video/*'],
|
||||
['video/x-msvideo,.jpeg', 'video/x-msvideo,.jpeg'],
|
||||
['.video', '.video'],
|
||||
//Remove duplicate entries
|
||||
['png, .gif, .png,', '.png,.gif'],
|
||||
];
|
||||
}
|
||||
|
||||
public function extensionAllowedDataProvider() : array
|
||||
{
|
||||
return [
|
||||
['', 'txt', true],
|
||||
['', 'everything_should_match', true],
|
||||
|
||||
['.jpg,.png', 'jpg', true],
|
||||
['.jpg,.png', 'png', true],
|
||||
['.jpg,.png', 'txt', false],
|
||||
|
||||
['image/*', 'jpeg', true],
|
||||
['image/*', 'png', true],
|
||||
['image/*', 'txt', false],
|
||||
|
||||
['application/pdf,.txt', 'pdf', true],
|
||||
['application/pdf,.txt', 'txt', true],
|
||||
['application/pdf,.txt', 'jpg', false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the validateFilterString method
|
||||
* @dataProvider validateDataProvider
|
||||
* @param string $filter
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testValidateFilterString(string $filter, bool $expected)
|
||||
{
|
||||
$this->assertEquals($expected, self::$service->validateFilterString($filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider normalizeDataProvider
|
||||
* @param string $filter
|
||||
* @param string $expected
|
||||
*/
|
||||
public function testNormalizeFilterString(string $filter, string $expected)
|
||||
{
|
||||
$this->assertEquals($expected, self::$service->normalizeFilterString($filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider extensionAllowedDataProvider
|
||||
*/
|
||||
public function testIsExtensionAllowed(string $filter, string $extension, bool $expected)
|
||||
{
|
||||
$this->assertEquals($expected, self::$service->isExtensionAllowed($filter, $extension), $expected);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue