about summary refs log tree commit diff
path: root/Tests
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-09-17 14:08:04 +0200
committerRory& <root@rory.gay>2024-09-17 14:08:04 +0200
commit80d0a3cdf6889ee360dad6a2c270fd42249bbf83 (patch)
tree81700b2c4d937d40cca554b136a65d7634fecee0 /Tests
parentArcanelibs changes (diff)
parentFix unit tests, add authenticated media (diff)
downloadLibMatrix-80d0a3cdf6889ee360dad6a2c270fd42249bbf83.tar.xz
Merge branch 'dev/authenticated-media'
Diffstat (limited to '')
-rw-r--r--Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs16
-rw-r--r--Tests/LibMatrix.Tests/Tests/AuthMediaTests.cs56
-rw-r--r--Tests/LibMatrix.Tests/Tests/AuthTests.cs27
-rw-r--r--Tests/LibMatrix.Tests/Tests/RemoteHomeserverTests.cs14
-rw-r--r--Tests/LibMatrix.Tests/Tests/TestCleanup.cs69
5 files changed, 88 insertions, 94 deletions
diff --git a/Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs b/Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs
index 401223c..2819f80 100644
--- a/Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs
+++ b/Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs
@@ -78,19 +78,7 @@ public class HomeserverAbstraction(HomeserverProviderService _hsProvider, Config
         
         var username = _config.TestUsername;
         var password = _config.TestPassword;
-        
-        LoginResponse reg;
-        try {
-            reg = await rhs.LoginAsync(username, password);
-        }
-        catch (MatrixException e) {
-            if (e.ErrorCode == "M_FORBIDDEN") {
-                await rhs.RegisterAsync(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Unit tests!");
-                reg = await rhs.RegisterAsync(username, password, "Unit tests!");
-            }
-            else throw new Exception("Failed to log in", e);
-        }
-        
-        return (username, password, reg.AccessToken);
+        var reg = await rhs.RegisterAsync(username, password, "Unit tests!");
+        return ("", "", "");
     }
 }
\ No newline at end of file
diff --git a/Tests/LibMatrix.Tests/Tests/AuthMediaTests.cs b/Tests/LibMatrix.Tests/Tests/AuthMediaTests.cs
new file mode 100644
index 0000000..712e45a
--- /dev/null
+++ b/Tests/LibMatrix.Tests/Tests/AuthMediaTests.cs
@@ -0,0 +1,56 @@
+using ArcaneLibs.Extensions;
+using ArcaneLibs.Extensions.Streams;
+using LibMatrix.Homeservers;
+using LibMatrix.Services;
+using LibMatrix.Tests.Abstractions;
+using LibMatrix.Tests.Fixtures;
+using Xunit.Abstractions;
+using Xunit.Microsoft.DependencyInjection.Abstracts;
+
+namespace LibMatrix.Tests.Tests;
+
+public class AuthMediaTests : TestBed<TestFixture> {
+    private readonly TestFixture _fixture;
+    private readonly HomeserverResolverService _resolver;
+    private readonly Config _config;
+    private readonly HomeserverProviderService _provider;
+    private readonly HomeserverAbstraction _hsAbstraction;
+
+    public AuthMediaTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : base(testOutputHelper, fixture) {
+        _fixture = fixture;
+        _resolver = _fixture.GetService<HomeserverResolverService>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverResolverService)}");
+        _config = _fixture.GetService<Config>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
+        _provider = _fixture.GetService<HomeserverProviderService>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverProviderService)}");
+        _hsAbstraction = _fixture.GetService<HomeserverAbstraction>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverAbstraction)}");
+    }
+
+    [Fact]
+    public async Task UploadFileAsync() {
+        var hs = await _hsAbstraction.GetConfiguredHomeserver();
+
+        var mxcUri = await hs.UploadFile("test", "LibMatrix test file".AsBytes());
+        Assert.NotNull(mxcUri);
+    }
+    
+    [Fact]
+    public async Task DownloadFileAsync() {
+        var hs = await _hsAbstraction.GetConfiguredHomeserver();
+
+        var mxcUri = await hs.UploadFile("test", "LibMatrix test file".AsBytes());
+        Assert.NotNull(mxcUri);
+        
+        var file = await hs.GetMediaStreamAsync(mxcUri);
+        Assert.NotNull(file);
+        
+        var data = file!.ReadToEnd().AsString();
+        Assert.Equal("LibMatrix test file", data);
+    }
+    
+    [SkippableFact(typeof(LibMatrixException))] // This test will fail if the homeserver does not support URL previews
+    public async Task GetUrlPreviewAsync() {
+        var hs = await _hsAbstraction.GetConfiguredHomeserver();
+        var preview = await hs.GetUrlPreviewAsync("https://matrix.org");
+        
+        Assert.NotNull(preview);
+    }
+}
\ No newline at end of file
diff --git a/Tests/LibMatrix.Tests/Tests/AuthTests.cs b/Tests/LibMatrix.Tests/Tests/AuthTests.cs
index 633c842..69e6231 100644
--- a/Tests/LibMatrix.Tests/Tests/AuthTests.cs
+++ b/Tests/LibMatrix.Tests/Tests/AuthTests.cs
@@ -20,17 +20,36 @@ public class AuthTests : TestBed<TestFixture> {
     
     [Fact]
     public async Task LoginWithPassword() {
-        var credentials = await _hsAbstraction.GetKnownCredentials();
+        Assert.False(string.IsNullOrWhiteSpace(_config.TestHomeserver), $"{nameof(_config.TestHomeserver)} must be set in appsettings!");
+        Assert.False(string.IsNullOrWhiteSpace(_config.TestUsername), $"{nameof(_config.TestUsername)} must be set in appsettings!");
+        Assert.False(string.IsNullOrWhiteSpace(_config.TestPassword), $"{nameof(_config.TestPassword)} must be set in appsettings!");
+
+        // var server = await _resolver.ResolveHomeserverFromWellKnown(_config.TestHomeserver!);
+        var rhs = await _provider.GetRemoteHomeserver(_config.TestHomeserver);
+        var username = Guid.NewGuid().ToString();
+        var password = Guid.NewGuid().ToString();
         
-        var login = await _provider.Login(_config.TestHomeserver!, credentials.username, credentials.password);
+        var reg = await rhs.RegisterAsync(username, password, "Unit tests!");
+        
+        var login = await _provider.Login(_config.TestHomeserver!, username, password);
         Assert.NotNull(login);
         Assert.NotNull(login.AccessToken);
     }
 
     [Fact]
     public async Task LoginWithToken() {
-        var credentials = await _hsAbstraction.GetKnownCredentials();
-        var hs = await _provider.GetAuthenticatedWithToken(_config.TestHomeserver!, credentials.token);
+        Assert.False(string.IsNullOrWhiteSpace(_config.TestHomeserver), $"{nameof(_config.TestHomeserver)} must be set in appsettings!");
+        Assert.False(string.IsNullOrWhiteSpace(_config.TestUsername), $"{nameof(_config.TestUsername)} must be set in appsettings!");
+        Assert.False(string.IsNullOrWhiteSpace(_config.TestPassword), $"{nameof(_config.TestPassword)} must be set in appsettings!");
+
+        // var server = await _resolver.ResolveHomeserverFromWellKnown(_config.TestHomeserver!);
+        var rhs = await _provider.GetRemoteHomeserver(_config.TestHomeserver);
+        var username = Guid.NewGuid().ToString();
+        var password = Guid.NewGuid().ToString();
+        
+        var reg = await rhs.RegisterAsync(username, password, "Unit tests!");
+        
+        var hs = await _provider.GetAuthenticatedWithToken(_config.TestHomeserver!, reg.AccessToken);
         Assert.NotNull(hs);
         Assert.NotNull(hs.WhoAmI);
         hs.WhoAmI.VerifyRequiredFields();
diff --git a/Tests/LibMatrix.Tests/Tests/RemoteHomeserverTests.cs b/Tests/LibMatrix.Tests/Tests/RemoteHomeserverTests.cs
index 03f3c24..20f975e 100644
--- a/Tests/LibMatrix.Tests/Tests/RemoteHomeserverTests.cs
+++ b/Tests/LibMatrix.Tests/Tests/RemoteHomeserverTests.cs
@@ -18,13 +18,13 @@ public class RemoteHomeserverTests : TestBed<TestFixture> {
         _provider = _fixture.GetService<HomeserverProviderService>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverProviderService)}");
     }
 
