From 4f8d405c59ec20a7e7b0b507c98a2db601332e17 Mon Sep 17 00:00:00 2001 From: Firekeeper <0xFirekeeper@gmail.com> Date: Mon, 8 Apr 2024 13:39:38 +0300 Subject: [PATCH] storage tests (#8) * storage tests * deserialization branch --- Thirdweb.Tests/Thirdweb.Storage.Tests.cs | 61 ++++++++++++++++++++ Thirdweb/Thirdweb.Storage/ThirdwebStorage.cs | 7 +-- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/Thirdweb.Tests/Thirdweb.Storage.Tests.cs b/Thirdweb.Tests/Thirdweb.Storage.Tests.cs index 616190e..0a9a3c6 100644 --- a/Thirdweb.Tests/Thirdweb.Storage.Tests.cs +++ b/Thirdweb.Tests/Thirdweb.Storage.Tests.cs @@ -21,6 +21,48 @@ public async Task DownloadTest_Client_BundleId() Assert.NotNull(res); } + [Fact] + public async Task DownloadTest_Deserialization() + { + var client = new ThirdwebClient(secretKey: _secretKey); + var res = await ThirdwebStorage.Download>(client, "https://1.rpc.thirdweb.com/providers"); + Assert.NotNull(res); + Assert.NotEmpty(res); + } + + [Fact] + public async Task DownloadTest_NullUri() + { + var client = new ThirdwebClient(secretKey: _secretKey); + var exception = await Assert.ThrowsAsync(() => ThirdwebStorage.Download(client, null)); + Assert.Equal("uri", exception.ParamName); + } + + [Fact] + public async Task DownloadTest_ThirdwebIPFS() + { + var client = new ThirdwebClient(secretKey: _secretKey); + var res = await ThirdwebStorage.Download(client, "ipfs://QmRHf3sBEAaSkaPdjrnYZS7VH1jVgvNBJNoUXmiUyvUpNM/8"); + Assert.NotNull(res); + } + + [Fact] + public async Task DownloadTest_400() + { + var client = new ThirdwebClient(secretKey: _secretKey); + var exception = await Assert.ThrowsAsync(() => ThirdwebStorage.Download(client, "https://0.rpc.thirdweb.com/")); + Assert.Contains("Failed to download", exception.Message); + Assert.Contains("BadRequest", exception.Message); + } + + [Fact] + public async Task DownloadTest_Timeout() + { + var client = new ThirdwebClient(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(storage: 0)); + var exception = await Assert.ThrowsAsync(() => ThirdwebStorage.Download(client, "https://1.rpc.thirdweb.com/providers", 1)); + Assert.Contains("A task was canceled", exception.Message); + } + [Fact] public async Task UploadTest_SecretKey() { @@ -40,4 +82,23 @@ public async Task UploadTest_Client_BundleId() var res = await ThirdwebStorage.Upload(client, path); Assert.StartsWith($"https://{client.ClientId}.ipfscdn.io/ipfs/", res.PreviewUrl); } + + [Fact] + public async Task UploadTest_NullPath() + { + var client = new ThirdwebClient(secretKey: _secretKey); + var exception = await Assert.ThrowsAsync(() => ThirdwebStorage.Upload(client, null)); + Assert.Equal("path", exception.ParamName); + } + + [Fact] + public async Task UploadTest_401() + { + var client = new ThirdwebClient(clientId: "invalid", bundleId: "hello"); + var path = Path.Combine(Path.GetTempPath(), "testJson.json"); + File.WriteAllText(path, "{\"test\": \"test\"}"); + var exception = await Assert.ThrowsAsync(() => ThirdwebStorage.Upload(client, path)); + Assert.Contains("Failed to upload", exception.Message); + Assert.Contains("Unauthorized", exception.Message); + } } diff --git a/Thirdweb/Thirdweb.Storage/ThirdwebStorage.cs b/Thirdweb/Thirdweb.Storage/ThirdwebStorage.cs index a0e8de5..47f4906 100644 --- a/Thirdweb/Thirdweb.Storage/ThirdwebStorage.cs +++ b/Thirdweb/Thirdweb.Storage/ThirdwebStorage.cs @@ -11,7 +11,7 @@ public static async Task Download(ThirdwebClient client, string uri, int? throw new ArgumentNullException(nameof(uri)); } - uri = uri.ReplaceIPFS(string.IsNullOrEmpty(client.ClientId) ? Constants.FALLBACK_IPFS_GATEWAY : $"https://{client.ClientId}.ipfscdn.io/ipfs/"); + uri = uri.ReplaceIPFS($"https://{client.ClientId}.ipfscdn.io/ipfs/"); using var httpClient = new HttpClient(); @@ -41,11 +41,6 @@ public static async Task Download(ThirdwebClient client, string uri, int? public static async Task Upload(ThirdwebClient client, string path) { - if (string.IsNullOrEmpty(client.ClientId)) - { - throw new UnauthorizedAccessException("You cannot use Upload features without setting a Client ID."); - } - if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path));