Ejemplo n.º 1
0
        public async Task CreateShouldThrowExceptionGivenNoSubscriptionFound()
        {
            ServiceTestContext testContext = new ServiceTestContext();

            testContext = CreateMocks(testContext);
            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, "invalid server"));
        }
Ejemplo n.º 2
0
        public async Task CreateShouldThrowExceptionGivenNullEndIp()
        {
            ServiceTestContext testContext = new ServiceTestContext();

            testContext.EndIpAddress = null;
            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, testContext.ServerName));
        }
Ejemplo n.º 3
0
        public async Task CreateShouldThrowExceptionGivenNullServerName()
        {
            string serverName = null;

            ServiceTestContext testContext = new ServiceTestContext();

            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, serverName));
        }
Ejemplo n.º 4
0
        public async Task CreateShouldCreateFirewallSuccessfullyGivenValidUserAccount()
        {
            ServiceTestContext testContext = new ServiceTestContext();

            testContext = CreateMocks(testContext);

            await VerifyCreateAsync(testContext, testContext.ServerName);
        }
Ejemplo n.º 5
0
        public async Task CreateThrowExceptionIfResourcesIsEmpty()
        {
            ServiceTestContext testContext = new ServiceTestContext();

            testContext.SubscriptionToResourcesMap[testContext.ValidSubscription.Subscription.SubscriptionId] = new List <IAzureSqlServerResource>();
            testContext = CreateMocks(testContext);

            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, testContext.ServerName, false));
        }
Ejemplo n.º 6
0
        public async Task CreateShouldThrowExceptionGivenInvalidStartIp()
        {
            string serverName = "serverName";

            ServiceTestContext testContext = new ServiceTestContext();

            testContext.StartIpAddress = "invalid ip";
            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, serverName));
        }
Ejemplo n.º 7
0
        public async Task CreateShouldThrowExceptionIfThereIsNoSubscriptionForUser()
        {
            ServiceTestContext testContext = new ServiceTestContext();

            testContext.Subscriptions = new List <IAzureUserAccountSubscriptionContext>();

            testContext = CreateMocks(testContext);

            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, testContext.ServerName, false));
        }
Ejemplo n.º 8
0
        public async Task CreateShouldCreateFirewallForTheRightServerFullyQualifiedName()
        {
            ServiceTestContext testContext = new ServiceTestContext();
            string             serverNameWithDifferentDomain = testContext.ServerNameWithoutDomain + ".myaliased.domain.name";

            testContext.ServerName = serverNameWithDifferentDomain;
            testContext.Initialize();
            testContext = CreateMocks(testContext);

            await VerifyCreateAsync(testContext, testContext.ServerName);
        }
Ejemplo n.º 9
0
        public async Task CreateShouldFindTheRightSubscriptionGivenValidSubscriptionInSecondPlace()
        {
            ServiceTestContext testContext = new ServiceTestContext();

            testContext.Subscriptions = new List <IAzureUserAccountSubscriptionContext>
            {
                ServiceTestContext.CreateSubscriptionContext(),
                    testContext.ValidSubscription,
                    ServiceTestContext.CreateSubscriptionContext(),
            };
            testContext.Initialize();
            testContext = CreateMocks(testContext);
            await VerifyCreateAsync(testContext, testContext.ServerName);
        }
Ejemplo n.º 10
0
        public async Task CreateThrowExceptionIfResourceNotFound()
        {
            ServiceTestContext testContext = new ServiceTestContext();
            var resources = new List <IAzureSqlServerResource>
            {
                ServiceTestContext.CreateAzureSqlServer(Guid.NewGuid().ToString()),
                ServiceTestContext.CreateAzureSqlServer(Guid.NewGuid().ToString()),
            };

            testContext.SubscriptionToResourcesMap[testContext.ValidSubscription.Subscription.SubscriptionId] = resources;

            testContext = CreateMocks(testContext);

            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, testContext.ServerName));
        }
Ejemplo n.º 11
0
        public async Task CreateShouldFindTheRightResourceGivenValidResourceInFirstPlace()
        {
            ServiceTestContext testContext = new ServiceTestContext();
            var resources = new List <IAzureSqlServerResource>
            {
                ServiceTestContext.CreateAzureSqlServer(testContext.ServerName),
                ServiceTestContext.CreateAzureSqlServer(Guid.NewGuid().ToString()),
            };

            testContext.SubscriptionToResourcesMap[testContext.ValidSubscription.Subscription.SubscriptionId] = resources;

            testContext = CreateMocks(testContext);

            await VerifyCreateAsync(testContext, testContext.ServerName);
        }
