public async Task AzureDevOpsRestApi_CreatePersonalAccessTokenAsync_IdentSvcReturnsHttp500_ThrowsException() { var context = new TestCommandContext(); var orgUri = new Uri("https://dev.azure.com/org/"); JsonWebToken accessToken = CreateJwt(); IEnumerable <string> scopes = new[] { AzureDevOpsConstants.PersonalAccessTokenScopes.ReposWrite }; var identityServiceUri = new Uri("https://identity.example.com/"); var locSvcRequestUri = new Uri(orgUri, ExpectedLocationServicePath); var locSvcResponse = CreateLocationServiceResponse(identityServiceUri); var identSvcRequestUri = new Uri(identityServiceUri, ExpectedIdentityServicePath); var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; httpHandler.Setup(HttpMethod.Get, locSvcRequestUri, x => { AssertAcceptJson(x); AssertBearerToken(x, accessToken); return(locSvcResponse); }); httpHandler.Setup(HttpMethod.Post, identSvcRequestUri, HttpStatusCode.InternalServerError); context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); await Assert.ThrowsAsync <Exception>(() => api.CreatePersonalAccessTokenAsync(orgUri, accessToken, scopes)); }
public async Task AzureDevOpsRestApi_CreatePersonalAccessTokenAsync_ReturnsPAT() { var context = new TestCommandContext(); var orgUri = new Uri("https://dev.azure.com/org/"); const string expectedPat = "PERSONAL-ACCESS-TOKEN"; JsonWebToken accessToken = CreateJwt(); IEnumerable <string> scopes = new[] { AzureDevOpsConstants.PersonalAccessTokenScopes.ReposWrite }; var identityServiceUri = new Uri("https://identity.example.com/"); var locSvcRequestUri = new Uri(orgUri, ExpectedLocationServicePath); var locSvcResponse = CreateLocationServiceResponse(identityServiceUri); var identSvcRequestUri = new Uri(identityServiceUri, ExpectedIdentityServicePath); var identSvcResponse = CreateIdentityServiceResponse(expectedPat); var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; httpHandler.Setup(HttpMethod.Get, locSvcRequestUri, x => { AssertAcceptJson(x); AssertBearerToken(x, accessToken); return(locSvcResponse); }); httpHandler.Setup(HttpMethod.Post, identSvcRequestUri, x => { AssertAcceptJson(x); AssertBearerToken(x, accessToken); return(identSvcResponse); }); context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); string actualPat = await api.CreatePersonalAccessTokenAsync(orgUri, accessToken, scopes); Assert.Equal(expectedPat, actualPat); }
public async Task AzureDevOpsRestApi_CreatePersonalAccessTokenAsync_LocSvcReturnsHttp500_ThrowsException() { var context = new TestCommandContext(); var orgUri = new Uri("https://dev.azure.com/org/"); const string accessToken = "ACCESS-TOKEN"; IEnumerable <string> scopes = new[] { AzureDevOpsConstants.PersonalAccessTokenScopes.ReposWrite }; var locSvcRequestUri = new Uri(orgUri, ExpectedLocationServicePath); var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; httpHandler.Setup(HttpMethod.Get, locSvcRequestUri, HttpStatusCode.InternalServerError); context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); await Assert.ThrowsAsync <Exception>(() => api.CreatePersonalAccessTokenAsync(orgUri, accessToken, scopes)); }
public async Task AzureDevOpsRestApi_CreatePersonalAccessTokenAsync_IdentSvcReturnsHttp500WithError_ThrowsExceptionWithErrorMessage() { const string serverErrorMessage = "ERROR123: This is a test error."; var context = new TestCommandContext(); var orgUri = new Uri("https://dev.azure.com/org/"); const string accessToken = "ACCESS-TOKEN"; IEnumerable <string> scopes = new[] { AzureDevOpsConstants.PersonalAccessTokenScopes.ReposWrite }; var identityServiceUri = new Uri("https://identity.example.com/"); var locSvcRequestUri = new Uri(orgUri, ExpectedLocationServicePath); var locSvcResponse = CreateLocationServiceResponse(identityServiceUri); var identSvcRequestUri = new Uri(identityServiceUri, ExpectedIdentityServicePath); var identSvcError = CreateIdentityServiceErrorResponse(serverErrorMessage); var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; httpHandler.Setup(HttpMethod.Get, locSvcRequestUri, x => { AssertAcceptJson(x); AssertBearerToken(x, accessToken); return(locSvcResponse); }); httpHandler.Setup(HttpMethod.Post, identSvcRequestUri, _ => identSvcError); context.HttpClientFactory.MessageHandler = httpHandler; var api = new AzureDevOpsRestApi(context); Exception exception = await Assert.ThrowsAsync <Exception>( () => api.CreatePersonalAccessTokenAsync(orgUri, accessToken, scopes)); Assert.Contains(serverErrorMessage, exception.Message, StringComparison.Ordinal); }