Skip to content

Commit

Permalink
Merge pull request #17 from vojtech-vit-massive/feature/#16-certificates
Browse files Browse the repository at this point in the history
feat: support for certificate APIs
  • Loading branch information
Swoogan authored Apr 15, 2019
2 parents f88030a + 911c8c4 commit a1ae6d3
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 5 deletions.
123 changes: 123 additions & 0 deletions Octopus-Cmdlets.Tests/AddCertificateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Collections.Generic;
using System.Management.Automation;
using Xunit;
using Moq;
using Octopus.Client.Model;
using Octopus.Client.Repositories;
using System.Text;

namespace Octopus_Cmdlets.Tests
{
public class AddCertificateTests
{
private const string CmdletName = "Add-OctoCertificate";
private PowerShell _ps;
private readonly List<CertificateResource> _certs = new List<CertificateResource>();

public AddCertificateTests()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof (AddCertificate));
var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);

_certs.Clear();

var envRepo = new Mock<ICertificateRepository>();
envRepo.Setup(c => c.Create(It.IsAny<CertificateResource>(), It.IsAny<object>()))
.Returns((CertificateResource c, object o) =>
{
_certs.Add(c);
return c;
});

octoRepo.Setup(o => o.Certificates).Returns(envRepo.Object);
}

[Fact]
public void With_Name()
{
// Execute cmdlet
_ps.AddCommand(CmdletName)
.AddParameter(nameof(AddCertificate.Name), "Octopus_Dev");

Assert.Throws<ParameterBindingException>(() => _ps.Invoke());
}

[Fact]
public void With_CertificateData()
{
// Execute cmdlet
_ps.AddCommand(CmdletName)
.AddParameter(nameof(AddCertificate.CertificateData), "CertData");

Assert.Throws<ParameterBindingException>(() => _ps.Invoke());
}

[Fact]
public void With_Name_And_CertificateData()
{
// Execute cmdlet
_ps.AddCommand(CmdletName)
.AddParameter(nameof(AddCertificate.Name), "Octopus_Dev")
.AddParameter(nameof(AddCertificate.CertificateData), "CertData");
_ps.Invoke();

Assert.Single(_certs);
Assert.Equal("Octopus_Dev", _certs[0].Name);
Assert.Equal("CertData", _certs[0].CertificateData.NewValue);
}

[Fact]
public void With_Name_And_CertificateData_And_Notes()
{
// Execute cmdlet
_ps.AddCommand(CmdletName)
.AddParameter(nameof(AddCertificate.Name), "Octopus_Dev")
.AddParameter(nameof(AddCertificate.CertificateData), "CertData")
.AddParameter(nameof(AddCertificate.Notes), "Octopus Development certificate");
_ps.Invoke();

Assert.Single(_certs);
Assert.Equal("Octopus_Dev", _certs[0].Name);
Assert.Equal("CertData", _certs[0].CertificateData.NewValue);
Assert.Equal("Octopus Development certificate", _certs[0].Notes);
}

[Fact]
public void With_Name_And_CertificateData_And_Environments()
{
// Execute cmdlet
_ps.AddCommand(CmdletName)
.AddParameter(nameof(AddCertificate.Name), "Octopus_Dev")
.AddParameter(nameof(AddCertificate.CertificateData), "CertData")
.AddParameter(nameof(AddCertificate.Environments), new[] { "Env1", "Env2" });
_ps.Invoke();

Assert.Single(_certs);
Assert.Equal("Octopus_Dev", _certs[0].Name);
Assert.Equal("CertData", _certs[0].CertificateData.NewValue);
Assert.True(_certs[0].EnvironmentIds.SetEquals(new[] { "Env1", "Env2" }));
}

[Fact]
public void With_Arguments()
{
// Execute cmdlet
_ps.AddCommand(CmdletName)
.AddArgument("Octopus_Dev")
.AddParameter(nameof(AddCertificate.CertificateData), "CertData");
_ps.Invoke();

Assert.Single(_certs);
Assert.Equal("Octopus_Dev", _certs[0].Name);
Assert.Equal("CertData", _certs[0].CertificateData.NewValue);
}

[Fact]
public void No_Arguments()
{
// Execute cmdlet
_ps.AddCommand(CmdletName);
Assert.Throws<ParameterBindingException>(() => _ps.Invoke());
}
}
}
111 changes: 111 additions & 0 deletions Octopus-Cmdlets.Tests/GetCertificateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Collections.Generic;
using System.Management.Automation;
using Xunit;
using Octopus.Client.Model;

namespace Octopus_Cmdlets.Tests
{
public class GetCertificateTests
{
private const string CmdletName = "Get-OctoCertificate";
private PowerShell _ps;

public GetCertificateTests()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(GetCertificate));

var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);

// Create some certificates
var certificates = new List<CertificateResource>
{
new CertificateResource("Octopus", "OctopusData") { Id = "certificates-1", EnvironmentIds = new ReferenceCollection("env1")},
new CertificateResource("Deploy", "DeployData") { Id = "certificates-2", EnvironmentIds = new ReferenceCollection("env2")},
new CertificateResource("Automation", "AutomationData") { Id = "certificates-3", EnvironmentIds = new ReferenceCollection("env3")},
new CertificateResource("Server", "ServerData") { Id = "certificates-4", EnvironmentIds = new ReferenceCollection("env4")},
};
octoRepo.Setup(o => o.Certificates.FindAll(null, null)).Returns(certificates);
}

