Commit 3b551fba authored by Ján Kušnír's avatar Ján Kušnír
Browse files

added another test for SettingsController and edited FileManager slightly for the test

parent 24460c4a
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -7,6 +7,11 @@ public class FileManager : IFileManager
{
    private string _storagePath;

    public string StoragePath
    {
        get { return _storagePath; }
    }

    private readonly string _sourceUrl;
    
    private readonly HttpClient _httpClient;
+27 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ public class StockWebApiTests
{
    private WebApplicationFactory<Program> _factory;
    private FakeClock _clock;
    private FileManager _fileManager;

    private void SetupFiles()
    {
@@ -63,7 +64,7 @@ public class StockWebApiTests

        // It really does not matter
        var downloadUrl = "https://ark-funds.com/";
        var fileManager = new FileManager($"filesystem", options.CreateHttpClient(), downloadUrl, _clock);
        _fileManager = new FileManager($"filesystem", options.CreateHttpClient(), downloadUrl, _clock);

        _factory = new WebApplicationFactory<Program>()
            .WithWebHostBuilder(builder =>
@@ -76,7 +77,7 @@ public class StockWebApiTests
                    descriptor = services.Single(s => s.ServiceType == typeof(IClock));
                    Assert.True(services.Remove(descriptor));

                    services.AddSingleton<IFileManager>(fileManager);
                    services.AddSingleton<IFileManager>(_fileManager);
                    services.AddSingleton<IClock>(_clock);
                });
            });
@@ -258,4 +259,28 @@ public class StockWebApiTests
        Directory.Delete("filesystem2", true);
    }

    [Test]
    public async Task Test_WhenChangingDirectory_Then_StoragePathChanges()
    {
        Directory.CreateDirectory("filesystem2");
        var client = _factory.CreateClient();
        var savedStoragePath = _fileManager.StoragePath;
        StockStorageLocation stockStorageLocation = new StockStorageLocation();
        stockStorageLocation.Location = "filesystem2";

        var jsonContent = JsonContent.Create(stockStorageLocation);
        var response = await client.PostAsync("/StockSettings", jsonContent);

        Assert.AreNotEqual(savedStoragePath, _fileManager.StoragePath);

        // set back to "filesystem"
        stockStorageLocation.Location = "filesystem";
        jsonContent = JsonContent.Create(stockStorageLocation);
        response = await client.PostAsync("/StockSettings", jsonContent);

        Assert.AreEqual(savedStoragePath, _fileManager.StoragePath);

        Directory.Delete("filesystem2", true);
    }

}