Skip to content

Commit

Permalink
Add tests for recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Nov 23, 2018
1 parent c4f067b commit 1a3ba64
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 7 deletions.
9 changes: 7 additions & 2 deletions Netkan/Transformers/AvcKrefTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CKAN.NetKAN.Extensions;
using CKAN.NetKAN.Model;
using CKAN.NetKAN.Sources.Avc;
using CKAN.NetKAN.Services;
using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -16,8 +17,12 @@ internal sealed class AvcKrefTransformer : ITransformer
private static readonly ILog Log = LogManager.GetLogger(typeof(AvcKrefTransformer));

public string Name { get { return "avc-kref"; } }
private IHttpService httpSvc;

public AvcKrefTransformer() { }
public AvcKrefTransformer(IHttpService http)
{
httpSvc = http;
}

public Metadata Transform(Metadata metadata)
{
Expand All @@ -29,7 +34,7 @@ public Metadata Transform(Metadata metadata)
Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

AvcVersion remoteAvc = JsonConvert.DeserializeObject<AvcVersion>(
Net.DownloadText(new Uri(metadata.Kref.Id))
httpSvc.DownloadText(new Uri(metadata.Kref.Id))
);

json.SafeAdd("name", remoteAvc.Name);
Expand Down
2 changes: 1 addition & 1 deletion Netkan/Transformers/NetkanTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool prerelease
new GithubTransformer(new GithubApi(githubToken), prerelease),
new HttpTransformer(),
new JenkinsTransformer(http),
new AvcKrefTransformer(),
new AvcKrefTransformer(http),
new InternalCkanTransformer(http, moduleService),
new AvcTransformer(http, moduleService),
new VersionEditTransformer(),
Expand Down
49 changes: 49 additions & 0 deletions Tests/Core/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,54 @@ public void ZipValidation()
Assert.IsTrue(cache.IsCached(url));
Assert.IsTrue(cache.IsCachedZip(url));
}

[Test]
public void ZipValid_ContainsFilenameWithBadChars_NoException()
{
bool valid = false;
string reason = "";
Assert.DoesNotThrow(() =>
valid = NetFileCache.ZipValid(TestData.ZipWithBadChars, out reason));

// The file is considered valid on Linux;
// only check the reason if found invalid
if (!valid)
{
Assert.AreEqual(reason, "Illegal characters in path.");
}
}

[Test]
public void EnforceSizeLimit_UnderLimit_FileRetained()
{
// Arrange
CKAN.Registry registry = CKAN.Registry.Empty();
long fileSize = new FileInfo(TestData.DogeCoinFlagZip()).Length;

// Act
Uri url = new Uri("http://kitte.nz/");
cache.Store(url, TestData.DogeCoinFlagZip());
cache.EnforceSizeLimit(fileSize + 100, registry);

// Assert
Assert.IsTrue(cache.IsCached(url));
}

[Test]
public void EnforceSizeLimit_OverLimit_FileRemoved()
{
// Arrange
CKAN.Registry registry = CKAN.Registry.Empty();
long fileSize = new FileInfo(TestData.DogeCoinFlagZip()).Length;

// Act
Uri url = new Uri("http://kitte.nz/");
cache.Store(url, TestData.DogeCoinFlagZip());
cache.EnforceSizeLimit(fileSize - 100, registry);

// Assert
Assert.IsFalse(cache.IsCached(url));
}

}
}
9 changes: 7 additions & 2 deletions Tests/Data/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,19 @@ public static string DogeCoinFlag_101_LZMA()
";
}

