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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Test authentication mechanisms in REST.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Webapi\Authentication;
/**
* @magentoApiDataFixture consumerFixture
*/
class RestTest extends \Magento\TestFramework\TestCase\WebapiAbstract
{
/** @var \Magento\TestFramework\Authentication\Rest\OauthClient[] */
protected $_oAuthClients = [];
/** @var \Magento\Integration\Model\Oauth\Consumer */
protected static $_consumer;
/** @var \Magento\Integration\Model\Oauth\Token */
protected static $_token;
/** @var string */
protected static $_consumerKey;
/** @var string */
protected static $_consumerSecret;
/** @var string */
protected static $_verifier;
protected function setUp()
{
$this->_markTestAsRestOnly();
parent::setUp();
}
/**
* Create a consumer
*/
public static function consumerFixture($date = null)
{
/** Clear the credentials because during the fixture generation, any previous credentials are invalidated */
\Magento\TestFramework\Authentication\OauthHelper::clearApiAccessCredentials();
$consumerCredentials = \Magento\TestFramework\Authentication\OauthHelper::getConsumerCredentials($date);
self::$_consumerKey = $consumerCredentials['key'];
self::$_consumerSecret = $consumerCredentials['secret'];
self::$_verifier = $consumerCredentials['verifier'];
self::$_consumer = $consumerCredentials['consumer'];
self::$_token = $consumerCredentials['token'];
}
protected function tearDown()
{
parent::tearDown();
$this->_oAuthClients = [];
if (isset(self::$_consumer)) {
self::$_consumer->delete();
self::$_token->delete();
}
}
public function testGetRequestToken()
{
/** @var $oAuthClient \Magento\TestFramework\Authentication\Rest\OauthClient */
$oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
$requestToken = $oAuthClient->requestRequestToken();
$this->assertNotEmpty($requestToken->getRequestToken(), "Request token value is not set");
$this->assertNotEmpty($requestToken->getRequestTokenSecret(), "Request token secret is not set");
$this->assertEquals(
\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN,
strlen($requestToken->getRequestToken()),
"Request token value length should be " . \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN
);
$this->assertEquals(
\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET,
strlen($requestToken->getRequestTokenSecret()),
"Request token secret length should be " . \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET
);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage 401 Unauthorized
*/
public function testGetRequestTokenExpiredConsumer()
{
$this::consumerFixture('2012-01-01 00:00:00');
$this::$_consumer->setUpdatedAt('2012-01-01 00:00:00');
$this::$_consumer->save();
/** @var $oAuthClient \Magento\TestFramework\Authentication\Rest\OauthClient */
$oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
$oAuthClient->requestRequestToken();
}
/**
* @expectedException \Exception
* @expectedExceptionMessage 401 Unauthorized
*/
public function testGetRequestTokenInvalidConsumerKey()
{
$oAuthClient = $this->_getOauthClient('invalid_key', self::$_consumerSecret);
$oAuthClient->requestRequestToken();
}
/**
* @expectedException \Exception
* @expectedExceptionMessage 401 Unauthorized
*/
public function testGetRequestTokenInvalidConsumerSecret()
{
$oAuthClient = $this->_getOauthClient(self::$_consumerKey, 'invalid_secret');
$oAuthClient->requestRequestToken();
}
public function testGetAccessToken()
{
$oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
$requestToken = $oAuthClient->requestRequestToken();
$accessToken = $oAuthClient->requestAccessToken(
$requestToken->getRequestToken(),
self::$_verifier,
$requestToken->getRequestTokenSecret()
);
$this->assertNotEmpty($accessToken->getAccessToken(), "Access token value is not set.");
$this->assertNotEmpty($accessToken->getAccessTokenSecret(), "Access token secret is not set.");
$this->assertEquals(
\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN,
strlen($accessToken->getAccessToken()),
"Access token value length should be " . \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN
);
$this->assertEquals(
\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET,
strlen($accessToken->getAccessTokenSecret()),
"Access token secret length should be " . \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET
);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage 401 Unauthorized
*/
public function testGetAccessTokenInvalidVerifier()
{
$oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
$requestToken = $oAuthClient->requestRequestToken();
$oAuthClient->requestAccessToken(
$requestToken->getRequestToken(),
'invalid verifier',
$requestToken->getRequestTokenSecret()
);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage 401 Unauthorized
*/
public function testGetAccessTokenConsumerMismatch()
{
$oAuthClientA = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
$requestTokenA = $oAuthClientA->requestRequestToken();
$oauthVerifierA = self::$_verifier;
self::consumerFixture();
$oAuthClientB = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
$oAuthClientB->requestRequestToken();
$oAuthClientB->requestAccessToken(
$requestTokenA->getRequestToken(),
$oauthVerifierA,
$requestTokenA->getRequestTokenSecret()
);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage 400 Bad Request
*/
public function testAccessApiInvalidAccessToken()
{
$oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
$requestToken = $oAuthClient->requestRequestToken();
$accessToken = $oAuthClient->requestAccessToken(
$requestToken->getRequestToken(),
self::$_verifier,
$requestToken->getRequestTokenSecret()
);
$accessToken->setAccessToken('invalid');
$oAuthClient->validateAccessToken($accessToken);
}
protected function _getOauthClient($consumerKey, $consumerSecret)
{
if (!isset($this->_oAuthClients[$consumerKey])) {
$credentials = new \OAuth\Common\Consumer\Credentials($consumerKey, $consumerSecret, TESTS_BASE_URL);
$this->_oAuthClients[$consumerKey] = new \Magento\TestFramework\Authentication\Rest\OauthClient(
$credentials
);
}
return $this->_oAuthClients[$consumerKey];
}
}