2022-10-07 13:43:19 +02:00
|
|
|
using System.Text.Json;
|
2022-10-06 23:54:44 +02:00
|
|
|
using Flurl.Http.Testing;
|
2022-10-07 13:43:19 +02:00
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2022-10-06 23:54:44 +02:00
|
|
|
|
|
|
|
namespace Piwigo.Client.Tests;
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
[TestFixture]
|
2022-10-06 23:54:44 +02:00
|
|
|
public class PiwigoClientTests
|
|
|
|
{
|
2022-10-07 13:43:19 +02:00
|
|
|
private const string TestUri = "http://localhost:8080/ws.php?format=json";
|
|
|
|
private const string Username = "admin";
|
|
|
|
private const string Password = "admin";
|
|
|
|
|
2022-10-06 23:54:44 +02:00
|
|
|
private PiwigoClient _piwigoClient = null!;
|
2022-10-07 13:43:19 +02:00
|
|
|
private HttpTest? _httpTest;
|
2022-10-06 23:54:44 +02:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
2022-10-07 13:43:19 +02:00
|
|
|
_piwigoClient = new PiwigoClient(new NullLogger<PiwigoClient>());
|
2022-10-06 23:54:44 +02:00
|
|
|
_httpTest = new HttpTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
public void TearDown()
|
|
|
|
{
|
2022-10-07 13:43:19 +02:00
|
|
|
_httpTest?.Dispose();
|
2022-10-06 23:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task Login_should_set_cookies_and_session()
|
|
|
|
{
|
|
|
|
await LoginAsync();
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
[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();
|
|
|
|
}
|
|
|
|
|
2022-10-06 23:54:44 +02:00
|
|
|
[Test]
|
|
|
|
public async Task Logout_should_set_IsLoggedIn_to_false()
|
|
|
|
{
|
|
|
|
await LoginAsync();
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
_httpTest?.RespondWith("OK");
|
|
|
|
await _piwigoClient.LogoutAsync();
|
2022-10-06 23:54:44 +02:00
|
|
|
_piwigoClient.IsLoggedIn.Should().BeFalse();
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
|
2022-10-06 23:54:44 +02:00
|
|
|
private async Task LoginAsync()
|
|
|
|
{
|
2022-10-07 13:43:19 +02:00
|
|
|
_httpTest?.RespondWith("{}", 200, cookies: new { pwg_id = "pwg_id" });
|
|
|
|
await _piwigoClient.LoginAsync(new Uri(TestUri), Username, Password);
|
2022-10-06 23:54:44 +02:00
|
|
|
_piwigoClient.IsLoggedIn.Should().BeTrue();
|
|
|
|
}
|
|
|
|
}
|