-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically Set CreatedAt and UpdatedAt (#244)
* Create IHasCreationTimestamp interface. * Automatically set creation timestamps. * Implement IHasCreationTimestamp. * Don't manually set CreatedAt. * Map all Category properties on creation. * Fix up tests. * Don't override Equals or GetHashCode. * Create IHasUpdateTimestamp. * Implement IHasUpdateTimestamp. * Automatically set update timestamp. * Simplify clock initialization. * Don't manually set LB CreatedAt in test. * Remove erroneous readonly modifiers.
- Loading branch information
Showing
19 changed files
with
207 additions
and
149 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,13 @@ public class ConfirmAccountTests : IntegrationTestsBase | |
[SetUp] | ||
public void Init() | ||
{ | ||
_scope = _factory.Services.CreateScope(); | ||
_scope = _factory.WithWebHostBuilder(builder => | ||
{ | ||
builder.ConfigureTestServices(services => | ||
{ | ||
services.AddSingleton<IClock, FakeClock>(_ => _clock); | ||
}); | ||
}).Services.CreateScope(); | ||
|
||
_client = _factory.WithWebHostBuilder(builder => | ||
{ | ||
|
@@ -44,12 +50,12 @@ public void TearDown() | |
[Test] | ||
public async Task ConfirmAccount_BadConfirmationId() | ||
{ | ||
_clock.Reset(Instant.FromUnixTimeSeconds(1)); | ||
Instant now = Instant.FromUnixTimeSeconds(1); | ||
_clock.Reset(now); | ||
ApplicationContext context = _scope.ServiceProvider.GetRequiredService<ApplicationContext>(); | ||
AccountConfirmation confirmation = new() | ||
{ | ||
CreatedAt = Instant.FromUnixTimeSeconds(0), | ||
ExpiresAt = Instant.FromUnixTimeSeconds(0).Plus(Duration.FromHours(1)), | ||
ExpiresAt = now.Plus(Duration.FromHours(1)), | ||
User = new() | ||
{ | ||
Email = "[email protected]", | ||
|
@@ -60,6 +66,7 @@ public async Task ConfirmAccount_BadConfirmationId() | |
|
||
context.AccountConfirmations.Add(confirmation); | ||
await context.SaveChangesAsync(); | ||
confirmation.CreatedAt.Should().Be(now); | ||
HttpResponseMessage res = await _client.PutAsync(Routes.ConfirmAccount(Guid.NewGuid()), null); | ||
res.StatusCode.Should().Be(HttpStatusCode.NotFound); | ||
context.ChangeTracker.Clear(); | ||
|
@@ -78,13 +85,12 @@ public async Task ConfirmAccount_MalformedConfirmationId() | |
[Test] | ||
public async Task ConfirmAccount_BadRole() | ||
{ | ||
_clock.Reset(Instant.FromUnixTimeSeconds(1)); | ||
Instant now = Instant.FromUnixTimeSeconds(1); | ||
_clock.Reset(now); | ||
ApplicationContext context = _scope.ServiceProvider.GetRequiredService<ApplicationContext>(); | ||
|
||
AccountConfirmation confirmation = new() | ||
{ | ||
CreatedAt = Instant.FromUnixTimeSeconds(0), | ||
ExpiresAt = Instant.FromUnixTimeSeconds(0).Plus(Duration.FromHours(1)), | ||
ExpiresAt = now.Plus(Duration.FromHours(1)), | ||
User = new() | ||
{ | ||
Email = "[email protected]", | ||
|
@@ -96,7 +102,7 @@ public async Task ConfirmAccount_BadRole() | |
|
||
context.AccountConfirmations.Add(confirmation); | ||
await context.SaveChangesAsync(); | ||
|
||
confirmation.CreatedAt.Should().Be(now); | ||
HttpResponseMessage res = await _client.PutAsync(Routes.ConfirmAccount(confirmation.Id), null); | ||
res.StatusCode.Should().Be(HttpStatusCode.Conflict); | ||
context.ChangeTracker.Clear(); | ||
|
@@ -107,13 +113,13 @@ public async Task ConfirmAccount_BadRole() | |
[Test] | ||
public async Task ConfirmAccount_Expired() | ||
{ | ||
_clock.Reset(Instant.FromUnixTimeSeconds(1) + Duration.FromHours(1)); | ||
Instant now = Instant.FromUnixTimeSeconds(1); | ||
_clock.Reset(now); | ||
ApplicationContext context = _scope.ServiceProvider.GetRequiredService<ApplicationContext>(); | ||
|
||
AccountConfirmation confirmation = new() | ||
{ | ||
CreatedAt = Instant.FromUnixTimeSeconds(0), | ||
ExpiresAt = Instant.FromUnixTimeSeconds(0).Plus(Duration.FromHours(1)), | ||
ExpiresAt = now.Plus(Duration.FromHours(1)), | ||
User = new() | ||
{ | ||
Email = "[email protected]", | ||
|
@@ -124,6 +130,7 @@ public async Task ConfirmAccount_Expired() | |
|
||
context.AccountConfirmations.Add(confirmation); | ||
await context.SaveChangesAsync(); | ||
_clock.Reset(now + Duration.FromHours(2)); | ||
HttpResponseMessage res = await _client.PutAsync(Routes.ConfirmAccount(confirmation.Id), null); | ||
res.StatusCode.Should().Be(HttpStatusCode.NotFound); | ||
context.ChangeTracker.Clear(); | ||
|
@@ -135,14 +142,14 @@ public async Task ConfirmAccount_Expired() | |
[Test] | ||
public async Task ConfirmAccount_AlreadyUsed() | ||
{ | ||
_clock.Reset(Instant.FromUnixTimeSeconds(1)); | ||
Instant now = Instant.FromUnixTimeSeconds(1); | ||
_clock.Reset(now); | ||
ApplicationContext context = _scope.ServiceProvider.GetRequiredService<ApplicationContext>(); | ||
|
||
AccountConfirmation confirmation = new() | ||
{ | ||
CreatedAt = Instant.FromUnixTimeSeconds(0), | ||
ExpiresAt = Instant.FromUnixTimeSeconds(0).Plus(Duration.FromHours(1)), | ||
UsedAt = Instant.FromUnixTimeSeconds(5), | ||
ExpiresAt = now.Plus(Duration.FromHours(1)), | ||
UsedAt = now.Plus(Duration.FromSeconds(5)), | ||
User = new() | ||
{ | ||
Email = "[email protected]", | ||
|
@@ -153,6 +160,7 @@ public async Task ConfirmAccount_AlreadyUsed() | |
|
||
context.AccountConfirmations.Add(confirmation); | ||
await context.SaveChangesAsync(); | ||
_clock.AdvanceMinutes(1); | ||
HttpResponseMessage res = await _client.PutAsync(Routes.ConfirmAccount(confirmation.Id), null); | ||
res.StatusCode.Should().Be(HttpStatusCode.NotFound); | ||
context.ChangeTracker.Clear(); | ||
|
@@ -163,12 +171,12 @@ public async Task ConfirmAccount_AlreadyUsed() | |
[Test] | ||
public async Task ConfirmAccount_Success() | ||
{ | ||
_clock.Reset(Instant.FromUnixTimeSeconds(1)); | ||
Instant now = Instant.FromUnixTimeSeconds(1); | ||
_clock.Reset(now); | ||
|
||
AccountConfirmation confirmation = new() | ||
{ | ||
CreatedAt = Instant.FromUnixTimeSeconds(0), | ||
ExpiresAt = Instant.FromUnixTimeSeconds(0).Plus(Duration.FromHours(1)), | ||
ExpiresAt = now.Plus(Duration.FromHours(1)), | ||
User = new() | ||
{ | ||
Email = "[email protected]", | ||
|
@@ -180,11 +188,12 @@ public async Task ConfirmAccount_Success() | |
ApplicationContext context = _scope.ServiceProvider.GetRequiredService<ApplicationContext>(); | ||
context.AccountConfirmations.Add(confirmation); | ||
await context.SaveChangesAsync(); | ||
_clock.AdvanceMinutes(5); | ||
HttpResponseMessage res = await _client.PutAsync(Routes.ConfirmAccount(confirmation.Id), null); | ||
res.Should().HaveStatusCode(HttpStatusCode.OK); | ||
context.ChangeTracker.Clear(); | ||
AccountConfirmation? conf = await context.AccountConfirmations.Include(c => c.User).SingleOrDefaultAsync(c => c.Id == confirmation.Id); | ||
conf!.UsedAt.Should().Be(Instant.FromUnixTimeSeconds(1)); | ||
conf!.UsedAt.Should().Be(now.Plus(Duration.FromMinutes(5))); | ||
conf!.User.Role.Should().Be(UserRole.Confirmed); | ||
} | ||
} |
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
Oops, something went wrong.