Ejemplo n.º 12
0
        public async Task CreateShouldThrowExceptionIfAuthenticationManagerFailsToReturnSubscription()
        {
            var applicationAuthenticationManagerMock = new Mock <IAzureAuthenticationManager>();

            applicationAuthenticationManagerMock.Setup(x => x.GetUserNeedsReauthenticationAsync()).Returns(Task.FromResult(false));
            applicationAuthenticationManagerMock.Setup(x => x.GetSubscriptionsAsync()).Throws(new Exception());
            var azureResourceManagerMock = new Mock <IAzureResourceManager>();

            ServiceTestContext testContext = new ServiceTestContext();

            testContext.ApplicationAuthenticationManagerMock = applicationAuthenticationManagerMock;
            testContext.AzureResourceManagerMock             = azureResourceManagerMock;
            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, "invalid server"));

            azureResourceManagerMock.Verify(x => x.CreateFirewallRuleAsync(
                                                It.IsAny <IAzureResourceManagementSession>(), It.IsAny <IAzureSqlServerResource>(), It.IsAny <FirewallRuleRequest>()),
                                            Times.Never);
        }
Ejemplo n.º 13
0
        public async Task CreateShouldThrowExceptionIfUserIsNotLoggedIn()
        {
            var applicationAuthenticationManagerMock = new Mock <IAzureAuthenticationManager>();

            applicationAuthenticationManagerMock.Setup(x => x.GetUserNeedsReauthenticationAsync()).Throws(new ApplicationException());
            var azureResourceManagerMock = new Mock <IAzureResourceManager>();

            ServiceTestContext testContext = new ServiceTestContext();

            testContext.ApplicationAuthenticationManagerMock = applicationAuthenticationManagerMock;
            testContext.AzureResourceManagerMock             = azureResourceManagerMock;

            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, testContext.ServerName));

            azureResourceManagerMock.Verify(x => x.CreateFirewallRuleAsync(
                                                It.IsAny <IAzureResourceManagementSession>(), It.IsAny <IAzureSqlServerResource>(), It.IsAny <FirewallRuleRequest>()),
                                            Times.Never);
        }
Ejemplo n.º 14
0
        private ServiceTestContext CreateMocks(ServiceTestContext testContext)
        {
            var accountMock = new Mock <IUserAccount>();

            accountMock.Setup(x => x.UniqueId).Returns(Guid.NewGuid().ToString());
            var applicationAuthenticationManagerMock = new Mock <IAzureAuthenticationManager>();

            applicationAuthenticationManagerMock.Setup(x => x.GetUserNeedsReauthenticationAsync())
            .Returns(Task.FromResult(false));
            applicationAuthenticationManagerMock.Setup(x => x.GetCurrentAccountAsync()).Returns(Task.FromResult(accountMock.Object));
            applicationAuthenticationManagerMock.Setup(x => x.GetSubscriptionsAsync()).Returns(Task.FromResult(testContext.Subscriptions as IEnumerable <IAzureUserAccountSubscriptionContext>));

            var azureResourceManagerMock = new Mock <IAzureResourceManager>();

            CreateMocksForResources(testContext, azureResourceManagerMock);

            testContext.ApplicationAuthenticationManagerMock = applicationAuthenticationManagerMock;
            testContext.AzureResourceManagerMock             = azureResourceManagerMock;
            return(testContext);
        }
Ejemplo n.º 15
0
        public async Task CreateShouldThrowExceptionIfUserDoesNotHaveSubscriptions()
        {
            var applicationAuthenticationManagerMock =
                new Mock <IAzureAuthenticationManager>();

            applicationAuthenticationManagerMock.Setup(x => x.GetUserNeedsReauthenticationAsync()).Returns(Task.FromResult(false));
            applicationAuthenticationManagerMock.Setup(x => x.GetSubscriptionsAsync())
            .Returns(Task.FromResult(Enumerable.Empty <IAzureUserAccountSubscriptionContext>()));
            var azureResourceManagerMock = new Mock <IAzureResourceManager>();

            ServiceTestContext testContext = new ServiceTestContext();

            testContext.ApplicationAuthenticationManagerMock = applicationAuthenticationManagerMock;
            testContext.AzureResourceManagerMock             = azureResourceManagerMock;

            await Assert.ThrowsAsync <FirewallRuleException>(() => VerifyCreateAsync(testContext, testContext.ServerName));

            azureResourceManagerMock.Verify(x => x.CreateFirewallRuleAsync(
                                                It.IsAny <IAzureResourceManagementSession>(), It.IsAny <IAzureSqlServerResource>(), It.IsAny <FirewallRuleRequest>()),
                                            Times.Never);
        }
