|
| 1 | +using System.Net; |
| 2 | +using FluentAssertions; |
| 3 | +using FluentAssertions.Extensions; |
| 4 | +using Humanizer; |
| 5 | +using JsonApiDotNetCore.Queries.Expressions; |
| 6 | +using JsonApiDotNetCore.QueryStrings; |
| 7 | +using JsonApiDotNetCore.Resources; |
| 8 | +using JsonApiDotNetCore.Serialization.Objects; |
| 9 | +using Microsoft.AspNetCore.Authentication; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using TestBuildingBlocks; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace JsonApiDotNetCoreTests.IntegrationTests.QueryStrings.FilterValueConversion; |
| 15 | + |
| 16 | +public sealed class RelativeTimeTests : IClassFixture<IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext>> |
| 17 | +{ |
| 18 | + private readonly IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> _testContext; |
| 19 | + private readonly QueryStringFakers _fakers = new(); |
| 20 | + |
| 21 | + public RelativeTimeTests(IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> testContext) |
| 22 | + { |
| 23 | + _testContext = testContext; |
| 24 | + |
| 25 | + testContext.UseController<CalendarsController>(); |
| 26 | + testContext.UseController<RemindersController>(); |
| 27 | + |
| 28 | + testContext.ConfigureServicesBeforeStartup(services => |
| 29 | + { |
| 30 | + services.AddSingleton<ISystemClock, FrozenSystemClock>(); |
| 31 | + services.AddSingleton<IFilterValueConverter, RelativeTimeFilterValueConverter>(); |
| 32 | + services.AddScoped(typeof(IResourceDefinition<,>), typeof(FilterRewritingResourceDefinition<,>)); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + [Theory] |
| 37 | + [InlineData("-0:10:00", ComparisonOperator.GreaterThan, "0")] // more than 10 minutes ago |
| 38 | + [InlineData("-0:10:00", ComparisonOperator.GreaterOrEqual, "0,1")] // at least 10 minutes ago |
| 39 | + [InlineData("-0:10:00", ComparisonOperator.Equals, "1")] // exactly 10 minutes ago |
| 40 | + [InlineData("-0:10:00", ComparisonOperator.LessThan, "2,3")] // less than 10 minutes ago |
| 41 | + [InlineData("-0:10:00", ComparisonOperator.LessOrEqual, "1,2,3")] // at most 10 minutes ago |
| 42 | + [InlineData("+0:10:00", ComparisonOperator.GreaterThan, "6")] // more than 10 minutes in the future |
| 43 | + [InlineData("+0:10:00", ComparisonOperator.GreaterOrEqual, "5,6")] // at least 10 minutes in the future |
| 44 | + [InlineData("+0:10:00", ComparisonOperator.Equals, "5")] // in exactly 10 minutes |
| 45 | + [InlineData("+0:10:00", ComparisonOperator.LessThan, "3,4")] // less than 10 minutes in the future |
| 46 | + [InlineData("+0:10:00", ComparisonOperator.LessOrEqual, "3,4,5")] // at most 10 minutes in the future |
| 47 | + public async Task Can_filter_comparison_on_relative_time(string filterValue, ComparisonOperator comparisonOperator, string matchingRowsExpected) |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var clock = _testContext.Factory.Services.GetRequiredService<ISystemClock>(); |
| 51 | + |
| 52 | + List<Reminder> reminders = _fakers.Reminder.Generate(7); |
| 53 | + reminders[0].RemindsAt = clock.UtcNow.Add(TimeSpan.FromMinutes(-15)).DateTime.AsUtc(); |
| 54 | + reminders[1].RemindsAt = clock.UtcNow.Add(TimeSpan.FromMinutes(-10)).DateTime.AsUtc(); |
| 55 | + reminders[2].RemindsAt = clock.UtcNow.Add(TimeSpan.FromMinutes(-5)).DateTime.AsUtc(); |
| 56 | + reminders[3].RemindsAt = clock.UtcNow.Add(TimeSpan.FromMinutes(0)).DateTime.AsUtc(); |
| 57 | + reminders[4].RemindsAt = clock.UtcNow.Add(TimeSpan.FromMinutes(5)).DateTime.AsUtc(); |
| 58 | + reminders[5].RemindsAt = clock.UtcNow.Add(TimeSpan.FromMinutes(10)).DateTime.AsUtc(); |
| 59 | + reminders[6].RemindsAt = clock.UtcNow.Add(TimeSpan.FromMinutes(15)).DateTime.AsUtc(); |
| 60 | + |
| 61 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 62 | + { |
| 63 | + await dbContext.ClearTableAsync<Reminder>(); |
| 64 | + dbContext.Reminders.AddRange(reminders); |
| 65 | + await dbContext.SaveChangesAsync(); |
| 66 | + }); |
| 67 | + |
| 68 | + string route = $"/reminders?filter={comparisonOperator.ToString().Camelize()}(remindsAt,'{filterValue.Replace("+", "%2B")}')"; |
| 69 | + |
| 70 | + // Act |
| 71 | + (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
| 72 | + |
| 73 | + // Assert |
| 74 | + httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); |
| 75 | + |
| 76 | + int[] matchingRowIndices = matchingRowsExpected.Split(',').Select(int.Parse).ToArray(); |
| 77 | + responseDocument.Data.ManyValue.ShouldHaveCount(matchingRowIndices.Length); |
| 78 | + |
| 79 | + foreach (int rowIndex in matchingRowIndices) |
| 80 | + { |
| 81 | + responseDocument.Data.ManyValue.Should().ContainSingle(resource => resource.Id == reminders[rowIndex].StringId); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task Cannot_filter_comparison_on_invalid_relative_time() |
| 87 | + { |
| 88 | + // Arrange |
| 89 | + const string route = "/reminders?filter=equals(remindsAt,'-*')"; |
| 90 | + |
| 91 | + // Act |
| 92 | + (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
| 93 | + |
| 94 | + // Assert |
| 95 | + httpResponse.ShouldHaveStatusCode(HttpStatusCode.BadRequest); |
| 96 | + |
| 97 | + responseDocument.Errors.ShouldHaveCount(1); |
| 98 | + |
| 99 | + ErrorObject error = responseDocument.Errors[0]; |
| 100 | + error.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 101 | + error.Title.Should().Be("The specified filter is invalid."); |
| 102 | + error.Detail.Should().Be("Failed to convert '*' of type 'String' to type 'TimeSpan'."); |
| 103 | + error.Source.ShouldNotBeNull(); |
| 104 | + error.Source.Parameter.Should().Be("filter"); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public async Task Cannot_filter_any_on_relative_time() |
| 109 | + { |
| 110 | + // Arrange |
| 111 | + const string route = "/reminders?filter=any(remindsAt,'-0:10:00')"; |
| 112 | + |
| 113 | + // Act |
| 114 | + (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
| 115 | + |
| 116 | + // Assert |
| 117 | + httpResponse.ShouldHaveStatusCode(HttpStatusCode.BadRequest); |
| 118 | + |
| 119 | + responseDocument.Errors.ShouldHaveCount(1); |
| 120 | + |
| 121 | + ErrorObject error = responseDocument.Errors[0]; |
| 122 | + error.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 123 | + error.Title.Should().Be("The specified filter is invalid."); |
| 124 | + error.Detail.Should().Be("A relative time can only be used in a comparison function."); |
| 125 | + error.Source.ShouldNotBeNull(); |
| 126 | + error.Source.Parameter.Should().Be("filter"); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public async Task Cannot_filter_text_match_on_relative_time() |
| 131 | + { |
| 132 | + // Arrange |
| 133 | + const string route = "/reminders?filter=startsWith(remindsAt,'-0:10:00')"; |
| 134 | + |
| 135 | + // Act |
| 136 | + (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
| 137 | + |
| 138 | + // Assert |
| 139 | + httpResponse.ShouldHaveStatusCode(HttpStatusCode.BadRequest); |
| 140 | + |
| 141 | + responseDocument.Errors.ShouldHaveCount(1); |
| 142 | + |
| 143 | + ErrorObject error = responseDocument.Errors[0]; |
| 144 | + error.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 145 | + error.Title.Should().Be("The specified filter is invalid."); |
| 146 | + error.Detail.Should().Be("A relative time can only be used in a comparison function."); |
| 147 | + error.Source.ShouldNotBeNull(); |
| 148 | + error.Source.Parameter.Should().Be("filter"); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public async Task Can_filter_comparison_on_relative_time_in_nested_expression() |
| 153 | + { |
| 154 | + // Arrange |
| 155 | + var clock = _testContext.Factory.Services.GetRequiredService<ISystemClock>(); |
| 156 | + |
| 157 | + Calendar calendar = _fakers.Calendar.Generate(); |
| 158 | + calendar.Appointments = _fakers.Appointment.Generate(2).ToHashSet(); |
| 159 | + |
| 160 | + calendar.Appointments.ElementAt(0).Reminders = _fakers.Reminder.Generate(1); |
| 161 | + calendar.Appointments.ElementAt(0).Reminders[0].RemindsAt = clock.UtcNow.DateTime.AsUtc(); |
| 162 | + |
| 163 | + calendar.Appointments.ElementAt(1).Reminders = _fakers.Reminder.Generate(1); |
| 164 | + calendar.Appointments.ElementAt(1).Reminders[0].RemindsAt = clock.UtcNow.Add(TimeSpan.FromMinutes(30)).DateTime.AsUtc(); |
| 165 | + |
| 166 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 167 | + { |
| 168 | + dbContext.Calendars.Add(calendar); |
| 169 | + await dbContext.SaveChangesAsync(); |
| 170 | + }); |
| 171 | + |
| 172 | + string route = $"/calendars/{calendar.StringId}/appointments?filter=has(reminders,equals(remindsAt,'%2B0:30:00'))"; |
| 173 | + |
| 174 | + // Act |
| 175 | + (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
| 176 | + |
| 177 | + // Assert |
| 178 | + httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); |
| 179 | + |
| 180 | + responseDocument.Data.ManyValue.ShouldHaveCount(1); |
| 181 | + |
| 182 | + responseDocument.Data.ManyValue[0].Id.Should().Be(calendar.Appointments.ElementAt(1).StringId); |
| 183 | + } |
| 184 | +} |
0 commit comments