Skip to content

Refactor tests #941

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

Merged
merged 48 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
355083c
Refactored tests for HttpReadOnly/NoHttpPost/Patch/Delete controller …
Jan 26, 2021
90274c6
Refactored tests for casing convention
Jan 26, 2021
4bdb3e8
Refactored tests for custom exception handler
Jan 26, 2021
cbe01c2
Refactored tests for nulls/default query string parameters; removed s…
Jan 26, 2021
0c5d5e8
Refactored tests for DisableQueryStringAttribute
Jan 27, 2021
7057c89
Refactored tests for resource injection
Jan 28, 2021
8756f25
Fixed assertions on DateTime/DateTimeOffset in tests
Jan 28, 2021
44653f8
Refactored tests for non-json:api controllers
Jan 28, 2021
22738b6
Refactored tests for ActionResult usage
Jan 28, 2021
e12d27f
Refactored tests for custom routes
Feb 2, 2021
ddcd4ac
Refactored tests for links rendering
Feb 2, 2021
8b443d8
Refactored tests for exception handling in serializer
Feb 2, 2021
64e09db
Refactored tests for serialization
Feb 2, 2021
8bf0989
Refactored tests for resource hooks
Feb 2, 2021
255ed36
General cleanup
Feb 3, 2021
c7000b8
Extracted test building blocks in shared project
Feb 4, 2021
db9778e
Auto-adjust namespaces
Feb 4, 2021
d9a2ac7
Refactored tests for service discovery
Feb 4, 2021
53f8c82
Refactored tests for no EF Core
Feb 4, 2021
86bac20
Renamed IntegrationTestContext to ExampleIntegrationTestContext becau…
Feb 4, 2021
d94235d
Adjusted test names for ModelStateValidationTests
Feb 4, 2021
c8a6012
Adjusted test names for pagination
Feb 4, 2021
cb983b9
Adjusted test names for meta
Feb 4, 2021
5ad5c76
Fixed broken tests; simpler bootstrap setup
Feb 4, 2021
838a158
Adjusted test names for hooks
Feb 4, 2021
d3ff09a
Enable concurrent testruns (makes running all tests 60% faster)
Feb 4, 2021
64c5b4a
Removed unused using
Feb 4, 2021
e2a2bb0
Merge branch 'master' into refactor-tests
Feb 4, 2021
bcb7320
Cleanup tests for hooks
Feb 4, 2021
60ac856
Cleanup example models
Feb 4, 2021
de40fb7
Revert "Enable concurrent testruns (makes running all tests 60% faste…
Feb 4, 2021
bd11b52
fixed failing testrunner
Feb 4, 2021
b0ac024
Enable running integration tests in parallel
Feb 4, 2021
f343d08
test
Feb 4, 2021
38c127a
Disable duplicate builds
Feb 4, 2021
9ee3fc5
Revert "Disable duplicate builds"
Feb 4, 2021
59fccd8
Addressed review feedback
Feb 5, 2021
21b94c5
Moved integration tests for filter/include/page/sort/fields into Quer…
Feb 5, 2021
a044cf0
Move types into separate files
Feb 5, 2021
78bc0c3
Moved unit tests for query string parameters into ExampleTests projec…
Feb 5, 2021
c2da909
Refactored query string integration tests to use private models. Chan…
Feb 5, 2021
e594391
Refactored query string unit tests to use private models.
Feb 5, 2021
f2fd733
Cleanup models in example project
Feb 6, 2021
f97b81f
Addressed review feedback
Feb 8, 2021
2516d2e
Rename
Feb 8, 2021
114dc07
Refactored tests for logging
Feb 8, 2021
37487cf
Refactored tests for meta
Feb 8, 2021
ea404bf
renamed ResourceHook tests and fixed a few wrong assertions
maurei Feb 8, 2021
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Controllers.Annotations;
using JsonApiDotNetCore.QueryStrings;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
{
[DisableQueryString(StandardQueryStringParameters.Sort | StandardQueryStringParameters.Page)]
public sealed class CountriesController : JsonApiController<Country>
{
public CountriesController(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Controllers.Annotations;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
{
[DisableQueryString("skipCache")]
public sealed class TagsController : JsonApiController<Tag>
{
public TagsController(
Expand Down
5 changes: 0 additions & 5 deletions src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.QueryStrings;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -28,9 +26,6 @@ public override void ConfigureServices(IServiceCollection services)
{
ConfigureClock(services);

services.AddScoped<SkipCacheQueryStringParameterReader>();
services.AddScoped<IQueryStringParameterReader>(sp => sp.GetRequiredService<SkipCacheQueryStringParameterReader>());

services.AddDbContext<AppDbContext>(options =>
{
options.EnableSensitiveDataLogging();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Controllers.Annotations;
using JsonApiDotNetCore.QueryStrings;
using JsonApiDotNetCore.Services;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
{
[NoHttpDelete]
[DisableQueryString(StandardQueryStringParameters.Sort | StandardQueryStringParameters.Page)]
public sealed class BlockingHttpDeleteController : JsonApiController<Sofa>
{
public BlockingHttpDeleteController(IJsonApiOptions options, ILoggerFactory loggerFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
{
[HttpReadOnly]
[DisableQueryString("skipCache")]
public sealed class BlockingWritesController : JsonApiController<Bed>
{
public BlockingWritesController(IJsonApiOptions options, ILoggerFactory loggerFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.QueryStrings;
using JsonApiDotNetCore.Serialization.Objects;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
{
public sealed class DisableQueryStringTests
: IClassFixture<IntegrationTestContext<TestableStartup<RestrictionDbContext>, RestrictionDbContext>>
{
private readonly IntegrationTestContext<TestableStartup<RestrictionDbContext>, RestrictionDbContext> _testContext;
private readonly RestrictionFakers _fakers = new RestrictionFakers();

public DisableQueryStringTests(IntegrationTestContext<TestableStartup<RestrictionDbContext>, RestrictionDbContext> testContext)
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddScoped<SkipCacheQueryStringParameterReader>();
services.AddScoped<IQueryStringParameterReader>(sp => sp.GetRequiredService<SkipCacheQueryStringParameterReader>());
});
}

[Fact]
public async Task Cannot_sort_if_query_string_parameter_is_blocked_by_controller()
{
// Arrange
var route = "/sofas?sort=id";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);

// Assert
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadRequest);

responseDocument.Errors.Should().HaveCount(1);
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.BadRequest);
responseDocument.Errors[0].Title.Should().Be("Usage of one or more query string parameters is not allowed at the requested endpoint.");
responseDocument.Errors[0].Detail.Should().Be("The parameter 'sort' cannot be used at this endpoint.");
responseDocument.Errors[0].Source.Parameter.Should().Be("sort");
}

[Fact]
public async Task Cannot_paginate_if_query_string_parameter_is_blocked_by_controller()
{
// Arrange
var route = "/sofas?page[number]=2";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);

// Assert
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadRequest);

responseDocument.Errors.Should().HaveCount(1);
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.BadRequest);
responseDocument.Errors[0].Title.Should().Be("Usage of one or more query string parameters is not allowed at the requested endpoint.");
responseDocument.Errors[0].Detail.Should().Be("The parameter 'page[number]' cannot be used at this endpoint.");
responseDocument.Errors[0].Source.Parameter.Should().Be("page[number]");
}

[Fact]
public async Task Cannot_use_custom_query_string_parameter_if_blocked_by_controller()
{
// Arrange
var route = "/beds?skipCache=true";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);

// Assert
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadRequest);

responseDocument.Errors.Should().HaveCount(1);
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.BadRequest);
responseDocument.Errors[0].Title.Should().Be("Usage of one or more query string parameters is not allowed at the requested endpoint.");
responseDocument.Errors[0].Detail.Should().Be("The parameter 'skipCache' cannot be used at this endpoint.");
responseDocument.Errors[0].Source.Parameter.Should().Be("skipCache");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using JsonApiDotNetCore.QueryStrings;
using Microsoft.Extensions.Primitives;

namespace JsonApiDotNetCoreExample.Services
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
{
public class SkipCacheQueryStringParameterReader : IQueryStringParameterReader
public sealed class SkipCacheQueryStringParameterReader : IQueryStringParameterReader
{
private const string _skipCacheParameterName = "skipCache";

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

SkipCache = skipCache;
Expand Down