77
88namespace Magento \AdobeIms \Test \Unit \Model ;
99
10+ use Laminas \Uri \Uri ;
1011use Magento \AdobeIms \Model \Authorization ;
1112use Magento \AdobeImsApi \Api \ConfigInterface ;
1213use Magento \Framework \Exception \InvalidArgumentException ;
1314use Magento \Framework \HTTP \Client \Curl ;
1415use Magento \Framework \HTTP \Client \CurlFactory ;
16+ use Magento \Framework \Stdlib \Parameters ;
1517use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
1618use PHPUnit \Framework \TestCase ;
1719
18- class GetAuthorizationUrlTest extends TestCase
20+ class AuthorizationTest extends TestCase
1921{
2022 private const AUTH_URL = 'https://adobe-login-url.com/authorize ' .
2123 '?client_id=AdobeCommerceIMS ' .
@@ -26,6 +28,8 @@ class GetAuthorizationUrlTest extends TestCase
2628
2729 private const AUTH_URL_ERROR = 'https://adobe-login-url.com/authorize?error=invalid_scope ' ;
2830
31+ private const REDIRECT_URL = 'https://magento-instance.local ' ;
32+
2933 /**
3034 * @var CurlFactory
3135 */
@@ -35,6 +39,14 @@ class GetAuthorizationUrlTest extends TestCase
3539 * @var Authorization
3640 */
3741 private $ authorizationUrl ;
42+ /**
43+ * @var Parameters|\PHPUnit\Framework\MockObject\MockObject
44+ */
45+ private mixed $ parametersMock ;
46+ /**
47+ * @var Parameters|\PHPUnit\Framework\MockObject\MockObject
48+ */
49+ private mixed $ uriMock ;
3850
3951 protected function setUp (): void
4052 {
@@ -45,16 +57,69 @@ protected function setUp(): void
4557 ->method ('getAuthUrl ' )
4658 ->willReturn (self ::AUTH_URL );
4759 $ this ->curlFactory = $ this ->createMock (CurlFactory::class);
48-
60+ $ this ->parametersMock = $ this ->createMock (Parameters::class);
61+ $ this ->uriMock = $ this ->createMock (Uri::class);
62+ $ urlParts = [];
63+ $ url = self ::AUTH_URL ;
64+ $ this ->uriMock ->expects ($ this ->any ())
65+ ->method ('parse ' )
66+ ->willReturnCallback (
67+ function ($ url ) use (&$ urlParts ) {
68+ $ urlParts = parse_url ($ url );
69+ }
70+ );
71+ $ this ->uriMock ->expects ($ this ->any ())
72+ ->method ('getHost ' )
73+ ->willReturnCallback (
74+ function () use (&$ urlParts ) {
75+ return array_key_exists ('host ' , $ urlParts ) ? $ urlParts ['host ' ] : '' ;
76+ }
77+ );
78+ $ this ->uriMock ->expects ($ this ->any ())
79+ ->method ('getQuery ' )
80+ ->willReturnCallback (
81+ function () {
82+ return 'callback= ' . self ::REDIRECT_URL ;
83+ }
84+ );
85+ $ this ->parametersMock ->method ('fromString ' )
86+ ->with ('callback= ' . self ::REDIRECT_URL )
87+ ->willReturnSelf ();
88+ $ this ->parametersMock ->method ('toArray ' )
89+ ->willReturn ([
90+ 'redirect_uri ' => self ::REDIRECT_URL
91+ ]);
4992 $ this ->authorizationUrl = $ objectManagerHelper ->getObject (
5093 Authorization::class,
5194 [
5295 'curlFactory ' => $ this ->curlFactory ,
53- 'imsConfig ' => $ imsConfigMock
96+ 'imsConfig ' => $ imsConfigMock ,
97+ 'parameters ' => $ this ->parametersMock ,
98+ 'uri ' => $ this ->uriMock
5499 ]
55100 );
56101 }
57102
103+ /**
104+ * Test IMS host belongs to correct project
105+ */
106+ public function testAuthUrlValidateImsHostBelongsToCorrectProject (): void
107+ {
108+ $ curlMock = $ this ->createMock (Curl::class);
109+ $ curlMock ->method ('getHeaders ' )
110+ ->willReturn (['location ' => self ::AUTH_URL ]);
111+ $ curlMock ->method ('getStatus ' )
112+ ->willReturn (302 );
113+
114+ $ this ->curlFactory ->method ('create ' )
115+ ->willReturn ($ curlMock );
116+
117+ $ this ->assertEquals ($ this ->authorizationUrl ->getAuthUrl (), self ::AUTH_URL );
118+ }
119+
120+ /**
121+ * Test auth throws exception code when response code is 200
122+ */
58123 public function testAuthThrowsExceptionWhenResponseCodeIs200 (): void
59124 {
60125 $ curlMock = $ this ->createMock (Curl::class);
@@ -71,6 +136,9 @@ public function testAuthThrowsExceptionWhenResponseCodeIs200(): void
71136 $ this ->authorizationUrl ->getAuthUrl ();
72137 }
73138
139+ /**
140+ * Test auth throws exception code when response contains error
141+ */
74142 public function testAuthThrowsExceptionWhenResponseContainsError (): void
75143 {
76144 $ curlMock = $ this ->createMock (Curl::class);
0 commit comments