tests(oss): 增加 oss 票据生成的测试文件

This commit is contained in:
李东云
2023-01-09 18:23:17 +08:00
parent 52ff5ccb04
commit b5970d5434

View File

@@ -0,0 +1,50 @@
<?php
/**
* OssServiceTest.php@HDK-Core
*
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
* Powered by PhpStorm
* Created on 2023/1/9
*/
use Hyperf\Utils\Codec\Json;
use Singularity\HDK\Core\Service\OssService;
use function Spatie\PestPluginTestTime\testTime;
it('asserts oss policy can be generated', function ($setDir, $isImage, $maxSize) {
testTime()->freeze();
$oss = new OssService(
'{MOCK_ACCESS_KEY_ID}',
'{MOCK_ACCESS_KEY_SECRET}',
'{MOCK_HOST}',
'{MOCK_CALLBACK_URL}',
30,
);
[
'accessid' => $accessId,
'host' => $host,
'policy' => $policy,
'signature' => $signature,
'expire' => $expire,
'dir' => $dir,
'callback' => $callback,
] = $oss->generatePolicy($setDir, $isImage, $maxSize);
$sign = base64_encode(hash_hmac('sha1', $policy, '{MOCK_ACCESS_KEY_SECRET}', true));
$policy = Json::decode(base64_decode($policy));
$callback = Json::decode(base64_decode($callback));
expect($accessId)->toBe('{MOCK_ACCESS_KEY_ID}')
->and($host)->toBe('{MOCK_HOST}')
->and($dir)->toBe($setDir)
->and($setDir)->toBe($policy['conditions'][1][2])
->and($expire)->toBe(testTime()->addSeconds(30)->unix())
->and($policy['expiration'])->toBe(testTime()->setMilli(0)->toIso8601ZuluString('m'))
->and($maxSize)->toBe($policy['conditions'][0][2])
->and($callback['callbackUrl'])->toBe('{MOCK_CALLBACK_URL}')
->and($signature)->toBe($sign);
})->with([
['uploaded', true, 1048576000],
])->only();