[Fact]
public void No_Arguments()
{
// Execute cmdlet
_ps.AddCommand(CmdletName);
var certificates = _ps.Invoke<CertificateResource>();

Assert.Equal(4, certificates.Count);
}

[Fact]
public void With_Name()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddArgument("Octopus");
var certificates = _ps.Invoke<CertificateResource>();

Assert.Single(certificates);
Assert.Equal("Octopus", certificates[0].Name);
}

[Fact]
public void With_Invalid_Name()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddArgument("Gibberish");
var certificates = _ps.Invoke<CertificateResource>();

Assert.Empty(certificates);
}

[Fact]
public void With_Id()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Id", "certificates-1");
var certificates = _ps.Invoke<CertificateResource>();

Assert.Single(certificates);
Assert.Equal("Octopus", certificates[0].Name);
}

[Fact]
public void With_Invalid_Id()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Id", "Gibberish");
var certificates = _ps.Invoke<CertificateResource>();

Assert.Empty(certificates);
}

[Fact]
public void With_Environment()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Environment", "env1");
var certificates = _ps.Invoke<CertificateResource>();

Assert.Single(certificates);
Assert.Equal("Octopus", certificates[0].Name);
}

[Fact]
public void With_Invalid_EnvironmentValue()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Environment", "Gibberish");
var certificates = _ps.Invoke<CertificateResource>();

Assert.Empty(certificates);
}

[Fact]
public void With_Id_And_Name()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Name", "Name").AddParameter("Id", "Id");
Assert.Throws<ParameterBindingException>(() => _ps.Invoke());
}
}
}
132 changes: 132 additions & 0 deletions Octopus-Cmdlets.Tests/RemoveCertificateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.Collections.Generic;
using System.Management.Automation;
using Xunit;
using Moq;
using Octopus.Client.Exceptions;
using Octopus.Client.Model;

namespace Octopus_Cmdlets.Tests
{
public class RemoveCertificateTests
{
private const string CmdletName = "Remove-OctoCertificate";
private PowerShell _ps;
private readonly List<CertificateResource> _certs = new List<CertificateResource>();

private readonly CertificateResource _cert = new CertificateResource("CERT2", "CertData2");

public RemoveCertificateTests()
{
_ps = Utilities.CreatePowerShell(CmdletName, typeof(RemoveCertificate));

var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);

// Create some library variable sets
_certs.Clear();
_certs.Add(new CertificateResource("CERT 1", "CertData1") { Id = "CERT1" });
_certs.Add(_cert);
_certs.Add(new CertificateResource("CERT 3", "CertData3") { Id = "CERT3" });

octoRepo.Setup(o => o.Certificates.Delete(It.IsAny<CertificateResource>())).Callback(
(CertificateResource set) =>
{
if (_certs.Contains(set))
_certs.Remove(set);
else
throw new KeyNotFoundException("The given key was not present in the dictionary.");
}
);

octoRepo.Setup(o => o.Certificates.Get("CERT1")).Returns(_cert);
octoRepo.Setup(o => o.Certificates.Get(It.IsNotIn(new[] { "CERT1" })))
.Throws(new OctopusResourceNotFoundException("Not Found"));

octoRepo.Setup(o => o.Certificates.FindByName("CERT 2", It.IsAny<string>(), It.IsAny<object>())).Returns(_cert);
octoRepo.Setup(o => o.Certificates.FindByName("Gibberish", It.IsAny<string>(), It.IsAny<object>())).Returns((CertificateResource) null);
}

[Fact]
public void No_Arguments()
{
// Execute cmdlet
_ps.AddCommand(CmdletName);
Assert.Throws<ParameterBindingException>(() => _ps.Invoke());
}

[Fact]
public void With_Id()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Id", new [] {"CERT1"});
_ps.Invoke();

Assert.Equal(2, _certs.Count);
Assert.DoesNotContain(_cert, _certs);
}

[Fact]
public void With_Invalid_Id()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Id", new[] {"Gibberish"});
_ps.Invoke();

Assert.Equal(3, _certs.Count);
Assert.Single(_ps.Streams.Warning);
Assert.Equal("A certificate with the id 'Gibberish' does not exist.", _ps.Streams.Warning[0].ToString());
}

[Fact]
public void With_Name()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Name", new[] {"CERT 2"});
_ps.Invoke();

Assert.Equal(2, _certs.Count);
Assert.DoesNotContain(_cert, _certs);
}

[Fact]
public void With_Invalid_Name()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Name", new[] { "Gibberish" });
_ps.Invoke();

Assert.Equal(3, _certs.Count);
Assert.Single(_ps.Streams.Warning);
Assert.Equal("The certificate 'Gibberish' does not exist.", _ps.Streams.Warning[0].ToString());
}

[Fact]
public void With_Valid_And_Invalid_Names()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Name", new[] { "CERT 2", "Gibberish" });
_ps.Invoke();

Assert.Equal(2, _certs.Count);
Assert.DoesNotContain(_cert, _certs);
}

[Fact]
public void With_Arguments()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddArgument(new[] { "CERT 2" });
_ps.Invoke();

Assert.Equal(2, _certs.Count);
Assert.DoesNotContain(_cert, _certs);
}

[Fact]
public void With_Name_And_Id()
{
// Execute cmdlet
_ps.AddCommand(CmdletName).AddParameter("Name", "Gibberish").AddParameter("Id", "Gibberish");
Assert.Throws<ParameterBindingException>(() => _ps.Invoke());
}
}
}
Loading

0 comments on commit a1ae6d3

Please sign in to comment.