Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Commit

Permalink
(GH-14) Adding tests
Browse files Browse the repository at this point in the history
- To cover the functionality of the get_domain_from_host method
  • Loading branch information
gep13 committed Dec 5, 2015
1 parent e554a61 commit 8217f67
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="..\SolutionVersion.cs">
<Link>Properties\SolutionVersion.cs</Link>
</Compile>
<Compile Include="infrastructure.app\IconUrlNotSameDomainAsProjectUrlDomainorRawGitGuidelineSpecs.cs" />
<Compile Include="infrastructure\events\context\FakeMessage.cs" />
<Compile Include="infrastructure\events\context\FakeSubscriber.cs" />
<Compile Include="infrastructure\events\EventSubscriptionManagerSpecs.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
namespace chocolatey.package.validator.tests.infrastructure.app
{
using chocolatey.package.validator.infrastructure.app.rules;
using Should;

public abstract class IconUrlNotSameDomainAsProjectUrlDomainorRawGitGuidelineSpecsBase : TinySpec
{
protected IconUrlNotSameDomainAsProjectUrlDomainOrRawGitGuideline guideline;

public override void Context()
{
this.guideline = new IconUrlNotSameDomainAsProjectUrlDomainOrRawGitGuideline();
}
}

public class when_comparing_hosts : IconUrlNotSameDomainAsProjectUrlDomainorRawGitGuidelineSpecsBase
{
private string result;

public override void Because()
{
}

[Fact]
public void should_return_true_from_same_hosts_with_single_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("test.com", "test.com").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_hosts_with_double_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("test.co.uk", "test.co.uk").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_rawgit_domain_with_single_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("cdn.rawgit.com", "test.com").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_rawgit_domain_with_double_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("cdn.rawgit.com", "test.co.uk").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_domain_with_www_subdomain_on_project_and_no_subdomain_on_icon_and_single_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("test.com", "www.test.com").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_domain_with_www_subdomain_on_project_and_no_subdomain_on_icon_and_double_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("test.co.uk", "www.test.co.uk").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_domain_with_no_subdomain_on_project_and_www_subdomain_on_icon_and_single_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("www.test.com", "test.com").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_domain_with_no_subdomain_on_project_and_www_subdomain_on_icon_and_double_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("www.test.co.uk", "test.co.uk").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_domain_with_no_subdomain_on_project_and_arbitrary_subdomain_on_icon_and_single_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("somewildsubdomain.test.com", "test.com").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_domain_with_www_subdomain_on_project_and_arbitrary_subdomain_on_icon_and_single_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("somewildsubdomain.test.com", "www.test.com").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_domain_with_no_subdomain_on_project_and_arbitrary_subdomain_on_icon_and_double_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("somewildsubdomain.test.co.uk", "test.co.uk").ShouldBeTrue();
}

[Fact]
public void should_return_true_from_same_domain_with_www_subdomain_on_project_and_arbitrary_subdomain_on_icon_and_double_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("somewildsubdomain.test.co.uk", "www.test.co.uk").ShouldBeTrue();
}

[Fact]
public void should_return_false_from_different_domain_and_single_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("test1.com", "test.com").ShouldBeFalse();
}

[Fact]
public void should_return_false_from_different_domain_and_double_level_top_level_domain()
{
guideline.is_icon_from_same_domain_as_project_or_rawgit("test1.co.uk", "test.co.uk").ShouldBeFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace chocolatey.package.validator.infrastructure.app.rules
{
using System.Linq;
using System.Text.RegularExpressions;
using infrastructure.rules;
using NuGet;
Expand All @@ -23,6 +24,39 @@ public class IconUrlNotSameDomainAsProjectUrlDomainOrRawGitGuideline : BasePacka
{
public override string ValidationFailureMessage { get { return "The package IconUrl should ideally come from the same domain name as the Project Url, or hosted on the RawGit CDN. **NOTE:** For further information on how to setup your icon with a RawGit CDN URL, please visit this [article](https://github.com/chocolatey/choco/wiki/CreatePackages#package-icon-guidelines)."; } }

public string get_domain_from_host(string host)
{
// Use Regular Expression to extract the Domain Name, from the Uri Host
// Taken from example shown here http://stackoverflow.com/a/17091145/671491
var match = Regex.Match(host, @"(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,3})");
return match.Groups[1].Success ? match.Groups[1].Value : string.Empty;
}

public bool is_icon_from_same_domain_as_project_or_rawgit(string iconHost, string projectHost)
{
if (iconHost == "cdn.rawgit.com") return true;

if (iconHost == projectHost) return true;

if (projectHost.EndsWith(iconHost)) return true;

if (iconHost.EndsWith(projectHost)) return true;

// let's start taking potential subdomains from the icon host, but only do this if parts > 2
var parts = iconHost.Split('.').ToList();

if (parts.Count > 2)
{
parts.RemoveAt(0);

var strippedIconHost = string.Join(".", parts);

if (projectHost.EndsWith(strippedIconHost)) return true;
}

return false;
}

protected override PackageValidationOutput is_valid(IPackage package)
{
if (package.IconUrl == null) return true;
Expand All @@ -35,15 +69,7 @@ protected override PackageValidationOutput is_valid(IPackage package)

var projectUrlDomain = this.get_domain_from_host(package.ProjectUrl.Host);

return iconUrlDomain == projectUrlDomain || iconUrlDomain == "rawgit.com";
}

private string get_domain_from_host(string host)
{
// Use Regular Expression to extract the Domain Name, from the Uri Host
// Taken from example shown here http://stackoverflow.com/a/17091145/671491
var match = Regex.Match(host, "([^.]+\\.[^.]{1,3}(\\.[^.]{1,3})?)$");
return match.Groups[1].Success ? match.Groups[1].Value : string.Empty;
return is_icon_from_same_domain_as_project_or_rawgit(iconUrlDomain, projectUrlDomain);
}
}
}

0 comments on commit 8217f67

Please sign in to comment.