Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Change-Id: I6476fa2afac4aa5b28342629ac563a4ff2717c72
  • Loading branch information
marxjohnson committed Jun 24, 2022
1 parent 37443fe commit b094334
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/fixtures/😀.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test text file with a unicode character in the file name
39 changes: 39 additions & 0 deletions tests/object_file_system_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,45 @@ public function test_can_generate_signed_url_by_hash_if_object_is_external() {
}
}

public function test_can_generate_signed_url_with_headers() {
$this->filesystem = new test_file_system();
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
try {
$headers = [
'Content-Disposition' => 'attachment; filename="filename.txt"',
'Content-Type' => 'text/plain',
];
$signedurl = $this->filesystem->externalclient->generate_presigned_url($filehash, $headers);
$this->assertTrue($this->is_externally_readable_by_url($signedurl));
$urlparams = $signedurl->url->params();
$this->assertEquals($urlparams['response-content-disposition'], 'attachment; filename="filename.txt"');
$this->assertEquals($urlparams['response-content-type'], 'text/plain');
} catch (\coding_exception $e) {
$this->assertEquals($e->a, 'Pre-signed URLs not supported');
}
}

public function test_can_generate_signed_url_with_unicode_filename() {
$this->filesystem = new test_file_system();
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
try {
$headers = [
'Content-Disposition' => 'attachment; filename="😀.txt"',
'Content-Type' => 'text/plain',
];
$signedurl = $this->filesystem->externalclient->generate_presigned_url($filehash, $headers);
$this->assertTrue($this->is_externally_readable_by_url($signedurl));
$urlparams = $signedurl->url->params();
$this->assertEquals($urlparams['response-content-disposition'], 'attachment; filename="%F0%9F%98%80.txt"');
$this->assertEquals($urlparams['response-content-type'], 'text/plain');

} catch (\coding_exception $e) {
$this->assertEquals($e->a, 'Pre-signed URLs not supported');
}
}

public function test_presigned_url_configured_method_returns_false_if_not_configured() {
$this->filesystem = new test_file_system();
$this->assertFalse($this->filesystem->presigned_url_configured());
Expand Down
5 changes: 3 additions & 2 deletions tests/tool_objectfs_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use tool_objectfs\local\manager;
use tool_objectfs\local\object_manipulator\candidates\candidates_finder;
use tool_objectfs\local\store\object_file_system;
use tool_objectfs\local\store\signed_url;

require_once(__DIR__ . '/classes/test_client.php');
require_once(__DIR__ . '/classes/test_file_system.php');
Expand Down Expand Up @@ -198,9 +199,9 @@ protected function delete_draft_files($contenthash) {
$DB->delete_records('files', array('contenthash' => $contenthash));
}

protected function is_externally_readable_by_url($url) {
protected function is_externally_readable_by_url(signed_url $signedurl) {
try {
$file = fopen($url, 'r');
$file = fopen($signedurl->url->out(false), 'r');
if ($file === false) {
$result = false;
} else {
Expand Down

0 comments on commit b094334

Please sign in to comment.