|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Framework\Oauth\Test\Unit\Helper; |
| 9 | + |
| 10 | +use Magento\Framework\Oauth\Helper\Utility as OauthUtility; |
| 11 | +use Laminas\OAuth\Http\Utility as LaminasUtility; |
| 12 | +use Magento\Framework\Oauth\Helper\Signature\Hmac256; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +class UtilityTest extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var LaminasUtility|MockObject |
| 20 | + */ |
| 21 | + private LaminasUtility $httpUtility; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var Hmac256|MockObject |
| 25 | + */ |
| 26 | + private Hmac256 $hmac256; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->httpUtility = $this->createMock(LaminasUtility::class); |
| 31 | + $this->hmac256 = $this->createMock(Hmac256::class); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public function testSignMethodUsesHmac256WhenSignatureMethodIsHmacSha256(): void |
| 38 | + { |
| 39 | + $params = ['param1' => 'value1']; |
| 40 | + $signatureMethod = 'HMACSHA256'; |
| 41 | + $consumerSecret = 'secret'; |
| 42 | + $tokenSecret = 'tokenSecret'; |
| 43 | + $method = 'POST'; |
| 44 | + $url = 'http://example.com'; |
| 45 | + |
| 46 | + $expectedSignature = 'expectedSignature'; |
| 47 | + |
| 48 | + $this->hmac256->expects($this->once()) |
| 49 | + ->method('sign') |
| 50 | + ->with($params, 'sha256', $consumerSecret, $tokenSecret, $method, $url) |
| 51 | + ->willReturn($expectedSignature); |
| 52 | + |
| 53 | + $utility = new OauthUtility($this->httpUtility, $this->hmac256); |
| 54 | + |
| 55 | + $signature = $utility->sign($params, $signatureMethod, $consumerSecret, $tokenSecret, $method, $url); |
| 56 | + |
| 57 | + $this->assertEquals($expectedSignature, $signature); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function testSignMethodUsesLaminasUtilityWhenSignatureMethodIsRsa(): void |
| 64 | + { |
| 65 | + $params = ['param1' => 'value1']; |
| 66 | + $signatureMethod = 'RSA'; |
| 67 | + $consumerSecret = 'secret'; |
| 68 | + $tokenSecret = 'tokenSecret'; |
| 69 | + $method = 'POST'; |
| 70 | + $url = 'http://example.com'; |
| 71 | + |
| 72 | + $expectedSignature = 'expectedSignature'; |
| 73 | + |
| 74 | + $this->httpUtility->expects($this->once()) |
| 75 | + ->method('sign') |
| 76 | + ->with($params, $signatureMethod, $consumerSecret, $tokenSecret, $method, $url) |
| 77 | + ->willReturn($expectedSignature); |
| 78 | + |
| 79 | + $utility = new OauthUtility($this->httpUtility, $this->hmac256); |
| 80 | + |
| 81 | + $signature = $utility->sign($params, $signatureMethod, $consumerSecret, $tokenSecret, $method, $url); |
| 82 | + |
| 83 | + $this->assertEquals($expectedSignature, $signature); |
| 84 | + } |
| 85 | +} |
0 commit comments