Skip to content

Commit 0c5d5e8

Browse files
author
Bart Koelman
committed
Refactored tests for DisableQueryStringAttribute
1 parent cbe01c2 commit 0c5d5e8

File tree

8 files changed

+91
-80
lines changed

8 files changed

+91
-80
lines changed

src/Examples/JsonApiDotNetCoreExample/Controllers/CountriesController.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Controllers.Annotations;
4-
using JsonApiDotNetCore.QueryStrings;
53
using JsonApiDotNetCore.Services;
64
using JsonApiDotNetCoreExample.Models;
75
using Microsoft.Extensions.Logging;
86

97
namespace JsonApiDotNetCoreExample.Controllers
108
{
11-
[DisableQueryString(StandardQueryStringParameters.Sort | StandardQueryStringParameters.Page)]
129
public sealed class CountriesController : JsonApiController<Country>
1310
{
1411
public CountriesController(

src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Controllers.Annotations;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65
using Microsoft.Extensions.Logging;
76

87
namespace JsonApiDotNetCoreExample.Controllers
98
{
10-
[DisableQueryString("skipCache")]
119
public sealed class TagsController : JsonApiController<Tag>
1210
{
1311
public TagsController(

src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using JsonApiDotNetCore.Configuration;
3-
using JsonApiDotNetCore.QueryStrings;
43
using JsonApiDotNetCoreExample.Data;
5-
using JsonApiDotNetCoreExample.Services;
64
using Microsoft.AspNetCore.Authentication;
75
using Microsoft.AspNetCore.Builder;
86
using Microsoft.AspNetCore.Hosting;
@@ -28,9 +26,6 @@ public override void ConfigureServices(IServiceCollection services)
2826
{
2927
ConfigureClock(services);
3028

31-
services.AddScoped<SkipCacheQueryStringParameterReader>();
32-
services.AddScoped<IQueryStringParameterReader>(sp => sp.GetRequiredService<SkipCacheQueryStringParameterReader>());
33-
3429
services.AddDbContext<AppDbContext>(options =>
3530
{
3631
options.EnableSensitiveDataLogging();

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DisableQueryAttributeTests.cs

Lines changed: 0 additions & 67 deletions
This file was deleted.

test/JsonApiDotNetCoreExampleTests/IntegrationTests/RestrictedControllers/BlockingHttpDeleteController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
33
using JsonApiDotNetCore.Controllers.Annotations;
4+
using JsonApiDotNetCore.QueryStrings;
45
using JsonApiDotNetCore.Services;
56
using Microsoft.Extensions.Logging;
67

78
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
89
{
910
[NoHttpDelete]
11+
[DisableQueryString(StandardQueryStringParameters.Sort | StandardQueryStringParameters.Page)]
1012
public sealed class BlockingHttpDeleteController : JsonApiController<Sofa>
1113
{
1214
public BlockingHttpDeleteController(IJsonApiOptions options, ILoggerFactory loggerFactory,

test/JsonApiDotNetCoreExampleTests/IntegrationTests/RestrictedControllers/BlockingWritesController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
88
{
99
[HttpReadOnly]
10+
[DisableQueryString("skipCache")]
1011
public sealed class BlockingWritesController : JsonApiController<Bed>
1112
{
1213
public BlockingWritesController(IJsonApiOptions options, ILoggerFactory loggerFactory,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Net;
2+
using System.Threading.Tasks;
3+
using FluentAssertions;
4+
using JsonApiDotNetCore.QueryStrings;
5+
using JsonApiDotNetCore.Serialization.Objects;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Xunit;
8+
9+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
10+
{
11+
public sealed class DisableQueryStringTests
12+
: IClassFixture<IntegrationTestContext<TestableStartup<RestrictionDbContext>, RestrictionDbContext>>
13+
{
14+
private readonly IntegrationTestContext<TestableStartup<RestrictionDbContext>, RestrictionDbContext> _testContext;
15+
private readonly RestrictionFakers _fakers = new RestrictionFakers();
16+
17+
public DisableQueryStringTests(IntegrationTestContext<TestableStartup<RestrictionDbContext>, RestrictionDbContext> testContext)
18+
{
19+
_testContext = testContext;
20+
21+
testContext.ConfigureServicesAfterStartup(services =>
22+
{
23+
services.AddScoped<SkipCacheQueryStringParameterReader>();
24+
services.AddScoped<IQueryStringParameterReader>(sp => sp.GetRequiredService<SkipCacheQueryStringParameterReader>());
25+
});
26+
}
27+
28+
[Fact]
29+
public async Task Cannot_sort_if_query_string_parameter_is_blocked_by_controller()
30+
{
31+
// Arrange
32+
var route = "/sofas?sort=id";
33+
34+
// Act
35+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
36+
37+
// Assert
38+
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadRequest);
39+
40+
responseDocument.Errors.Should().HaveCount(1);
41+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.BadRequest);
42+
responseDocument.Errors[0].Title.Should().Be("Usage of one or more query string parameters is not allowed at the requested endpoint.");
43+
responseDocument.Errors[0].Detail.Should().Be("The parameter 'sort' cannot be used at this endpoint.");
44+
responseDocument.Errors[0].Source.Parameter.Should().Be("sort");
45+
}
46+
47+
[Fact]
48+
public async Task Cannot_paginate_if_query_string_parameter_is_blocked_by_controller()
49+
{
50+
// Arrange
51+
var route = "/sofas?page[number]=2";
52+
53+
// Act
54+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
55+
56+
// Assert
57+
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadRequest);
58+
59+
responseDocument.Errors.Should().HaveCount(1);
60+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.BadRequest);
61+
responseDocument.Errors[0].Title.Should().Be("Usage of one or more query string parameters is not allowed at the requested endpoint.");
62+
responseDocument.Errors[0].Detail.Should().Be("The parameter 'page[number]' cannot be used at this endpoint.");
63+
responseDocument.Errors[0].Source.Parameter.Should().Be("page[number]");
64+
}
65+
66+
[Fact]
67+
public async Task Cannot_use_custom_query_string_parameter_if_blocked_by_controller()
68+
{
69+
// Arrange
70+
var route = "/beds?skipCache=true";
71+
72+
// Act
73+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
74+
75+
// Assert
76+
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadRequest);
77+
78+
responseDocument.Errors.Should().HaveCount(1);
79+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.BadRequest);
80+
responseDocument.Errors[0].Title.Should().Be("Usage of one or more query string parameters is not allowed at the requested endpoint.");
81+
responseDocument.Errors[0].Detail.Should().Be("The parameter 'skipCache' cannot be used at this endpoint.");
82+
responseDocument.Errors[0].Source.Parameter.Should().Be("skipCache");
83+
}
84+
}
85+
}

src/Examples/JsonApiDotNetCoreExample/Services/SkipCacheQueryStringParameterReader.cs renamed to test/JsonApiDotNetCoreExampleTests/IntegrationTests/RestrictedControllers/SkipCacheQueryStringParameterReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using JsonApiDotNetCore.QueryStrings;
55
using Microsoft.Extensions.Primitives;
66

7-
namespace JsonApiDotNetCoreExample.Services
7+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
88
{
9-
public class SkipCacheQueryStringParameterReader : IQueryStringParameterReader
9+
public sealed class SkipCacheQueryStringParameterReader : IQueryStringParameterReader
1010
{
1111
private const string _skipCacheParameterName = "skipCache";
1212

@@ -27,7 +27,7 @@ public void Read(string parameterName, StringValues parameterValue)
2727
if (!bool.TryParse(parameterValue, out bool skipCache))
2828
{
2929
throw new InvalidQueryStringParameterException(parameterName, "Boolean value required.",
30-
$"The value {parameterValue} is not a valid boolean.");
30+
$"The value '{parameterValue}' is not a valid boolean.");
3131
}
3232

3333
SkipCache = skipCache;

0 commit comments

Comments
 (0)