initial commit

This commit is contained in:
Milos Stojanovic
2019-06-13 18:52:40 +02:00
commit 261607e1d3
160 changed files with 41704 additions and 0 deletions

View File

@@ -0,0 +1,221 @@
<?php
/*
* This file is part of the FileGator package.
*
* (c) Milos Stojanovic <alcalbg@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
*/
namespace Tests\Feature;
use Tests\TestCase;
/**
* @internal
*/
class AdminTest extends TestCase
{
public function testOnlyAdminCanPerformUserActions()
{
$this->signOut();
$this->sendRequest('GET', '/listusers');
$this->assertStatus(404);
$this->sendRequest('POST', '/storeuser');
$this->assertStatus(404);
$this->sendRequest('POST', '/updateuser/test@example.com');
$this->assertStatus(404);
$this->sendRequest('POST', '/deleteuser/test@example.com');
$this->assertStatus(404);
}
public function testListUsers()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('GET', '/listusers');
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
[
'role' => 'guest',
'permissions' => [],
'homedir' => '/',
'username' => 'guest',
'name' => 'Guest',
],
[
'role' => 'admin',
'permissions' => [],
'homedir' => '/',
'username' => 'admin@example.com',
'name' => 'Admin',
],
[
'role' => 'user',
'permissions' => [],
'homedir' => '/john',
'username' => 'john@example.com',
'name' => 'John Doe',
],
[
'role' => 'user',
'permissions' => [],
'homedir' => '/jane',
'username' => 'jane@example.com',
'name' => 'Jane Doe',
],
],
]);
}
public function testAddingNewUser()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('POST', '/storeuser', [
'name' => 'Mike Test',
'username' => 'mike@example.com',
'role' => 'user',
'permissions' => [],
'password' => 'pass123',
'homedir' => '/john',
]);
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'role' => 'user',
'permissions' => [],
'homedir' => '/john',
'username' => 'mike@example.com',
'name' => 'Mike Test',
],
]);
}
public function testAddingNewUserValidation()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('POST', '/storeuser', [
'name' => '',
'username' => '',
'role' => 'user',
'permissions' => [],
'password' => 'pass123',
'homedir' => '',
]);
$this->assertStatus(422);
$this->sendRequest('POST', '/storeuser', [
'name' => 'Mike Test',
'username' => 'mike@example.com',
'role' => 'bear',
'permissions' => ['xxx'],
'password' => 'pass123',
'homedir' => '/john',
]);
$this->assertStatus(422);
}
public function testUpdatingUser()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('POST', '/updateuser/john@example.com', [
'name' => 'Johnny Doe',
'username' => 'john2@example.com',
'role' => 'admin',
'permissions' => ['read', 'write'],
'homedir' => '/jane',
]);
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'role' => 'admin',
'permissions' => ['read', 'write'],
'homedir' => '/jane',
'username' => 'john2@example.com',
'name' => 'Johnny Doe',
],
]);
}
public function testDeletingUser()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('POST', '/deleteuser/john@example.com');
$this->assertOk();
}
public function testUpdatingNonExistingUser()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('POST', '/updateuser/nonexisting@example.com');
$this->assertStatus(422);
}
public function testUpdatingUserValidation()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('POST', '/updateuser/john@example.com', [
'name' => '',
'username' => '',
'homedir' => '',
]);
$this->assertStatus(422);
$this->sendRequest('POST', '/updateuser/john@example.com', [
'name' => 'something',
'username' => 'something',
'homedir' => '/',
'permissions' => ['xxx', 'write'],
]);
$this->assertStatus(422);
}
public function testDeletingNonExistingUser()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('POST', '/deleteuser/nonexisting@example.com');
$this->assertStatus(422);
}
public function testAddingOrEditingUserWithUsernameThatIsAlreadyTaken()
{
$this->signIn('admin@example.com', 'admin123');
$this->sendRequest('POST', '/storeuser', [
'name' => 'Mike Test',
'username' => 'admin@example.com',
'role' => 'user',
'password' => '123',
'permissions' => [],
'homedir' => '/mike',
]);
$this->assertStatus(422);
$this->sendRequest('POST', '/updateuser/admin@example.com', [
'name' => 'Admin',
'username' => 'john@example.com',
'role' => 'admin',
'permissions' => [],
'homedir' => '/',
]);
$this->assertStatus(422);
}
}

View File

