Example #1
0
        public void UnexpectedTenantConfigThrows(string tenantName, string projectName, string environment, Mock <IOctopusAsyncRepository> octoMoq)
        {
            octoMoq.Setup(o => o.Projects).Returns(mockProj(projectName).Object);
            var tenProj = new TenantVariableResource.Project("");

            tenProj.Variables.Add(environment, new Dictionary <string, PropertyValueResource>());
            octoMoq.Setup(o => o.Tenants).Returns(mockedTen(tenantName, projectName, tenProj).Object);
            octoMoq.Setup(o => o.Environments).Returns(mockEnv(environment).Object);

            var args = new TenantTargetArgs()
            {
                TenantName = tenantName, ProjectName = projectName, Environments = new List <string>()
                {
                    environment
                }
            };
            var sut = new TenantManager(args, octoMoq.Object, Mock.Of <ILogger>());

            Func <Task> test = () => sut.CreateTenantVariables(new List <SecretVariable>()
            {
                new SecretVariable("aA", "g")
            });

            test.Should().Throw <ArgumentException>().WithMessage($"The loaded configuration for tenant '{tenantName}' has variable 'aA' not found in the project '{projectName}'");
        }
Example #2
0
        public void TenantWithExistingVariableShouldNotThrow(string tenantName, string projectName, string environment, string variableName,
                                                             string variableValue, Mock <IOctopusAsyncRepository> octoMoq)
        {
            octoMoq.Setup(o => o.Projects).Returns(mockProj(projectName).Object);
            var tenProj = new TenantVariableResource.Project("");

            tenProj.Variables.Add(environment, new Dictionary <string, PropertyValueResource>()
            {
                { variableName, new PropertyValueResource(variableValue) }
            });
            tenProj.Templates.Add(new ActionTemplateParameterResource()
            {
                Name            = variableName,
                Id              = variableName,
                DefaultValue    = "",
                DisplaySettings = new Dictionary <string, string>()
                {
                    { "Octopus.ControlType", "Sensitive" }
                }
            });
            var mockTen = mockedTen(tenantName, projectName, tenProj);

            mockTen.Setup(t => t.ModifyVariables(It.IsAny <TenantResource>(), It.IsAny <TenantVariableResource>()))
            .Returns <TenantResource, TenantVariableResource>((_, tvr) => Task.FromResult(tvr));

            octoMoq.Setup(o => o.Tenants).Returns(mockTen.Object);
            octoMoq.Setup(o => o.Environments).Returns(mockEnv(environment).Object);

            var args = new TenantTargetArgs()
            {
                TenantName = tenantName, ProjectName = projectName, Environments = new List <string>()
                {
                    environment
                }
            };
            var sut = new TenantManager(args, octoMoq.Object, Mock.Of <ILogger>());

            Func <Task> act = () => sut.CreateTenantVariables(new List <SecretVariable>()
            {
                new SecretVariable(variableName, variableValue)
            });

            act.Should().NotThrow();
        }
Example #3
0
        public async Task SensitiveSecretShouldMakeTenantVariableSecret(string tenantName, string projectName, string environment, string variableName,
                                                                        string variableValue, Mock <IOctopusAsyncRepository> octoMoq)
        {
            octoMoq.Setup(o => o.Projects).Returns(mockProj(projectName).Object);
            var tenProj = new TenantVariableResource.Project("");

            tenProj.Variables.Add(environment, new Dictionary <string, PropertyValueResource>());
            tenProj.Templates.Add(new ActionTemplateParameterResource()
            {
                Name = variableName, Id = variableName
            });
            var mockTen = mockedTen(tenantName, projectName, tenProj);

            mockTen.Setup(t => t.ModifyVariables(It.IsAny <TenantResource>(), It.IsAny <TenantVariableResource>()))
            .Returns <TenantResource, TenantVariableResource>((tr, tvr) =>
            {
                tr.Name.Should().Be(tenantName);
                var prj = tvr.ProjectVariables[projectName];
                prj.Variables.Single().Key.Should().Be(environment);
                var env = prj.Variables.Single().Value;
                env.ContainsKey(variableName).Should().BeTrue($"Variable dictionary should contain variable with name {variableName}");
                env[variableName].IsSensitive.Should().BeTrue();
                env[variableName].SensitiveValue.HasValue.Should().BeTrue();
                env[variableName].SensitiveValue.NewValue.Should().Be(variableValue);
                return(Task.FromResult(tvr));
            });

            octoMoq.Setup(o => o.Tenants).Returns(mockTen.Object);
            octoMoq.Setup(o => o.Environments).Returns(mockEnv(environment).Object);

            var args = new TenantTargetArgs()
            {
                TenantName = tenantName, ProjectName = projectName, Environments = new List <string>()
                {
                    environment
                }
            };
            var sut = new TenantManager(args, octoMoq.Object, Mock.Of <ILogger>());

            await sut.CreateTenantVariables(new List <SecretVariable>() { new SecretVariable(variableName, variableValue)
                                                                          {
                                                                              IsSecret = true
                                                                          } }).ConfigureAwait(false);
        }
Example #4
0
        private Mock <ITenantRepository> mockedTen(string tenantName, string projectName, TenantVariableResource.Project obj)
        {
            var tenMoq = new Mock <ITenantRepository>();

            tenMoq.Setup(p => p.FindByName(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <object>()))
            .Returns(Task.FromResult(new TenantResource()
            {
                Name = tenantName
            }));
            tenMoq.Setup(t => t.GetVariables(It.IsAny <TenantResource>()))
            .Returns(Task.FromResult(
                         new TenantVariableResource()
            {
                ProjectVariables = new Dictionary <string, TenantVariableResource.Project>()
                {
                    { projectName, obj }
                }
            }));
            return(tenMoq);
        }