public void ProfileGetListTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType())) { // Create clients var cdnMgmtClient = CdnTestUtilities.GetCdnManagementClient(context, handler1); var resourcesClient = CdnTestUtilities.GetResourceManagementClient(context, handler2); // Create resource group var resourceGroupName = CdnTestUtilities.CreateResourceGroup(resourcesClient); // List profiles should return none var profiles = cdnMgmtClient.Profiles.ListByResourceGroup(resourceGroupName); Assert.Empty(profiles); // Create a standard cdn profile string profileName = TestUtilities.GenerateName("profile"); Profile createParameters = new Profile { Location = "WestUs", Sku = new Sku { Name = SkuName.StandardVerizon }, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } } }; var profile = cdnMgmtClient.Profiles.Create(resourceGroupName, profileName, createParameters); VerifyProfileCreated(profile, createParameters); // Get profile returns the created profile var existingProfile = cdnMgmtClient.Profiles.Get(resourceGroupName, profileName); VerifyProfilesEqual(profile, existingProfile); // List profiles should return one profile profiles = cdnMgmtClient.Profiles.ListByResourceGroup(resourceGroupName); Assert.Single(profiles); // Create a second cdn profile and don't wait for creation to finish var profileName2 = TestUtilities.GenerateName("profile"); createParameters = new Profile { Location = "WestUs", Sku = new Sku { Name = SkuName.StandardVerizon }, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } } }; cdnMgmtClient.Profiles.BeginCreateAsync(resourceGroupName, profileName2, createParameters) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // List profiles should return two profiles profiles = cdnMgmtClient.Profiles.ListByResourceGroup(resourceGroupName); Assert.Equal(2, profiles.Count()); // Delete first profile cdnMgmtClient.Profiles.Delete(resourceGroupName, profileName); // Get deleted profile should fail Assert.ThrowsAny <ErrorResponseException>(() => { cdnMgmtClient.Profiles.Get(resourceGroupName, profileName); }); // List profiles should return only one profile profiles = cdnMgmtClient.Profiles.ListByResourceGroup(resourceGroupName); Assert.Single(profiles); // Wait for second profile to complete creation CdnTestUtilities.WaitIfNotInPlaybackMode(); // Delete second profile but don't wait for operation to complete cdnMgmtClient.Profiles.BeginDeleteAsync(resourceGroupName, profileName2) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Get second profile returns profile in Deleting state existingProfile = cdnMgmtClient.Profiles.Get(resourceGroupName, profileName2); Assert.Equal(existingProfile.ResourceState, ProfileResourceState.Deleting); // Wait for second profile to complete creation CdnTestUtilities.WaitIfNotInPlaybackMode(); // List profiles should none profiles = cdnMgmtClient.Profiles.ListByResourceGroup(resourceGroupName); Assert.Empty(profiles); // Delete resource group CdnTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }
public void ProfileDeleteTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType())) { // Create clients var cdnMgmtClient = CdnTestUtilities.GetCdnManagementClient(context, handler1); var resourcesClient = CdnTestUtilities.GetResourceManagementClient(context, handler2); // Create resource group var resourceGroupName = CdnTestUtilities.CreateResourceGroup(resourcesClient); // Create a standard cdn profile string profileName = TestUtilities.GenerateName("profile"); Profile createParameters = new Profile { Location = "WestUs", Sku = new Sku { Name = SkuName.StandardVerizon }, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } } }; var profile = cdnMgmtClient.Profiles.Create(resourceGroupName, profileName, createParameters); VerifyProfileCreated(profile, createParameters); // Delete existing profile should succeed cdnMgmtClient.Profiles.Delete(resourceGroupName, profile.Name); // List profiles should return none var profiles = cdnMgmtClient.Profiles.ListByResourceGroup(resourceGroupName); Assert.Empty(profiles); // Delete non-existing profile should succeed cdnMgmtClient.Profiles.Delete(resourceGroupName, profile.Name); // Create a standard cdn profile and don't wait for creation to finish profileName = TestUtilities.GenerateName("profile"); createParameters = new Profile { Location = "WestUs", Sku = new Sku { Name = SkuName.StandardVerizon }, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } } }; cdnMgmtClient.Profiles.BeginCreateAsync(resourceGroupName, profileName, createParameters) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Wait for second profile to complete creation CdnTestUtilities.WaitIfNotInPlaybackMode(); // Delete profile should succeed cdnMgmtClient.Profiles.Delete(resourceGroupName, profileName); // List profiles should return none profiles = cdnMgmtClient.Profiles.ListByResourceGroup(resourceGroupName); Assert.Empty(profiles); // Delete resource group CdnTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }
public void EndpointGetListTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType().FullName)) { // Create clients var cdnMgmtClient = CdnTestUtilities.GetCdnManagementClient(context, handler1); var resourcesClient = CdnTestUtilities.GetResourceManagementClient(context, handler2); // Create resource group var resourceGroupName = CdnTestUtilities.CreateResourceGroup(resourcesClient); // Create a standard cdn profile string profileName = TestUtilities.GenerateName("profile"); ProfileCreateParameters createParameters = new ProfileCreateParameters { Location = "WestUs", Sku = new Sku { Name = SkuName.StandardVerizon }, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } } }; var profile = cdnMgmtClient.Profiles.Create(profileName, createParameters, resourceGroupName); // List endpoints should return none var endpoints = cdnMgmtClient.Endpoints.ListByProfile(profileName, resourceGroupName); Assert.Equal(0, endpoints.Count()); // Create a cdn endpoint should succeed string endpointName = TestUtilities.GenerateName("endpoint"); var endpointCreateParameters = new EndpointCreateParameters { Location = "WestUs", IsHttpAllowed = true, IsHttpsAllowed = true, Origins = new List <DeepCreatedOrigin> { new DeepCreatedOrigin { Name = "origin1", HostName = "host1.hello.com" } } }; var endpoint = cdnMgmtClient.Endpoints.Create(endpointName, endpointCreateParameters, profileName, resourceGroupName); // Get endpoint returns the created endpoint var existingEndpoint = cdnMgmtClient.Endpoints.Get(endpointName, profileName, resourceGroupName); Assert.NotNull(existingEndpoint); Assert.Equal(existingEndpoint.ResourceState, EndpointResourceState.Running); // List endpoints should return one endpoint endpoints = cdnMgmtClient.Endpoints.ListByProfile(profileName, resourceGroupName); Assert.Equal(1, endpoints.Count()); // Create a cdn endpoint and don't wait for creation to finish string endpointName2 = TestUtilities.GenerateName("endpoint"); endpointCreateParameters = new EndpointCreateParameters { Location = "WestUs", IsHttpAllowed = true, IsHttpsAllowed = true, Origins = new List <DeepCreatedOrigin> { new DeepCreatedOrigin { Name = "origin1", HostName = "host1.hello.com" } } }; cdnMgmtClient.Endpoints.BeginCreateAsync(endpointName2, endpointCreateParameters, profileName, resourceGroupName).Wait(5000); // List endpoints should return two endpoints endpoints = cdnMgmtClient.Endpoints.ListByProfile(profileName, resourceGroupName); Assert.Equal(2, endpoints.Count()); // Delete first endpoint should succeed cdnMgmtClient.Endpoints.DeleteIfExists(endpointName, profileName, resourceGroupName); // Get deleted endpoint fails Assert.ThrowsAny <ErrorResponseException>(() => { cdnMgmtClient.Endpoints.Get(endpointName, profileName, resourceGroupName); }); // List endpoints should return 1 endpoint endpoints = cdnMgmtClient.Endpoints.ListByProfile(profileName, resourceGroupName); Assert.Equal(1, endpoints.Count()); // Wait for second endpoint to complete creation CdnTestUtilities.WaitIfNotInPlaybackMode(); // Delete second endpoint but don't wait for operation to complete cdnMgmtClient.Endpoints.BeginDeleteIfExistsAsync(endpointName2, profileName, resourceGroupName).Wait(2000); // Get second endpoint returns endpoint in Deleting state existingEndpoint = cdnMgmtClient.Endpoints.Get(endpointName2, profileName, resourceGroupName); Assert.Equal(existingEndpoint.ResourceState, EndpointResourceState.Deleting); // Wait for second endpoint deletion to complete CdnTestUtilities.WaitIfNotInPlaybackMode(); // List endpoints should return none endpoints = cdnMgmtClient.Endpoints.ListByProfile(profileName, resourceGroupName); Assert.Equal(0, endpoints.Count()); // Delete resource group CdnTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }
public void ProfileUpdateTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType())) { // Create clients var cdnMgmtClient = CdnTestUtilities.GetCdnManagementClient(context, handler1); var resourcesClient = CdnTestUtilities.GetResourceManagementClient(context, handler2); // Create resource group var resourceGroupName = CdnTestUtilities.CreateResourceGroup(resourcesClient); // Create a standard cdn profile string profileName = TestUtilities.GenerateName("profile"); Profile createParameters = new Profile { Location = "WestUs", Sku = new Sku { Name = SkuName.StandardAkamai }, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } } }; var profile = cdnMgmtClient.Profiles.Create(resourceGroupName, profileName, createParameters); VerifyProfileCreated(profile, createParameters); // Update profile with new tags var newTags = new Dictionary <string, string> { { "newkey1", "newValue1" } }; var updatedProfile = cdnMgmtClient.Profiles.Update( resourceGroupName, profileName, newTags); VerifyProfileUpdated(profile, updatedProfile, newTags); // Create a standard cdn profile and don't wait for creation to finish profileName = TestUtilities.GenerateName("profile"); createParameters = new Profile { Location = "WestUs", Sku = new Sku { Name = SkuName.StandardVerizon }, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } } }; cdnMgmtClient.Profiles.BeginCreateAsync(resourceGroupName, profileName, createParameters) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Update profile in creating state should fail var tags = new Dictionary <string, string> { { "key", "value" } }; Assert.ThrowsAny <ErrorResponseException>(() => { cdnMgmtClient.Profiles.Update(resourceGroupName, profileName, tags); }); // Wait for second profile to complete creation CdnTestUtilities.WaitIfNotInPlaybackMode(2); // Update profile now should succeed cdnMgmtClient.Profiles.Update(resourceGroupName, profileName, tags); // Delete resource group CdnTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }
public void WafPolicyLinkTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType())) { // Create clients var cdnMgmtClient = CdnTestUtilities.GetCdnManagementClient(context, handler1); var resourcesClient = CdnTestUtilities.GetResourceManagementClient(context, handler2); // Create resource group var resourceGroupName = CdnTestUtilities.CreateResourceGroup(resourcesClient); // Create a cdn waf policy var policyName = TestUtilities.GenerateName("policy"); var policy = cdnMgmtClient.Policies.CreateOrUpdate(resourceGroupName, policyName, new CdnWebApplicationFirewallPolicy { Location = "Global", Sku = new Sku("Standard_Microsoft"), PolicySettings = new PolicySettings { EnabledState = "Enabled", Mode = "Detection", DefaultCustomBlockResponseBody = "PGh0bWw+PGJvZHk+PGgzPkludmFsaWQgcmVxdWVzdDwvaDM+PC9ib2R5PjwvaHRtbD4=", DefaultRedirectUrl = "https://example.com/redirected", DefaultCustomBlockResponseStatusCode = 406 }, CustomRules = new CustomRuleList(new List <CustomRule> { new CustomRule { Name = "CustomRule1", Priority = 2, EnabledState = "Enabled", Action = "Block", MatchConditions = new List <MatchCondition> { new MatchCondition { MatchVariable = "QueryString", OperatorProperty = "Contains", NegateCondition = false, MatchValue = new List <string> { "TestTrigger123" }, //Transforms = new List<String> { "Uppercase" } } } } }), RateLimitRules = new RateLimitRuleList(new List <RateLimitRule> { new RateLimitRule { Name = "RateLimitRule1", Priority = 1, EnabledState = "Disabled", RateLimitDurationInMinutes = 1, RateLimitThreshold = 3, MatchConditions = new List <MatchCondition> { new MatchCondition { MatchVariable = "RemoteAddr", Selector = null, OperatorProperty = "IPMatch", NegateCondition = false, MatchValue = new List <string> { "131.107.0.0/16", "167.220.0.0/16" } } }, Action = "Block" }, new RateLimitRule { Name = "RateLimitRule2", Priority = 10, EnabledState = "Enabled", RateLimitDurationInMinutes = 1, RateLimitThreshold = 1, MatchConditions = new List <MatchCondition> { new MatchCondition { MatchVariable = "RequestUri", Selector = null, OperatorProperty = "Contains", NegateCondition = false, MatchValue = new List <string> { "yes" } } }, Action = "Block" } }), ManagedRules = new ManagedRuleSetList(new List <ManagedRuleSet> { new ManagedRuleSet { RuleSetType = "DefaultRuleSet", RuleSetVersion = "1.0", RuleGroupOverrides = new List <ManagedRuleGroupOverride> { new ManagedRuleGroupOverride { RuleGroupName = "JAVA", Rules = new List <ManagedRuleOverride> { new ManagedRuleOverride { RuleId = "944100", EnabledState = "Disabled", Action = "Redirect" } } } } } }), Tags = new Dictionary <string, string> { { "abc", "123" } } }); // Create a standard Microsoft cdn profile string profileName = TestUtilities.GenerateName("profile"); Profile createParameters = new Profile { Location = "WestUs", Sku = new Sku { Name = SkuName.StandardMicrosoft }, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } } }; // Wait for policy to complete creation CdnTestUtilities.WaitIfNotInPlaybackMode(); var profile = cdnMgmtClient.Profiles.Create(resourceGroupName, profileName, createParameters); // Create a cdn endpoint with waf link should succeed string endpointName = TestUtilities.GenerateName("endpoint"); var endpoint = cdnMgmtClient.Endpoints.Create(resourceGroupName, profileName, endpointName, new Endpoint { Location = "WestUs", IsHttpAllowed = true, IsHttpsAllowed = true, WebApplicationFirewallPolicyLink = new EndpointPropertiesUpdateParametersWebApplicationFirewallPolicyLink(policy.Id.ToString()), Origins = new List <DeepCreatedOrigin> { new DeepCreatedOrigin { Name = "origin1", HostName = "host1.hello.com" } } }); // Verify linked endpoint var existingEndpoint = cdnMgmtClient.Endpoints.Get(resourceGroupName, profileName, endpointName); Assert.Equal(policy.Id.ToString(), existingEndpoint.WebApplicationFirewallPolicyLink.Id); // Verify policy shows linked endpoint var existingPolicy = cdnMgmtClient.Policies.Get(resourceGroupName, policyName); Assert.Single(existingPolicy.EndpointLinks); Assert.Contains(endpoint.Id, existingPolicy.EndpointLinks.Select(l => l.Id)); // Create second endpoint linked to the same profile should succeed var endpoint2Name = TestUtilities.GenerateName("endpoint"); var endpoint2 = cdnMgmtClient.Endpoints.Create(resourceGroupName, profileName, endpoint2Name, new Endpoint { Location = "EastUs", IsHttpAllowed = false, IsHttpsAllowed = true, WebApplicationFirewallPolicyLink = new EndpointPropertiesUpdateParametersWebApplicationFirewallPolicyLink(policy.Id.ToString()), Origins = new List <DeepCreatedOrigin> { new DeepCreatedOrigin { Name = "origin2", HostName = "host2.hello.com" } } }); // Verify second linked endpoint var existingEndpoint2 = cdnMgmtClient.Endpoints.Get(resourceGroupName, profileName, endpoint2Name); Assert.Equal(policy.Id.ToString(), existingEndpoint2.WebApplicationFirewallPolicyLink.Id); // Verify policy shows both linked endpoints. existingPolicy = cdnMgmtClient.Policies.Get(resourceGroupName, policyName); Assert.Equal(2, existingPolicy.EndpointLinks.Count()); Assert.Contains(endpoint.Id, existingPolicy.EndpointLinks.Select(l => l.Id)); Assert.Contains(endpoint2.Id, existingPolicy.EndpointLinks.Select(l => l.Id)); // Unlink endpoint should succeed. endpoint.WebApplicationFirewallPolicyLink = null; endpoint = cdnMgmtClient.Endpoints.Create(resourceGroupName, profileName, endpointName, endpoint); Assert.Null(endpoint.WebApplicationFirewallPolicyLink); // Verify policy shows only second linked endpoint. existingPolicy = cdnMgmtClient.Policies.Get(resourceGroupName, policyName); Assert.Single(existingPolicy.EndpointLinks); Assert.Contains(endpoint2.Id, existingPolicy.EndpointLinks.Select(l => l.Id)); // Unlink second endpoint should succeed endpoint2.WebApplicationFirewallPolicyLink = null; endpoint2 = cdnMgmtClient.Endpoints.Create(resourceGroupName, profileName, endpoint2Name, endpoint2); Assert.Null(endpoint2.WebApplicationFirewallPolicyLink); // Verify policy shows no linked endpoints existingPolicy = cdnMgmtClient.Policies.Get(resourceGroupName, policyName); Assert.Empty(existingPolicy.EndpointLinks); // Delete resource group CdnTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }