Skip to content

Commit

Permalink
storage tests (#8)
Browse files Browse the repository at this point in the history
* storage tests

* deserialization branch
  • Loading branch information
0xFirekeeper authored Apr 8, 2024
1 parent b440095 commit 4f8d405
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
61 changes: 61 additions & 0 deletions Thirdweb.Tests/Thirdweb.Storage.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<string>>(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<ArgumentNullException>(() => ThirdwebStorage.Download<string>(client, null));
Assert.Equal("uri", exception.ParamName);
}

[Fact]
public async Task DownloadTest_ThirdwebIPFS()
{
var client = new ThirdwebClient(secretKey: _secretKey);
var res = await ThirdwebStorage.Download<string>(client, "ipfs://QmRHf3sBEAaSkaPdjrnYZS7VH1jVgvNBJNoUXmiUyvUpNM/8");
Assert.NotNull(res);
}

[Fact]
public async Task DownloadTest_400()
{
var client = new ThirdwebClient(secretKey: _secretKey);
var exception = await Assert.ThrowsAsync<Exception>(() => ThirdwebStorage.Download<string>(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<TaskCanceledException>(() => ThirdwebStorage.Download<string>(client, "https://1.rpc.thirdweb.com/providers", 1));
Assert.Contains("A task was canceled", exception.Message);
}

[Fact]
public async Task UploadTest_SecretKey()
{
Expand All @@ -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<ArgumentNullException>(() => 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<Exception>(() => ThirdwebStorage.Upload(client, path));
Assert.Contains("Failed to upload", exception.Message);
Assert.Contains("Unauthorized", exception.Message);
}
}
7 changes: 1 addition & 6 deletions Thirdweb/Thirdweb.Storage/ThirdwebStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static async Task<T> Download<T>(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();

Expand Down Expand Up @@ -41,11 +41,6 @@ public static async Task<T> Download<T>(ThirdwebClient client, string uri, int?

public static async Task<IPFSUploadResult> 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));
Expand Down

0 comments on commit 4f8d405

Please sign in to comment.