public void WhenLoadingDataCausesReauthError_ThenGetRootNodeAsyncPropagatesException()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);
            var resourceManagerAdapter = serviceRegistry.AddMock <IResourceManagerAdapter>();

            resourceManagerAdapter.Setup(a => a.GetProjectAsync(
                                             It.IsAny <string>(),
                                             It.IsAny <CancellationToken>()))
            .ThrowsAsync(new TokenResponseException(new TokenErrorResponse()
            {
                Error = "invalid_grant"
            }));

            serviceRegistry.AddSingleton(resourceManagerAdapter.Object);


            var modelService = new ProjectModelService(serviceRegistry);

            AssertEx.ThrowsAggregateException <TokenResponseException>(
                () => modelService.GetRootNodeAsync(false, CancellationToken.None).Wait());
        }
예제 #2
0
        public async Task WhenNoSuggestedUserNameProvidedAndSilentIsTrue_ThenSuggestionIsDerivedFromSigninNameWithoutPrompting()
        {
            var serviceRegistry = new ServiceRegistry();

            var auth = new Mock <IAuthorization>();

            auth.SetupGet(a => a.Email).Returns("*****@*****.**");
            serviceRegistry.AddMock <IAuthorizationAdapter>()
            .SetupGet(a => a.Authorization).Returns(auth.Object);

            serviceRegistry.AddSingleton <IJobService, SynchronousJobService>();
            serviceRegistry.AddMock <IComputeEngineAdapter>()
            .Setup(a => a.ResetWindowsUserAsync(
                       It.IsAny <InstanceLocator>(),
                       It.Is <string>(user => user == "bobsemail"),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(new NetworkCredential("bobsemail", "password"));

            var credDialog = serviceRegistry.AddMock <IGenerateCredentialsDialog>();
            var settings   = InstanceConnectionSettings.CreateNew(SampleInstance);

            var credentialsService = new CredentialsService(serviceRegistry);
            await credentialsService.GenerateCredentialsAsync(
                null,
                SampleInstance,
                settings,
                true);

            Assert.AreEqual("bobsemail", settings.RdpUsername.Value);
            Assert.AreEqual("password", settings.RdpPassword.ClearTextValue);
            credDialog.Verify(d => d.PromptForUsername(
                                  It.IsAny <IWin32Window>(),
                                  It.IsAny <string>()), Times.Never);
        }
예제 #3
0
        public void WhenNoSuggestedUserNameProvided_ThenSuggestionIsDerivedFromSigninName()
        {
            var serviceRegistry = new ServiceRegistry();

            var auth = new Mock <IAuthorization>();

            auth.SetupGet(a => a.Email).Returns("*****@*****.**");

            serviceRegistry.AddMock <IAuthorizationAdapter>()
            .SetupGet(a => a.Authorization).Returns(auth.Object);

            var credDialog = serviceRegistry.AddMock <IGenerateCredentialsDialog>();

            credDialog
            .Setup(d => d.PromptForUsername(
                       It.IsAny <IWin32Window>(),
                       It.IsAny <string>()))
            .Returns <string>(null);    // Cancel dialog


            var settings = InstanceConnectionSettings.CreateNew(SampleInstance);

            var credentialsService = new CredentialsService(serviceRegistry);

            AssertEx.ThrowsAggregateException <TaskCanceledException>(
                () => credentialsService.GenerateCredentialsAsync(
                    null,
                    SampleInstance,
                    settings,
                    false).Wait());

            credDialog.Verify(d => d.PromptForUsername(
                                  It.IsAny <IWin32Window>(),
                                  It.Is <string>(u => u == "bobsemail")), Times.Once);
        }
        public async Task WhenProjectRemoved_ThenProjectIsRemovedFromRepositoryAndEventIsRaised()
        {
            var serviceRegistry   = new ServiceRegistry();
            var projectRepository = serviceRegistry.AddMock <IProjectRepository>();
            var eventService      = serviceRegistry.AddMock <IEventService>();

            var modelService = new ProjectModelService(serviceRegistry);
            await modelService.RemoveProjectAsync(new ProjectLocator(SampleProjectId));

            projectRepository.Verify(p => p.RemoveProject(
                                         It.Is <ProjectLocator>(id => id.Name == SampleProjectId)),
                                     Times.Once);
            eventService.Verify(s => s.FireAsync <ProjectDeletedEvent>(
                                    It.Is <ProjectDeletedEvent>(e => e.ProjectId == SampleProjectId)),
                                Times.Once);
        }
        public async Task WhenInstanceIsTerminated_ThenGetZoneNodesAsyncMarksInstanceAsNotRunning()
        {
            var serviceRegistry = new ServiceRegistry();

            serviceRegistry.AddMock <IEventService>();
            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);

            var computeAdapter = CreateComputeEngineAdapterMock(
                SampleProjectId,
                SampleTerminatedLinuxInstanceInZone1,
                SampleLinuxInstanceInZone1);

            serviceRegistry.AddSingleton(computeAdapter.Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var zones        = await modelService.GetZoneNodesAsync(
                new ProjectLocator(SampleProjectId),
                false,
                CancellationToken.None);

            Assert.AreEqual(1, zones.Count);
            var zone1 = zones.First();

            Assert.AreEqual(2, zone1.Instances.Count());

            var terminated = zone1.Instances.First();
            var running    = zone1.Instances.Last();

            Assert.AreEqual(SampleTerminatedLinuxInstanceInZone1.Name, terminated.DisplayName);
            Assert.IsFalse(terminated.IsRunning);

            Assert.AreEqual(SampleLinuxInstanceInZone1.Name, running.DisplayName);
            Assert.IsTrue(running.IsRunning);
        }
        public async Task WhenInstanceHasNoDisk_ThenGetZoneNodesAsyncSkipsInstance()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);

            var computeAdapter = CreateComputeEngineAdapterMock(
                SampleProjectId,
                SampleLinuxInstanceInZone1,
                SampleLinuxInstanceWithoutDiskInZone1);

            serviceRegistry.AddSingleton(computeAdapter.Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var zones        = await modelService.GetZoneNodesAsync(
                new ProjectLocator(SampleProjectId),
                false,
                CancellationToken.None);

            Assert.AreEqual(1, zones.Count);
            var zone1 = zones.First();

            Assert.AreEqual(1, zone1.Instances.Count());
            Assert.AreEqual(
                SampleLinuxInstanceInZone1.Name,
                zone1.Instances.First().DisplayName);
        }
        public async Task WhenZonesCachedButForceRefreshIsTrue_ThenGetZoneNodesAsyncLoadsZones()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);

            var computeAdapter = CreateComputeEngineAdapterMock(
                SampleProjectId,
                SampleLinuxInstanceInZone1,
                SampleLinuxInstanceInZone2);

            serviceRegistry.AddSingleton(computeAdapter.Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var zones        = await modelService.GetZoneNodesAsync(
                new ProjectLocator(SampleProjectId),
                true,
                CancellationToken.None);

            var zonesSecondLoad = await modelService.GetZoneNodesAsync(
                new ProjectLocator(SampleProjectId),
                true,
                CancellationToken.None);

            Assert.AreNotSame(zones, zonesSecondLoad);

            computeAdapter.Verify(a => a.ListInstancesAsync(
                                      It.Is <string>(id => id == SampleProjectId),
                                      It.IsAny <CancellationToken>()),
                                  Times.Exactly(2));
        }
        public async Task WhenZonesNotCached_ThenGetZoneNodesAsyncLoadsZones()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);

            var computeAdapter = CreateComputeEngineAdapterMock(
                SampleProjectId,
                SampleLinuxInstanceInZone1,
                SampleLinuxInstanceInZone2);

            serviceRegistry.AddSingleton(computeAdapter.Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var zones        = await modelService.GetZoneNodesAsync(
                new ProjectLocator(SampleProjectId),
                false,
                CancellationToken.None);

            var zone1 = zones.First();
            var zone2 = zones.Last();

            Assert.AreEqual(new ZoneLocator(SampleProjectId, "zone-1"), zone1.Zone);
            Assert.AreEqual(new ZoneLocator(SampleProjectId, "zone-2"), zone2.Zone);

            Assert.AreEqual(1, zone1.Instances.Count());
            Assert.AreEqual(1, zone2.Instances.Count());

            computeAdapter.Verify(a => a.ListInstancesAsync(
                                      It.Is <string>(id => id == SampleProjectId),
                                      It.IsAny <CancellationToken>()),
                                  Times.Once);
        }
        public async Task WhenActiveNodeSetToNonexistingLocator_ThenEventIsFired()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var model        = await modelService.GetRootNodeAsync(false, CancellationToken.None);

            // Pre-warm cache.
            await modelService.GetRootNodeAsync(
                false,
                CancellationToken.None);

            await modelService.SetActiveNodeAsync(
                new ProjectLocator("nonexisting-1"),
                CancellationToken.None);

            eventService.Verify(s => s.FireAsync <ActiveProjectChangedEvent>(
                                    It.Is <ActiveProjectChangedEvent>(e => e.ActiveNode is IProjectModelCloudNode)),
                                Times.Once);
        }
        public async Task WhenActiveNodeSet_ThenGetActiveNodeAsyncReturnsNode()
        {
            var serviceRegistry = new ServiceRegistry();

            serviceRegistry.AddMock <IEventService>();
            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(
                                             SampleProjectId,
                                             SampleWindowsInstanceInZone1).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);

            var root = await modelService.GetRootNodeAsync(
                false,
                CancellationToken.None);

            await modelService.SetActiveNodeAsync(
                new ProjectLocator(SampleProjectId),
                CancellationToken.None);

            var activeNode = await modelService.GetActiveNodeAsync(CancellationToken.None);

            Assert.IsNotNull(activeNode);
            Assert.IsInstanceOf(typeof(IProjectModelProjectNode), activeNode);
        }
