Skip to content

Commit 2011cd4

Browse files
committed
When a package is unlisted, return the package's published date as 1900 (#41)
Fix NuGet/NuGetGallery#4020
1 parent fee4754 commit 2011cd4

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/NuGet.Server.Core/DataServices/PackageExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace NuGet.Server.Core.DataServices
1111
{
1212
public static class PackageExtensions
1313
{
14+
private static readonly DateTime PublishedForUnlisted = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);
15+
1416
public static ODataPackage AsODataPackage(this IServerPackage package, ClientCompatibility compatibility)
1517
{
1618
return new ODataPackage
@@ -31,7 +33,7 @@ public static ODataPackage AsODataPackage(this IServerPackage package, ClientCom
3133
Description = package.Description,
3234
Summary = package.Summary,
3335
ReleaseNotes = package.ReleaseNotes,
34-
Published = package.Created.UtcDateTime,
36+
Published = package.Listed ? package.Created.UtcDateTime : PublishedForUnlisted,
3537
LastUpdated = package.LastUpdated.UtcDateTime,
3638
Dependencies = string.Join("|", package.DependencySets.SelectMany(ConvertDependencySetToStrings)),
3739
PackageHash = package.PackageHash,

test/NuGet.Server.Core.Tests/PackageExtensionsTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Linq;
56
using NuGet.Server.Core.DataServices;
67
using NuGet.Server.Core.Infrastructure;
@@ -10,6 +11,52 @@ namespace NuGet.Server.Core.Tests
1011
{
1112
public class PackageExtensionsTest
1213
{
14+
[Fact]
15+
public void AsODataPackage_Uses1900ForUnlistedPublished()
16+
{
17+
// Arrange
18+
var package = new ServerPackage
19+
{
20+
Version = new SemanticVersion("0.1.0"),
21+
Authors = Enumerable.Empty<string>(),
22+
Owners = Enumerable.Empty<string>(),
23+
24+
Listed = false,
25+
Created = new DateTimeOffset(2017, 11, 29, 21, 21, 32, TimeSpan.FromHours(-8)),
26+
};
27+
28+
// Act
29+
var actual = package.AsODataPackage(ClientCompatibility.Max);
30+
31+
// Assert
32+
Assert.Equal(
33+
new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc),
34+
actual.Published);
35+
}
36+
37+
[Fact]
38+
public void AsODataPackage_UsesCreatedForListedPublished()
39+
{
40+
// Arrange
41+
var package = new ServerPackage
42+
{
43+
Version = new SemanticVersion("0.1.0"),
44+
Authors = Enumerable.Empty<string>(),
45+
Owners = Enumerable.Empty<string>(),
46+
47+
Listed = true,
48+
Created = new DateTimeOffset(2017, 11, 29, 21, 21, 32, TimeSpan.FromHours(-8)),
49+
};
50+
51+
// Act
52+
var actual = package.AsODataPackage(ClientCompatibility.Max);
53+
54+
// Assert
55+
Assert.Equal(
56+
new DateTime(2017, 11, 30, 5, 21, 32, DateTimeKind.Utc),
57+
actual.Published);
58+
}
59+
1360
[Theory]
1461
[InlineData(true, true, false, false, 1, true, true)]
1562
[InlineData(false, false, true, true, 1, false, false)]

0 commit comments

Comments
 (0)