Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public async Task<Result<DateTime>> Handle(ConfirmAccountDeletionCommand request
var scheduledDate = user.HasProAccess && user.PlanExpiresAt.HasValue && user.PlanExpiresAt.Value > nowAtUtc
? user.PlanExpiresAt.Value.AddDays(7)
: nowAtUtc.AddDays(7);
var maximumScheduledDate = nowAtUtc.AddDays(AppConstants.MaxDeletionGraceDays);
scheduledDate = scheduledDate < maximumScheduledDate ? scheduledDate : maximumScheduledDate;

user.Deactivate(scheduledDate);
await unitOfWork.SaveChangesAsync(cancellationToken);
Expand Down
1 change: 1 addition & 0 deletions src/Orbit.Application/Common/AppConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static class AppConstants
public const int MaxLanguageLength = 10;
public const int MaxVerificationAttempts = 3;
public const int VerificationAttemptWindowMinutes = 15;
public const int MaxDeletionGraceDays = 30;
public const int MaxChatMessageLength = 4000;
public const int MaxClarificationValueLength = 2048;
public const int MaxClarificationArgsLength = 16384;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,110 @@ public async Task Handle_ValidCode_DeactivatesUserAndReturnsScheduledDate()
_cache.TryGetValue($"delete:{TestEmail}", out _).Should().BeFalse();
}

[Fact]
public async Task Handle_ProPlanBeyondGraceCap_SchedulesDeletionAtThirtyDays()
{
var user = User.Create("Test", TestEmail).Value;
user.SetStripeSubscription("sub_123", DateTime.UtcNow.AddDays(340));
SetupUser(user);
SetupDeletionCode(TestEmail, "123456");
var earliestScheduledDeletionAtUtc = DateTime.UtcNow.AddDays(30);

var result = await _handler.Handle(
new ConfirmAccountDeletionCommand(UserId, "123456"),
CancellationToken.None);

var latestScheduledDeletionAtUtc = DateTime.UtcNow.AddDays(30);
result.Value.Should().BeOnOrAfter(earliestScheduledDeletionAtUtc)
.And.BeOnOrBefore(latestScheduledDeletionAtUtc);
}

[Fact]
public async Task Handle_ProPlanWithinGraceCap_SchedulesDeletionSevenDaysAfterPlanExpiry()
{
var user = User.Create("Test", TestEmail).Value;
var planExpiresAtUtc = DateTime.UtcNow.AddDays(3);
user.SetStripeSubscription("sub_123", planExpiresAtUtc);
SetupUser(user);
SetupDeletionCode(TestEmail, "123456");

var result = await _handler.Handle(
new ConfirmAccountDeletionCommand(UserId, "123456"),
CancellationToken.None);

result.Value.Should().Be(planExpiresAtUtc.AddDays(7));
}

[Fact]
public async Task Handle_ProPlanAtGraceCapBoundary_SchedulesDeletionSevenDaysAfterPlanExpiry()
{
var user = User.Create("Test", TestEmail).Value;
var planExpiresAtUtc = DateTime.UtcNow.AddDays(23);
user.SetStripeSubscription("sub_123", planExpiresAtUtc);
SetupUser(user);
SetupDeletionCode(TestEmail, "123456");

var result = await _handler.Handle(
new ConfirmAccountDeletionCommand(UserId, "123456"),
CancellationToken.None);

result.Value.Should().Be(planExpiresAtUtc.AddDays(7));
}

[Fact]
public async Task Handle_FreeUser_SchedulesDeletionInSevenDays()
{
var user = User.Create("Test", TestEmail).Value;
user.StartTrial(DateTime.UtcNow.AddDays(-1));
SetupUser(user);
SetupDeletionCode(TestEmail, "123456");
var earliestScheduledDeletionAtUtc = DateTime.UtcNow.AddDays(7);

var result = await _handler.Handle(
new ConfirmAccountDeletionCommand(UserId, "123456"),
CancellationToken.None);

var latestScheduledDeletionAtUtc = DateTime.UtcNow.AddDays(7);
result.Value.Should().BeOnOrAfter(earliestScheduledDeletionAtUtc)
.And.BeOnOrBefore(latestScheduledDeletionAtUtc);
}

[Fact]
public async Task Handle_TrialUserWithoutPlanExpiry_SchedulesDeletionInSevenDays()
{
var user = User.Create("Test", TestEmail).Value;
SetupUser(user);
SetupDeletionCode(TestEmail, "123456");
var earliestScheduledDeletionAtUtc = DateTime.UtcNow.AddDays(7);

var result = await _handler.Handle(
new ConfirmAccountDeletionCommand(UserId, "123456"),
CancellationToken.None);

var latestScheduledDeletionAtUtc = DateTime.UtcNow.AddDays(7);
result.Value.Should().BeOnOrAfter(earliestScheduledDeletionAtUtc)
.And.BeOnOrBefore(latestScheduledDeletionAtUtc);
}

[Fact]
public async Task Handle_ExpiredProPlan_SchedulesDeletionInSevenDays()
{
var user = User.Create("Test", TestEmail).Value;
user.SetStripeSubscription("sub_123", DateTime.UtcNow.AddDays(-1));
user.StartTrial(DateTime.UtcNow.AddDays(-1));
SetupUser(user);
SetupDeletionCode(TestEmail, "123456");
var earliestScheduledDeletionAtUtc = DateTime.UtcNow.AddDays(7);

var result = await _handler.Handle(
new ConfirmAccountDeletionCommand(UserId, "123456"),
CancellationToken.None);

var latestScheduledDeletionAtUtc = DateTime.UtcNow.AddDays(7);
result.Value.Should().BeOnOrAfter(earliestScheduledDeletionAtUtc)
.And.BeOnOrBefore(latestScheduledDeletionAtUtc);
}

[Fact]
public async Task Handle_ValidCode_ClearsGoogleOAuthTokens()
{
Expand Down Expand Up @@ -85,6 +189,7 @@ public async Task Handle_InvalidCode_ReturnsFailureAndIncrementsAttempts()
result.IsFailure.Should().BeTrue();
result.Error.Should().Contain("Invalid");
user.IsDeactivated.Should().BeFalse();
user.ScheduledDeletionAt.Should().BeNull();
_cache.TryGetValue($"delete-attempts:{TestEmail}", out int attempts).Should().BeTrue();
attempts.Should().Be(1);
}
Expand Down
Loading