public async Task TopicCreateGetUpdateDeleteAuthorizationRule() { IgnoreTestInLiveMode(); //create topic string topicName = Recording.GenerateAssetName("topic"); ServiceBusTopicResource topic = (await _topicCollection.CreateOrUpdateAsync(WaitUntil.Completed, topicName, new ServiceBusTopicData())).Value; //create an authorization rule string ruleName = Recording.GenerateAssetName("authorizationrule"); NamespaceTopicAuthorizationRuleCollection ruleCollection = topic.GetNamespaceTopicAuthorizationRules(); ServiceBusAuthorizationRuleData parameter = new ServiceBusAuthorizationRuleData() { Rights = { AccessRights.Listen, AccessRights.Send } }; NamespaceTopicAuthorizationRuleResource authorizationRule = (await ruleCollection.CreateOrUpdateAsync(WaitUntil.Completed, ruleName, parameter)).Value; Assert.NotNull(authorizationRule); Assert.AreEqual(authorizationRule.Data.Rights.Count, parameter.Rights.Count); //get authorization rule authorizationRule = await ruleCollection.GetAsync(ruleName); Assert.AreEqual(authorizationRule.Id.Name, ruleName); Assert.NotNull(authorizationRule); Assert.AreEqual(authorizationRule.Data.Rights.Count, parameter.Rights.Count); //get all authorization rules List <NamespaceTopicAuthorizationRuleResource> rules = await ruleCollection.GetAllAsync().ToEnumerableAsync(); //validate Assert.True(rules.Count == 1); bool isContainAuthorizationRuleName = false; foreach (NamespaceTopicAuthorizationRuleResource rule in rules) { if (rule.Id.Name == ruleName) { isContainAuthorizationRuleName = true; } } Assert.True(isContainAuthorizationRuleName); //update authorization rule parameter.Rights.Add(AccessRights.Manage); authorizationRule = (await ruleCollection.CreateOrUpdateAsync(WaitUntil.Completed, ruleName, parameter)).Value; Assert.NotNull(authorizationRule); Assert.AreEqual(authorizationRule.Data.Rights.Count, parameter.Rights.Count); //delete authorization rule await authorizationRule.DeleteAsync(WaitUntil.Completed); //validate if deleted Assert.IsFalse(await ruleCollection.ExistsAsync(ruleName)); rules = await ruleCollection.GetAllAsync().ToEnumerableAsync(); Assert.True(rules.Count == 0); }
public async Task TopicAuthorizationRuleRegenerateKey() { IgnoreTestInLiveMode(); //create topic string topicName = Recording.GenerateAssetName("topic"); ServiceBusTopicResource topic = (await _topicCollection.CreateOrUpdateAsync(WaitUntil.Completed, topicName, new ServiceBusTopicData())).Value; NamespaceTopicAuthorizationRuleCollection ruleCollection = topic.GetNamespaceTopicAuthorizationRules(); //create authorization rule string ruleName = Recording.GenerateAssetName("authorizationrule"); ServiceBusAuthorizationRuleData parameter = new ServiceBusAuthorizationRuleData() { Rights = { AccessRights.Listen, AccessRights.Send } }; NamespaceTopicAuthorizationRuleResource authorizationRule = (await ruleCollection.CreateOrUpdateAsync(WaitUntil.Completed, ruleName, parameter)).Value; Assert.NotNull(authorizationRule); Assert.AreEqual(authorizationRule.Data.Rights.Count, parameter.Rights.Count); AccessKeys keys1 = await authorizationRule.GetKeysAsync(); Assert.NotNull(keys1); Assert.NotNull(keys1.PrimaryConnectionString); Assert.NotNull(keys1.SecondaryConnectionString); AccessKeys keys2 = await authorizationRule.RegenerateKeysAsync(new RegenerateAccessKeyOptions(KeyType.PrimaryKey)); //the recordings are sanitized therefore cannot be compared if (Mode != RecordedTestMode.Playback) { Assert.AreNotEqual(keys1.PrimaryKey, keys2.PrimaryKey); Assert.AreEqual(keys1.SecondaryKey, keys2.SecondaryKey); } AccessKeys keys3 = await authorizationRule.RegenerateKeysAsync(new RegenerateAccessKeyOptions(KeyType.SecondaryKey)); if (Mode != RecordedTestMode.Playback) { Assert.AreEqual(keys2.PrimaryKey, keys3.PrimaryKey); Assert.AreNotEqual(keys2.SecondaryKey, keys3.SecondaryKey); } }