using Flurl.Http.Configuration; using Flurl.Http.Testing; using Microsoft.Extensions.Logging.Abstractions; using Newtonsoft.Json; namespace Piwigo.Client.Tests; [TestFixture] public class PiwigoClientTests { private const string TestUri = "http://localhost:8080/ws.php?format=json"; private const string Username = "admin"; private const string Password = "admin"; private PiwigoClient _piwigoClient = null!; private HttpTest? _httpTest; [SetUp] public void SetUp() { _piwigoClient = new PiwigoClient(new NullLogger()); _httpTest = new HttpTest(); } [TearDown] public void TearDown() { _httpTest?.Dispose(); } [Test] public async Task Login_should_set_cookies_and_session() { await LoginAsync(); } [Test] public async Task GetStatus_should_return_config() { await LoginAsync(); var serverResponse = new PiwigoResponse { Status = "OK", Result = new PiwigoStatus { Username = "admin", Version = "12.0.0" } }; SetJsonResult(serverResponse); var status = await _piwigoClient.GetStatusAsync(); status.Should().NotBeNull(); status.Username.Should().Be("admin"); status.Version.Should().NotBeEmpty(); } [Test] public async Task Logout_should_set_IsLoggedIn_to_false() { await LoginAsync(); _httpTest?.RespondWith("OK"); await _piwigoClient.LogoutAsync(); _piwigoClient.IsLoggedIn.Should().BeFalse(); } [Test] public async Task GetAllCategories_should_return_all_existing_categories() { await LoginAsync(); var serverResponse = new PiwigoResponse { Result = new PiwigoCategoryList { Categories = new List { new() { Id = 1, Name = "UnitTestMain" }, new() { Id = 3, Name = "UnitTestSub2", IdUpperCat = 1 }, new() { Id = 2, Name = "UnitTestSub1", IdUpperCat = 1 } } } }; SetJsonResult(serverResponse); var response = await _piwigoClient.GetAllCategoriesAsync(); response.Should().HaveCount(3); response.Should().SatisfyRespectively(c => { c.Id.Should().Be(1); c.Name.Should().Be("UnitTestMain"); }, c => { c.Id.Should().Be(3); c.Name.Should().Be("UnitTestSub2"); c.IdUpperCat.Should().Be(1); }, c => { c.Id.Should().Be(2); c.Name.Should().Be("UnitTestSub1"); c.IdUpperCat.Should().Be(1); }); } private void SetJsonResult(PiwigoResponse serverResponse) { var settings = new JsonSerializerSettings { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; var serializer = new NewtonsoftJsonSerializer(settings); var jsonResponse = serializer.Serialize(serverResponse); _httpTest?.RespondWith(jsonResponse); } private async Task LoginAsync() { _httpTest?.RespondWith("{}", 200, cookies: new { pwg_id = "pwg_id" }); await _piwigoClient.LoginAsync(new Uri(TestUri), Username, Password); _piwigoClient.IsLoggedIn.Should().BeTrue(); } }