mirror of
https://github.com/filegator/filegator.git
synced 2025-08-22 19:32:48 +02:00
initial commit
This commit is contained in:
574
tests/backend/Feature/FilesTest.php
Normal file
574
tests/backend/Feature/FilesTest.php
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user