Skip to content

Commit

Permalink
Use includeDelisted query parameter (#56)
Browse files Browse the repository at this point in the history
* Use includeDelisted query parameter

* Rename parameter to share the same paradigm

with NuGetGallery.

* Simplify tests
  • Loading branch information
patricksadowski authored and chenriksson committed Feb 26, 2018
1 parent 0f46245 commit 5a354c6
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ Task<IEnumerable<IServerPackage>> SearchAsync(
ClientCompatibility compatibility,
CancellationToken token);

Task<IEnumerable<IServerPackage>> SearchAsync(
string searchTerm,
IEnumerable<string> targetFrameworks,
bool allowPrereleaseVersions,
bool allowUnlistedVersions,
ClientCompatibility compatibility,
CancellationToken token);

Task ClearCacheAsync(CancellationToken token);

Task RemovePackageAsync(string packageId, SemanticVersion version, CancellationToken token);
Expand Down
17 changes: 14 additions & 3 deletions src/NuGet.Server.Core/Infrastructure/ServerPackageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,25 @@ public async Task<IEnumerable<IServerPackage>> SearchAsync(
bool allowPrereleaseVersions,
ClientCompatibility compatibility,
CancellationToken token)
{
return await SearchAsync(searchTerm, targetFrameworks, allowPrereleaseVersions, false, compatibility, token);
}

public async Task<IEnumerable<IServerPackage>> SearchAsync(
string searchTerm,
IEnumerable<string> targetFrameworks,
bool allowPrereleaseVersions,
bool allowUnlistedVersions,
ClientCompatibility compatibility,
CancellationToken token)
{
var cache = await GetPackagesAsync(compatibility, token);

var packages = cache
.Find(searchTerm)
.FilterByPrerelease(allowPrereleaseVersions);

if (EnableDelisting)
if (EnableDelisting && !allowUnlistedVersions)
{
packages = packages.Where(p => p.Listed);
}
Expand Down Expand Up @@ -496,7 +507,7 @@ private async Task<HashSet<ServerPackage>> ReadPackagesFromDiskWithoutLockingAsy
throw;
}
}

/// <summary>
/// Sets the current cache to null so it will be regenerated next time.
/// </summary>
Expand Down Expand Up @@ -605,7 +616,7 @@ private async void FileSystemChangedAsync(object sender, FileSystemEventArgs e)
return;
}

changedDirectory = Path.GetFullPath(changedDirectory);
changedDirectory = Path.GetFullPath(changedDirectory);