public static CkanModule DogeCoinFlag_101_LZMA_module = CkanModule.FromJson(
public static readonly CkanModule DogeCoinFlag_101_LZMA_module = CkanModule.FromJson(
DogeCoinFlag_101_LZMA()
);

/// <summary>
/// Test case for LZMA-format ZIPs
/// </summary>
public static string DogeCoinFlagZipLZMA = Path.Combine(DataDir(), "DogeCoinFlag-1.01-LZMA.zip");
public static readonly string DogeCoinFlagZipLZMA = DataDir("DogeCoinFlag-1.01-LZMA.zip");

/// <summary>
/// Contains files with names that include characters that aren't allowed on Windows
/// </summary>
public static readonly string ZipWithBadChars = DataDir("ZipWithBadChars.zip");

///<summary>
/// DogeCoinFlag 1.01 info. This contains a bug where the
Expand Down
Binary file added Tests/Data/ZipWithBadChars.zip
Binary file not shown.
34 changes: 33 additions & 1 deletion Tests/NetKAN/AVC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void WildcardMinor_VersionOnlyHasMajor()
var result = (KspVersion) converter.ReadJson(reader, null, null, null);
Assert.That(result, Is.EqualTo(KspVersion.Parse("1")));
}

[Test]
public void WildcardPatch_VersionOnlyHasMajorMinor()
{
Expand All @@ -65,5 +66,36 @@ public void WildcardPatch_VersionOnlyHasMajorMinor()
var result = (KspVersion)converter.ReadJson(reader, null, null, null);
Assert.That(result, Is.EqualTo(KspVersion.Parse("1.5")));
}

[Test]
public void MissingMajor_OutputsAnyVersion()
{
var converter = new JsonAvcToKspVersion();
string json = @"{}";
var reader = new JsonTextReader(new StringReader(json));
var result = (KspVersion) converter.ReadJson(reader, null, null, null);
Assert.That(!result.IsMajorDefined);
}

[Test]
public void MissingMinor_VersionOnlyHasMajor()
{
var converter = new JsonAvcToKspVersion();
string json = @"{""MAJOR"":1}";
var reader = new JsonTextReader(new StringReader(json));
var result = (KspVersion) converter.ReadJson(reader, null, null, null);
Assert.That(result, Is.EqualTo(KspVersion.Parse("1")));
}

[Test]
public void MissingPatch_VersionOnlyHasMajorMinor()
{
var converter = new JsonAvcToKspVersion();
string json = @"{""MAJOR"":1, ""MINOR"":5}";
var reader = new JsonTextReader(new StringReader(json));
var result = (KspVersion)converter.ReadJson(reader, null, null, null);
Assert.That(result, Is.EqualTo(KspVersion.Parse("1.5")));
}

}
}
}
72 changes: 72 additions & 0 deletions Tests/NetKAN/Transformers/AvcKrefTransformerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using Moq;
using CKAN.NetKAN.Services;
using CKAN.NetKAN.Model;
using CKAN.NetKAN.Transformers;

