示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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);
        }
示例#4
0
        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);
        }
示例#5
0
        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));
        }
示例#6
0
        public async Task AzureDevOpsRestApi_GetAuthorityAsync_NullUri_ThrowsException()
        {
            var api = new AzureDevOpsRestApi(new TestCommandContext());

            await Assert.ThrowsAsync <ArgumentNullException>(() => api.GetAuthorityAsync(null));
        }