public async Task CreateListUpdateDelete() { Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback"); using (MockContext context = MockContext.Start(this.GetType().FullName)) { var testBase = new ApiManagementTestBase(context); testBase.TryCreateApiManagementService(); // list all the APIs var listResponse = testBase.client.Api.ListByService( testBase.rgName, testBase.serviceName, null); Assert.NotNull(listResponse); Assert.Single(listResponse); Assert.Null(listResponse.NextPageLink); var echoApi = listResponse.First(); Assert.Equal("Echo API", echoApi.DisplayName); Assert.Null(echoApi.Description); Assert.Equal("http://echoapi.cloudapp.net/api", echoApi.ServiceUrl); Assert.Equal("echo", echoApi.Path); Assert.NotNull(echoApi.Protocols); Assert.Equal(1, echoApi.Protocols.Count); Assert.Equal(Protocol.Https, echoApi.Protocols[0]); // get the API by id var getResponse = await testBase.client.Api.GetWithHttpMessagesAsync( testBase.rgName, testBase.serviceName, echoApi.Name); Assert.NotNull(getResponse); Assert.Equal("Echo API", getResponse.Body.DisplayName); Assert.Null(getResponse.Body.Description); Assert.Equal("http://echoapi.cloudapp.net/api", getResponse.Body.ServiceUrl); Assert.Equal("echo", getResponse.Body.Path); Assert.NotNull(getResponse.Body.Protocols); Assert.Equal(1, getResponse.Body.Protocols.Count); Assert.Equal(Protocol.Https, getResponse.Body.Protocols[0]); // add new api // create autorization server string newApiAuthorizationServerId = TestUtilities.GenerateName("authorizationServerId"); string newApiId = TestUtilities.GenerateName("apiid"); string newOpenApiId = TestUtilities.GenerateName("openApiid"); string openIdNoSecret = TestUtilities.GenerateName("openId"); try { var createAuthServerParams = new AuthorizationServerContract { DisplayName = TestUtilities.GenerateName("authName"), DefaultScope = TestUtilities.GenerateName("oauth2scope"), AuthorizationEndpoint = "https://contoso.com/auth", TokenEndpoint = "https://contoso.com/token", ClientRegistrationEndpoint = "https://contoso.com/clients/reg", GrantTypes = new List <string> { GrantType.AuthorizationCode, GrantType.Implicit }, AuthorizationMethods = new List <AuthorizationMethod?> { AuthorizationMethod.POST, AuthorizationMethod.GET }, BearerTokenSendingMethods = new List <string> { BearerTokenSendingMethod.AuthorizationHeader, BearerTokenSendingMethod.Query }, ClientId = TestUtilities.GenerateName("clientid") }; testBase.client.AuthorizationServer.CreateOrUpdate( testBase.rgName, testBase.serviceName, newApiAuthorizationServerId, createAuthServerParams); string newApiName = TestUtilities.GenerateName("apiname"); string newApiDescription = TestUtilities.GenerateName("apidescription"); string newApiPath = "newapiPath"; string newApiServiceUrl = "http://newechoapi.cloudapp.net/api"; string subscriptionKeyParametersHeader = TestUtilities.GenerateName("header"); string subscriptionKeyQueryStringParamName = TestUtilities.GenerateName("query"); string newApiAuthorizationScope = TestUtilities.GenerateName("oauth2scope"); var newApiAuthenticationSettings = new AuthenticationSettingsContract { OAuth2 = new OAuth2AuthenticationSettingsContract { AuthorizationServerId = newApiAuthorizationServerId, Scope = newApiAuthorizationScope } }; var createdApiContract = testBase.client.Api.CreateOrUpdate( testBase.rgName, testBase.serviceName, newApiId, new ApiCreateOrUpdateParameter { DisplayName = newApiName, Description = newApiDescription, Path = newApiPath, ServiceUrl = newApiServiceUrl, Protocols = new List <Protocol?> { Protocol.Https, Protocol.Http }, SubscriptionKeyParameterNames = new SubscriptionKeyParameterNamesContract { Header = subscriptionKeyParametersHeader, Query = subscriptionKeyQueryStringParamName }, AuthenticationSettings = newApiAuthenticationSettings }); // get new api to check it was added var apiGetResponse = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiGetResponse); Assert.Equal(newApiId, apiGetResponse.Name); Assert.Equal(newApiName, apiGetResponse.DisplayName); Assert.Equal(newApiDescription, apiGetResponse.Description); Assert.Equal(newApiPath, apiGetResponse.Path); Assert.Equal(newApiServiceUrl, apiGetResponse.ServiceUrl); Assert.Equal(subscriptionKeyParametersHeader, apiGetResponse.SubscriptionKeyParameterNames.Header); Assert.Equal(subscriptionKeyQueryStringParamName, apiGetResponse.SubscriptionKeyParameterNames.Query); Assert.Equal(2, apiGetResponse.Protocols.Count); Assert.True(apiGetResponse.Protocols.Contains(Protocol.Http)); Assert.True(apiGetResponse.Protocols.Contains(Protocol.Https)); Assert.NotNull(apiGetResponse.AuthenticationSettings); Assert.NotNull(apiGetResponse.AuthenticationSettings.OAuth2); Assert.Equal(newApiAuthorizationServerId, apiGetResponse.AuthenticationSettings.OAuth2.AuthorizationServerId); // get the API Entity Tag ApiGetEntityTagHeaders apiTag = testBase.client.Api.GetEntityTag( testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiTag); Assert.NotNull(apiTag.ETag); // patch added api string patchedName = TestUtilities.GenerateName("patchedname"); string patchedDescription = TestUtilities.GenerateName("patchedDescription"); string patchedPath = TestUtilities.GenerateName("patchedPath"); testBase.client.Api.Update( testBase.rgName, testBase.serviceName, newApiId, new ApiUpdateContract { DisplayName = patchedName, Description = patchedDescription, Path = patchedPath, AuthenticationSettings = new AuthenticationSettingsContract { OAuth2 = null } }, apiTag.ETag); // get patched api to check it was patched apiGetResponse = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiGetResponse); Assert.Equal(newApiId, apiGetResponse.Name); Assert.Equal(patchedName, apiGetResponse.DisplayName); Assert.Equal(patchedDescription, apiGetResponse.Description); Assert.Equal(patchedPath, apiGetResponse.Path); Assert.Equal(newApiServiceUrl, apiGetResponse.ServiceUrl); Assert.Equal(subscriptionKeyParametersHeader, apiGetResponse.SubscriptionKeyParameterNames.Header); Assert.Equal(subscriptionKeyQueryStringParamName, apiGetResponse.SubscriptionKeyParameterNames.Query); Assert.Equal(2, apiGetResponse.Protocols.Count); Assert.True(apiGetResponse.Protocols.Contains(Protocol.Http)); Assert.True(apiGetResponse.Protocols.Contains(Protocol.Https)); // add an api with OpenId authentication settings // create a openId connect provider string openIdProviderName = TestUtilities.GenerateName("openIdName"); string metadataEndpoint = testBase.GetOpenIdMetadataEndpointUrl(); string clientId = TestUtilities.GenerateName("clientId"); var openIdConnectCreateParameters = new OpenidConnectProviderContract(openIdProviderName, metadataEndpoint, clientId); var openIdCreateResponse = testBase.client.OpenIdConnectProvider.CreateOrUpdate( testBase.rgName, testBase.serviceName, openIdNoSecret, openIdConnectCreateParameters); Assert.NotNull(openIdCreateResponse); Assert.Equal(openIdProviderName, openIdCreateResponse.DisplayName); Assert.Equal(openIdNoSecret, openIdCreateResponse.Name); string newOpenIdApiName = TestUtilities.GenerateName("apiname"); string newOpenIdApiDescription = TestUtilities.GenerateName("apidescription"); string newOpenIdApiPath = "newOpenapiPath"; string newOpenIdApiServiceUrl = "http://newechoapi2.cloudapp.net/api"; string newOpenIdAuthorizationScope = TestUtilities.GenerateName("oauth2scope"); var newnewOpenIdAuthenticationSettings = new AuthenticationSettingsContract { Openid = new OpenIdAuthenticationSettingsContract { OpenidProviderId = openIdCreateResponse.Name, BearerTokenSendingMethods = new[] { BearerTokenSendingMethods.AuthorizationHeader } } }; var createdOpenApiIdContract = testBase.client.Api.CreateOrUpdate( testBase.rgName, testBase.serviceName, newOpenApiId, new ApiCreateOrUpdateParameter { DisplayName = newOpenIdApiName, Description = newOpenIdApiDescription, Path = newOpenIdApiPath, ServiceUrl = newOpenIdApiServiceUrl, Protocols = new List <Protocol?> { Protocol.Https, Protocol.Http }, SubscriptionKeyParameterNames = new SubscriptionKeyParameterNamesContract { Header = subscriptionKeyParametersHeader, Query = subscriptionKeyQueryStringParamName }, AuthenticationSettings = newnewOpenIdAuthenticationSettings }); // get new api to check it was added var openApiGetResponse = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newOpenApiId); Assert.NotNull(openApiGetResponse); Assert.Equal(newOpenApiId, openApiGetResponse.Name); Assert.Equal(newOpenIdApiName, openApiGetResponse.DisplayName); Assert.Equal(newOpenIdApiDescription, openApiGetResponse.Description); Assert.Equal(newOpenIdApiPath, openApiGetResponse.Path); Assert.Equal(newOpenIdApiServiceUrl, openApiGetResponse.ServiceUrl); Assert.Equal(subscriptionKeyParametersHeader, openApiGetResponse.SubscriptionKeyParameterNames.Header); Assert.Equal(subscriptionKeyQueryStringParamName, openApiGetResponse.SubscriptionKeyParameterNames.Query); Assert.Equal(2, openApiGetResponse.Protocols.Count); Assert.True(openApiGetResponse.Protocols.Contains(Protocol.Http)); Assert.True(openApiGetResponse.Protocols.Contains(Protocol.Https)); Assert.NotNull(openApiGetResponse.AuthenticationSettings.Openid); Assert.Equal(openIdCreateResponse.Name, openApiGetResponse.AuthenticationSettings.Openid.OpenidProviderId); // list with paging listResponse = testBase.client.Api.ListByService( testBase.rgName, testBase.serviceName, new Microsoft.Rest.Azure.OData.ODataQuery <ApiContract> { Top = 1 }); Assert.NotNull(listResponse); Assert.Single(listResponse); Assert.NotNull(listResponse.NextPageLink); listResponse = testBase.client.Api.ListByServiceNext(listResponse.NextPageLink); Assert.NotNull(listResponse); Assert.Single(listResponse); Assert.NotNull(listResponse.NextPageLink); // delete the api testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newApiId, "*"); // get the deleted api to make sure it was deleted try { testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId); throw new Exception("This code should not have been executed."); } catch (ErrorResponseException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } // delete the api testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newOpenApiId, "*"); // get the deleted api to make sure it was deleted try { testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newOpenApiId); throw new Exception("This code should not have been executed."); } catch (ErrorResponseException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } } finally { // delete api server testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newApiId, "*"); testBase.client.AuthorizationServer.Delete( testBase.rgName, testBase.serviceName, newApiAuthorizationServerId, "*"); // delete api server testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newOpenApiId, "*"); testBase.client.OpenIdConnectProvider.Delete( testBase.rgName, testBase.serviceName, openIdNoSecret, "*"); } } }
public async Task CreateListUpdateDelete() { Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback"); using (MockContext context = MockContext.Start(this.GetType().FullName)) { var testBase = new ApiManagementTestBase(context); testBase.TryCreateApiManagementService(); string openIdNoSecret = TestUtilities.GenerateName("openId"); string openId2 = TestUtilities.GenerateName("openId"); try { // create a openId connect provider string openIdProviderName = TestUtilities.GenerateName("openIdName"); string metadataEndpoint = testBase.GetOpenIdMetadataEndpointUrl(); string clientId = TestUtilities.GenerateName("clientId"); var openIdConnectCreateParameters = new OpenidConnectProviderContract(openIdProviderName, metadataEndpoint, clientId); var createResponse = testBase.client.OpenIdConnectProvider.CreateOrUpdate( testBase.rgName, testBase.serviceName, openIdNoSecret, openIdConnectCreateParameters); Assert.NotNull(createResponse); Assert.Equal(openIdProviderName, createResponse.DisplayName); Assert.Equal(openIdNoSecret, createResponse.Name); // get to check it was created var openIdConnectProviderContract = await testBase.client.OpenIdConnectProvider.GetAsync( testBase.rgName, testBase.serviceName, openIdNoSecret); Assert.NotNull(openIdConnectProviderContract); Assert.Equal(openIdProviderName, openIdConnectProviderContract.DisplayName); Assert.Equal(metadataEndpoint, openIdConnectProviderContract.MetadataEndpoint); Assert.Equal(clientId, openIdConnectProviderContract.ClientId); Assert.Equal(openIdNoSecret, openIdConnectProviderContract.Name); Assert.Null(openIdConnectProviderContract.ClientSecret); Assert.Null(openIdConnectProviderContract.Description); // create a Secret property string openIdProviderName2 = TestUtilities.GenerateName("openIdName"); string metadataEndpoint2 = testBase.GetOpenIdMetadataEndpointUrl(); string clientId2 = TestUtilities.GenerateName("clientId"); string clientSecret = TestUtilities.GenerateName("clientSecret"); var openIdConnectCreateParameters2 = new OpenidConnectProviderContract(openIdProviderName2, metadataEndpoint2, clientId2); openIdConnectCreateParameters2.ClientSecret = clientSecret; openIdConnectCreateParameters2.Description = TestUtilities.GenerateName("description"); var createResponse2 = testBase.client.OpenIdConnectProvider.CreateOrUpdate( testBase.rgName, testBase.serviceName, openId2, openIdConnectCreateParameters2); Assert.NotNull(createResponse2); Assert.Equal(openIdProviderName2, createResponse2.DisplayName); Assert.Equal(openId2, createResponse2.Name); // get to check it was created var getResponse2 = testBase.client.OpenIdConnectProvider.Get(testBase.rgName, testBase.serviceName, openId2); Assert.NotNull(getResponse2); Assert.Equal(openIdProviderName2, getResponse2.DisplayName); Assert.Equal(clientId2, getResponse2.ClientId); Assert.Equal(metadataEndpoint2, getResponse2.MetadataEndpoint); Assert.NotNull(getResponse2.ClientSecret); Assert.Equal(clientSecret, getResponse2.ClientSecret); Assert.NotNull(getResponse2.Description); Assert.Equal(openId2, getResponse2.Name); // list the openId Connect Providers var listResponse = testBase.client.OpenIdConnectProvider.ListByService(testBase.rgName, testBase.serviceName, null); Assert.NotNull(listResponse); // there should be atleast 2 openId connect Providers. Assert.True(listResponse.Count() >= 2); Assert.NotNull(listResponse.NextPageLink); // list using Query listResponse = testBase.client.OpenIdConnectProvider.ListByService( testBase.rgName, testBase.serviceName, new Microsoft.Rest.Azure.OData.ODataQuery <OpenidConnectProviderContract> { Top = 1 }); Assert.NotNull(listResponse); Assert.Single(listResponse); Assert.NotNull(listResponse.NextPageLink); // get the openid connect provider tag var openIdConnectProviderTag = await testBase.client.OpenIdConnectProvider.GetEntityTagAsync( testBase.rgName, testBase.serviceName, openIdNoSecret); Assert.NotNull(openIdConnectProviderTag); Assert.NotNull(openIdConnectProviderTag.ETag); // delete a OpenId Connect Provider await testBase.client.OpenIdConnectProvider.DeleteAsync( testBase.rgName, testBase.serviceName, openIdNoSecret, openIdConnectProviderTag.ETag); // get the deleted openId Connect Provider to make sure it was deleted try { testBase.client.OpenIdConnectProvider.Get(testBase.rgName, testBase.serviceName, openIdNoSecret); throw new Exception("This code should not have been executed."); } catch (ErrorResponseException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } // get the etag of openId2 openIdConnectProviderTag = await testBase.client.OpenIdConnectProvider.GetEntityTagAsync( testBase.rgName, testBase.serviceName, openId2); Assert.NotNull(openIdConnectProviderTag); Assert.NotNull(openIdConnectProviderTag.ETag); // patch the openId Connect Provider string updateMetadataEndpoint = testBase.GetOpenIdMetadataEndpointUrl(); string updatedClientId = TestUtilities.GenerateName("updatedClient"); testBase.client.OpenIdConnectProvider.Update( testBase.rgName, testBase.serviceName, openId2, new OpenidConnectProviderUpdateContract { MetadataEndpoint = updateMetadataEndpoint, ClientId = updatedClientId }, openIdConnectProviderTag.ETag); // get to check it was patched var getResponseOpendId2 = await testBase.client.OpenIdConnectProvider.GetWithHttpMessagesAsync( testBase.rgName, testBase.serviceName, openId2); Assert.NotNull(getResponseOpendId2); Assert.NotNull(getResponseOpendId2.Body); Assert.NotNull(getResponseOpendId2.Headers.ETag); Assert.Equal(openId2, getResponseOpendId2.Body.Name); Assert.Equal(updatedClientId, getResponseOpendId2.Body.ClientId); Assert.Equal(updateMetadataEndpoint, getResponseOpendId2.Body.MetadataEndpoint); Assert.Equal(clientSecret, getResponseOpendId2.Body.ClientSecret); Assert.NotNull(getResponseOpendId2.Body.Description); // delete the openId Connect Provider testBase.client.OpenIdConnectProvider.Delete( testBase.rgName, testBase.serviceName, openId2, getResponseOpendId2.Headers.ETag); // get the deleted openId Connect Provider to make sure it was deleted try { testBase.client.OpenIdConnectProvider.Get(testBase.rgName, testBase.serviceName, openId2); throw new Exception("This code should not have been executed."); } catch (ErrorResponseException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } } finally { testBase.client.OpenIdConnectProvider.Delete( testBase.rgName, testBase.serviceName, openIdNoSecret, "*"); testBase.client.OpenIdConnectProvider.Delete( testBase.rgName, testBase.serviceName, openId2, "*"); } } }