@@ -0,0 +1,74 @@
<?php
/*
* This file is part of the FileGator package.
*
* (c) Milos Stojanovic <alcalbg@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
*/
namespace Tests\Unit;
use Filegator\Kernel\Request;
use Filegator\Kernel\Response;
use Filegator\Services\Auth\AuthInterface;
use Filegator\Services\Session\Session;
use Filegator\Services\Session\SessionStorageInterface;
use Tests\TestCase;
/**
* @internal
*/
class AppTest extends TestCase
{
public function testAppWithoutSession()
{
$app = $this->bootFreshApp();
$request = $app->resolve(Request::class);
$response = $app->resolve(Response::class);
$session = $app->resolve(SessionStorageInterface::class);
$auth = $app->resolve(AuthInterface::class);
$this->assertNotNull($request);
$this->assertNotNull($response);
$this->assertNotNull($session);
$this->assertNull($auth->user());
}
public function testAppWithSession()
{
// first login request
$request1 = Request::create(
'?r=/login',
'POST',
[],
[],
[],
[
'CONTENT_TYPE' => 'application/json',
'HTTP_ACCEPT' => 'application/json',
],
'{"username":"admin@example.com","password":"admin123"}'
);
$config = $this->getMockConfig();
$app1 = $this->bootFreshApp($config, $request1, null, true);
$prev_session = $request1->getSession();
// another request with previous session
$request2 = Request::create(
'?r=/',
'GET',
);
$request2->setSession($prev_session);
$app2 = $this->bootFreshApp($config, $request2);
$auth = $app2->resolve(AuthInterface::class);
$this->assertNotNull($auth->user());
}
}

View File

@@ -0,0 +1,145 @@
<?php
/*
* This file is part of the FileGator package.
*
* (c) Milos Stojanovic <alcalbg@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
*/
namespace Tests\Feature;
use Filegator\Kernel\Request;
use Filegator\Kernel\Response;
use Tests\TestCase;
/**
* @internal
*/
class AuthTest extends TestCase
{
public function testSuccessfulLogin()
{
$ret = $this->sendRequest('POST', '/login', [
'username' => 'john@example.com',
'password' => 'john123',
]);
$this->assertOk();
}
public function testBadLogin()
{
$this->sendRequest('POST', '/login', [
'username' => 'fake',
'password' => 'fake',
]);
$this->assertUnprocessable();
}
public function testAlreadyLoggedIn()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
$this->sendRequest('POST', '/login', ['username' => $username, 'password' => 'john123']);
$this->assertStatus(404);
}
public function testGetUser()
{
$user = 'john@example.com';
$this->signIn($user, 'john123');
$this->sendRequest('GET', '/getuser');
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'username' => $user,
'name' => 'John Doe',
'role' => 'user',
'homedir' => '/john',
],
]);
}
public function testGetAdmin()
{
$admin = 'admin@example.com';
$this->signIn($admin, 'admin123');
$this->sendRequest('GET', '/getuser');
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'username' => $admin,
'name' => 'Admin',
'role' => 'admin',
'homedir' => '/',
],
]);
}
public function testReceiveGuestIfNoUserIsLoggedIn()
{
$this->sendRequest('GET', '/getuser');
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'role' => 'guest',
],
]);
}
public function testLogout()
{
$this->signIn('john@example.com', 'john123');
$this->sendRequest('POST', '/logout');
$this->assertOk();
}
public function testResponseThrows404()
{
$request = Request::create(
'?r=/notfound',
'GET',
);
$app = $this->bootFreshApp(null, $request);
$response = $app->resolve(Response::class);
$this->assertEquals($response->getStatusCode(), 404);
}
public function testChangePassword()
{
$this->signIn('john@example.com', 'john123');
$this->sendRequest('POST', '/changepassword', [
'oldpassword' => 'john123',
'newpassword' => '',
]);
$this->assertStatus(422);
$this->signIn('john@example.com', 'john123');
$this->sendRequest('POST', '/changepassword', [
'oldpassword' => 'wrongpass',
'newpassword' => 'password123',
]);
$this->assertStatus(422);
$this->signIn('john@example.com', 'john123');
$this->sendRequest('POST', '/changepassword', [
'oldpassword' => 'john123',
'newpassword' => 'password123',
]);
$this->assertOk();
}
}

View File

