-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SemVer2] Filter SemVer v2.0.0 package versions by default on v1 and v2 OData endpoints #3694
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a086df
#3686 Filter SemVer v2.0.0 packages from v1 endpoints + unit tests
xavierdecoster b143666
#3686 Filter SemVer v2.0.0 packages from v2 endpoints + unit tests
xavierdecoster a850e96
#3686 Filter SemVer v2.0.0 packages from v2 curated endpoints + unit …
xavierdecoster 788921d
#3686 Filter SemVer v2.0.0 packages from v2 auto-complete endpoints
xavierdecoster 3508122
Sorting System.* using statements first
xavierdecoster ad99c7c
#3686 Filter SemVer v2.0.0 packages from v2 GetUpdates(Count) endpoints
xavierdecoster 6be4a72
Reducing code duplication in tests
xavierdecoster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
tests/NuGetGallery.Facts/Controllers/ODataFeedControllerFactsBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
// 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.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using System.Web.Http; | ||
using System.Web.Http.OData; | ||
using System.Web.Http.OData.Builder; | ||
using System.Web.Http.OData.Query; | ||
using Moq; | ||
using NuGetGallery.Configuration; | ||
using NuGetGallery.Framework; | ||
using NuGetGallery.OData; | ||
using NuGetGallery.WebApi; | ||
|
||
namespace NuGetGallery.Controllers | ||
{ | ||
public abstract class ODataFeedControllerFactsBase<TController> | ||
where TController : NuGetODataController | ||
{ | ||
private const string _siteRoot = "https://nuget.localtest.me"; | ||
protected const string TestPackageId = "Some.Awesome.Package"; | ||
|
||
protected readonly IReadOnlyCollection<Package> NonSemVer2Packages; | ||
protected readonly IReadOnlyCollection<Package> SemVer2Packages; | ||
protected readonly IEntityRepository<Package> PackagesRepository; | ||
protected readonly IQueryable<Package> AllPackages; | ||
|
||
protected ODataFeedControllerFactsBase() | ||
{ | ||
// Arrange | ||
AllPackages = CreatePackagesQueryable(); | ||
NonSemVer2Packages = AllPackages.Where(p => p.SemVerLevelKey == SemVerLevelKey.Unknown).ToList(); | ||
SemVer2Packages = AllPackages.Where(p => p.SemVerLevelKey == SemVerLevelKey.SemVer2).ToList(); | ||
|
||
var packagesRepositoryMock = new Mock<IEntityRepository<Package>>(MockBehavior.Strict); | ||
packagesRepositoryMock.Setup(m => m.GetAll()).Returns(AllPackages).Verifiable(); | ||
PackagesRepository = packagesRepositoryMock.Object; | ||
} | ||
|
||
protected abstract TController CreateController( | ||
IEntityRepository<Package> packagesRepository, | ||
IGalleryConfigurationService configurationService, | ||
ISearchService searchService); | ||
|
||
protected TController CreateTestableODataFeedController(HttpRequestMessage request) | ||
{ | ||
var searchService = new Mock<ISearchService>().Object; | ||
var configurationService = new TestGalleryConfigurationService(); | ||
configurationService.Current.SiteRoot = _siteRoot; | ||
|
||
var controller = CreateController(PackagesRepository, configurationService, searchService); | ||
|
||
var httpRequest = new HttpRequest(string.Empty, request.RequestUri.AbsoluteUri, request.RequestUri.Query); | ||
var httpResponse = new HttpResponse(new StringWriter()); | ||
var httpContext = new HttpContext(httpRequest, httpResponse); | ||
|
||
request.Properties.Add("MS_HttpContext", httpContext); | ||
controller.Request = request; | ||
|
||
HttpContext.Current = httpContext; | ||
|
||
controller.ControllerContext.Controller = controller; | ||
controller.ControllerContext.Configuration = new HttpConfiguration(); | ||
|
||
return controller; | ||
} | ||
|
||
protected async Task<IReadOnlyCollection<TFeedPackage>> GetCollection<TFeedPackage>( | ||
Func<TController, ODataQueryOptions<TFeedPackage>, IHttpActionResult> controllerAction, | ||
string requestPath) | ||
where TFeedPackage : class | ||
{ | ||
var queryResult = InvokeODataFeedControllerAction(controllerAction, requestPath); | ||
|
||
return await GetValueFromQueryResult(queryResult); | ||
} | ||
|
||
protected async Task<IReadOnlyCollection<TFeedPackage>> GetCollection<TFeedPackage>( | ||
Func<TController, ODataQueryOptions<TFeedPackage>, Task<IHttpActionResult>> asyncControllerAction, | ||
string requestPath) | ||
where TFeedPackage : class | ||
{ | ||
var queryResult = await InvokeODataFeedControllerActionAsync(asyncControllerAction, requestPath); | ||
|
||
return await GetValueFromQueryResult(queryResult); | ||
} | ||
|
||
protected async Task<int> GetInt<TFeedPackage>( | ||
Func<TController, ODataQueryOptions<TFeedPackage>, IHttpActionResult> controllerAction, | ||
string requestPath) | ||
where TFeedPackage : class | ||
{ | ||
var queryResult = InvokeODataFeedControllerAction(controllerAction, requestPath); | ||
|
||
return int.Parse(await GetValueFromQueryResult(queryResult)); | ||
} | ||
|
||
protected async Task<int> GetInt<TFeedPackage>( | ||
Func<TController, ODataQueryOptions<TFeedPackage>, Task<IHttpActionResult>> asyncControllerAction, | ||
string requestPath) | ||
where TFeedPackage : class | ||
{ | ||
var queryResult = await InvokeODataFeedControllerActionAsync(asyncControllerAction, requestPath); | ||
|
||
return int.Parse(await GetValueFromQueryResult(queryResult)); | ||
} | ||
|
||
private static IQueryable<Package> CreatePackagesQueryable() | ||
{ | ||
var packageRegistration = new PackageRegistration { Id = TestPackageId }; | ||
|
||
var list = new List<Package> | ||
{ | ||
new Package | ||
{ | ||
PackageRegistration = packageRegistration, | ||
Version = "1.0.0.0", | ||
NormalizedVersion = "1.0.0.0", | ||
SemVerLevelKey = SemVerLevelKey.Unknown | ||
}, | ||
new Package | ||
{ | ||
PackageRegistration = packageRegistration, | ||
Version = "1.0.0.0-alpha", | ||
NormalizedVersion = "1.0.0.0-alpha", | ||
SemVerLevelKey = SemVerLevelKey.Unknown | ||
}, | ||
new Package | ||
{ | ||
PackageRegistration = packageRegistration, | ||
Version = "2.0.0", | ||
NormalizedVersion = "2.0.0", | ||
SemVerLevelKey = SemVerLevelKey.Unknown | ||
}, | ||
new Package | ||
{ | ||
PackageRegistration = packageRegistration, | ||
Version = "2.0.0-alpha", | ||
NormalizedVersion = "2.0.0-alpha", | ||
SemVerLevelKey = SemVerLevelKey.SemVer2 | ||
}, | ||
new Package | ||
{ | ||
PackageRegistration = packageRegistration, | ||
Version = "2.0.0-alpha.1", | ||
NormalizedVersion = "2.0.0-alpha.1", | ||
SemVerLevelKey = SemVerLevelKey.SemVer2 | ||
}, | ||
new Package | ||
{ | ||
PackageRegistration = packageRegistration, | ||
Version = "2.0.0+metadata", | ||
NormalizedVersion = "2.0.0", | ||
SemVerLevelKey = SemVerLevelKey.SemVer2 | ||
} | ||
}; | ||
|
||
return list.AsQueryable(); | ||
} | ||
|
||
private static ODataQueryContext CreateODataQueryContext<TFeedPackage>() | ||
where TFeedPackage : class | ||
{ | ||
var oDataModelBuilder = new ODataConventionModelBuilder(); | ||
oDataModelBuilder.EntitySet<TFeedPackage>("Packages"); | ||
|
||
return new ODataQueryContext(oDataModelBuilder.GetEdmModel(), typeof(TFeedPackage)); | ||
} | ||
|
||
private static async Task<dynamic> GetValueFromQueryResult<TEntity>(QueryResult<TEntity> queryResult) | ||
{ | ||
var httpResponseMessage = await queryResult.ExecuteAsync(CancellationToken.None); | ||
|
||
if (queryResult.FormatAsCountResult) | ||
{ | ||
var stringContent = (StringContent)httpResponseMessage.Content; | ||
return await stringContent.ReadAsStringAsync(); | ||
} | ||
else if (queryResult.FormatAsSingleResult) | ||
{ | ||
var objectContent = (ObjectContent<TEntity>)httpResponseMessage.Content; | ||
return (TEntity)objectContent.Value; | ||
} | ||
else | ||
{ | ||
var objectContent = (ObjectContent<IQueryable<TEntity>>)httpResponseMessage.Content; | ||
return ((IQueryable<TEntity>)objectContent.Value).ToList(); | ||
} | ||
} | ||
|
||
private async Task<QueryResult<TFeedPackage>> InvokeODataFeedControllerActionAsync<TFeedPackage>( | ||
Func<TController, ODataQueryOptions<TFeedPackage>, Task<IHttpActionResult>> asyncControllerAction, | ||
string requestPath) | ||
where TFeedPackage : class | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Get, $"{_siteRoot}{requestPath}"); | ||
var controller = CreateTestableODataFeedController(request); | ||
|
||
return (QueryResult<TFeedPackage>)await asyncControllerAction(controller, | ||
new ODataQueryOptions<TFeedPackage>(CreateODataQueryContext<TFeedPackage>(), request)); | ||
} | ||
|
||
private QueryResult<TFeedPackage> InvokeODataFeedControllerAction<TFeedPackage>( | ||
Func<TController, ODataQueryOptions<TFeedPackage>, IHttpActionResult> controllerAction, | ||
string requestPath) | ||
where TFeedPackage : class | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Get, $"{_siteRoot}{requestPath}"); | ||
var controller = CreateTestableODataFeedController(request); | ||
|
||
return (QueryResult<TFeedPackage>)controllerAction(controller, | ||
new ODataQueryOptions<TFeedPackage>(CreateODataQueryContext<TFeedPackage>(), request)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish there was an easier way to add conditions here rather than having to change every place where we query the packages repository.
Do you think we could extract these checks out to a function?
You could add this as a method in the base class (
NuGetODataController
) and use it inODataV2FeedController
as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about consolidating this somehow too, however, the logic is not the same for v1 and v2. E.g. v1 also filters out pre-release packages from the feed.