|
21 | 21 | import java.net.MalformedURLException; |
22 | 22 | import java.net.URL; |
23 | 23 | import java.util.ArrayList; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.HashSet; |
24 | 26 | import java.util.List; |
25 | 27 |
|
26 | 28 | public class TokenCredentialAuthProviderTest { |
27 | 29 |
|
28 | 30 | public final String testToken = "CredentialTestToken"; |
| 31 | + private static final HashSet<String> validGraphHostNames = new HashSet<>(Arrays.asList("graph.microsoft.com", "graph.microsoft.us", "dod-graph.microsoft.us", "graph.microsoft.de", "microsoftgraph.chinacloudapi.cn")); |
29 | 32 |
|
30 | 33 | @Test |
31 | 34 | public void TokenCredentialAuthProviderTestICoreAuthentication() throws AuthenticationException { |
32 | 35 |
|
33 | | - // Arrange |
34 | | - final Request request = new Request.Builder().url("https://graph.microsoft.com").build(); |
35 | | - final TokenCredential mockCredential = MockTokenCredential.getMockTokenCredential(); |
36 | | - final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(mockCredential); |
37 | | - |
38 | | - // Act |
39 | | - Assert.assertNull(request.header(InternalAuthConstants.AUTHORIZATION_HEADER)); |
40 | | - final Request authenticatedRequest = authProvider.authenticateRequest(request); |
41 | | - |
42 | | - // Assert |
43 | | - Assert.assertEquals(request.url(), authenticatedRequest.url()); |
44 | | - Assert.assertNotNull(authenticatedRequest.header(InternalAuthConstants.AUTHORIZATION_HEADER)); |
45 | | - assertEquals(InternalAuthConstants.BEARER + testToken, authenticatedRequest.header(InternalAuthConstants.AUTHORIZATION_HEADER)); |
| 36 | + for(final String hostName : validGraphHostNames) { |
| 37 | + // Arrange |
| 38 | + final Request request = new Request.Builder().url("https://" + hostName).build(); |
| 39 | + final TokenCredential mockCredential = MockTokenCredential.getMockTokenCredential(); |
| 40 | + final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(mockCredential); |
| 41 | + |
| 42 | + // Act |
| 43 | + Assert.assertNull(request.header(InternalAuthConstants.AUTHORIZATION_HEADER)); |
| 44 | + final Request authenticatedRequest = authProvider.authenticateRequest(request); |
| 45 | + |
| 46 | + // Assert |
| 47 | + Assert.assertEquals(request.url(), authenticatedRequest.url()); |
| 48 | + Assert.assertNotNull(authenticatedRequest.header(InternalAuthConstants.AUTHORIZATION_HEADER)); |
| 49 | + assertEquals(InternalAuthConstants.BEARER + testToken, authenticatedRequest.header(InternalAuthConstants.AUTHORIZATION_HEADER)); |
| 50 | + } |
46 | 51 | } |
47 | 52 |
|
48 | 53 | @Test |
49 | 54 | public void TokenCredentialAuthProviderTestIAuthentication() throws AuthenticationException, MalformedURLException { |
50 | 55 |
|
51 | | - // Arrange |
52 | | - final TokenCredential mockCredential = MockTokenCredential.getMockTokenCredential(); |
53 | | - final IHttpRequest request = mock(IHttpRequest.class); |
54 | | - final List<HeaderOption> headersOptions = new ArrayList<>(); |
55 | | - when(request.getRequestUrl()).thenReturn(new URL("https://graph.microsoft.com")); |
56 | | - doAnswer(new Answer<Void>() { |
57 | | - |
58 | | - @Override |
59 | | - public Void answer(InvocationOnMock invocation) throws Throwable { |
60 | | - headersOptions.add(new HeaderOption((String)invocation.getArguments()[0], (String)invocation.getArguments()[1])); |
61 | | - return null; |
62 | | - } |
63 | | - |
64 | | - }).when(request).addHeader(any(String.class), any(String.class)); |
65 | | - when(request.getHeaders()).thenReturn(headersOptions); |
66 | | - final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(mockCredential); |
67 | | - |
68 | | - //Act |
69 | | - Assert.assertTrue(request.getHeaders().isEmpty()); |
70 | | - authProvider.authenticateRequest(request); |
71 | | - Assert.assertFalse(request.getHeaders().isEmpty()); |
72 | | - |
73 | | - //Assert |
74 | | - Assert.assertTrue(request.getHeaders().get(0).getName().equals(InternalAuthConstants.AUTHORIZATION_HEADER)); |
75 | | - Assert.assertTrue(request.getHeaders().get(0).getValue().equals(InternalAuthConstants.BEARER + this.testToken)); |
| 56 | + for(final String hostName : validGraphHostNames) { |
| 57 | + // Arrange |
| 58 | + final TokenCredential mockCredential = MockTokenCredential.getMockTokenCredential(); |
| 59 | + final IHttpRequest request = mock(IHttpRequest.class); |
| 60 | + final List<HeaderOption> headersOptions = new ArrayList<>(); |
| 61 | + when(request.getRequestUrl()).thenReturn(new URL("https://" + hostName)); |
| 62 | + doAnswer(new Answer<Void>() { |
| 63 | + |
| 64 | + @Override |
| 65 | + public Void answer(InvocationOnMock invocation) throws Throwable { |
| 66 | + headersOptions.add(new HeaderOption((String)invocation.getArguments()[0], (String)invocation.getArguments()[1])); |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + }).when(request).addHeader(any(String.class), any(String.class)); |
| 71 | + when(request.getHeaders()).thenReturn(headersOptions); |
| 72 | + final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(mockCredential); |
| 73 | + |
| 74 | + //Act |
| 75 | + Assert.assertTrue(request.getHeaders().isEmpty()); |
| 76 | + authProvider.authenticateRequest(request); |
| 77 | + Assert.assertFalse(request.getHeaders().isEmpty()); |
| 78 | + |
| 79 | + //Assert |
| 80 | + Assert.assertTrue(request.getHeaders().get(0).getName().equals(InternalAuthConstants.AUTHORIZATION_HEADER)); |
| 81 | + Assert.assertTrue(request.getHeaders().get(0).getValue().equals(InternalAuthConstants.BEARER + this.testToken)); |
| 82 | + } |
76 | 83 | } |
77 | 84 | @Test |
78 | 85 | public void TokenCredentialAuthProviderDoesNotAddTokenOnInvalidDomainsTestICoreAuthentication() throws AuthenticationException { |
|
0 commit comments