@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the FileGator package.
*
* (c) Milos Stojanovic <alcalbg@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
*/
namespace Tests\Feature;
use Tests\TestCase;
/**
* @internal
*/
class FeatureTest extends TestCase
{
public function testHome()
{
$this->sendRequest('GET', '/');
$this->assertOk();
}
public function testMethodNotAllowed()
{
$this->sendRequest('DELETE', '/');
$this->assertStatus(401);
}
public function testNotFoundPage()
{
$this->sendRequest('GET', '/fakeroute');
$this->assertStatus(404);
}
public function testUnprocessableRequest()
{
$this->sendRequest('POST', '/login', 'dddddd');
$this->assertUnprocessable();
}
public function testGetFrontendConfig()
{
$this->sendRequest('GET', '/getconfig');
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'app_name' => 'FileGator',
'upload_max_size' => 2 * 1024 * 1024,
'upload_chunk_size' => 1 * 1024 * 1024,
'upload_simultaneous' => 3,
],
]);
}
}

View File

@@ -0,0 +1,574 @@
<?php
/*
* This file is part of the FileGator package.
*
* (c) Milos Stojanovic <alcalbg@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
*/
namespace Tests\Feature;
use Tests\TestCase;
/**
* @internal
*/
class FilesTest extends TestCase
{
protected $timestamp;
public function setUp(): void
{
$this->resetTempDir();
$this->timestamp = time();
}
public function tearDown(): void
{
$this->resetTempDir();
}
public function testGuestCannotListDirectories()
{
$this->signOut();
$this->sendRequest('POST', '/changedir', [
'to' => '/',
]);
$this->assertStatus(404);
$this->sendRequest('POST', '/getdir', [
'to' => '/',
]);
$this->assertStatus(404);
}
public function testUserCanChangeDir()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
mkdir(TEST_REPOSITORY.'/john');
mkdir(TEST_REPOSITORY.'/john/johnsub');
touch(TEST_REPOSITORY.'/john/john.txt', $this->timestamp);
$this->sendRequest('POST', '/changedir', [
'to' => '/',
]);
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'files' => [
0 => [
'type' => 'dir',
'path' => '/johnsub',
'name' => 'johnsub',
],
1 => [
'type' => 'file',
'path' => '/john.txt',
'name' => 'john.txt',
'time' => $this->timestamp,
],
],
],
]);
}
public function testUserCanListHisHomeDir()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
mkdir(TEST_REPOSITORY.'/john');
mkdir(TEST_REPOSITORY.'/john/johnsub');
touch(TEST_REPOSITORY.'/john/john.txt', $this->timestamp);
$this->sendRequest('POST', '/getdir', [
'dir' => '/',
]);
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'files' => [
0 => [
'type' => 'dir',
'path' => '/johnsub',
'name' => 'johnsub',
],
1 => [
'type' => 'file',
'path' => '/john.txt',
'name' => 'john.txt',
'time' => $this->timestamp,
],
],
],
]);
}
public function testDeleteItems()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
mkdir(TEST_REPOSITORY.'/john');
mkdir(TEST_REPOSITORY.'/john/johnsub');
touch(TEST_REPOSITORY.'/john/john.txt', $this->timestamp);
$items = [
0 => [
'type' => 'dir',
'path' => '/johnsub',
'name' => 'johnsub',
'time' => $this->timestamp,
],
1 => [
'type' => 'file',
'path' => '/john.txt',
'name' => 'john.txt',
'time' => $this->timestamp,
],
];
$this->sendRequest('POST', '/deleteitems', [
'items' => $items,
]);
$this->assertOk();
}
public function testDownloadFile()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
mkdir(TEST_REPOSITORY.'/john');
touch(TEST_REPOSITORY.'/john/john.txt', $this->timestamp);
$path_encoded = base64_encode('john.txt');
$this->sendRequest('GET', '/download/'.$path_encoded);
$this->assertOk();
}
public function testGuestCannotDownloadFilesWithoutDownloadPermissions()
{
touch(TEST_REPOSITORY.'/test.txt', $this->timestamp);
$path_encoded = base64_encode('test.txt');
$this->sendRequest('GET', '/download/'.$path_encoded);
$this->assertStatus(404);
}
public function testDownloadFileOnlyWithPermissions()
{
// jane does not have download permissions
$username = 'jane@example.com';
$this->signIn($username, 'jane123');
mkdir(TEST_REPOSITORY.'/jane');
touch(TEST_REPOSITORY.'/jane/jane.txt', $this->timestamp);
$path_encoded = base64_encode('jane.txt');
$this->sendRequest('GET', '/download/'.$path_encoded);
$this->assertStatus(404);
}
public function testDownloadMissingFileThrowsRedirect()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
$path_encoded = base64_encode('missing.txt');
$this->sendRequest('GET', '/download/'.$path_encoded);
$this->assertStatus(302);
}
public function testRenameJohnsFile()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
mkdir(TEST_REPOSITORY.'/john');
touch(TEST_REPOSITORY.'/john/john.txt', $this->timestamp);
$this->sendRequest('POST', '/renameitem', [
'from' => '/john.txt',
'to' => '/john2.txt',
]);
$this->assertOk();
$this->assertFileExists(TEST_REPOSITORY.'/john/john2.txt');
$this->assertFileNotExists(TEST_REPOSITORY.'/john/john.txt');
}
public function testRenameMissingfileThrowsException()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
$this->expectException(\Exception::class);
$this->sendRequest('POST', '/renameitem', [
'from' => 'missing.txt',
'to' => 'john2.txt',
]);
}
public function testDeleteMissingItemsThrowsException()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
$items = [
0 => [
'type' => 'file',
'path' => '/missing',
'name' => 'missing',
'time' => $this->timestamp,
],
];
$this->expectException(\Exception::class);
$this->sendRequest('POST', '/deleteitems', [
'items' => $items,
]);
}
public function testCreateNewDirAndFileInside()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
mkdir(TEST_REPOSITORY.'/john');
$this->sendRequest('POST', '/createnew', [
'type' => 'dir',
'name' => 'maximus',
]);
$this->assertOk();
$this->sendRequest('POST', '/changedir', [
'to' => '/maximus/',
]);
$this->assertOk();
$this->sendRequest('POST', '/createnew', [
'type' => 'file',
'name' => 'samplefile.txt',
]);
$this->assertOk();
$this->assertDirectoryExists(TEST_REPOSITORY.'/john/maximus');
$this->assertFileExists(TEST_REPOSITORY.'/john/maximus/samplefile.txt');
}
public function testCopyAdminFiles()
{
$username = 'admin@example.com';
$this->signIn($username, 'admin123');
touch(TEST_REPOSITORY.'/a.txt', $this->timestamp);
touch(TEST_REPOSITORY.'/c.zip', $this->timestamp);
mkdir(TEST_REPOSITORY.'/sub');
mkdir(TEST_REPOSITORY.'/sub/sub1');
mkdir(TEST_REPOSITORY.'/john');
mkdir(TEST_REPOSITORY.'/john/johnsub');
$items = [
0 => [
'type' => 'file',
'path' => '/a.txt',
'name' => 'a.txt',
'time' => $this->timestamp,
],
1 => [
'type' => 'file',
'path' => '/c.zip',
'name' => 'c.zip',
'time' => $this->timestamp,
],
2 => [
'type' => 'dir',
'path' => '/sub',
'name' => 'sub',
'time' => $this->timestamp,
],
];
$this->sendRequest('POST', '/copyitems', [
'items' => $items,
'destination' => '/john/johnsub/',
]);
$this->assertOk();
$this->assertFileExists(TEST_REPOSITORY.'/john/johnsub/a.txt');
$this->assertFileExists(TEST_REPOSITORY.'/john/johnsub/c.zip');
$this->assertDirectoryExists(TEST_REPOSITORY.'/john/johnsub/sub/');
$this->assertDirectoryExists(TEST_REPOSITORY.'/john/johnsub/sub/sub1');
}
public function testCopyInvalidFilesThrowsException()
{
$username = 'admin@example.com';
$this->signIn($username, 'admin123');
$items = [
0 => [
'type' => 'file',
'path' => '/missin.txt',
'name' => 'missina.txt',
'time' => $this->timestamp,
],
];
$this->expectException(\Exception::class);
$this->sendRequest('POST', '/copyitems', [
'items' => $items,
'destination' => '/john/johnsub/',
]);
}
public function testMoveFiles()
{
$username = 'admin@example.com';
$this->signIn($username, 'admin123');
mkdir(TEST_REPOSITORY.'/john');
touch(TEST_REPOSITORY.'/a.txt', $this->timestamp);
touch(TEST_REPOSITORY.'/b.txt', $this->timestamp);
$items = [
0 => [
'type' => 'file',
'path' => '/a.txt',
'name' => 'a.txt',
'time' => $this->timestamp,
],
1 => [
'type' => 'file',
'path' => '/b.txt',
'name' => 'b.txt',
'time' => $this->timestamp,
],
];
$this->sendRequest('POST', '/moveitems', [
'items' => $items,
'destination' => '/john',
]);
$this->assertOk();
$this->assertFileExists(TEST_REPOSITORY.'/john/a.txt');
$this->assertFileExists(TEST_REPOSITORY.'/john/b.txt');
$this->assertFileNotExists(TEST_REPOSITORY.'/a.txt');
$this->assertFileNotExists(TEST_REPOSITORY.'/b.txt');
}
public function testMoveDirsWithContent()
{
$username = 'admin@example.com';
$this->signIn($username, 'admin123');
mkdir(TEST_REPOSITORY.'/sub');
mkdir(TEST_REPOSITORY.'/sub/sub1');
touch(TEST_REPOSITORY.'/sub/sub1/f.txt', $this->timestamp);
mkdir(TEST_REPOSITORY.'/jane');
touch(TEST_REPOSITORY.'/jane/cookie.txt', $this->timestamp);
mkdir(TEST_REPOSITORY.'/john');
$items = [
0 => [
'type' => 'dir',
'path' => '/sub',
'name' => 'sub',
'time' => $this->timestamp,
],
1 => [
'type' => 'dir',
'path' => '/jane',
'name' => 'jane',
'time' => $this->timestamp,
],
];
$this->sendRequest('POST', '/moveitems', [
'items' => $items,
'destination' => '/john',
]);
$this->assertOk();
$this->assertDirectoryNotExists(TEST_REPOSITORY.'/jane');
$this->assertDirectoryNotExists(TEST_REPOSITORY.'/sub');
$this->assertFileNotExists(TEST_REPOSITORY.'/sub/sub1/f.txt');
$this->assertDirectoryExists(TEST_REPOSITORY.'/john/jane');
$this->assertDirectoryExists(TEST_REPOSITORY.'/john/sub');
$this->assertDirectoryExists(TEST_REPOSITORY.'/john/sub/sub1');
$this->assertFileExists(TEST_REPOSITORY.'/john/sub/sub1/f.txt');
$this->assertFileExists(TEST_REPOSITORY.'/john/jane/cookie.txt');
}
public function testZipFilesOnly()
{
$username = 'admin@example.com';
$this->signIn($username, 'admin123');
touch(TEST_REPOSITORY.'/a.txt', $this->timestamp);
touch(TEST_REPOSITORY.'/b.txt', $this->timestamp);
mkdir(TEST_REPOSITORY.'/john');
$items = [
0 => [
'type' => 'file',
'path' => '/a.txt',
'name' => 'a.txt',
'time' => $this->timestamp,
],
1 => [
'type' => 'file',
'path' => '/b.txt',
'name' => 'b.txt',
'time' => $this->timestamp,
],
];
$this->sendRequest('POST', '/zipitems', [
'name' => 'compressed.zip',
'items' => $items,
'destination' => '/john',
]);
$this->assertOk();
$this->assertFileExists(TEST_REPOSITORY.'/a.txt');
$this->assertFileExists(TEST_REPOSITORY.'/b.txt');
$this->assertFileExists(TEST_REPOSITORY.'/john/compressed.zip');
}
public function testZipFilesAndDirectories()
{
$username = 'admin@example.com';
$this->signIn($username, 'admin123');
touch(TEST_REPOSITORY.'/a.txt', $this->timestamp);
touch(TEST_REPOSITORY.'/b.txt', $this->timestamp);
mkdir(TEST_REPOSITORY.'/sub');
mkdir(TEST_REPOSITORY.'/jane');
$items = [
0 => [
'type' => 'file',
'path' => '/a.txt',
'name' => 'a.txt',
'time' => $this->timestamp,
],
1 => [
'type' => 'file',
'path' => '/b.txt',
'name' => 'b.txt',
'time' => $this->timestamp,
],
2 => [
'type' => 'dir',
'path' => '/sub',
'name' => 'sub',
'time' => $this->timestamp,
],
];
$this->sendRequest('POST', '/zipitems', [
'name' => 'compressed2.zip',
'items' => $items,
'destination' => '/jane',
]);
$this->assertOk();
$this->assertFileExists(TEST_REPOSITORY.'/a.txt');
$this->assertFileExists(TEST_REPOSITORY.'/b.txt');
$this->assertDirectoryExists(TEST_REPOSITORY.'/sub');
$this->assertFileExists(TEST_REPOSITORY.'/jane/compressed2.zip');
}
public function testUnzipArchive()
{
$username = 'admin@example.com';
$this->signIn($username, 'admin123');
copy(TEST_ARCHIVE, TEST_REPOSITORY.'/c.zip');
mkdir(TEST_REPOSITORY.'/jane');
$this->sendRequest('POST', '/unzipitem', [
'item' => '/c.zip',
'destination' => '/jane',
]);
$this->assertOk();
$this->assertFileExists(TEST_REPOSITORY.'/jane/one.txt');
$this->assertFileExists(TEST_REPOSITORY.'/jane/two.txt');
$this->assertDirectoryExists(TEST_REPOSITORY.'/jane/onetwo');
$this->assertFileExists(TEST_REPOSITORY.'/jane/onetwo/three.txt');
}
public function testDownloadMultipleItems()
{
$username = 'john@example.com';
$this->signIn($username, 'john123');
mkdir(TEST_REPOSITORY.'/john');
touch(TEST_REPOSITORY.'/john/john.txt', $this->timestamp);
mkdir(TEST_REPOSITORY.'/john/johnsub');
touch(TEST_REPOSITORY.'/john/johnsub/sub.txt', $this->timestamp);
mkdir(TEST_REPOSITORY.'/john/johnsub/sub2');
$items = [
0 => [
'type' => 'dir',
'path' => '/johnsub',
'name' => 'johnsub',
'time' => $this->timestamp,
],
1 => [
'type' => 'file',
'path' => '/john.txt',
'name' => 'john.txt',
'time' => $this->timestamp,
],
];
$this->sendRequest('POST', '/batchdownload', [
'items' => $items,
]);
$this->assertOk();
$res = json_decode($this->response->getContent());
$uniqid = $res->data->uniqid;
$this->sendRequest('GET', '/batchdownload', [
'uniqid' => $uniqid,
]);
$this->assertOk();
}
}

