Amazon-S3-Thin

 view release on metacpan or  search on metacpan

t/05_presigned_post.t  view on Meta::CPAN

use JSON::PP ();

BEGIN {
    *CORE::GLOBAL::time = sub() { 1572566400 }; # Fri Nov  1 00:00:00 2019
}

my $_JSON;
sub policy { MIME::Base64::encode_base64(($_JSON ||= JSON::PP->new->utf8->canonical)->encode(@_), '') }

my $args = {
    aws_access_key_id     => 'dummy_access_key_id',
    aws_secret_access_key => 'dummy_secret_access_key',
    region                => 'ap-north-east-1',
    ua                    => MockUA->new,
};
my $client = Amazon::S3::Thin->new({%$args});

my $bucket = 'test-bucket';
my $key = 'dir/private.txt';

is_deeply $client->generate_presigned_post($bucket, $key), {
    url    => 'http://s3.ap-north-east-1.amazonaws.com/test-bucket/',
    fields => [
        key                => $key,
        'x-amz-algorithm'  => 'AWS4-HMAC-SHA256',
        'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request',
        'x-amz-date'       => '20191101T000000Z',
        policy             => policy({
            conditions => [
                {bucket             => 'test-bucket'},
                {key                => 'dir/private.txt'},
                {'x-amz-algorithm'  => 'AWS4-HMAC-SHA256'},
                {'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request'},
                {'x-amz-date'       => '20191101T000000Z'},
            ],
            expiration => '2019-11-01T01:00:00Z',
        }),
        'x-amz-signature' => 'ef0b9394345aa20773e07ecdf1f704135a0fd7a5db78e2e5433b7331668f1cc3',
    ],
};

is_deeply $client->generate_presigned_post($bucket, 'foo-${filename}'), {
    url    => 'http://s3.ap-north-east-1.amazonaws.com/test-bucket/',
    fields => [
        key                => 'foo-${filename}',
        'x-amz-algorithm'  => 'AWS4-HMAC-SHA256',
        'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request',
        'x-amz-date'       => '20191101T000000Z',
        policy             => policy({
            conditions => [
                {bucket             => 'test-bucket'},
                ['starts-with'      => '$key', 'foo-'],
                {'x-amz-algorithm'  => 'AWS4-HMAC-SHA256'},
                {'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request'},
                {'x-amz-date'       => '20191101T000000Z'},
            ],
            expiration => '2019-11-01T01:00:00Z',
        }),
        'x-amz-signature' => '28baaf6392b4a747f1e65d9bb33f205342e025bf23f3d7ab37de880828ac0f52',
    ],
}, '${filename} expands starts-with condition';

is_deeply $client->generate_presigned_post($bucket, $key, [
    'Content-Type'   => 'image/png',
    'x-amz-meta-foo' => 'bar',
]), {
    url    => 'http://s3.ap-north-east-1.amazonaws.com/test-bucket/',
    fields => [
        'Content-Type'   => 'image/png',
        'x-amz-meta-foo' => 'bar',
        key                => $key,
        'x-amz-algorithm'  => 'AWS4-HMAC-SHA256',
        'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request',
        'x-amz-date'       => '20191101T000000Z',
        policy             => policy({
            conditions => [
                {bucket             => 'test-bucket'},
                {key                => 'dir/private.txt'},
                {'x-amz-algorithm'  => 'AWS4-HMAC-SHA256'},
                {'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request'},
                {'x-amz-date'       => '20191101T000000Z'},
            ],
            expiration => '2019-11-01T01:00:00Z',
        }),
        'x-amz-signature' => 'ef0b9394345aa20773e07ecdf1f704135a0fd7a5db78e2e5433b7331668f1cc3',
    ],
}, 'passing $fields is prepended to fields';

is_deeply $client->generate_presigned_post($bucket, $key, [], [
    {acl => 'public-read'},
    ['content-length-range' => 1048579, 10485760],
]), {
    url    => 'http://s3.ap-north-east-1.amazonaws.com/test-bucket/',
    fields => [
        key                => $key,
        'x-amz-algorithm'  => 'AWS4-HMAC-SHA256',
        'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request',
        'x-amz-date'       => '20191101T000000Z',
        policy             => policy({
            conditions => [
                {acl => 'public-read'},
                ['content-length-range' => 1048579, 10485760],
                {bucket             => 'test-bucket'},
                {key                => 'dir/private.txt'},
                {'x-amz-algorithm'  => 'AWS4-HMAC-SHA256'},
                {'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request'},
                {'x-amz-date'       => '20191101T000000Z'},
            ],
            expiration => '2019-11-01T01:00:00Z',
        }),
        'x-amz-signature' => 'bde43ca051cb44488f029018910a13e3dfca0d9bbfcc02c19629c17d2626af0d',
    ],
}, 'passing $conditions is prepended to conditions in policy';

is_deeply $client->generate_presigned_post($bucket, $key, [], [], 24 * 60 * 60), {
    url    => 'http://s3.ap-north-east-1.amazonaws.com/test-bucket/',
    fields => [
        key                => $key,
        'x-amz-algorithm'  => 'AWS4-HMAC-SHA256',
        'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request',
        'x-amz-date'       => '20191101T000000Z',



( run in 2.575 seconds using v1.01-cache-2.11-cpan-5c0b1e786e0 )