using System; using System.IO; using System.Net; using System.Reflection; using System.Threading; using DotNet.Testcontainers.Builders; using DotNet.Testcontainers.Containers; using FitConnect; using FitConnect.Models; using FitConnect.Services; using FluentAssertions; using NUnit.Framework; namespace IntegrationTests; // Change to https://hub.docker.com/r/mitmproxy/mitmproxy/ [Ignore("Not testable in docker container, take to long to run every time")] [TestFixture] public class ProxyTest { [OneTimeSetUp] public void OneTimeSetup() { var path = $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/proxy"; Console.WriteLine($"Creating directory: {path}"); Directory.CreateDirectory(path); (_id, _secret) = HelperMethods.GetSecrets(); File.WriteAllText("proxy/access.log", ""); _container = new TestcontainersBuilder<TestcontainersContainer>() .WithImage("mitmproxy/mitmproxy") .WithPortBinding("3128", "8081") .WithBindMount(path, @"/var/log/squid") .Build(); _container.StartAsync().Wait(); Thread.Sleep(5000); } [SetUp] public void Setup() { sender = new FitConnect.Sender(FitConnectEnvironment.Testing, _id, _secret) .WithProxy("localhost", 3128, null, null); #pragma warning disable SYSLIB0014 _webClient = new WebClient { #pragma warning restore SYSLIB0014 Proxy = new WebProxy("http://localhost:3128") }; _webClient.DownloadString("https://www.fitko.de/"); } [TearDown] public void TearDown() { _webClient.DownloadString("https://www.google.de/").Should().NotBeNull(); } [OneTimeTearDown] public void OneTimeTearDown() { Thread.Sleep(10000); _container.StopAsync().Wait(); var content = File.ReadAllText("proxy/access.log"); Console.WriteLine(content); content.Should().Contain("auth-testing.fit-connect.fitko.dev:443"); } private WebClient _webClient = null!; private TestcontainersContainer _container = null!; private string _id = null!; private string _secret = null!; private FitConnectClient sender = null!; [Test] [Order(1)] public void ContainerIsRunning() { sender.GetProxy()!.Address.Should().Be("http://localhost:3128"); _container.Should().NotBeNull(); _container.State.Should().Be(TestcontainersState.Running); } [Test] [Order(10)] public void SendSubmissionViaProxy_None_Work() { var sender = new FitConnect.Sender( FitConnectEnvironment.Testing, "", "") .WithProxy("localhost", 3128); sender.OAuthService.Proxy.Should().NotBeNull(); } [Test] [Order(20)] public void RequestOAuthToken() { // Arrange var testUrl = FitConnectEnvironment.Testing .TokenUrl; var oAuthService = new OAuthService(testUrl, "v1", _id, _secret, null) { Proxy = new WebProxy("http://localhost:3128") }; // Act oAuthService.AuthenticateAsync().Wait(); var token = oAuthService.Token; // Assert token.Should().NotBeNull(); } }