Skip to content

Commit 59b5122

Browse files
authored
License file package - License info should appear only after validation job and redirect to correct Url. (#6738)
* Redirect license file * only show alert for license file type * Add indentation * Refactor and add tests
1 parent 53e1f7e commit 59b5122

4 files changed

Lines changed: 79 additions & 20 deletions

File tree

src/NuGetGallery/UrlHelperExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,20 @@ public static string ContactOwners(this UrlHelper url, string id, string version
12671267
});
12681268
}
12691269

1270+
public static string License(this UrlHelper url, string id, string version, bool relativeUrl = true)
1271+
{
1272+
return GetActionLink(
1273+
url,
1274+
"License",
1275+
"Packages",
1276+
relativeUrl,
1277+
routeValues: new RouteValueDictionary
1278+
{
1279+
{ "id", id },
1280+
{ "version", version }
1281+
});
1282+
}
1283+
12701284
public static string Terms(this UrlHelper url, bool relativeUrl = true)
12711285
{
12721286
if (!String.IsNullOrEmpty(_configuration.Current.ExternalTermsOfUseUrl))

src/NuGetGallery/ViewModels/DisplayPackageViewModel.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using NuGet.Services.Entities;
88
using NuGet.Services.Validation.Issues;
99
using NuGet.Versioning;
10-
using NuGetGallery.Helpers;
1110

1211
namespace NuGetGallery
1312
{
@@ -62,12 +61,10 @@ public DisplayPackageViewModel(Package package, User currentUser, string pushedB
6261
ProjectUrl = projectUrl;
6362
}
6463

65-
var licenseExpression = package.LicenseExpression;
66-
if (!string.IsNullOrWhiteSpace(licenseExpression))
67-
{
68-
LicenseUrl = LicenseExpressionRedirectUrlHelper.GetLicenseExpressionRedirectUrl(licenseExpression);
69-
}
70-
else if (PackageHelper.TryPrepareUrlForRendering(package.LicenseUrl, out string licenseUrl))
64+
EmbeddedLicenseType = package.EmbeddedLicenseType;
65+
LicenseExpression = package.LicenseExpression;
66+
67+
if (PackageHelper.TryPrepareUrlForRendering(package.LicenseUrl, out string licenseUrl))
7168
{
7269
LicenseUrl = licenseUrl;
7370

@@ -132,6 +129,8 @@ public bool HasNewerRelease
132129
public string ProjectUrl { get; set; }
133130
public string LicenseUrl { get; set; }
134131
public IEnumerable<string> LicenseNames { get; set; }
132+
public string LicenseExpression { get; set; }
133+
public EmbeddedLicenseFileType EmbeddedLicenseType { get; set; }
135134

136135
private IDictionary<User, string> _pushedByCache = new Dictionary<User, string>();
137136

src/NuGetGallery/Views/Packages/DisplayPackage.cshtml

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
PackageManagerViewModel[] packageManagers;
1515

16+
if (!string.IsNullOrWhiteSpace(Model.LicenseExpression))
17+
{
18+
Model.LicenseUrl = LicenseExpressionRedirectUrlHelper.GetLicenseExpressionRedirectUrl(Model.LicenseExpression);
19+
}
20+
21+
if (Model.EmbeddedLicenseType != EmbeddedLicenseFileType.Absent)
22+
{
23+
Model.LicenseUrl = Url.License(Model.Id, Model.Version);
24+
}
25+
1626
if (Model.IsDotnetToolPackageType)
1727
{
1828
packageManagers = new PackageManagerViewModel[]
@@ -182,6 +192,14 @@
182192
validation and indexing may take up to an hour. <a href="https://aka.ms/NuGetPackageValidation">Read more</a>.
183193
</text>
184194
)
195+
if (Model.EmbeddedLicenseType != EmbeddedLicenseFileType.Absent)
196+
{
197+
@ViewHelpers.AlertWarning(
198+
@<text>
199+
The license link will become available once package validation has completed successfully.
200+
</text>
201+
)
202+
}
185203

186204
if (Model.ValidatingTooLong)
187205
{
@@ -618,19 +636,23 @@
618636
}
619637
@if (!Model.Deleted && Model.LicenseUrl != null)
620638
{
621-
<li>
622-
<i class="ms-Icon ms-Icon--Certificate" aria-hidden="true"></i>
623-
<a href="@Model.LicenseUrl" data-track="outbound-license-url" title="Make sure you agree with the license" rel="nofollow">
624-
@if (Model.LicenseNames.AnySafe())
625-
{
626-
@(string.Join(", ", Model.LicenseNames.Select(x => x + " License")))
627-
}
628-
else
629-
{
630-
@:License Info
631-
}
632-
</a>
633-
</li>
639+
if (Model.EmbeddedLicenseType == EmbeddedLicenseFileType.Absent || !Model.Validating)
640+
{
641+
<li>
642+
<i class="ms-Icon ms-Icon--Certificate" aria-hidden="true"></i>
643+
<a href="@Model.LicenseUrl"
644+
data-track="outbound-license-url" title="Make sure you agree with the license" rel="nofollow">
645+
@if (Model.LicenseNames.AnySafe())
646+
{
647+
@(string.Join(", ", Model.LicenseNames.Select(x => x + " License")))
648+
}
649+
else
650+
{
651+
@:License Info
652+
}
653+
</a>
654+
</li>
655+
}
634656
}
635657
<li>
636658
<i class="ms-Icon ms-Icon--Mail" aria-hidden="true"></i>

tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ public void ReturnsCorrectRouteLink(string siteRoot, bool relativeUrl, string ex
7272
}
7373
}
7474

75+
public class TheLicenseMethod
76+
: TestContainer
77+
{
78+
[Theory]
79+
[InlineData("https://nuget.org", "TestPackageId", "1.0.0", "/packages/TestPackageId/1.0.0/License", true)]
80+
[InlineData("https://nuget.org", "TestPackageId", "1.0.0", "https://nuget.org/packages/TestPackageId/1.0.0/License", false)]
81+
[InlineData("https://localhost:66", "AnotherTestPackageId", "3.0.0", "/packages/AnotherTestPackageId/3.0.0/License", true)]
82+
[InlineData("https://localhost:66", "AnotherTestPackageId", "3.0.0", "https://localhost:66/packages/AnotherTestPackageId/3.0.0/License", false)]
83+
public void ReturnsCorrectLicenseLink(string siteRoot, string packageId, string packageVersion, string expectedUrl, bool relativeUrl)
84+
{
85+
// Arrange
86+
var configurationService = GetConfigurationService();
87+
configurationService.Current.SiteRoot = siteRoot;
88+
89+
var urlHelper = TestUtility.MockUrlHelper(siteRoot);
90+
91+
// Act
92+
var result = urlHelper.License(packageId, packageVersion, relativeUrl);
93+
94+
// Assert
95+
Assert.Equal(expectedUrl, result);
96+
}
97+
}
98+
7599
public class ThePackageRegistrationTemplateHelperMethod
76100
: TestContainer
77101
{

0 commit comments

Comments
 (0)