-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathAssetAuthTokenTest.php
More file actions
177 lines (149 loc) · 5.86 KB
/
AssetAuthTokenTest.php
File metadata and controls
177 lines (149 loc) · 5.86 KB
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
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Test\Unit\Asset;
use Cloudinary\Asset\AuthToken;
use Cloudinary\Asset\Image;
use Cloudinary\Asset\DeliveryType;
use Cloudinary\Transformation\Scale;
use Cloudinary\Utils;
use UnexpectedValueException;
/**
* Class AuthTokenTest
*/
class AssetAuthTokenTest extends AuthTokenTestCase
{
const EXPECTED_VERSIONED_PATH = self::TEST_ASSET_VERSION_STR.'/'.self::AUTH_TOKEN_TEST_IMAGE;
/**
* @var Image $image Test image that is commonly reused by tests
*/
protected $image;
public function setUp()
{
parent::setUp();
$this->image = new Image(self::AUTH_TOKEN_TEST_IMAGE);
$this->image->asset->deliveryType = DeliveryType::AUTHENTICATED;
$this->image->asset->version = self::TEST_ASSET_VERSION;
}
public function testShouldAddTokenIfAuthTokenIsGloballySetAndSignedIsTrue()
{
$message = 'Should add token if authToken is globally set and signed = true';
$expectedToken = '__cld_token__=st=11111111~exp=11111411~'.
'hmac=8db0d753ee7bbb9e2eaf8698ca3797436ba4c20e31f44527e43b6a6e995cfdb3';
self::assertImageUrl(
self::EXPECTED_VERSIONED_PATH."?$expectedToken",
$this->image,
[
'delivery_type' => DeliveryType::AUTHENTICATED,
'private_cdn' => true,
'message' => $message,
]
);
}
public function testShouldAddTokenForPublicResource()
{
$message = "Should add token for 'public' resource";
$this->image->asset->deliveryType = DeliveryType::PUBLIC_DELIVERY;
$expectedToken = '__cld_token__=st=11111111~exp=11111411~'.
'hmac=c2b77d9f81be6d89b5d0ebc67b671557e88a40bcf03dd4a6997ff4b994ceb80e';
self::assertImageUrl(
self::EXPECTED_VERSIONED_PATH."?$expectedToken",
$this->image,
[
'delivery_type' => DeliveryType::PUBLIC_DELIVERY,
'private_cdn' => true,
'message' => $message,
]
);
}
public function testShouldNotAddTokenIfSignedIsFalse()
{
$message = 'Should not add token if signed is null';
self::assertImageUrl(
self::EXPECTED_VERSIONED_PATH,
$this->image->signUrl(null),
[
'delivery_type' => DeliveryType::AUTHENTICATED,
'private_cdn' => true,
'message' => $message,
]
);
}
public function testNullToken()
{
$message = 'Should not add token if authToken is globally set but null auth token is explicitly set '.
'and signed = true';
$this->image->authToken->config->key = null;
$this->image->cloud->apiSecret = 'b';
$this->image->cloud->signatureAlgorithm = Utils::ALGO_SHA1;
$this->image->urlConfig->longUrlSignature = false;
self::assertImageUrl(
's--v2fTPYTu--/'.self::EXPECTED_VERSIONED_PATH,
$this->image,
[
'delivery_type' => DeliveryType::AUTHENTICATED,
'private_cdn' => true,
'message' => $message,
]
);
}
public function testExplicitAuthTokenShouldOverrideGlobalSetting()
{
$message = 'Explicit authToken should override global setting';
$this->image->authToken->config->key = self::AUTH_TOKEN_ALT_KEY;
$this->image->authToken->config->startTime = 222222222;
$this->image->authToken->config->duration = 100;
$this->image->resize(Scale::scale(300));
$this->image->asset->version = null;
$expectedToken = '__cld_token__=st=222222222~exp=222222322~'.
'hmac=55cfe516530461213fe3b3606014533b1eca8ff60aeab79d1bb84c9322eebc1f';
self::assertImageUrl(
'c_scale,w_300/'.self::AUTH_TOKEN_TEST_IMAGE."?$expectedToken",
$this->image,
[
'delivery_type' => DeliveryType::AUTHENTICATED,
'private_cdn' => true,
'message' => $message,
]
);
}
public function testShouldComputeExpirationAsStartTimePlusDuration()
{
$message = 'Should compute expiration as start time + duration';
$expectedToken = '__cld_token__=st=11111111~exp=11111411~'.
'hmac=8db0d753ee7bbb9e2eaf8698ca3797436ba4c20e31f44527e43b6a6e995cfdb3';
self::assertImageUrl(
self::EXPECTED_VERSIONED_PATH."?$expectedToken",
$this->image,
[
'delivery_type' => DeliveryType::AUTHENTICATED,
'private_cdn' => true,
'message' => $message,
]
);
}
public function testShouldThrowWhenAclAndUrlAreMissing()
{
$authToken = new AuthToken();
$authToken->config->startTime = self::START_TIME;
$authToken->config->duration = self::DURATION;
$authToken->config->acl = self::AUTH_TOKEN_TEST_CONFIG_ACL;
$authToken->generate();
$authToken = new AuthToken();
$authToken->config->startTime = self::START_TIME;
$authToken->config->duration = self::DURATION;
$authToken->generate(self::AUTH_TOKEN_TEST_PATH);
$authToken = new AuthToken();
$authToken->config->startTime = self::START_TIME;
$authToken->config->duration = self::DURATION;
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('AuthToken must contain either acl or url property');
$authToken->generate();
}
}