Skip to content

Commit

Permalink
When a package is unlisted, return the package's published date as 1900
Browse files Browse the repository at this point in the history
This ports commit 2011cd4 from the 3.x release.
Fix NuGet/NuGetGallery#4020
  • Loading branch information
joelverhagen committed Nov 30, 2017
1 parent cd2b318 commit 4877389
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/NuGet.Server/DataServices/PackageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace NuGet.Server.DataServices
{
public static class PackageExtensions
{
private static readonly DateTime PublishedForUnlisted = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static ODataPackage AsODataPackage(this IPackage package, ClientCompatibility compatibility)
{
var serverPackage = package as ServerPackage;
Expand All @@ -19,7 +21,7 @@ public static ODataPackage AsODataPackage(this IPackage package, ClientCompatibi
return AsODataPackage(serverPackage, compatibility);
}

var utcNow = DateTime.UtcNow;
var published = package.Published.HasValue ? package.Published.Value.UtcDateTime : DateTime.UtcNow;

return new ODataPackage
{
Expand All @@ -39,8 +41,8 @@ public static ODataPackage AsODataPackage(this IPackage package, ClientCompatibi
Description = package.Description,
Summary = package.Summary,
ReleaseNotes = package.ReleaseNotes,
Published = package.Published.HasValue ? package.Published.Value.UtcDateTime : utcNow,
LastUpdated = package.Published.HasValue ? package.Published.Value.UtcDateTime : utcNow,
Published = package.Listed ? published : PublishedForUnlisted,
LastUpdated = published,
Dependencies = string.Join("|", package.DependencySets.SelectMany(ConvertDependencySetToStrings)),
PackageHash = package.GetHash(Constants.HashAlgorithm),
PackageHashAlgorithm = Constants.HashAlgorithm,
Expand Down Expand Up @@ -76,7 +78,7 @@ public static ODataPackage AsODataPackage(this ServerPackage package, ClientComp
Description = package.Description,
Summary = package.Summary,
ReleaseNotes = package.ReleaseNotes,
Published = package.Created.UtcDateTime,
Published = package.Listed ? package.Created.UtcDateTime : PublishedForUnlisted,
LastUpdated = package.LastUpdated.UtcDateTime,
Dependencies = string.Join("|", package.DependencySets.SelectMany(ConvertDependencySetToStrings)),
PackageHash = package.PackageHash,
Expand Down
47 changes: 47 additions & 0 deletions test/NuGet.Server.Tests/PackageExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using NuGet.Server.DataServices;
using NuGet.Server.Infrastructure;
Expand All @@ -10,6 +11,52 @@ namespace NuGet.Server.Tests
{
public class PackageExtensionsTest
{
[Fact]
public void AsODataPackage_Uses1900ForUnlistedPublished()
{
// Arrange
var package = new ServerPackage
{
Version = new SemanticVersion("0.1.0"),
Authors = Enumerable.Empty<string>(),
Owners = Enumerable.Empty<string>(),

Listed = false,
Created = new DateTimeOffset(2017, 11, 29, 21, 21, 32, TimeSpan.FromHours(-8)),
};

// Act
var actual = package.AsODataPackage(ClientCompatibility.Max);

// Assert
Assert.Equal(
new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc),
actual.Published);
}

[Fact]
public void AsODataPackage_UsesCreatedForListedPublished()
{
// Arrange
var package = new ServerPackage
{
Version = new SemanticVersion("0.1.0"),
Authors = Enumerable.Empty<string>(),
Owners = Enumerable.Empty<string>(),

Listed = true,
Created = new DateTimeOffset(2017, 11, 29, 21, 21, 32, TimeSpan.FromHours(-8)),
};

// Act
var actual = package.AsODataPackage(ClientCompatibility.Max);

// Assert
Assert.Equal(
new DateTime(2017, 11, 30, 5, 21, 32, DateTimeKind.Utc),
actual.Published);
}

[Theory]
[InlineData(true, true, false, false, 1, true, true)]
[InlineData(false, false, true, true, 1, false, false)]
Expand Down

0 comments on commit 4877389

Please sign in to comment.