Completed
Pull Request — master (#498)
by Dragonqos
02:27
created

testGetSignatureWithBarePathWithExplicitTrailingHostSlash()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth1\Signature;
4
5
use OAuth\OAuth1\Signature\Signature;
6
7
class SignatureTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @covers OAuth\OAuth1\Signature\Signature::__construct
11
     */
12
    public function testConstructCorrectInterface()
13
    {
14
        $signature = new Signature($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'));
15
16
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Signature\\SignatureInterface', $signature);
17
    }
18
19
    /**
20
     * @covers OAuth\OAuth1\Signature\Signature::__construct
21
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
22
     */
23
    public function testSetHashingAlgorithm()
24
    {
25
        $signature = new Signature($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'));
26
27
        $this->assertNull($signature->setHashingAlgorithm('foo'));
28
    }
29
30
    /**
31
     * @covers OAuth\OAuth1\Signature\Signature::__construct
32
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
33
     */
34
    public function testSetTokenSecret()
35
    {
36
        $signature = new Signature($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'));
37
38
        $this->assertNull($signature->setTokenSecret('foo'));
39
    }
40
41
    /**
42
     * @covers OAuth\OAuth1\Signature\Signature::__construct
43
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
44
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
45
     * @covers OAuth\OAuth1\Signature\Signature::getSignature
46
     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
47
     * @covers OAuth\OAuth1\Signature\Signature::hash
48
     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
49
     */
50
    public function testGetSignatureBareUri()
51
    {
52
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
53
        $credentials->expects($this->any())
54
            ->method('getConsumerSecret')
55
            ->will($this->returnValue('foo'));
56
57
58
        $signature = new Signature($credentials);
59
60
        $signature->setHashingAlgorithm('HMAC-SHA1');
61
        $signature->setTokenSecret('foo');
62
63
        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
64
        $uri->expects($this->any())
65
            ->method('getQuery')
66
            ->will($this->returnValue(''));
67
        $uri->expects($this->any())
68
            ->method('getScheme')
69
            ->will($this->returnValue('http'));
70
        $uri->expects($this->any())
71
            ->method('getRawAuthority')
72
            ->will($this->returnValue(''));
73
        $uri->expects($this->any())
74
            ->method('getPath')
75
            ->will($this->returnValue('/foo'));
76
77
        $this->assertSame('uoCpiII/Lg/cPiF0XrU2pj4eGFQ=', $signature->getSignature($uri, array('pee' => 'haa')));
78
    }
79
80
    /**
81
     * @covers OAuth\OAuth1\Signature\Signature::__construct
82
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
83
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
84
     * @covers OAuth\OAuth1\Signature\Signature::getSignature
85
     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
86
     * @covers OAuth\OAuth1\Signature\Signature::hash
87
     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
88
     */
89
    public function testGetSignatureWithQueryString()
90
    {
91
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
92
        $credentials->expects($this->any())
93
            ->method('getConsumerSecret')
94
            ->will($this->returnValue('foo'));
95
96
97
        $signature = new Signature($credentials);
98
99
        $signature->setHashingAlgorithm('HMAC-SHA1');
100
        $signature->setTokenSecret('foo');
101
102
        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
103
        $uri->expects($this->any())
104
            ->method('getQuery')
105
            ->will($this->returnValue('param1=value1'));
106
        $uri->expects($this->any())
107
            ->method('getScheme')
108
            ->will($this->returnValue('http'));
109
        $uri->expects($this->any())
110
            ->method('getRawAuthority')
111
            ->will($this->returnValue(''));
112
        $uri->expects($this->any())
113
            ->method('getPath')
114
            ->will($this->returnValue('/foo'));
115
116
        $this->assertSame('LxtD+WjJBRppIUvEI79iQ7I0hSo=', $signature->getSignature($uri, array('pee' => 'haa')));
117
    }
118
119
    /**
120
     * @covers OAuth\OAuth1\Signature\Signature::__construct
121
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
122
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
123
     * @covers OAuth\OAuth1\Signature\Signature::getSignature
124
     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
125
     * @covers OAuth\OAuth1\Signature\Signature::hash
126
     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
127
     * @covers OAuth\OAuth1\Signature\Signature::prepareParameters
128
     */
129
    public function testGetSignatureWithQueryStringAndBrackets()
130
    {
131
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
132
        $credentials->expects($this->any())
133
            ->method('getConsumerSecret')
134
            ->will($this->returnValue('foo'));
135
136
137
        $signature = new Signature($credentials);
138
139
        $signature->setHashingAlgorithm('HMAC-SHA1');
140
        $signature->setTokenSecret('foo');
141
142
        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
143
        $uri->expects($this->any())
144
            ->method('getQuery')
145
            ->will($this->returnValue('filter[0][attribute]=value1&filter[0][gt]=1'));
146
        $uri->expects($this->any())
147
            ->method('getScheme')
148
            ->will($this->returnValue('http'));
149
        $uri->expects($this->any())
150
            ->method('getRawAuthority')
151
            ->will($this->returnValue(''));
152
        $uri->expects($this->any())
153
            ->method('getPath')
154
            ->will($this->returnValue('/foo'));
155
156
        $this->assertSame('yPiUORaBLEcLBcbOoqQqktcYtlw=', $signature->getSignature($uri, array('pee' => 'haa')));
157
    }
158
159
    /**
160
     * @covers OAuth\OAuth1\Signature\Signature::__construct
161
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
162
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
163
     * @covers OAuth\OAuth1\Signature\Signature::getSignature
164
     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
165
     * @covers OAuth\OAuth1\Signature\Signature::hash
166
     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
167
     */
168
    public function testGetSignatureWithAuthority()
169
    {
170
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
171
        $credentials->expects($this->any())
172
            ->method('getConsumerSecret')
173
            ->will($this->returnValue('foo'));
174
175
176
        $signature = new Signature($credentials);
177
178
        $signature->setHashingAlgorithm('HMAC-SHA1');
179
        $signature->setTokenSecret('foo');
180
181
        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
182
        $uri->expects($this->any())
183
            ->method('getQuery')
184
            ->will($this->returnValue('param1=value1'));
185
        $uri->expects($this->any())
186
            ->method('getScheme')
187
            ->will($this->returnValue('http'));
188
        $uri->expects($this->any())
189
            ->method('getRawAuthority')
190
            ->will($this->returnValue('peehaa:pass'));
191
        $uri->expects($this->any())
192
            ->method('getPath')
193
            ->will($this->returnValue('/foo'));
194
195
        $this->assertSame('MHvkRndIntLrxiPkjkiCNsMEqv4=', $signature->getSignature($uri, array('pee' => 'haa')));
196
    }
197
198
    /**
199
     * @covers OAuth\OAuth1\Signature\Signature::__construct
200
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
201
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
202
     * @covers OAuth\OAuth1\Signature\Signature::getSignature
203
     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
204
     * @covers OAuth\OAuth1\Signature\Signature::hash
205
     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
206
     */
207
    public function testGetSignatureWithBarePathNonExplicitTrailingHostSlash()
208
    {
209
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
210
        $credentials->expects($this->any())
211
            ->method('getConsumerSecret')
212
            ->will($this->returnValue('foo'));
213
214
215
        $signature = new Signature($credentials);
216
217
        $signature->setHashingAlgorithm('HMAC-SHA1');
218
        $signature->setTokenSecret('foo');
219
220
        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
221
        $uri->expects($this->any())
222
            ->method('getQuery')
223
            ->will($this->returnValue('param1=value1'));
224
        $uri->expects($this->any())
225
            ->method('getScheme')
226
            ->will($this->returnValue('http'));
227
        $uri->expects($this->any())
228
            ->method('getRawAuthority')
229
            ->will($this->returnValue('peehaa:pass'));
230
        $uri->expects($this->any())
231
            ->method('getPath')
232
            ->will($this->returnValue('/'));
233
        $uri->expects($this->any())
234
            ->method('hasExplicitTrailingHostSlash')
235
            ->will($this->returnValue(false));
236
237
        $this->assertSame('iFELDoiI5Oj9ixB3kHzoPvBpq0w=', $signature->getSignature($uri, array('pee' => 'haa')));
238
    }
239
240
    /**
241
     * @covers OAuth\OAuth1\Signature\Signature::__construct
242
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
243
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
244
     * @covers OAuth\OAuth1\Signature\Signature::getSignature
245
     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
246
     * @covers OAuth\OAuth1\Signature\Signature::hash
247
     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
248
     */
249
    public function testGetSignatureWithBarePathWithExplicitTrailingHostSlash()
250
    {
251
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
252
        $credentials->expects($this->any())
253
            ->method('getConsumerSecret')
254
            ->will($this->returnValue('foo'));
255
256
257
        $signature = new Signature($credentials);
258
259
        $signature->setHashingAlgorithm('HMAC-SHA1');
260
        $signature->setTokenSecret('foo');
261
262
        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
263
        $uri->expects($this->any())
264
            ->method('getQuery')
265
            ->will($this->returnValue('param1=value1'));
266
        $uri->expects($this->any())
267
            ->method('getScheme')
268
            ->will($this->returnValue('http'));
269
        $uri->expects($this->any())
270
            ->method('getRawAuthority')
271
            ->will($this->returnValue('peehaa:pass'));
272
        $uri->expects($this->any())
273
            ->method('getPath')
274
            ->will($this->returnValue('/'));
275
        $uri->expects($this->any())
276
            ->method('hasExplicitTrailingHostSlash')
277
            ->will($this->returnValue(true));
278
279
        $this->assertSame('IEhUsArSTLvbQ3QYr0zzn+Rxpjg=', $signature->getSignature($uri, array('pee' => 'haa')));
280
    }
281
282
    /**
283
     * @covers OAuth\OAuth1\Signature\Signature::__construct
284
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
285
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
286
     * @covers OAuth\OAuth1\Signature\Signature::getSignature
287
     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
288
     * @covers OAuth\OAuth1\Signature\Signature::hash
289
     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
290
     */
291
    public function testGetSignatureNoTokenSecretSet()
292
    {
293
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
294
        $credentials->expects($this->any())
295
            ->method('getConsumerSecret')
296
            ->will($this->returnValue('foo'));
297
298
299
        $signature = new Signature($credentials);
300
301
        $signature->setHashingAlgorithm('HMAC-SHA1');
302
303
        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
304
        $uri->expects($this->any())
305
            ->method('getQuery')
306
            ->will($this->returnValue('param1=value1'));
307
        $uri->expects($this->any())
308
            ->method('getScheme')
309
            ->will($this->returnValue('http'));
310
        $uri->expects($this->any())
311
            ->method('getRawAuthority')
312
            ->will($this->returnValue('peehaa:pass'));
313
        $uri->expects($this->any())
314
            ->method('getPath')
315
            ->will($this->returnValue('/'));
316
        $uri->expects($this->any())
317
            ->method('hasExplicitTrailingHostSlash')
318
            ->will($this->returnValue(true));
319
320
        $this->assertSame('YMHF7FYmLq7wzGnnHWYtd1VoBBE=', $signature->getSignature($uri, array('pee' => 'haa')));
321
    }
322
323
    /**
324
     * @covers OAuth\OAuth1\Signature\Signature::__construct
325
     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
326
     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
327
     * @covers OAuth\OAuth1\Signature\Signature::getSignature
328
     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
329
     * @covers OAuth\OAuth1\Signature\Signature::hash
330
     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
331
     */
332
    public function testGetSignatureThrowsExceptionOnUnsupportedAlgo()
333
    {
334
        $this->setExpectedException('\\OAuth\\OAuth1\\Signature\\Exception\\UnsupportedHashAlgorithmException');
335
336
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
337
        $credentials->expects($this->any())
338
            ->method('getConsumerSecret')
339
            ->will($this->returnValue('foo'));
340
341
342
        $signature = new Signature($credentials);
343
344
        $signature->setHashingAlgorithm('UnsupportedAlgo');
345
346
        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
347
        $uri->expects($this->any())
348
            ->method('getQuery')
349
            ->will($this->returnValue('param1=value1'));
350
        $uri->expects($this->any())
351
            ->method('getScheme')
352
            ->will($this->returnValue('http'));
353
        $uri->expects($this->any())
354
            ->method('getRawAuthority')
355
            ->will($this->returnValue('peehaa:pass'));
356
        $uri->expects($this->any())
357
            ->method('getPath')
358
            ->will($this->returnValue('/'));
359
        $uri->expects($this->any())
360
            ->method('hasExplicitTrailingHostSlash')
361
            ->will($this->returnValue(true));
362
363
        $signature->getSignature($uri, array('pee' => 'haa'));
364
    }
365
}
366