using Cleanuparr.Domain.Enums; using Cleanuparr.Infrastructure.Events.Interfaces; using Cleanuparr.Infrastructure.Features.DownloadClient.Transmission; using Cleanuparr.Infrastructure.Features.Files; using Cleanuparr.Infrastructure.Features.ItemStriker; using Cleanuparr.Infrastructure.Features.MalwareBlocker; using Cleanuparr.Infrastructure.Http; using Cleanuparr.Infrastructure.Interceptors; using Cleanuparr.Infrastructure.Services.Interfaces; using Cleanuparr.Infrastructure.Tests.TestHelpers; using Cleanuparr.Persistence.Models.Configuration; using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; using Shouldly; using Xunit; namespace Cleanuparr.Infrastructure.Tests.Features.DownloadClient; /// /// Uses the public constructor, since the test constructor skips the FLM.Transmission client. /// public sealed class TransmissionServiceHttpAuthTests { private static (TransmissionService service, FakeHttpMessageHandler handler) CreateService(HttpAuthType httpAuthType) { FakeHttpMessageHandler handler = new(); handler.SetupResponse((_, _) => Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent("""{"result":"success","arguments":{}}""") })); DownloadClientConfig config = new() { Id = Guid.NewGuid(), Name = "transmission", TypeName = DownloadClientTypeName.Transmission, Type = DownloadClientType.Torrent, Host = new Uri("http://localhost:9091"), HttpAuthType = httpAuthType, Username = "user", Password = "pass", }; IDynamicHttpClientProvider httpClientProvider = Substitute.For(); httpClientProvider.CreateClient(Arg.Any()).Returns(new HttpClient(handler)); IDryRunInterceptor dryRunInterceptor = Substitute.For(); dryRunInterceptor .InterceptAsync(Arg.Any>(), Arg.Any()) .ReturnsForAnyArgs(callInfo => callInfo.ArgAt>(0).Invoke()); TransmissionService service = new( NullLogger.Instance, Substitute.For(), Substitute.For(), dryRunInterceptor, Substitute.For(), httpClientProvider, Substitute.For(), Substitute.For(), config, Substitute.For(), Substitute.For(), TimeProvider.System); return (service, handler); } [Fact] public async Task LoginAsync_InBasicMode_SendsBasicHeader() { (TransmissionService service, FakeHttpMessageHandler handler) = CreateService(HttpAuthType.Basic); await service.LoginAsync(); handler.CapturedRequests[0].Headers.Authorization!.Scheme.ShouldBe("Basic"); } [Fact] public async Task LoginAsync_InDigestMode_SendsNoManualHeader() { (TransmissionService service, FakeHttpMessageHandler handler) = CreateService(HttpAuthType.Digest); await service.LoginAsync(); handler.CapturedRequests[0].Headers.Authorization.ShouldBeNull(); } }