namespace Tests.NetKAN.Transformers
{
[TestFixture]
public sealed class AvcKrefTransformerTests
{
[Test,
TestCase(
"https://mysite.org/AwesomeMod.version",
"1.0.0",
"1.4.1",
"https://mysite.org/AwesomeMod.zip",
@"{
""NAME"": ""AwesomeMod"",
""URL"": ""https://mysite.org/AwesomeMod.version"",
""DOWNLOAD"": ""https://mysite.org/AwesomeMod.zip"",
""VERSION"": {
""MAJOR"": 1,
""MINOR"": 0,
""PATCH"": 0
},
""KSP_VERSION"": {
""MAJOR"": 1,
""MINOR"": 4,
""PATCH"": 1
}
}"
)
]
public void Transform_SimpleVersionFile_PropertiesSet(string remoteUrl, string version, string kspVersion, string download, string remoteAvc)
{
// Arrange
var mHttp = new Mock<IHttpService>();
mHttp.Setup(i => i.DownloadText(It.IsAny<Uri>()))
.Returns(remoteAvc);

// Act
Metadata m = null;
Assert.DoesNotThrow(() => m = TryKref(mHttp.Object, $"#/ckan/ksp-avc/{remoteUrl}"));

// Assert
var json = m.Json();
Assert.AreEqual((string)json["version"], version);
Assert.AreEqual((string)json["ksp_version"], kspVersion);
Assert.AreEqual((string)json["download"], download);
}

private Metadata TryKref(IHttpService http, string kref)
{
// Arrange
var json = new JObject();
json["spec_version"] = 1;
json["identifier"] = "AwesomeMod";
if (kref != null)
{
json["$kref"] = kref;
}

// Act
var tran = new AvcKrefTransformer(http);
return tran.Transform(new Metadata(json));
}

}
}
15 changes: 15 additions & 0 deletions Tests/NetKAN/Transformers/AvcTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ public void PreferentiallyAddsRangedKspVersionInfo()
"1.0.2", "1.0.3", "1.0.4",
null, "1.0.0", "1.0.4"
)]
[TestCase(
null, null, null,
"1.5.1", "1.5.1", null,
null, "1.5.1", null
)]
[TestCase(
null, null, null,
"1.5.1", null, "1.5.1",
null, null, "1.5.1"
)]
[TestCase(
"any", null, null,
null, null, null,
"any", null, null
)]
public void CorrectlyCalculatesKspVersionInfo(
string existingKsp, string existingKspMin, string existingKspMax,
string avcKsp, string avcKspMin, string avcKspMax,
Expand Down
52 changes: 52 additions & 0 deletions Tests/NetKAN/Validators/KrefValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using CKAN.NetKAN.Model;
using CKAN.NetKAN.Validators;

namespace Tests.NetKAN.Validators
{
[TestFixture]
public sealed class KrefValidatorTests
{
[Test,
TestCase(null),
TestCase("#/ckan/curse/12345"),
TestCase("#/ckan/github/AGoodModder/AGoodMod"),
TestCase("#/ckan/http/https://mysite.org/AGoodMod.zip"),
TestCase("#/ckan/ksp-avc/https://mysite.org/AGoodMod.version"),
TestCase("#/ckan/netkan/https://mysite.org/AGoodMod.netkan"),
TestCase("#/ckan/jenkins/https://mysite.org/AGoodMod/"),
TestCase("#/ckan/spacedock/12345")
]
public void Validate_KnownKref_Valid(string kref)
{
Assert.DoesNotThrow(() => TryKref(kref));
}

[Test,
TestCase("#/ckan/techman/iscool"),
TestCase("#/ckan/spaceport/305")
]
public void Validate_TechManIsCool_Invalid(string kref)
{
Assert.Throws<CKAN.Kraken>(() => TryKref(kref));
}

private void TryKref(string kref)
{
// Arrange
var json = new JObject();
json["spec_version"] = 1;
json["identifier"] = "AwesomeMod";
if (kref != null)
{
json["$kref"] = kref;
}

// Act
var val = new KrefValidator();
val.Validate(new Metadata(json));
}

}
}
4 changes: 3 additions & 1 deletion Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Include="NetKAN\Sources\Curse\CurseApiTests.cs" />
<Compile Include="NetKAN\Sources\Github\GithubApiTests.cs" />
<Compile Include="NetKAN\Sources\Spacedock\SpacedockApiTests.cs" />
<Compile Include="NetKAN\Transformers\AvcKrefTransformerTests.cs" />
<Compile Include="NetKAN\Transformers\AvcTransformerTests.cs" />
<Compile Include="NetKAN\Transformers\CurseTransformerTests.cs" />
<Compile Include="NetKAN\Transformers\DownloadAttributeTransformerTests.cs" />
Expand All @@ -130,6 +131,7 @@
<Compile Include="NetKAN\Validators\IsCkanModuleValidatorTests.cs" />
<Compile Include="NetKAN\Validators\MatchingIdentifiersValidatorTests.cs" />
<Compile Include="NetKAN\Validators\NetkanValidatorTests.cs" />
<Compile Include="NetKAN\Validators\KrefValidatorTests.cs" />
<Compile Include="Util.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -211,4 +213,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\_build\lib\nuget\NUnit3TestAdapter.3.9.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\_build\lib\nuget\NUnit3TestAdapter.3.9.0\build\net35\NUnit3TestAdapter.props'))" />
</Target>
</Project>
</Project>

0 comments on commit 1a3ba64

Please sign in to comment.