Skip to content

Commit 6aa02bc

Browse files
authored
Transform to https license URLs for known domains (#6418)
1 parent a9ed486 commit 6aa02bc

6 files changed

Lines changed: 50 additions & 147 deletions

File tree

src/NuGetGallery/ViewModels/DisplayPackageViewModel.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ public DisplayPackageViewModel(Package package, User currentUser, string pushedB
6262
{
6363
ProjectUrl = projectUrl;
6464
}
65+
66+
if (PackageHelper.TryPrepareUrlForRendering(package.LicenseUrl, out string licenseUrl))
67+
{
68+
LicenseUrl = licenseUrl;
69+
70+
var licenseNames = package.LicenseNames;
71+
if (!string.IsNullOrEmpty(licenseNames))
72+
{
73+
LicenseNames = licenseNames.Split(',').Select(l => l.Trim());
74+
}
75+
}
6576
}
6677

6778
public bool ValidatingTooLong { get; set; }
@@ -115,6 +126,8 @@ public bool HasNewerRelease
115126
public string RepositoryUrl { get; private set; }
116127
public RepositoryKind RepositoryType { get; private set; }
117128
public string ProjectUrl { get; set; }
129+
public string LicenseUrl { get; set; }
130+
public IEnumerable<string> LicenseNames { get; set; }
118131

119132
private IDictionary<User, string> _pushedByCache = new Dictionary<User, string>();
120133

src/NuGetGallery/ViewModels/PackageViewModel.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
75
using NuGet.Versioning;
86

97
namespace NuGetGallery
@@ -32,8 +30,6 @@ public PackageViewModel(Package package)
3230
Description = package.Description;
3331
ReleaseNotes = package.ReleaseNotes;
3432
IconUrl = package.IconUrl;
35-
LicenseUrl = package.LicenseUrl;
36-
HideLicenseReport = package.HideLicenseReport;
3733
LatestVersion = package.IsLatest;
3834
LatestVersionSemVer2 = package.IsLatestSemVer2;
3935
LatestStableVersion = package.IsLatestStable;
@@ -43,22 +39,11 @@ public PackageViewModel(Package package)
4339
_packageStatus = package.PackageStatusKey;
4440
DownloadCount = package.DownloadCount;
4541
Prerelease = package.IsPrerelease;
46-
LicenseReportUrl = package.LicenseReportUrl;
47-
48-
var licenseNames = package.LicenseNames;
49-
if (!string.IsNullOrEmpty(licenseNames))
50-
{
51-
LicenseNames = licenseNames.Split(',').Select(l => l.Trim());
52-
}
5342
}
5443

5544
public string Description { get; set; }
5645
public string ReleaseNotes { get; set; }
5746
public string IconUrl { get; set; }
58-
public string LicenseUrl { get; set; }
59-
public Boolean HideLicenseReport { get; set; }
60-
public IEnumerable<string> LicenseNames { get; set; }
61-
public string LicenseReportUrl { get; set; }
6247
public DateTime LastUpdated { get; set; }
6348
public bool LatestVersion { get; set; }
6449
public bool LatestStableVersion { get; set; }

src/NuGetGallery/Views/Packages/DisplayPackage.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@
618618
</a>
619619
</li>
620620
}
621-
@if (!Model.Deleted && PackageHelper.ShouldRenderUrl(Model.LicenseUrl))
621+
@if (!Model.Deleted && Model.LicenseUrl != null)
622622
{
623623
<li>
624624
<i class="ms-Icon ms-Icon--Certificate" aria-hidden="true"></i>

tests/NuGetGallery.Facts/ViewModels/DisplayPackageViewModelFacts.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,42 @@ public void ItInitializesProjectUrl(string projectUrl, string expected)
8888
Assert.Equal(expected, model.ProjectUrl);
8989
}
9090

91+
92+
[Theory]
93+
[InlineData(null, null)]
94+
[InlineData("not a url", null)]
95+
[InlineData("git://github.com/notavalidscheme", null)]
96+
[InlineData("http://www.microsoft.com/web/webpi/eula/webpages_2_eula_enu.htm", "https://www.microsoft.com/web/webpi/eula/webpages_2_eula_enu.htm")]
97+
[InlineData("http://aspnetwebstack.codeplex.com/license", "https://aspnetwebstack.codeplex.com/license")]
98+
[InlineData("http://go.microsoft.com/?linkid=9809688", "https://go.microsoft.com/?linkid=9809688")]
99+
[InlineData("http://github.com/url", "https://github.com/url")]
100+
public void ItInitializesLicenseUrl(string licenseUrl, string expected)
101+
{
102+
var package = new Package
103+
{
104+
Version = "1.0.0",
105+
LicenseUrl = licenseUrl
106+
};
107+
108+
var model = new DisplayPackageViewModel(package, null, "test");
109+
Assert.Equal(expected, model.LicenseUrl);
110+
}
111+
112+
[Fact]
113+
public void LicenseNamesAreParsedByCommas()
114+
{
115+
var package = new Package
116+
{
117+
LicenseUrl = "https://mylicense.com",
118+
Version = "1.0.0",
119+
LicenseNames = "l1,l2, l3 ,l4 , l5 ",
120+
};
121+
122+
var packageViewModel = new DisplayPackageViewModel(package, currentUser: null, pushedBy: "test");
123+
Assert.Equal(new string[] { "l1", "l2", "l3", "l4", "l5" }, packageViewModel.LicenseNames);
124+
}
125+
126+
91127
[Fact]
92128
public void TheCtorSortsPackageVersionsProperly()
93129
{

tests/NuGetGallery.Facts/ViewModels/ListPackageItemViewModelFacts.cs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,71 +37,6 @@ public void UsesNormalizedPackageVersionIfNormalizedVersionMissing()
3737
Assert.Equal("1.2.0", packageViewModel.Version);
3838
}
3939

