5
5
6
6
import com .azure .core .credential .TokenRequestContext ;
7
7
import com .azure .core .exception .ClientAuthenticationException ;
8
+ import com .azure .core .http .HttpClient ;
9
+ import com .azure .core .test .http .MockHttpResponse ;
8
10
import com .azure .core .test .utils .TestConfigurationSource ;
9
11
import com .azure .core .util .Configuration ;
10
12
import com .azure .identity .implementation .IdentityClient ;
13
+ import com .azure .identity .implementation .IdentityClientOptions ;
11
14
import com .azure .identity .util .TestUtils ;
12
15
import org .junit .jupiter .api .Assertions ;
13
16
import org .junit .jupiter .api .Test ;
17
+ import org .junit .jupiter .params .ParameterizedTest ;
18
+ import org .junit .jupiter .params .provider .ValueSource ;
14
19
import org .mockito .MockedConstruction ;
15
20
import reactor .test .StepVerifier ;
16
21
22
+ import java .nio .charset .StandardCharsets ;
17
23
import java .time .OffsetDateTime ;
18
24
import java .time .ZoneOffset ;
19
25
import java .util .UUID ;
20
26
21
27
import static org .hamcrest .MatcherAssert .assertThat ;
22
28
import static org .hamcrest .core .IsInstanceOf .instanceOf ;
29
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
30
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
23
31
import static org .mockito .Mockito .mockConstruction ;
24
32
import static org .mockito .Mockito .when ;
25
33
@@ -31,7 +39,40 @@ public class ManagedIdentityCredentialTest {
31
39
@ Test
32
40
public void testVirtualMachineMSICredentialConfigurations () {
33
41
ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder ().clientId ("foo" ).build ();
34
- Assertions .assertEquals ("foo" , credential .getClientId ());
42
+ assertEquals ("foo" , credential .getClientId ());
43
+ }
44
+
45
+ @ ParameterizedTest
46
+ @ ValueSource (booleans = {true , false })
47
+ public void testInvalidJsonResponse (boolean isChained ) {
48
+ HttpClient client = TestUtils .getMockHttpClient (
49
+ getMockResponse (400 , "{\" error\" :\" invalid_request\" ,\" error_description\" :\" Required metadata header not specified\" }" ),
50
+ getMockResponse ( 200 , "invalid json" )
51
+ );
52
+
53
+ String endpoint = "http://localhost" ;
54
+ String secret = "secret" ;
55
+
56
+ Configuration configuration
57
+ = TestUtils .createTestConfiguration (new TestConfigurationSource ().put ("MSI_ENDPOINT" , endpoint ) // This must stay to signal we are in an app service context
58
+ .put ("MSI_SECRET" , secret )
59
+ .put ("IDENTITY_ENDPOINT" , endpoint )
60
+ .put ("IDENTITY_HEADER" , secret ));
61
+
62
+ IdentityClientOptions options = new IdentityClientOptions ()
63
+ .setChained (isChained )
64
+ .setHttpClient (client )
65
+ .setConfiguration (configuration );
66
+ ManagedIdentityCredential cred = new ManagedIdentityCredential ("clientId" , null , null , options );
67
+ StepVerifier .create (cred .getToken (new TokenRequestContext ().addScopes ("https://management.azure.com" )))
68
+ .expectErrorMatches (t -> {
69
+ if (isChained ) {
70
+ return t instanceof CredentialUnavailableException ;
71
+ } else {
72
+ return t instanceof ClientAuthenticationException ;
73
+ }
74
+ }).verify ();
75
+
35
76
}
36
77
37
78
@ Test
@@ -129,19 +170,19 @@ public void testInvalidIdCombination() {
129
170
String objectId = "2323-sd2323s-32323-32334-34343" ;
130
171
131
172
// test
132
- Assertions . assertThrows (IllegalStateException .class ,
173
+ assertThrows (IllegalStateException .class ,
133
174
() -> new ManagedIdentityCredentialBuilder ().clientId (CLIENT_ID ).resourceId (resourceId ).build ());
134
175
135
- Assertions . assertThrows (IllegalStateException .class ,
176
+ assertThrows (IllegalStateException .class ,
136
177
() -> new ManagedIdentityCredentialBuilder ().clientId (CLIENT_ID )
137
178
.resourceId (resourceId )
138
179
.objectId (objectId )
139
180
.build ());
140
181
141
- Assertions . assertThrows (IllegalStateException .class ,
182
+ assertThrows (IllegalStateException .class ,
142
183
() -> new ManagedIdentityCredentialBuilder ().clientId (CLIENT_ID ).objectId (objectId ).build ());
143
184
144
- Assertions . assertThrows (IllegalStateException .class ,
185
+ assertThrows (IllegalStateException .class ,
145
186
() -> new ManagedIdentityCredentialBuilder ().resourceId (resourceId ).objectId (objectId ).build ());
146
187
}
147
188
@@ -156,4 +197,8 @@ public void testAksExchangeTokenCredentialCreated() {
156
197
assertThat ("Received class " + cred .managedIdentityServiceCredential .getClass ().toString (),
157
198
cred .managedIdentityServiceCredential , instanceOf (AksExchangeTokenCredential .class ));
158
199
}
200
+
201
+ private MockHttpResponse getMockResponse (int code , String body ) {
202
+ return new MockHttpResponse (null , code , body .getBytes (StandardCharsets .UTF_8 ));
203
+ }
159
204
}
0 commit comments