Skip to content
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

Add OData error code for deprecate response messages #8297

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/NuGetGallery/Controllers/ODataV1FeedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
using System;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Extensions;
using System.Web.Http.OData.Query;
using Microsoft.Data.OData;
using NuGet.Services.Entities;
using NuGetGallery.Configuration;
using NuGetGallery.OData;
Expand Down Expand Up @@ -69,7 +72,7 @@ private IHttpActionResult Get(ODataQueryOptions<V1FeedPackage> options, bool isN
{
if (!isNonHijackEnabled)
{
return BadRequest(Strings.ODataDisabled);
return DeprecatedRequest(Strings.ODataDisabled);
}

if (!ODataQueryVerifier.AreODataOptionsAllowed(options, ODataQueryVerifier.V1Packages,
Expand Down Expand Up @@ -235,7 +238,7 @@ private async Task<IHttpActionResult> GetCoreAsync(
// non-hijacked queries are disabled, stop here.
if (!isNonHijackEnabled)
{
return BadRequest(Strings.ODataParametersDisabled);
return DeprecatedRequest(Strings.ODataParametersDisabled);
}

if (return404NotFoundWhenNoResults && !packages.Any())
Expand Down Expand Up @@ -372,7 +375,7 @@ private async Task<IHttpActionResult> SearchAsync(

if (!isNonHijackEnabled)
{
return BadRequest(Strings.ODataParametersDisabled);
return DeprecatedRequest(Strings.ODataParametersDisabled);
}

if (!ODataQueryVerifier.AreODataOptionsAllowed(options, ODataQueryVerifier.V1Search,
Expand Down
6 changes: 3 additions & 3 deletions src/NuGetGallery/Controllers/ODataV2FeedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private async Task<IHttpActionResult> GetAsync(

if (!isNonHijackEnabled)
{
return BadRequest(Strings.ODataParametersDisabled);
return DeprecatedRequest(Strings.ODataParametersDisabled);
}

// Reject only when try to reach database.
Expand Down Expand Up @@ -389,7 +389,7 @@ private async Task<IHttpActionResult> GetCoreAsync(
{
if (!isSimpleLookup)
{
return BadRequest(Strings.ODataParametersDisabled);
return DeprecatedRequest(Strings.ODataParametersDisabled);
}

customQuery = true;
Expand Down Expand Up @@ -557,7 +557,7 @@ private async Task<IHttpActionResult> SearchAsync(

if (!isNonHijackEnabled)
{
return BadRequest(Strings.ODataParametersDisabled);
return DeprecatedRequest(Strings.ODataParametersDisabled);
}

//Reject only when try to reach database.
Expand Down
14 changes: 14 additions & 0 deletions src/NuGetGallery/OData/NuGetODataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

using System;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Extensions;
using System.Web.Http.OData.Query;
using System.Web.Http.Results;
using Microsoft.Data.OData;
using NuGetGallery.Configuration;
using NuGetGallery.OData.QueryFilter;
Expand Down Expand Up @@ -63,6 +66,17 @@ protected virtual string GetSiteRoot()
return _configurationService.GetSiteRoot(UseHttps()).TrimEnd('/') + '/';
}

protected ResponseMessageResult DeprecatedRequest(string message)
{
return ResponseMessage(Request.CreateErrorResponse(
HttpStatusCode.BadRequest,
new ODataError
{
ErrorCode = "NuGet.V2.Deprecated",
Message = message,
}));
}

/// <summary>
/// Generates a QueryResult.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,12 +14,14 @@
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Query;
using System.Web.Http.Results;
using Moq;
using NuGet.Services.Entities;
using NuGetGallery.Configuration;
using NuGetGallery.Framework;
using NuGetGallery.OData;
using NuGetGallery.WebApi;
using Xunit;

namespace NuGetGallery.Controllers
{
Expand Down Expand Up @@ -49,6 +52,15 @@ protected ODataFeedControllerFactsBase()
PackagesRepository = packagesRepositoryMock.Object;
}

protected static async Task VerifyODataDeprecation(IHttpActionResult resultSet, string message)
{
var result = Assert.IsType<ResponseMessageResult>(resultSet);
Assert.Equal(HttpStatusCode.BadRequest, result.Response.StatusCode);
var content = await result.Response.Content.ReadAsStringAsync();
Assert.Contains("NuGet.V2.Deprecated", content);
Assert.Contains(message, content);
}

protected abstract TController CreateController(
IReadOnlyEntityRepository<Package> packagesRepository,
IEntityRepository<Package> readWritePackagesRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using NuGetGallery.OData;
using Moq;
using Xunit;
using System.Net;
using System.Web.Http;

namespace NuGetGallery.Controllers
{
Expand Down Expand Up @@ -42,7 +44,7 @@ public void Get_ReturnsBadRequestWhenOrderByInvalidColumn()
}

[Fact]
public void GetAll_ReturnsBadRequestWhenGetAllIsDisabled()
public async Task GetAll_ReturnsBadRequestWhenGetAllIsDisabled()
{
// Arrange
var featureFlagService = new Mock<IFeatureFlagService>();
Expand All @@ -55,12 +57,12 @@ public void GetAll_ReturnsBadRequestWhenGetAllIsDisabled()
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataDisabled);
featureFlagService.Verify(x => x.IsODataV1GetAllEnabled());
}

[Fact]
public void GetAllCount_ReturnsBadRequestWhenGetAllIsDisabled()
public async Task GetAllCount_ReturnsBadRequestWhenGetAllIsDisabled()
{
// Arrange
var featureFlagService = new Mock<IFeatureFlagService>();
Expand All @@ -73,7 +75,7 @@ public void GetAllCount_ReturnsBadRequestWhenGetAllIsDisabled()
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataDisabled);
featureFlagService.Verify(x => x.IsODataV1GetAllCountEnabled());
}

Expand All @@ -91,7 +93,7 @@ public async Task GetSpecific_ReturnsBadRequestNonHijackedIsDisabledAndQueryCann
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV1GetSpecificNonHijackedEnabled());
}

Expand Down Expand Up @@ -134,7 +136,7 @@ public async Task FindPackagesById_ReturnsBadRequestNonHijackedIsDisabledAndQuer
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV1FindPackagesByIdNonHijackedEnabled());
}

Expand All @@ -152,7 +154,7 @@ public async Task FindPackagesByIdCount_ReturnsBadRequestNonHijackedIsDisabledAn
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV1FindPackagesByIdCountNonHijackedEnabled());
}

Expand Down Expand Up @@ -195,7 +197,7 @@ public async Task Search_ReturnsBadRequestNonHijackedIsDisabledAndQueryCannotBeH
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV1SearchNonHijackedEnabled());
}

Expand All @@ -213,7 +215,7 @@ public async Task SearchCount_ReturnsBadRequestNonHijackedIsDisabledAndQueryCann
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV1SearchCountNonHijackedEnabled());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task GetAll_ReturnsBadRequestNonHijackedIsDisabledAndQueryCannotBeH
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV2GetAllNonHijackedEnabled());
}