40-
[Fact]
41-
public void LicenseNamesAreParsedByCommas()
42-
{
43-
var package = new Package
44-
{
45-
Version = "1.0.0",
46-
LicenseNames = "l1,l2, l3 ,l4 , l5 ",
47-
};
48-
var packageViewModel = new ListPackageItemViewModel(package, currentUser: null);
49-
Assert.Equal(new string[] { "l1", "l2", "l3", "l4", "l5" }, packageViewModel.LicenseNames);
50-
}
51-
52-
[Fact]
53-
public void LicenseReportFieldsKeptWhenLicenseReportDisabled()
54-
{
55-
var package = new Package
56-
{
57-
Version = "1.0.0",
58-
HideLicenseReport = true,
59-
LicenseNames = "l1",
60-
LicenseReportUrl = "url"
61-
};
62-
var packageViewModel = new ListPackageItemViewModel(package, currentUser: null);
63-
Assert.NotNull(packageViewModel.LicenseNames);
64-
Assert.NotNull(packageViewModel.LicenseReportUrl);
65-
}
66-
67-
[Fact]
68-
public void LicenseReportUrlKeptWhenLicenseReportEnabled()
69-
{
70-
var package = new Package
71-
{
72-
Version = "1.0.0",
73-
HideLicenseReport = false,
74-
LicenseReportUrl = "url"
75-
};
76-
var packageViewModel = new ListPackageItemViewModel(package, currentUser: null);
77-
Assert.NotNull(packageViewModel.LicenseReportUrl);
78-
}
79-
80-
[Fact]
81-
public void LicenseNamesKeptWhenLicenseReportEnabled()
82-
{
83-
var package = new Package
84-
{
85-
Version = "1.0.0",
86-
HideLicenseReport = false,
87-
LicenseNames = "l1"
88-
};
89-
var packageViewModel = new ListPackageItemViewModel(package, currentUser: null);
90-
Assert.NotNull(packageViewModel.LicenseNames);
91-
}
92-
93-
[Fact]
94-
public void LicenseUrlKeptWhenLicenseReportDisabled()
95-
{
96-
var package = new Package
97-
{
98-
Version = "1.0.0",
99-
HideLicenseReport = true,
100-
LicenseUrl = "url"
101-
};
102-
var packageViewModel = new ListPackageItemViewModel(package, currentUser: null);
103-
Assert.NotNull(packageViewModel.LicenseUrl);
104-
}
10540
#endregion
10641

10742
[Fact]

tests/NuGetGallery.Facts/ViewModels/PackageViewModelFacts.cs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -31,72 +31,6 @@ public void UsesNormalizedPackageVersionIfNormalizedVersionMissing()
3131
Assert.Equal("1.2.0", packageViewModel.Version);
3232
}
3333

34-
[Fact]
35-
public void LicenseNamesAreParsedByCommas()
36-
{
37-
var package = new Package
38-
{
39-
Version = "1.0.0",
40-
LicenseNames = "l1,l2, l3 ,l4 , l5 ",
41-
};
42-
var packageViewModel = new PackageViewModel(package);
43-
Assert.Equal(new string[] { "l1", "l2", "l3", "l4", "l5" }, packageViewModel.LicenseNames);
44-
}
45-
46-
[Fact]
47-
public void LicenseReportFieldsKeptWhenLicenseReportDisabled()
48-
{
49-
var package = new Package
50-
{
51-
Version = "1.0.0",
52-
HideLicenseReport = true,
53-
LicenseNames = "l1",
54-
LicenseReportUrl = "url"
55-
};
56-
var packageViewModel = new PackageViewModel(package);
57-
Assert.NotNull(packageViewModel.LicenseNames);
58-
Assert.NotNull(packageViewModel.LicenseReportUrl);
59-
}
60-
61-
[Fact]
62-
public void LicenseReportUrlKeptWhenLicenseReportEnabled()
63-
{
64-
var package = new Package
65-
{
66-
Version = "1.0.0",
67-
HideLicenseReport = false,
68-
LicenseReportUrl = "url"
69-
};
70-
var packageViewModel = new PackageViewModel(package);
71-
Assert.NotNull(packageViewModel.LicenseReportUrl);
72-
}
73-
74-
[Fact]
75-
public void LicenseNamesKeptWhenLicenseReportEnabled()
76-
{
77-
var package = new Package
78-
{
79-
Version = "1.0.0",
80-
HideLicenseReport = false,
81-
LicenseNames = "l1"
82-
};
83-
var packageViewModel = new PackageViewModel(package);
84-
Assert.NotNull(packageViewModel.LicenseNames);
85-
}
86-
87-
[Fact]
88-
public void LicenseUrlKeptWhenLicenseReportDisabled()
89-
{
90-
var package = new Package
91-
{
92-
Version = "1.0.0",
93-
HideLicenseReport = true,
94-
LicenseUrl = "url"
95-
};
96-
var packageViewModel = new PackageViewModel(package);
97-
Assert.NotNull(packageViewModel.LicenseUrl);
98-
}
99-
10034
[Theory]
10135
[InlineData(PackageStatus.Available, true, PackageStatusSummary.Listed)]
10236
[InlineData(PackageStatus.Available, false, PackageStatusSummary.Unlisted)]

0 commit comments

Comments
 (0)