Skip to content
Merged
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 @@ -28,6 +28,7 @@ public class LogHabitCommandHandlerTests
private readonly IUserStreakService _userStreakService = Substitute.For<IUserStreakService>();
private readonly IGamificationService _gamificationService = Substitute.For<IGamificationService>();
private readonly IChallengeProgressService _challengeProgressService = Substitute.For<IChallengeProgressService>();
private readonly IPayGateService _payGate = Substitute.For<IPayGateService>();
private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();
private readonly MemoryCache _cache = new MemoryCache(new MemoryCacheOptions());
private readonly MediatR.IMediator _mediator = Substitute.For<MediatR.IMediator>();
Expand All @@ -40,7 +41,13 @@ public class LogHabitCommandHandlerTests
public LogHabitCommandHandlerTests()
{
var repos = new LogHabitRepositories(_habitRepo, _habitLogRepo, _goalRepo, _userRepo);
var services = new LogHabitServices(_userDateService, _userStreakService, _gamificationService, _challengeProgressService, _mediator);
var services = new LogHabitServices(
_userDateService,
_userStreakService,
_gamificationService,
_challengeProgressService,
_mediator,
_payGate);
_handler = new LogHabitCommandHandler(
repos, services, _unitOfWork, _cache, _logger);

Expand Down Expand Up @@ -157,6 +164,34 @@ public async Task Handle_AlreadyLogged_TogglesUnlog()
await _unitOfWork.Received(2).SaveChangesAsync(Arg.Any<CancellationToken>());
}

[Fact]
public async Task Handle_CompletedOneTimeTask_PayGateFailureRejectsWithoutChangingState()
{
var habit = Habit.Create(new HabitCreateParams(
UserId, "Finished task", null, null, DueDate: Today)).Value;
habit.Log(Today).IsSuccess.Should().BeTrue();
var existingLog = habit.Logs.Should().ContainSingle().Which;
_habitRepo.FindOneTrackedAsync(
Arg.Any<Expression<Func<Habit, bool>>>(),
Arg.Any<Func<IQueryable<Habit>, IQueryable<Habit>>?>(),
Arg.Any<CancellationToken>())
.Returns(habit);
_payGate.CanCreateHabits(UserId, 1, Arg.Any<CancellationToken>())
.Returns(Result.PayGateFailure("Habit limit reached"));

var result = await _handler.Handle(
new LogHabitCommand(UserId, habit.Id),
CancellationToken.None);

result.IsFailure.Should().BeTrue();
result.ErrorCode.Should().Be(Result.PayGateErrorCode);
habit.IsCompleted.Should().BeTrue();
habit.Logs.Should().ContainSingle().Which.Should().BeSameAs(existingLog);
existingLog.IsDeleted.Should().BeFalse();
await _payGate.Received(1).CanCreateHabits(UserId, 1, Arg.Any<CancellationToken>());
await _unitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}

[Fact]
public async Task Handle_HabitNotFound_ReturnsFailure()
{
Expand Down
Loading