piwigodotnet/PiwigoDotnet/Piwigo.Client.Tests/PiwigoClientTests.cs

77 lines
1.9 KiB
C#
Raw Normal View History

using System.Text.Json;
using Flurl.Http.Testing;
using Microsoft.Extensions.Logging.Abstractions;
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<PiwigoClient>());
_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 expectedResponse = new PiwigoResponse<PiwigoStatus>
{
Status = "OK",
Result = new PiwigoStatus
{
Username = "admin",
Version = "12.0.0"
}
};
var jsonResponse = JsonSerializer.Serialize(expectedResponse);
_httpTest?.RespondWith(jsonResponse);
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();
}
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();
}
}