|
8 | 8 | import org.powermock.modules.testng.PowerMockTestCase; |
9 | 9 | import org.testng.Assert; |
10 | 10 | import org.testng.annotations.BeforeMethod; |
| 11 | +import org.testng.annotations.DataProvider; |
11 | 12 | import org.testng.annotations.Test; |
12 | 13 |
|
13 | 14 | import java.net.URI; |
14 | 15 | import java.net.URL; |
| 16 | +import java.util.Collections; |
15 | 17 |
|
16 | 18 | @PrepareForTest(AadInstanceDiscoveryProvider.class) |
17 | 19 | public class AadInstanceDiscoveryTest extends PowerMockTestCase { |
@@ -186,4 +188,137 @@ public void aadInstanceDiscoveryTest_AutoDetectRegion_NoRegionDetected() throws |
186 | 188 | Assert.assertTrue(entry.aliases().contains("login.microsoft.com")); |
187 | 189 | Assert.assertTrue(entry.aliases().contains("sts.windows.net")); |
188 | 190 | } |
| 191 | + |
| 192 | + @DataProvider(name = "aadClouds") |
| 193 | + private static Object[][] getAadClouds(){ |
| 194 | + return new Object[][] {{"https://login.microsoftonline.com/common"} , // #Known to Microsoft |
| 195 | + {"https://private.cloud/foo"}//Private Cloud |
| 196 | + }; |
| 197 | + } |
| 198 | + |
| 199 | + @DataProvider(name = "b2cAdfsClouds") |
| 200 | + private static Object[][] getNonAadClouds(){ |
| 201 | + return new Object[][] {{"https://contoso.com/adfs"}//ADFS |
| 202 | +// {"https://login.b2clogin.com/contoso/b2c_policy"},//B2C |
| 203 | + }; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * when instance_discovery flag is set to true (by default), an instance_discovery is performed for authorityType = AAD and |
| 208 | + * hence, an exception is thrown while making a call to getMetaDataEntry() if instanceDiscoveryResponse is not mocked. |
| 209 | + */ |
| 210 | + @Test( dataProvider = "aadClouds", |
| 211 | + expectedExceptions = StringIndexOutOfBoundsException.class) |
| 212 | + public void aad_instance_discovery_true(String authority) throws Exception { |
| 213 | + |
| 214 | + PublicClientApplication app = PublicClientApplication.builder("client_id") |
| 215 | + .authority(authority) |
| 216 | + .build(); |
| 217 | + |
| 218 | + AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder( |
| 219 | + "code", new URI("http://my.redirect.com")) |
| 220 | + .scopes(Collections.singleton("scope")).build(); |
| 221 | + |
| 222 | + MsalRequest msalRequest = new AuthorizationCodeRequest( |
| 223 | + parameters, |
| 224 | + app, |
| 225 | + new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters)); |
| 226 | + |
| 227 | + URL authorityURL = new URL(authority); |
| 228 | + |
| 229 | + AadInstanceDiscoveryProvider.getMetadataEntry( |
| 230 | + authorityURL, |
| 231 | + false, |
| 232 | + msalRequest, |
| 233 | + app.getServiceBundle()); |
| 234 | + |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * when instance_discovery flag is set to true (by default), an instance_discovery is NOT performed for b2c. |
| 239 | + */ |
| 240 | + @Test( dataProvider = "b2cAdfsClouds") |
| 241 | + public void b2c_adfs_instance_discovery_true(String authority) throws Exception { |
| 242 | + |
| 243 | + PublicClientApplication app = PublicClientApplication.builder("client_id") |
| 244 | + .authority(authority) |
| 245 | + .build(); |
| 246 | + |
| 247 | + AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder( |
| 248 | + "code", new URI("http://my.redirect.com")) |
| 249 | + .scopes(Collections.singleton("scope")).build(); |
| 250 | + |
| 251 | + MsalRequest msalRequest = new AuthorizationCodeRequest( |
| 252 | + parameters, |
| 253 | + app, |
| 254 | + new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters)); |
| 255 | + |
| 256 | + URL authorityURL = new URL(authority); |
| 257 | + |
| 258 | + AadInstanceDiscoveryProvider.getMetadataEntry( |
| 259 | + authorityURL, |
| 260 | + false, |
| 261 | + msalRequest, |
| 262 | + app.getServiceBundle()); |
| 263 | + } |
| 264 | + |
| 265 | + @Test (dataProvider = "aadClouds") |
| 266 | + /** |
| 267 | + * when instance_discovery flag is set to false, instance_discovery is not performed and hence, |
| 268 | + * no exception is thrown while making a call to getMetaDataEntry() even when instanceDiscoveryResponse is not mocked. |
| 269 | + */ |
| 270 | + public void aad_instance_discovery_false(String authority) throws Exception{ |
| 271 | + |
| 272 | + PublicClientApplication app = PublicClientApplication.builder("client_id") |
| 273 | + .authority(authority) |
| 274 | + .instanceDiscovery(false) |
| 275 | + .build(); |
| 276 | + |
| 277 | + AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder( |
| 278 | + "code", new URI("http://my.redirect.com")) |
| 279 | + .scopes(Collections.singleton("scope")).build(); |
| 280 | + |
| 281 | + MsalRequest msalRequest = new AuthorizationCodeRequest( |
| 282 | + parameters, |
| 283 | + app, |
| 284 | + new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters)); |
| 285 | + |
| 286 | + URL authorityURL = new URL(authority); |
| 287 | + |
| 288 | + AadInstanceDiscoveryProvider.getMetadataEntry( |
| 289 | + authorityURL, |
| 290 | + false, |
| 291 | + msalRequest, |
| 292 | + app.getServiceBundle()); |
| 293 | + } |
| 294 | + |
| 295 | + @Test (dataProvider = "b2cAdfsClouds") |
| 296 | + /** |
| 297 | + * when instance_discovery flag is set to true, instance_discovery is not performed and hence, |
| 298 | + * no exception is thrown while making a call to getMetaDataEntry() even when instanceDiscoveryResponse is not mocked. |
| 299 | + */ |
| 300 | + public void b2c_adfs_instance_discovery_false(String authority) throws Exception{ |
| 301 | + |
| 302 | + PublicClientApplication app = PublicClientApplication.builder("client_id") |
| 303 | + .authority(authority) |
| 304 | + .instanceDiscovery(false) |
| 305 | + .build(); |
| 306 | + |
| 307 | + AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder( |
| 308 | + "code", new URI("http://my.redirect.com")) |
| 309 | + .scopes(Collections.singleton("scope")).build(); |
| 310 | + |
| 311 | + MsalRequest msalRequest = new AuthorizationCodeRequest( |
| 312 | + parameters, |
| 313 | + app, |
| 314 | + new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters)); |
| 315 | + |
| 316 | + URL authorityURL = new URL(authority); |
| 317 | + |
| 318 | + AadInstanceDiscoveryProvider.getMetadataEntry( |
| 319 | + authorityURL, |
| 320 | + false, |
| 321 | + msalRequest, |
| 322 | + app.getServiceBundle()); |
| 323 | + } |
189 | 324 | } |
0 commit comments