/
SignatureTest.php
179 lines (152 loc) · 6.08 KB
/
SignatureTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace EddTurtle\DirectUpload\Tests;
use EddTurtle\DirectUpload\InvalidOptionException;
use EddTurtle\DirectUpload\Signature;
class SignatureTest extends \PHPUnit_Framework_TestCase
{
// Bucket contains a '/' just to test that the name in the url is urlencoded.
private $testBucket = "test/bucket";
private $testRegion = "eu-west-1";
public function testInit()
{
$object = new Signature('key', 'secret', $this->testBucket, $this->testRegion);
$this->assertTrue($object instanceof Signature);
return $object;
}
/**
* @expectedException \InvalidArgumentException
*/
public function testMissingKeyOrSecret()
{
new Signature('', '', '', $this->testRegion);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testUnchangedKey()
{
new Signature('YOUR_S3_KEY', 'secret', 'bucket', $this->testRegion);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testUnchangedSecret()
{
new Signature('key', 'YOUR_S3_SECRET', 'bucket', $this->testRegion);
}
/**
* @depends testInit
* @param Signature $object
*/
public function testBuildUrl($object)
{
$url = $object->getFormUrl();
$this->assertEquals("//" . "s3-" . $this->testRegion . ".amazonaws.com/" . urlencode($this->testBucket), $url);
// S3 Url is case-sensitive, make sure casing is preserved
$url = (new Signature('key', 'secret', 'CAPS_BUCKET', 'eu-west-1'))->getFormUrl();
$this->assertEquals("//s3-eu-west-1.amazonaws.com/CAPS_BUCKET", $url);
}
public function testBuildUrlForUsEast()
{
// Note: US East shouldn't contain region in url.
$url = (new Signature('key', 'secret', 'bucket', 'us-east-1'))->getFormUrl();
$this->assertEquals("//s3.amazonaws.com/bucket", $url);
// Test default region param
$url = (new Signature('key', 'secret', 'bucket'))->getFormUrl();
$this->assertEquals("//s3.amazonaws.com/bucket", $url);
}
public function testBuildUrlWithCustomUrl()
{
$url = (new Signature('key', 'secret', 'bucket', 'us-east-1', [
'custom_url' => 'http://www.example.co.uk/'
]))->getFormUrl();
$this->assertEquals("http://www.example.co.uk/bucket", $url);
// Test that trailing slash doesn't matter
$url = (new Signature('key', 'secret', 'bucket', 'us-east-1', [
'custom_url' => 'http://www.example.co.uk'
]))->getFormUrl();
$this->assertEquals("http://www.example.co.uk/bucket", $url);
// Test Invalid Url Exception
try {
(new Signature('key', 'secret', 'testbucket', $this->testRegion, [
'custom_url' => 'not a url'
]))->getFormUrl();
} catch (\Exception $e) {
$this->assertTrue($e instanceof InvalidOptionException);
}
}
public function testGetOptions()
{
$object = new Signature('key', 'secret', 'test', $this->testRegion);
$options = $object->getOptions();
$this->assertTrue(count($options) === 11);
$this->assertArrayHasKey('success_status', $options);
$this->assertArrayHasKey('acl', $options);
$this->assertArrayHasKey('default_filename', $options);
$this->assertArrayHasKey('max_file_size', $options);
$this->assertArrayHasKey('expires', $options);
$this->assertArrayHasKey('valid_prefix', $options);
}
public function testGetSignature()
{
$object = new Signature('key', 'secret', 'testbucket', $this->testRegion);
$signature = $object->getSignature();
$this->assertTrue(strlen($signature) === 64);
// Is alpha numeric?
$this->assertTrue(ctype_alnum($signature));
}
public function testGetFormInputs()
{
$object = new Signature('key', 'secret', 'testbucket', $this->testRegion, [
'acl' => 'public-read',
'success_status' => 200,
'valid_prefix' => 'test/'
]);
$inputs = $object->getFormInputs();
// Check Everything's There
$this->assertArrayHasKey('Content-Type', $inputs);
$this->assertArrayHasKey('policy', $inputs);
$this->assertArrayHasKey('X-amz-credential', $inputs);
$this->assertArrayHasKey('X-amz-algorithm', $inputs);
$this->assertArrayHasKey('X-amz-signature', $inputs);
// Check Values
$this->assertEquals('public-read', $inputs['acl']);
$this->assertEquals('200', $inputs['success_action_status']);
$this->assertEquals(gmdate("Ymd\THis\Z"), $inputs['X-amz-date']);
$this->assertEquals(Signature::ALGORITHM, $inputs['X-amz-algorithm']);
$this->assertEquals('test/${filename}', $inputs['key']);
$this->assertEquals('key/' . date('Ymd') . '/' . $this->testRegion . '/s3/aws4_request', $inputs['X-amz-credential']);
// Test all values as string (and not objects which get cast later)
foreach ($inputs as $input) {
$this->assertInternalType('string', $input);
}
return $object;
}
/**
* @depends testGetFormInputs
* @param Signature $object
*/
public function testGetFormInputsAsHtml($object)
{
$html = $object->getFormInputsAsHtml();
$this->assertContains($object->getSignature(), $html);
$this->assertStringStartsWith('<input type', $html);
}
public function testInvalidExpiryDate()
{
// Test Successful Build
$object = new Signature('key', 'secret', 'testbucket', $this->testRegion, [
'expires' => '+6 hours'
]);
$object->getFormInputs(); // Forces the signature to be built
// Test Exception
try {
$object = new Signature('key', 'secret', 'testbucket', $this->testRegion, [
'expires' => PHP_INT_MAX
]);
$object->getFormInputs(); // Forces the signature to be built
} catch (\Exception $e) {
$this->assertTrue($e instanceof \InvalidArgumentException);
}
}
}