public async Task AzureDevOpsRestApi_GetAuthorityAsync_WwwAuthenticateMultiple_ReturnsBearerAuthority() { var context = new TestCommandContext(); var uri = new Uri("https://example.com"); const string expectedAuthority = "https://login.microsoftonline.com/test-authority"; var httpResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized); httpResponse.Headers.WwwAuthenticate.ParseAdd("Bearer"); httpResponse.Headers.WwwAuthenticate.ParseAdd($"Bearer authorization_uri={expectedAuthority}"); httpResponse.Headers.WwwAuthenticate.ParseAdd("NTLM [test-challenge-string]"); var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; httpHandler.Setup(HttpMethod.Head, uri, httpResponse); context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); string actualAuthority = await api.GetAuthorityAsync(uri); Assert.Equal(expectedAuthority, actualAuthority); }
public async Task AzureDevOpsRestApi_GetAuthorityAsync_BothWwwAuthAndVssResourceHeaders_ReturnsWwwAuthAuthority() { var context = new TestCommandContext(); var uri = new Uri("https://example.com"); var aadTenantIdWwwAuth = Guid.NewGuid(); var aadTenantIdVssRes = Guid.NewGuid(); string expectedAuthority = $"https://login.microsoftonline.com/{aadTenantIdWwwAuth:D}"; var httpResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized); httpResponse.Headers.Add(AzureDevOpsConstants.VssResourceTenantHeader, aadTenantIdVssRes.ToString("D")); httpResponse.Headers.WwwAuthenticate.ParseAdd($"Bearer authorization_uri={expectedAuthority}"); var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; httpHandler.Setup(HttpMethod.Head, uri, httpResponse); context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); string actualAuthority = await api.GetAuthorityAsync(uri); Assert.Equal(expectedAuthority, actualAuthority); }
public async Task AzureDevOpsRestApi_GetAuthorityAsync_VssResourceTenantMsa_ReturnsCommonAuthority() { var context = new TestCommandContext(); var uri = new Uri("https://example.com"); var msaTenantId = Guid.Empty; const string expectedAuthority = CommonAuthority; var httpResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized) { Headers = { { AzureDevOpsConstants.VssResourceTenantHeader, msaTenantId.ToString("D") } } }; var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; httpHandler.Setup(HttpMethod.Head, uri, httpResponse); context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); string actualAuthority = await api.GetAuthorityAsync(uri); Assert.Equal(expectedAuthority, actualAuthority); }
public async Task AzureDevOpsRestApi_GetAuthorityAsync_VssResourceTenantMsa_ReturnsOrganizationsAuthority() { var context = new TestCommandContext(); var uri = new Uri("https://example.com"); var msaTenantId = Guid.Empty; // This is only the case because we're using MSA pass-through.. in the future, if and when we // move away from MSA pass-through, this should be the common authority. const string expectedAuthority = OrganizationsAuthority; var httpResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized) { Headers = { { AzureDevOpsConstants.VssResourceTenantHeader, msaTenantId.ToString("D") } } }; var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; httpHandler.Setup(HttpMethod.Head, uri, httpResponse); context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); string actualAuthority = await api.GetAuthorityAsync(uri); Assert.Equal(expectedAuthority, actualAuthority); }
public async Task AzureDevOpsRestApi_GetAuthorityAsync_NoNetwork_ThrowsException() { var context = new TestCommandContext(); var uri = new Uri("https://example.com"); var httpHandler = new TestHttpMessageHandler { SimulateNoNetwork = true }; context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); await Assert.ThrowsAsync <HttpRequestException>(() => api.GetAuthorityAsync(uri)); }
public async Task AzureDevOpsRestApi_GetAuthorityAsync_NullUri_ThrowsException() { var api = new AzureDevOpsRestApi(new TestCommandContext()); await Assert.ThrowsAsync <ArgumentNullException>(() => api.GetAuthorityAsync(null)); }