View File

@@ -0,0 +1,303 @@
<?php
/*
* This file is part of the FileGator package.
*
* (c) Milos Stojanovic <alcalbg@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
*/
namespace Tests\Feature;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Tests\TestCase;
/**
* @internal
*/
class UploadTest extends TestCase
{
public function setUp(): void
{
$this->resetTempDir();
parent::setUp();
}
public function testSimpleFileUpload()
{
$this->signIn('john@example.com', 'john123');
// create 5Kb dummy file
$fp = fopen(TEST_FILE, 'w');
fseek($fp, 0.5 * 1024 * 1024 - 1, SEEK_CUR);
fwrite($fp, 'a');
fclose($fp);
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 1,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 0.5 * 1024 * 1024,
'resumableTotalChunks' => 1,
'resumableTotalSize' => 0.5 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-SIMPLE-TEST',
'resumableFilename' => 'sample.txt',
'resumableRelativePath' => '/',
];
$this->sendRequest('GET', '/upload', $data, $files);
$this->assertStatus(204);
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertOk();
$this->sendRequest('POST', '/getdir', [
'dir' => '/',
]);
$this->assertOk();
$this->assertResponseJsonHas([
'data' => [
'files' => [
0 => [
'type' => 'file',
'path' => '/sample.txt',
'name' => 'sample.txt',
],
],
],
]);
}
public function testFileUploadWithTwoChunks()
{
$this->signIn('john@example.com', 'john123');
// create 1MB dummy file part 1
$fp = fopen(TEST_FILE, 'w');
fseek($fp, 1 * 1024 * 1024 - 1, SEEK_CUR);
fwrite($fp, 'a');
fclose($fp);
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 1,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 1 * 1024 * 1024,
'resumableTotalChunks' => 2,
'resumableTotalSize' => 1.5 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-MULTIPART-TEST',
'resumableFilename' => 'sample.txt',
'resumableRelativePath' => '/',
];
// part does not exists
$this->sendRequest('GET', '/upload', $data, $files);
$this->assertStatus(204);
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertOk();
// this part should already exists, no need to upload again
$this->sendRequest('GET', '/upload', $data, $files);
$this->assertOk();
// create 512Kb dummy file part 2
$fp = fopen(TEST_FILE, 'w');
fseek($fp, 0.5 * 1024 * 1024 - 1, SEEK_CUR);
fwrite($fp, 'a');
fclose($fp);
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 2,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 0.5 * 1024 * 1024,
'resumableTotalChunks' => 2,
'resumableTotalSize' => 1.5 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-MULTIPART-TEST',
'resumableFilename' => 'sample.txt',
'resumableRelativePath' => '/',
];
// part does not exists
$this->sendRequest('GET', '/upload', $data, $files);
$this->assertStatus(204);
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertOk();
}
public function testUploadFileBiggerThanAllowed()
{
$this->signIn('john@example.com', 'john123');
// create 3MB dummy file
$fp = fopen(TEST_FILE, 'w');
fseek($fp, 3 * 1024 * 1024 - 1, SEEK_CUR);
fwrite($fp, 'a');
fclose($fp);
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 1,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 1 * 1024 * 1024,
'resumableTotalChunks' => 1,
'resumableTotalSize' => 1 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-FAILED-TEST',
'resumableFilename' => 'sample.txt',
'resumableRelativePath' => '/',
];
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertStatus(422);
}
public function testUploadFileBiggerThanAllowedUsingChunks()
{
$this->signIn('john@example.com', 'john123');
// create 1MB dummy file
$fp = fopen(TEST_FILE, 'w');
fseek($fp, 1 * 1024 * 1024 - 1, SEEK_CUR);
fwrite($fp, 'a');
fclose($fp);
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 1,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 1 * 1024 * 1024,
'resumableTotalChunks' => 3,
'resumableTotalSize' => 2 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-FAILED2-TEST',
'resumableFilename' => 'sample.txt',
'resumableRelativePath' => '/',
];
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertOk();
// create 512Kb dummy file
$fp = fopen(TEST_FILE, 'w');
fseek($fp, .5 * 1024 * 1024 - 1, SEEK_CUR);
fwrite($fp, 'a');
fclose($fp);
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 2,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 0.5 * 1024 * 1024,
'resumableTotalChunks' => 3,
'resumableTotalSize' => 2 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-FAILED2-TEST',
'resumableFilename' => 'sample.txt',
'resumableRelativePath' => '/',
];
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertOk();
// create 1MB dummy file
$fp = fopen(TEST_FILE, 'w');
fseek($fp, 1 * 1024 * 1024 - 1, SEEK_CUR);
fwrite($fp, 'a');
fclose($fp);
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 3,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 1 * 1024 * 1024,
'resumableTotalChunks' => 3,
'resumableTotalSize' => 2 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-FAILED2-TEST',
'resumableFilename' => 'sample.txt',
'resumableRelativePath' => '/',
];
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertStatus(422);
// create 1MB dummy file
$fp = fopen(TEST_FILE, 'w');
fseek($fp, 1 * 1024 * 1024 - 1, SEEK_CUR);
fwrite($fp, 'a');
fclose($fp);
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 3,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 1 * 1024 * 1024,
'resumableTotalChunks' => 3,
'resumableTotalSize' => 2 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-FAILED2-TEST',
'resumableFilename' => 'sample.txt',
'resumableRelativePath' => '/',
];
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertStatus(422);
}
public function testFileUploadWithBadName()
{
$this->signIn('john@example.com', 'john123');
$files = ['file' => new UploadedFile(TEST_FILE, 'sample.txt', 'text/plain', null, true)];
$data = [
'resumableChunkNumber' => 1,
'resumableChunkSize' => 1048576,
'resumableCurrentChunkSize' => 0.5 * 1024 * 1024,
'resumableTotalChunks' => 1,
'resumableTotalSize' => 0.5 * 1024 * 1024,
'resumableType' => 'text/plain',
'resumableIdentifier' => 'CHUNKS-SIMPLE-TEST',
'resumableFilename' => "../\\s\"u<:>pe////rm?*|an\\.t\txt../;",
'resumableRelativePath' => '/',
];
$this->sendRequest('POST', '/upload', $data, $files);
$this->assertOk();
$this->sendRequest('POST', '/getdir', [
'dir' => '/',
]);
$this->assertResponseJsonHas([
'data' => [
'files' => [
0 => [
'type' => 'file',
'path' => '/..--s-u---pe----rm---an-.t-xt..--',
'name' => '..--s-u---pe----rm---an-.t-xt..--',
],
],
],
]);
}
}