forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthTest.php
More file actions
116 lines (98 loc) · 3.43 KB
/
Copy pathOAuthTest.php
File metadata and controls
116 lines (98 loc) · 3.43 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
<?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\Upload;
use Cloudinary\Api\Exception\ApiError;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Test\Helpers\MockUploadApi;
use Cloudinary\Test\Helpers\RequestAssertionsTrait;
use Cloudinary\Test\Unit\Asset\AssetTestCase;
use InvalidArgumentException;
/**
* Class OAuthTest
*/
final class OAuthTest extends AssetTestCase
{
use RequestAssertionsTrait;
const FAKE_OAUTH_TOKEN = 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4';
const API_TEST_PRESET = 'api_test_upload_preset';
/**
* Should upload an asset using an Oauth Token.
*
* @throws ApiError
*/
public function testOauthTokenUploadApi()
{
$config = new Configuration(Configuration::instance());
$config->cloud->oauthToken(self::FAKE_OAUTH_TOKEN);
$uploadApi = new MockUploadApi($config);
$uploadApi->upload(self::TEST_BASE64_IMAGE);
$lastRequest = $uploadApi->getMockHandler()->getLastRequest();
self::assertRequestHeaderSubset(
$lastRequest,
[
'Authorization' => ['Bearer ' . self::FAKE_OAUTH_TOKEN]
]
);
self::assertArrayHasKey(
'User-Agent',
$lastRequest->getHeaders(),
'User-Agent header must be present alongside Authorization when using OAuth token'
);
}
/**
* Should upload an asset using `apiKey` and `apiSecret` if an Oauth Token is absent.
*
* @throws ApiError
*/
public function testKeyAndSecretUploadApi()
{
$config = new Configuration(Configuration::instance());
$config->cloud->oauthToken(null);
$uploadApi = new MockUploadApi($config);
$uploadApi->upload(self::TEST_BASE64_IMAGE);
$params = $uploadApi->getApiClient()->getRequestMultipartOptions();
self::assertArrayHasKey('api_key', $params);
self::assertArrayHasKey('signature', $params);
}
/**
* Should be thrown an exception if `apiKey` and `apiSecret` or an Oauth Token are absent.
*
* @throws ApiError
*/
public function testMissingCredentialsUploadApi()
{
$config = new Configuration(Configuration::instance());
$config->cloud->oauthToken(null);
$config->cloud->apiKey = "";
$config->cloud->apiSecret = "";
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Must supply apiKey');
$uploadApi = new MockUploadApi($config);
$uploadApi->upload(self::TEST_BASE64_IMAGE);
}
/**
* Should upload an asset using an upload preset if `apiKey` and `apiSecret` or an Oauth Token are absent.
*
* @throws ApiError
*/
public function testMissingCredentialsUnsignedUploadApi()
{
$config = new Configuration(Configuration::instance());
$config->cloud->oauthToken(null);
$config->cloud->apiKey = null;
$config->cloud->apiSecret = null;
$uploadApi = new MockUploadApi($config);
$uploadApi->unsignedUpload(self::TEST_BASE64_IMAGE, self::API_TEST_PRESET);
self::assertSame(
self::API_TEST_PRESET,
$uploadApi->getApiClient()->getRequestMultipartOptions()['upload_preset']
);
}
}