mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-07 16:19:07 +00:00
fix(vendors): preserve omitted status
This commit is contained in:
parent
1a5587126e
commit
9a35db58b3
3 changed files with 109 additions and 2 deletions
107
Api.SeaHavenIndustries.Tests/VendorControllerTests.cs
Normal file
107
Api.SeaHavenIndustries.Tests/VendorControllerTests.cs
Normal file
|
|
@ -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<IVendorService> 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<IVendorService>();
|
||||
service.Setup(x => x.UpdateVendorAsync(It.IsAny<int>(), It.IsAny<UpdateVendorDTO>(), It.IsAny<string>()))
|
||||
.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<OkObjectResult>();
|
||||
service.Verify(x => x.UpdateVendorAsync(
|
||||
8,
|
||||
It.Is<UpdateVendorDTO>(dto => dto.IsActive == null),
|
||||
"42"), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Update_WithExplicitIsActive_ForwardsValueUnchanged()
|
||||
{
|
||||
var service = new Mock<IVendorService>();
|
||||
service.Setup(x => x.UpdateVendorAsync(It.IsAny<int>(), It.IsAny<UpdateVendorDTO>(), It.IsAny<string>()))
|
||||
.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<UpdateVendorDTO>(dto => dto.IsActive == false),
|
||||
"42"), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_WithoutIsActive_DefaultsToActive()
|
||||
{
|
||||
var service = new Mock<IVendorService>();
|
||||
service.Setup(x => x.CreateVendorAsync(It.IsAny<CreateVendorDTO>(), It.IsAny<string>()))
|
||||
.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<CreateVendorDTO>(dto => dto.IsActive == true),
|
||||
"42"), Times.Once);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue