Esempio n. 1
0
 internal GalleryContext(string account, string workspace, SandboxCollection sandboxes,
     WorkspacesContextConnector workspaces, AppsContextConnector apps)
 {
     this.Account = account;
     this.Workspace = workspace;
     this.Sandboxes = sandboxes;
     this.workspaces = workspaces;
     this.apps = apps;
 }
 public WorkspacesContextConnector(IExtendedWorkspacesClient client, string account, string workspace,
     SandboxCollection sandboxes, string serviceName, string[] excludePrefixes)
     : base(serviceName, excludePrefixes)
 {
     this.account = account;
     this.workspace = workspace;
     this.sandboxes = sandboxes;
     this.client = client;
 }
 public AppsContextConnector(IAppsClient appsClient, ISandboxesClient sandboxesClient,
     IReadOnlyList<App> apps, SandboxCollection sandboxes, string serviceName,
     string[] excludePrefixes)
     : base(serviceName, excludePrefixes)
 {
     this.appsClient = appsClient;
     this.sandboxesClient = sandboxesClient;
     this.apps = apps;
     this.indexedApps = apps.ToDictionary(app => app.Id);
     this.sandboxes = sandboxes ?? EmptySandboxCollection;
 }
        public async Task<Settings> GetSettingsAsync(string account, string workspace, SandboxCollection sandboxes,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(account))
                throw new ArgumentNullException(nameof(account));

            if (string.IsNullOrWhiteSpace(workspace))
                throw new ArgumentNullException(nameof(workspace));

            try
            {
                return await this.Connector.GetJsonAsync<Settings>(Routes.Settings.FormatUri(account, workspace),
                    cancellationToken, sandboxes?.AsHeaders()).ConfigureAwait(false);
            }
            catch (ResourceNotFoundException e)
            {
                throw e.With(account, workspace);
            }
        }
        public async Task Get_apps_with_sandbox()
        {
            // Arrange
            var expected = new List<App>();
            var expectedHeaders = new NameValueCollection
            {
                {"x-vtex-sandbox", "oscorp/tests=foo+bar"}
            };
            var connector = Mock.Of<IWorkspacesConnector>()
                .ThatGetsJson<IReadOnlyList<App>>("/acme/workspaces/cool/apps", expectedHeaders, expected);
            var client = new ExtendedWorkspacesClient(connector);
            var sandboxes = new SandboxCollection(new[]
            {
                new Sandbox("oscorp", "tests", new[] {"foo", "bar"})
            });

            // Act
            var response = await client.GetAppsAsync("acme", "cool", sandboxes, None);

            // Assert
            response.ShouldBe(expected);
        }
 public TestableAppsContextConnector(IAppsClient appsClient, ISandboxesClient sandboxesClient,
     SandboxCollection sandboxes, string serviceName, string[] excludes)
     : base(appsClient, sandboxesClient, new List<App>(), sandboxes, serviceName, excludes)
 {
 }
 public TestableAppsContextConnector(IAppsClient appsClient, ISandboxesClient sandboxesClient,
     SandboxCollection sandboxes)
     : base(appsClient, sandboxesClient, new List<App>(), sandboxes, "anything", new string[0])
 {
 }
            public async Task Return_from_sandbox()
            {
                // Arrange
                var archive = new FileList(new Dictionary<string, FileListItem>
                {
                    ["a"] = new FileListItem("A", 1, AnyContent, AnyEncoding),
                    ["b"] = new FileListItem("B", 2, AnyContent, AnyEncoding),
                    ["c"] = new FileListItem("C", 3, AnyContent, AnyEncoding)
                });
                var appsClient = Substitute.For<IAppsClient>();
                var sandboxesClient = Substitute.For<ISandboxesClient>();
                sandboxesClient.ListFilesAsync("acme", "cool", "vanilla", Arg.Any<FileListingOptions>(), None)
                    .Returns(Task.FromResult(archive));
                
                var sandboxes = new SandboxCollection(new[] {new Sandbox("acme", "cool", new[] {"vanilla"})});
                var connector = new TestableAppsContextConnector(appsClient, sandboxesClient, sandboxes);

                // Act
                var files = await connector.ListFilesAsync("acme.vanilla", "1.2.3");

                // Assert
                files.ShouldBe(new[]
                {
                    new AppFileDescriptor("acme.vanilla", "a", "A", 1),
                    new AppFileDescriptor("acme.vanilla", "b", "B", 2),
                    new AppFileDescriptor("acme.vanilla", "c", "C", 3),
                });
                connector.CachedFileLists["acme.vanilla"].ShouldBe(files);
            }
        public async Task<IGalleryContext> CreateFromPageRequestAsync(HttpRequestMessage request,
            ContextConfiguration config, CancellationToken cancellationToken)
        {
            var specifiedWorkspace = config.Workspace ?? GetWorkspaceFromCookie(request);
            var acceptSnapshot = specifiedWorkspace == null;
            var workspace = specifiedWorkspace ?? DefaultWorkspace;

            var extendedWorkspacesClient = ExtendedWorkspacesClient.FromEndpointUri(config.WorkspacesEndpoint,
                config.AuthenticationToken, acceptSnapshot);
            var sandboxes = new SandboxCollection();

            if (workspace != DefaultWorkspace)
            {
                sandboxes = await extendedWorkspacesClient.GetActiveSandboxesAsync(config.AccountName, workspace,
                cancellationToken).ConfigureAwait(false);
            }

            if (string.IsNullOrWhiteSpace(config.AccountName))
                throw new ArgumentException("You must set the account name in ContextConfiguration.");

            return await ConnectAsync(extendedWorkspacesClient, config.AppsEndpoint, config.AuthenticationToken,
                config.AccountName, workspace, sandboxes, config.ServiceName, config.ArchiveExcludePrefixes,
                cancellationToken).ConfigureAwait(false);
        }
        protected static async Task<IGalleryContext> ConnectAsync(IExtendedWorkspacesClient extendedWorkspacesClient,
            Uri appsEndpoint, string authenticationToken, string account, string workspace, SandboxCollection sandboxes, 
            string serviceName, string[] excludePrefixes, CancellationToken cancellationToken)
        {
            workspace = workspace ?? DefaultWorkspace;
            excludePrefixes = excludePrefixes ?? EmptyStringArray;

            var workspacesContextConnector = new WorkspacesContextConnector(extendedWorkspacesClient, account, workspace,
                sandboxes, serviceName, excludePrefixes);

            var apps = await workspacesContextConnector.GetAppsAsync(cancellationToken).ConfigureAwait(false);

            var appsConnector = new AppsConnector(appsEndpoint, authenticationToken);
            var appsClient = new AppsClient(appsConnector);

            var sandboxesClient = new SandboxesClient(appsConnector);
            var appsContextConnector = new AppsContextConnector(appsClient, sandboxesClient, apps, sandboxes,
                serviceName, excludePrefixes);

            return new GalleryContext(account, workspace, sandboxes, workspacesContextConnector, appsContextConnector);
        }
        protected static Task<IGalleryContext> ConnectAsync(Uri workspacesEndpoint, Uri appsEndpoint,
            string authenticationToken, string account, bool acceptSnapshot, string workspace,
            SandboxCollection sandboxes, string serviceName, string[] excludePrefixes,
            CancellationToken cancellationToken)
        {
            var extendedWorkspacesClient = ExtendedWorkspacesClient.FromEndpointUri(workspacesEndpoint, 
                authenticationToken, acceptSnapshot);

            return ConnectAsync(extendedWorkspacesClient, appsEndpoint, authenticationToken, account, 
                workspace, sandboxes, serviceName, excludePrefixes, cancellationToken);
        }
        public async Task Get_active_sandboxes()
        {
            // Arrange
            var expected =
                new SandboxCollection(new[]
                {
                    new Sandbox(null, "joao", new[] {"vanilla", "choco"}),
                    new Sandbox(null, "maria", new[] {"icecream", "brownie"})
                });

            var connector = Mock.Of<IWorkspacesConnector>()
                .ThatGetsJson("/acme/workspaces/cool/sandboxes", expected);
            var client = new ExtendedWorkspacesClient(connector);

            // Act
            var response = await client.GetActiveSandboxesAsync("acme", "cool", None);

            // Assert
            response.ShouldBe(expected);
        }