Ejemplo n.º 16
0
        private void CreateMocksForResources(
            ServiceTestContext testContext,
            Mock <IAzureResourceManager> azureResourceManagerMock)
        {
            foreach (IAzureUserAccountSubscriptionContext subscription in testContext.Subscriptions)
            {
                var sessionMock = new Mock <IAzureResourceManagementSession>();
                sessionMock.Setup(x => x.SubscriptionContext).Returns(subscription);
                azureResourceManagerMock.Setup(x => x.CreateSessionAsync(subscription)).Returns(Task.FromResult(sessionMock.Object));

                List <IAzureSqlServerResource> resources;
                if (testContext.SubscriptionToResourcesMap.TryGetValue(subscription.Subscription.SubscriptionId,
                                                                       out resources))
                {
                    azureResourceManagerMock.Setup(x => x.GetSqlServerAzureResourcesAsync(It.Is <IAzureResourceManagementSession>(
                                                                                              m => m.SubscriptionContext.Subscription.SubscriptionId == subscription.Subscription.SubscriptionId)))
                    .Returns(Task.FromResult(resources as IEnumerable <IAzureSqlServerResource>));
                }
                else
                {
                    azureResourceManagerMock.Setup(x => x.GetSqlServerAzureResourcesAsync(
                                                       It.Is <IAzureResourceManagementSession>(m => m.SubscriptionContext.Subscription.SubscriptionId == subscription.Subscription.SubscriptionId)))
                    .Returns(Task.FromResult <IEnumerable <IAzureSqlServerResource> >(null));
                }
            }

            azureResourceManagerMock
            .Setup(x => x.CreateFirewallRuleAsync(
                       It.IsAny <IAzureResourceManagementSession>(),
                       It.IsAny <IAzureSqlServerResource>(),
                       It.Is <FirewallRuleRequest>(
                           y => y.EndIpAddress.ToString().Equals(testContext.EndIpAddress) &&
                           y.StartIpAddress.ToString().Equals(testContext.StartIpAddress))))
            .Returns(Task.FromResult(new FirewallRuleResponse()
            {
                Created = true
            }));
        }
Ejemplo n.º 17
0
        private async Task <FirewallRuleResponse> VerifyCreateAsync(ServiceTestContext testContext, string serverName, bool verifyFirewallRuleCreated = true)
        {
            try
            {
                FirewallRuleService service = new FirewallRuleService();
                service.AuthenticationManager = testContext.ApplicationAuthenticationManager;
                service.ResourceManager       = testContext.AzureResourceManager;
                FirewallRuleResponse response = await service.CreateFirewallRuleAsync(serverName, testContext.StartIpAddress, testContext.EndIpAddress);

                if (verifyFirewallRuleCreated)
                {
                    testContext.AzureResourceManagerMock.Verify(x => x.CreateFirewallRuleAsync(
                                                                    It.Is <IAzureResourceManagementSession>(s => s.SubscriptionContext.Subscription.SubscriptionId == testContext.ValidSubscription.Subscription.SubscriptionId),
                                                                    It.Is <IAzureSqlServerResource>(r => r.FullyQualifiedDomainName == serverName),
                                                                    It.Is <FirewallRuleRequest>(y => y.EndIpAddress.ToString().Equals(testContext.EndIpAddress) && y.StartIpAddress.ToString().Equals(testContext.StartIpAddress))),
                                                                Times.AtLeastOnce);
                }
                else
                {
                    testContext.AzureResourceManagerMock.Verify(x => x.CreateFirewallRuleAsync(
                                                                    It.Is <IAzureResourceManagementSession>(s => s.SubscriptionContext.Subscription.SubscriptionId == testContext.ValidSubscription.Subscription.SubscriptionId),
                                                                    It.Is <IAzureSqlServerResource>(r => r.FullyQualifiedDomainName == serverName),
                                                                    It.Is <FirewallRuleRequest>(y => y.EndIpAddress.ToString().Equals(testContext.EndIpAddress) && y.StartIpAddress.ToString().Equals(testContext.StartIpAddress))),
                                                                Times.Never);
                }

                return(response);
            }
            catch (Exception ex)
            {
                if (ex is FirewallRuleException)
                {
                    Assert.True(ex.InnerException == null || !(ex.InnerException is FirewallRuleException));
                }
                throw;
            }
        }