Fixed some bugs, added working example.
[Net-Amazon-S3-Policy.git] / t / 01.test.t
blobb4ee9bf138102b657cc190687c35ab9229e5c36d
1 # vim: filetype=perl :
2 use strict;
3 use warnings;
5 use Test::More tests => 14; # last test to print
7 my $module;
9 BEGIN {
10    $module = 'Net::Amazon::S3::Policy';
11    use_ok($module);
15    my $expiration = '1220292602';
16    my $policy = $module->new({expiration => $expiration});
18    ok($policy, 'new with expiration time');
19    isa_ok($policy, $module);
20    is($policy->expiration(), '2008-09-01T18:10:02.000Z',
21       'expiration time');
23    $policy->add($_)
24      for (
25       q( prova eq ciao ),
26       q( provaxxx starts-with ciao ),
27       q( 0 <= something <= 123312391 ),
28       q( anything * ),
29       q( anything2 starts_with ),
30       q( anything2 starts_with blah ),
31      );
33    my $conditions = $policy->conditions();
34    ok($conditions, 'conditions exists');
35    is(scalar(@$conditions), 6, 'all conditions added');
36    is_deeply(
37       $conditions,
38       [
39          [qw( eq $prova ciao )],
40          [qw( starts-with $provaxxx ciao )],
41          [qw( something 0 123312391 )],
42          [qw( starts-with $anything ),  ''],
43          [qw( starts-with $anything2 ), ''],
44          [qw( starts-with $anything2 blah )],
45       ],
46       'conditions match'
47    );
49    my $expected_json =
50 '{"expiration":"2008-09-01T18:10:02.000Z","conditions":[["eq","$prova","ciao"],["starts-with","$provaxxx","ciao"],["something","0","123312391"],["starts-with","$anything",""],["starts-with","$anything2",""],["starts-with","$anything2","blah"]]}';
51    is($policy->stringify(), $expected_json, 'JSON generation');
55    my $json =
56 '{"expiration":"2008-09-01T18:10:02.000Z","conditions":[["eq","$prova","ciao"],["starts-with","$provaxxx","ciao"],["something","0","123312391"],["starts-with","$anything",""],["starts-with","$anything2",""],["starts-with","$anything2","blah"],{"what":"this"}]}';
58    my $policy = Net::Amazon::S3::Policy->new(json => $json);
59    ok($policy, 'new with expiration time');
60    isa_ok($policy, $module);
61    is($policy->expiration(), '2008-09-01T18:10:02.000Z',
62       'expiration time');
64    my $conditions = $policy->conditions();
65    ok($conditions, 'conditions exists');
66    is(scalar(@$conditions), 7, 'all conditions present');
67    is_deeply(
68       $conditions,
69       [
70          [qw( eq $prova ciao )],
71          [qw( starts-with $provaxxx ciao )],
72          [qw( something 0 123312391 )],
73          [qw( starts-with $anything ),  ''],
74          [qw( starts-with $anything2 ), ''],
75          [qw( starts-with $anything2 blah )],
76          [qw( eq $what this )],
77       ],
78       'conditions match',
79    );