예제 #11
0
        public void WhenSuggestedUserNameProvided_ThenSuggestionIsUsed()
        {
            var serviceRegistry = new ServiceRegistry();
            var credDialog      = serviceRegistry.AddMock <IGenerateCredentialsDialog>();

            credDialog
            .Setup(d => d.PromptForUsername(
                       It.IsAny <IWin32Window>(),
                       It.IsAny <string>()))
            .Returns <string>(null);    // Cancel dialog

            var settings = InstanceConnectionSettings.CreateNew(SampleInstance);

            settings.RdpUsername.Value = "alice";

            var credentialsService = new CredentialsService(serviceRegistry);

            AssertEx.ThrowsAggregateException <TaskCanceledException>(
                () => credentialsService.GenerateCredentialsAsync(
                    null,
                    SampleInstance,
                    settings,
                    false).Wait());

            credDialog.Verify(d => d.PromptForUsername(
                                  It.IsAny <IWin32Window>(),
                                  It.Is <string>(u => u == "alice")), Times.Once);
        }
        public async Task WhenLocatorOfUnknownType_ThenGetNodeAsyncThrowsArgumentException()
        {
            var serviceRegistry = new ServiceRegistry();

            serviceRegistry.AddMock <IEventService>();
            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            await modelService.GetRootNodeAsync(false, CancellationToken.None);

            AssertEx.ThrowsAggregateException <ArgumentException>(() => modelService.GetNodeAsync(
                                                                      new DiskTypeLocator(SampleProjectId, "zone-1", "type-1"),
                                                                      CancellationToken.None).Wait());
        }
        public void WhenLocatorIsInvalid_ThenSetActiveNodeAsyncRaisesArgumentException()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);

            AssertEx.ThrowsAggregateException <ArgumentException>(
                () => modelService.SetActiveNodeAsync(
                    new DiskTypeLocator(SampleProjectId, "zone-1", "type-1"),
                    CancellationToken.None).Wait());
        }
        public async Task WhenCacheEmptyAndActiveNodeSetToNull_ThenNoEventIsFired()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);

            await modelService.SetActiveNodeAsync((ResourceLocator)null, CancellationToken.None);

            eventService.Verify(s => s.FireAsync <ActiveProjectChangedEvent>(
                                    It.IsAny <ActiveProjectChangedEvent>()),
                                Times.Never);
        }
        public async Task WhenNoActiveNodeSet_ThenGetActiveNodeAsyncReturnsRoot()
        {
            var serviceRegistry = new ServiceRegistry();

            serviceRegistry.AddMock <IEventService>();
            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var activeNode   = await modelService.GetActiveNodeAsync(CancellationToken.None);

            Assert.IsNotNull(activeNode);
            Assert.AreSame(
                await modelService.GetRootNodeAsync(false, CancellationToken.None),
                activeNode);
        }
        public async Task WhenProjectNotAddedButInstanceLocatorValid_ThenGetNodeAsyncReturnsNull()
        {
            var serviceRegistry = new ServiceRegistry();

            serviceRegistry.AddMock <IEventService>();
            serviceRegistry.AddSingleton(CreateProjectRepositoryMock().Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(
                                             SampleProjectId,
                                             SampleWindowsInstanceInZone1).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);

            Assert.IsNull(await modelService.GetNodeAsync(
                              new InstanceLocator(SampleProjectId, "zone-1", SampleWindowsInstanceInZone1.Name),
                              CancellationToken.None));
        }
        private ProjectExplorerViewModel CreateViewModel()
        {
            var serviceRegistry = new ServiceRegistry();

            serviceRegistry.AddSingleton <IProjectRepository>(this.projectRepository);
            serviceRegistry.AddSingleton(this.computeEngineAdapterMock.Object);
            serviceRegistry.AddSingleton(this.resourceManagerAdapterMock.Object);
            serviceRegistry.AddMock <IEventService>();

            return(new ProjectExplorerViewModel(
                       new Control(),
                       this.settingsRepository,
                       new SynchrounousJobService(),
                       this.eventServiceMock.Object,
                       this.sessionBrokerMock.Object,
                       new ProjectModelService(serviceRegistry),
                       this.cloudConsoleServiceMock.Object));
        }
        public async Task WhenProjectInaccessible_ThenGetRootNodeAsyncLoadsRemainingProjects()
        {
            var serviceRegistry = new ServiceRegistry();

            serviceRegistry.AddMock <IEventService>();
            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(
                                             SampleProjectId,
                                             "nonexisting-1").Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var projects     = (await modelService.GetRootNodeAsync(false, CancellationToken.None))
                               .Projects
                               .ToList();

            Assert.AreEqual(2, projects.Count);

            Assert.AreEqual(SampleProjectId, projects[0].Project.Name);
            Assert.AreEqual("nonexisting-1", projects[1].Project.Name);
        }
        public async Task WhenProjectLocatorValid_ThenGetNodeAsyncReturnsNode()
        {
            var serviceRegistry = new ServiceRegistry();

            serviceRegistry.AddMock <IEventService>();
            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateResourceManagerAdapterMock().Object);

            var modelService = new ProjectModelService(serviceRegistry);
            await modelService.GetRootNodeAsync(false, CancellationToken.None);

            Assert.IsNull(await modelService.GetNodeAsync(
                              new ProjectLocator("nonexisting-1"),
                              CancellationToken.None));

            var project = await modelService.GetNodeAsync(
                new ProjectLocator(SampleProjectId),
                CancellationToken.None);

            Assert.IsInstanceOf(typeof(IProjectModelProjectNode), project);
            Assert.IsNotNull(project);
        }
        public async Task WhenProjectsNotCached_ThenGetRootNodeLoadsProjects()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);

            var resourceManagerAdapter = CreateResourceManagerAdapterMock();

            serviceRegistry.AddSingleton(resourceManagerAdapter.Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var model        = await modelService.GetRootNodeAsync(false, CancellationToken.None);

            Assert.AreEqual(1, model.Projects.Count());
            Assert.AreEqual(SampleProjectId, model.Projects.First().Project.Name);
            Assert.AreEqual("[project-1]", model.Projects.First().DisplayName);

            resourceManagerAdapter.Verify(a => a.GetProjectAsync(
                                              It.Is <string>(id => id == SampleProjectId),
                                              It.IsAny <CancellationToken>()),
                                          Times.Once);
        }
        public async Task WhenProjectsCachedButForceRefreshIsTrue_ThenGetRootNodeAsyncLoadsProjects()
        {
            var serviceRegistry = new ServiceRegistry();
            var eventService    = serviceRegistry.AddMock <IEventService>();

            serviceRegistry.AddSingleton(CreateProjectRepositoryMock(SampleProjectId).Object);
            serviceRegistry.AddSingleton(CreateComputeEngineAdapterMock(SampleProjectId).Object);

            var resourceManagerAdapter = CreateResourceManagerAdapterMock();

            serviceRegistry.AddSingleton(resourceManagerAdapter.Object);

            var modelService = new ProjectModelService(serviceRegistry);
            var model        = await modelService.GetRootNodeAsync(false, CancellationToken.None);

            var modelSecondLoad = await modelService.GetRootNodeAsync(true, CancellationToken.None);

            Assert.AreNotSame(model, modelSecondLoad);

            resourceManagerAdapter.Verify(a => a.GetProjectAsync(
                                              It.Is <string>(id => id == SampleProjectId),
                                              It.IsAny <CancellationToken>()),
                                          Times.Exactly(2));
        }