-    [Fact]
-    public async Task ResolveMedia() {
-        var hs = await _provider.GetRemoteHomeserver("matrix.org");
-        var media = hs.ResolveMediaUri("mxc://matrix.org/eqwrRZRoPpNbcMeUwyXAuVRo");
-        
-        Assert.Equal("https://matrix-client.matrix.org/_matrix/media/v3/download/matrix.org/eqwrRZRoPpNbcMeUwyXAuVRo", media);
-    }
+    // [Fact]
+    // public async Task ResolveMedia() {
+    //     var hs = await _provider.GetRemoteHomeserver("matrix.org");
+    //     var media = hs.ResolveMediaUri("mxc://matrix.org/eqwrRZRoPpNbcMeUwyXAuVRo");
+    //     
+    //     Assert.Equal("https://matrix-client.matrix.org/_matrix/media/v3/download/matrix.org/eqwrRZRoPpNbcMeUwyXAuVRo", media);
+    // }
 
     [Fact]
     public async Task ResolveRoomAliasAsync() {
diff --git a/Tests/LibMatrix.Tests/Tests/TestCleanup.cs b/Tests/LibMatrix.Tests/Tests/TestCleanup.cs
deleted file mode 100644
index 1c5747c..0000000
--- a/Tests/LibMatrix.Tests/Tests/TestCleanup.cs
+++ /dev/null
@@ -1,69 +0,0 @@
-// using System.Diagnostics;
-// using LibMatrix.Helpers;
-// using LibMatrix.Services;
-// using LibMatrix.Tests.Abstractions;
-// using LibMatrix.Tests.Fixtures;
-// using Microsoft.Extensions.Logging;
-// using Xunit.Abstractions;
-// using Xunit.Microsoft.DependencyInjection.Abstracts;
-//
-// namespace LibMatrix.Tests.Tests;
-//
-// public class TestCleanup : TestBed<TestFixture> {
-//     private readonly HomeserverAbstraction _hsAbstraction;
-//     private readonly ILogger<TestCleanup> _logger;
-//
-//     public TestCleanup(ITestOutputHelper testOutputHelper, TestFixture fixture) : base(testOutputHelper, fixture) {
-//         // _fixture = fixture;
-//         _logger = _fixture.GetService<ILogger<TestCleanup>>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(ILogger<TestCleanup>)}");
-//         _hsAbstraction = _fixture.GetService<HomeserverAbstraction>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverAbstraction)}");
-//     }
-//
-//     [SkippableFact(typeof(MatrixException))]
-//     public async Task Cleanup() {
-//         // Assert.False(string.IsNullOrWhiteSpace(_config.TestHomeserver), $"{nameof(_config.TestHomeserver)} must be set in appsettings!");
-//         // Assert.False(string.IsNullOrWhiteSpace(_config.TestUsername), $"{nameof(_config.TestUsername)} must be set in appsettings!");
-//         // Assert.False(string.IsNullOrWhiteSpace(_config.TestPassword), $"{nameof(_config.TestPassword)} must be set in appsettings!");
-//
-//         var hs = await _hsAbstraction.GetConfiguredHomeserver();
-//         Assert.NotNull(hs);
-//
-//         var syncHelper = new SyncHelper(hs, _logger) {
-//             Timeout = 3000
-//         };
-//         _testOutputHelper.WriteLine("Starting sync loop");
-//         var cancellationTokenSource = new CancellationTokenSource();
-//         var sw = Stopwatch.StartNew();
-//         syncHelper.SyncReceivedHandlers.Add(async response => {
-//             // if (sw.ElapsedMilliseconds >= 3000) {
-//                 // _testOutputHelper.WriteLine("Cancelling sync loop");
-//
-//                 var tasks = (await hs.GetJoinedRooms()).Select(async room => {
-//                     _logger.LogInformation("Leaving room: {}", room.RoomId);
-//                     await room.LeaveAsync();
-//                     await room.ForgetAsync();
-//                     return room;
-//                 }).ToList();
-//                 await Task.WhenAll(tasks);
-//
-//                 // cancellationTokenSource.Cancel();
-//             // }
-//
-//             sw.Restart();
-//             if (response.Rooms?.Leave is { Count: > 0 }) {
-//                 // foreach (var room in response.Rooms.Leave) {
-//                 // await hs.GetRoom(room.Key).ForgetAsync();
-//                 // }
-//                 var tasks2 = response.Rooms.Leave.Select(async room => {
-//                     await hs.GetRoom(room.Key).ForgetAsync();
-//                     return room;
-//                 }).ToList();
-//                 await Task.WhenAll(tasks2);
-//             }
-//         });
-//         await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationTokenSource.Token);
-//
-//         Assert.NotNull(hs);
-//         await hs.Logout();
-//     }
-// }
\ No newline at end of file