Expand All @@ -91,7 +91,7 @@ public async Task GetAllCount_ReturnsBadRequestNonHijackedIsDisabledAndQueryCann
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV2GetAllCountNonHijackedEnabled());
}

Expand All @@ -109,7 +109,7 @@ public async Task GetSpecific_ReturnsBadRequestNonHijackedIsDisabledAndQueryCann
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV2GetSpecificNonHijackedEnabled());
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public async Task FindPackagesById_ReturnsBadRequestNonHijackedIsDisabledAndQuer
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV2FindPackagesByIdNonHijackedEnabled());
}

Expand All @@ -186,7 +186,7 @@ public async Task FindPackagesByIdCount_ReturnsBadRequestNonHijackedIsDisabledAn
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV2FindPackagesByIdCountNonHijackedEnabled());
}

Expand Down Expand Up @@ -249,7 +249,7 @@ public async Task Search_ReturnsBadRequestNonHijackedIsDisabledAndQueryCannotBeH
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV2SearchNonHijackedEnabled());
}

Expand All @@ -267,7 +267,7 @@ public async Task SearchCount_ReturnsBadRequestNonHijackedIsDisabledAndQueryCann
featureFlagService);

// Assert
Assert.IsType<BadRequestErrorMessageResult>(resultSet);
await VerifyODataDeprecation(resultSet, Strings.ODataParametersDisabled);
featureFlagService.Verify(x => x.IsODataV2SearchCountNonHijackedEnabled());
}

Expand Down