|
| 1 | +using LantanaGroup.Link.Shared.Application.Models.Tenant; |
| 2 | +using LantanaGroup.Link.Tenant.Business.Queries; |
| 3 | +using LantanaGroup.Link.Tenant.Data.Entities; |
| 4 | +using LantanaGroup.Link.Tenant.Entities; |
| 5 | +using LantanaGroup.Link.Tenant.Repository.Context; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Task = System.Threading.Tasks.Task; |
| 8 | + |
| 9 | +namespace IntegrationTests.Tenant; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Data Acquisition reads the vendor's signing key off the facility response rather than making a |
| 13 | +/// second call for the vendor, so the facility projection has to carry it. |
| 14 | +/// </summary> |
| 15 | +[Collection("IntegrationTests")] |
| 16 | +[Trait("Category", "IntegrationTests")] |
| 17 | +public class FacilityVendorAuthenticationTests : IDisposable |
| 18 | +{ |
| 19 | + private readonly IServiceScope _scope; |
| 20 | + private readonly IFacilityQueries _facilityQueries; |
| 21 | + private readonly TenantDbContext _dbContext; |
| 22 | + |
| 23 | + public FacilityVendorAuthenticationTests(TenantIntegrationTestFixture fixture) |
| 24 | + { |
| 25 | + _scope = fixture.ServiceProvider.CreateScope(); |
| 26 | + var serviceProvider = _scope.ServiceProvider; |
| 27 | + |
| 28 | + _facilityQueries = serviceProvider.GetRequiredService<IFacilityQueries>(); |
| 29 | + _dbContext = serviceProvider.GetRequiredService<TenantDbContext>(); |
| 30 | + } |
| 31 | + |
| 32 | + public void Dispose() => _scope.Dispose(); |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task GetAsync_CarriesTheVendorsSigningKeySecretId() |
| 36 | + { |
| 37 | + var facilityId = await CreateFacilityWithVendorAsync( |
| 38 | + new VendorAuthenticationSettings { SigningKeySecretId = "epic-signing-key" }); |
| 39 | + |
| 40 | + var facility = await _facilityQueries.GetAsync(facilityId, null, CancellationToken.None); |
| 41 | + |
| 42 | + Assert.Equal("epic-signing-key", facility?.Vendor?.Authentication?.SigningKeySecretId); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public async Task GetAsync_ReturnsNoSigningKey_WhenTheVendorHasNoneConfigured() |
| 47 | + { |
| 48 | + var facilityId = await CreateFacilityWithVendorAsync(authentication: null); |
| 49 | + |
| 50 | + var facility = await _facilityQueries.GetAsync(facilityId, null, CancellationToken.None); |
| 51 | + |
| 52 | + Assert.NotNull(facility?.Vendor); |
| 53 | + Assert.Null(facility!.Vendor!.Authentication?.SigningKeySecretId); |
| 54 | + } |
| 55 | + |
| 56 | + private async Task<string> CreateFacilityWithVendorAsync(VendorAuthenticationSettings? authentication) |
| 57 | + { |
| 58 | + var vendor = new Vendor |
| 59 | + { |
| 60 | + Id = Guid.NewGuid(), |
| 61 | + Name = $"Vendor-{Guid.NewGuid():N}", |
| 62 | + Authentication = authentication |
| 63 | + }; |
| 64 | + var vendorVersion = new VendorVersion |
| 65 | + { |
| 66 | + Id = Guid.NewGuid(), |
| 67 | + VendorId = vendor.Id, |
| 68 | + Version = "default" |
| 69 | + }; |
| 70 | + var facility = new Facility |
| 71 | + { |
| 72 | + Id = Guid.NewGuid(), |
| 73 | + FacilityId = $"facility-{Guid.NewGuid():N}", |
| 74 | + FacilityName = "Vendor Authentication Test Facility", |
| 75 | + TimeZone = "America/Chicago", |
| 76 | + VendorVersionId = vendorVersion.Id, |
| 77 | + ScheduledReports = new ScheduledReportModel |
| 78 | + { |
| 79 | + Daily = [], |
| 80 | + Weekly = [], |
| 81 | + Monthly = [] |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + await _dbContext.Vendors.AddAsync(vendor); |
| 86 | + await _dbContext.VendorVersions.AddAsync(vendorVersion); |
| 87 | + await _dbContext.Facilities.AddAsync(facility); |
| 88 | + await _dbContext.SaveChangesAsync(); |
| 89 | + |
| 90 | + return facility.FacilityId; |
| 91 | + } |
| 92 | +} |
0 commit comments