// 1) If a .nupkg is dropped in the root, add it as a package
if (string.Equals(changedDirectory, _watchDirectory, StringComparison.OrdinalIgnoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ public static async Task<IEnumerable<IServerPackage>> SearchAsync(
token);
}

public static async Task<IEnumerable<IServerPackage>> SearchAsync(
this IServerPackageRepository repository,
string searchTerm,
bool allowPrereleaseVersions,
bool allowUnlistedVersions,
ClientCompatibility compatibility,
CancellationToken token)
{
return await repository.SearchAsync(
searchTerm,
Enumerable.Empty<string>(),
allowPrereleaseVersions,
allowUnlistedVersions,
compatibility,
token);
}

public static async Task<IServerPackage> FindPackageAsync(
this IServerPackageRepository repository,
string id,
Expand Down
19 changes: 10 additions & 9 deletions src/NuGet.Server.V2/Controllers/NuGetODataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected NuGetODataController(
_serverRepository = repository ?? throw new ArgumentNullException(nameof(repository));
_authenticationService = authenticationService;
}

// GET /Packages
[HttpGet]
public virtual async Task<IHttpActionResult> Get(
Expand Down Expand Up @@ -129,8 +129,8 @@ public virtual IHttpActionResult GetPropertyFromPackages(string propertyName, st
[HttpPost]
public virtual async Task<IHttpActionResult> Search(
ODataQueryOptions<ODataPackage> options,
[FromODataUri] string searchTerm = "",
[FromODataUri] string targetFramework = "",
[FromODataUri] string searchTerm = "",
[FromODataUri] string targetFramework = "",
[FromODataUri] bool includePrerelease = false,
[FromODataUri] bool includeDelisted = false,
[FromUri] string semVerLevel = "",
Expand All @@ -144,6 +144,7 @@ public virtual async Task<IHttpActionResult> Search(
searchTerm,
targetFrameworks,
includePrerelease,
includeDelisted,
clientCompatibility,
token);

Expand Down Expand Up @@ -203,8 +204,8 @@ public virtual async Task<IHttpActionResult> GetUpdates(

var idValues = packageIds.Trim().Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
var versionValues = versions.Trim().Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
var targetFrameworkValues = String.IsNullOrEmpty(targetFrameworks)
? null
var targetFrameworkValues = String.IsNullOrEmpty(targetFrameworks)
? null
: targetFrameworks.Split('|').Select(VersionUtility.ParseFrameworkName).ToList();
var versionConstraintValues = (String.IsNullOrEmpty(versionConstraints)
? new string[idValues.Length]
Expand All @@ -219,7 +220,7 @@ public virtual async Task<IHttpActionResult> GetUpdates(
var packagesToUpdate = new List<IPackageMetadata>();
for (var i = 0; i < idValues.Length; i++)
{
if(SemanticVersion.TryParse(versionValues[i], out var semVersion))
if (SemanticVersion.TryParse(versionValues[i], out var semVersion))
{
packagesToUpdate.Add(new PackageBuilder { Id = idValues[i], Version = semVersion });
}
Expand Down Expand Up @@ -365,7 +366,7 @@ public virtual async Task<HttpResponseMessage> DeletePackage(
}
else
{
return CreateStringResponse(HttpStatusCode.Forbidden, string.Format("Access denied for package '{0}', version '{1}'.", requestedPackage.Id,version));
return CreateStringResponse(HttpStatusCode.Forbidden, string.Format("Access denied for package '{0}', version '{1}'.", requestedPackage.Id, version));
}
}

Expand Down Expand Up @@ -416,7 +417,7 @@ public virtual async Task<HttpResponseMessage> UploadPackage(CancellationToken t
File.Delete(temporaryFile);
}
catch (Exception)
{
{
retValue = CreateStringResponse(HttpStatusCode.InternalServerError, "Could not remove temporary upload file.");
}

Expand All @@ -436,7 +437,7 @@ private string GetApiKeyFromHeader()
{
apiKey = values.FirstOrDefault();
}

return apiKey;
}

Expand Down
100 changes: 96 additions & 4 deletions test/NuGet.Server.Core.Tests/ServerPackageRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public async Task ServerPackageRepositoryAddsPackagesFromDropFolderOnStart(bool
foreach (var packageToAddToDropFolder in packagesToAddToDropFolder)
{
var package = packages.FirstOrDefault(
p => p.Id == packageToAddToDropFolder.Value.Id
p => p.Id == packageToAddToDropFolder.Value.Id
&& p.Version == packageToAddToDropFolder.Value.Version);

// check the package from drop folder has been added
Assert.NotNull(package);
Assert.NotNull(package);

// check the package in the drop folder has been removed
Assert.False(File.Exists(Path.Combine(temporaryDirectory.Path, packageToAddToDropFolder.Key)));
Expand Down Expand Up @@ -356,6 +356,98 @@ public async Task ServerPackageRepositorySearchUnlisted()
}
}

[Fact]
public async Task ServerPackageRepositorySearchUnlistingDisabledAndExclude()
{
await ServerPackageRepositorySearchUnlistedWithOptions(
enableUnlisting: false,
allowUnlistedVersions: false,
searchable: false,
gettable: false);
}

[Fact]
public async Task ServerPackageRepositorySearchUnlistingDisabledAndInclude()
{
await ServerPackageRepositorySearchUnlistedWithOptions(
enableUnlisting: false,
allowUnlistedVersions: true,
searchable: false,
gettable: false);
}

[Fact]
public async Task ServerPackageRepositorySearchUnlistingEnabledAndExclude()
{
await ServerPackageRepositorySearchUnlistedWithOptions(
enableUnlisting: true,
allowUnlistedVersions: false,
searchable: false,
gettable: true);
}

[Fact]
public async Task ServerPackageRepositorySearchUnlistingEnabledAndInclude()
{
await ServerPackageRepositorySearchUnlistedWithOptions(
enableUnlisting: true,
allowUnlistedVersions: true,
searchable: true,
gettable: true);
}

private async Task ServerPackageRepositorySearchUnlistedWithOptions(
bool enableUnlisting, bool allowUnlistedVersions, bool searchable, bool gettable)
{
using (var temporaryDirectory = new TemporaryDirectory())
{
// Arrange
var getSetting = enableUnlisting ? EnableDelisting : (Func<string, object, object>)null;
var serverRepository = await CreateServerPackageRepositoryAsync(temporaryDirectory.Path, repository =>
{
repository.AddPackage(CreatePackage("test1", "1.0"));
}, getSetting);

// Remove the package
await serverRepository.RemovePackageAsync("test1", new SemanticVersion("1.0"), Token);

// Verify that the package is not returned by search
var packages = (await serverRepository.SearchAsync(
"test1",
allowPrereleaseVersions: true,
allowUnlistedVersions: allowUnlistedVersions,
compatibility: ClientCompatibility.Max,
token: Token)).ToList();
if (searchable)
{
Assert.Equal(1, packages.Count);
Assert.Equal("test1", packages[0].Id);
Assert.Equal("1.0", packages[0].Version.ToString());
Assert.False(packages[0].Listed);
}
else
{
Assert.Equal(0, packages.Count);
}

// Act: search with includeDelisted=true
packages = (await serverRepository.GetPackagesAsync(ClientCompatibility.Max, Token)).ToList();

// Assert
if (gettable)
{
Assert.Equal(1, packages.Count);
Assert.Equal("test1", packages[0].Id);
Assert.Equal("1.0", packages[0].Version.ToString());
Assert.False(packages[0].Listed);
}
else
{
Assert.Equal(0, packages.Count);
}
}
}

[Fact]
public async Task ServerPackageRepositoryFindPackageById()
{
Expand Down Expand Up @@ -463,7 +555,7 @@ public async Task ServerPackageRepositoryFindPackage()
new SemanticVersion("1.0.0-alpha"),
Token);
var invalidPreRel = await serverRepository.FindPackageAsync(
"test3",
"test3",
new SemanticVersion("1.0.0"),
Token);
var invalid = await serverRepository.FindPackageAsync("bad", new SemanticVersion("1.0"), Token);
Expand Down Expand Up @@ -586,7 +678,7 @@ public async Task ServerPackageRepositoryIsLatestOnlyPreRel()
repository.AddPackage(CreatePackage("test", "2.1-alpha"));
repository.AddPackage(CreatePackage("test", "2.2-beta+tagged"));
});

// Act
var packages = await serverRepository.GetPackagesAsync(ClientCompatibility.Max, Token);

Expand Down

0 comments on commit 5a354c6

Please sign in to comment.