7
7
8
8
namespace Magento \AdobeIms \Test \Unit \Model ;
9
9
10
+ use Laminas \Uri \Uri ;
10
11
use Magento \AdobeIms \Model \Authorization ;
11
12
use Magento \AdobeImsApi \Api \ConfigInterface ;
12
13
use Magento \Framework \Exception \InvalidArgumentException ;
13
14
use Magento \Framework \HTTP \Client \Curl ;
14
15
use Magento \Framework \HTTP \Client \CurlFactory ;
16
+ use Magento \Framework \Stdlib \Parameters ;
15
17
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
16
18
use PHPUnit \Framework \TestCase ;
17
19
18
- class GetAuthorizationUrlTest extends TestCase
20
+ class AuthorizationTest extends TestCase
19
21
{
20
22
private const AUTH_URL = 'https://adobe-login-url.com/authorize ' .
21
23
'?client_id=AdobeCommerceIMS ' .
@@ -26,6 +28,8 @@ class GetAuthorizationUrlTest extends TestCase
26
28
27
29
private const AUTH_URL_ERROR = 'https://adobe-login-url.com/authorize?error=invalid_scope ' ;
28
30
31
+ private const REDIRECT_URL = 'https://magento-instance.local ' ;
32
+
29
33
/**
30
34
* @var CurlFactory
31
35
*/
@@ -35,6 +39,14 @@ class GetAuthorizationUrlTest extends TestCase
35
39
* @var Authorization
36
40
*/
37
41
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 ;
38
50
39
51
protected function setUp (): void
40
52
{
@@ -45,16 +57,69 @@ protected function setUp(): void
45
57
->method ('getAuthUrl ' )
46
58
->willReturn (self ::AUTH_URL );
47
59
$ 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
+ ]);
49
92
$ this ->authorizationUrl = $ objectManagerHelper ->getObject (
50
93
Authorization::class,
51
94
[
52
95
'curlFactory ' => $ this ->curlFactory ,
53
- 'imsConfig ' => $ imsConfigMock
96
+ 'imsConfig ' => $ imsConfigMock ,
97
+ 'parameters ' => $ this ->parametersMock ,
98
+ 'uri ' => $ this ->uriMock
54
99
]
55
100
);
56
101
}
57
102
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
+ */
58
123
public function testAuthThrowsExceptionWhenResponseCodeIs200 (): void
59
124
{
60
125
$ curlMock = $ this ->createMock (Curl::class);
@@ -71,6 +136,9 @@ public function testAuthThrowsExceptionWhenResponseCodeIs200(): void
71
136
$ this ->authorizationUrl ->getAuthUrl ();
72
137
}
73
138
139
+ /**
140
+ * Test auth throws exception code when response contains error
141
+ */
74
142
public function testAuthThrowsExceptionWhenResponseContainsError (): void
75
143
{
76
144
$ curlMock = $ this ->createMock (Curl::class);
0 commit comments