diff --git a/Api.SeaHavenIndustries.Tests/VendorControllerTests.cs b/Api.SeaHavenIndustries.Tests/VendorControllerTests.cs new file mode 100644 index 0000000..4357926 --- /dev/null +++ b/Api.SeaHavenIndustries.Tests/VendorControllerTests.cs @@ -0,0 +1,107 @@ +using Api.SeaHavenIndustries.Controllers; +using Api.SeaHavenIndustries.DTOs; +using FluentAssertions; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Moq; +using SeaHaven.Services.DTOs; +using SeaHaven.Services.Interfaces; +using System.Security.Claims; +using Xunit; + +namespace Api.SeaHavenIndustries.Tests; + +public class VendorControllerTests +{ + private static VendorController NewController(Mock service) + { + var controller = new VendorController( + service.Object, + db: null!, + zipDistance: null!, + vendorTokens: null!, + config: null!); + + controller.ControllerContext = new ControllerContext + { + HttpContext = new DefaultHttpContext + { + User = new ClaimsPrincipal(new ClaimsIdentity(new[] + { + new Claim(ClaimTypes.NameIdentifier, "42") + }, "Test")) + } + }; + + return controller; + } + + [Fact] + public async Task Update_WithoutIsActive_PassesNullToServiceToPreserveStatus() + { + var service = new Mock(); + service.Setup(x => x.UpdateVendorAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(new VendorDTO()); + + var controller = NewController(service); + + var model = new EditVendor_DTO + { + Id = 8, + CompanyName = "Updated Name" + }; + + var result = await controller.Update(8, model); + + result.Should().BeOfType(); + service.Verify(x => x.UpdateVendorAsync( + 8, + It.Is(dto => dto.IsActive == null), + "42"), Times.Once); + } + + [Fact] + public async Task Update_WithExplicitIsActive_ForwardsValueUnchanged() + { + var service = new Mock(); + service.Setup(x => x.UpdateVendorAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(new VendorDTO()); + + var controller = NewController(service); + + var model = new EditVendor_DTO + { + Id = 8, + CompanyName = "Updated Name", + IsActive = false + }; + + await controller.Update(8, model); + + service.Verify(x => x.UpdateVendorAsync( + 8, + It.Is(dto => dto.IsActive == false), + "42"), Times.Once); + } + + [Fact] + public async Task Create_WithoutIsActive_DefaultsToActive() + { + var service = new Mock(); + service.Setup(x => x.CreateVendorAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new VendorDTO()); + + var controller = NewController(service); + + var model = new Vendor_DTO + { + CompanyName = "New Co" + }; + + await controller.Create(model); + + service.Verify(x => x.CreateVendorAsync( + It.Is(dto => dto.IsActive == true), + "42"), Times.Once); + } +} diff --git a/Api.SeaHavenIndustries/Controllers/VendorController.cs b/Api.SeaHavenIndustries/Controllers/VendorController.cs index a1ae149..143f5ce 100644 --- a/Api.SeaHavenIndustries/Controllers/VendorController.cs +++ b/Api.SeaHavenIndustries/Controllers/VendorController.cs @@ -134,7 +134,7 @@ namespace Api.SeaHavenIndustries.Controllers TradeSpecialties = model.TradeSpecialties, GoogleMapsUrl = model.GoogleMapsUrl, Notes = model.Notes, - IsActive = model.IsActive + IsActive = model.IsActive ?? true }; var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); diff --git a/Api.SeaHavenIndustries/DTOs/Vendor_DTO.cs b/Api.SeaHavenIndustries/DTOs/Vendor_DTO.cs index 0d167da..d2a4717 100644 --- a/Api.SeaHavenIndustries/DTOs/Vendor_DTO.cs +++ b/Api.SeaHavenIndustries/DTOs/Vendor_DTO.cs @@ -14,7 +14,7 @@ namespace Api.SeaHavenIndustries.DTOs public string? TradeSpecialties { get; set; } public string? GoogleMapsUrl { get; set; } public string? Notes { get; set; } - public bool IsActive { get; set; } = true; + public bool? IsActive { get; set; } } public class EditVendor_DTO : Vendor_DTO