Skip to content

Commit a262eba

Browse files
committed
Finish Storage tests
1 parent 00e5697 commit a262eba

14 files changed

+329
-40
lines changed

.env.sample

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AWS_ACCESS_KEY=
2+
AWS_SECRET_ACCESS_KEY=
3+
AWS_BUCKET=
4+
AWS_REGION=us-east-1

.github/workflows/dotnet-core.yml

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ on:
99
jobs:
1010
buildAndTest:
1111
runs-on: ubuntu-latest
12+
13+
env:
14+
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
15+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
16+
AWS_BUCKET: ${{ secrets.AWS_BUCKET }}
17+
AWS_REGION: ${{ secrets.AWS_REGION }}
18+
1219
steps:
1320
- uses: actions/checkout@v2
1421

.github/workflows/release.yml

+7-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@ jobs:
1515
with:
1616
dotnet-version: 3.1.301
1717

18-
- name: Install dependencies
19-
run: dotnet restore
20-
21-
- name: Initialize Testing Stack
22-
run: docker-compose up -d
23-
24-
- name: Build
25-
run: dotnet build --configuration Release --no-restore
26-
27-
- name: Test
28-
run: dotnet test --no-restore
18+
- name: Wait for tests to succeed
19+
uses: lewagon/wait-on-check-action@v1.0.0
20+
with:
21+
ref: ${{ github.ref }}
22+
check-name: 'buildAndTest'
23+
repo-token: ${{ secrets.GITHUB_TOKEN }}
24+
wait-interval: 10
2925

3026
# Publish
3127
- name: publish on version change

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ autom4te.cache/
1111
tarballs/
1212
test-results/
1313

14+
.env
15+
1416
# Mac bundle stuff
1517
*.dmg
1618
*.app

Supabase/Storage/Helpers.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ public static async Task<HttpResponseMessage> MakeRequest(HttpMethod method, str
8989

9090
public class BadRequestException : Exception
9191
{
92-
public ErrorResponse Error { get; private set; }
92+
public ErrorResponse ErrorResponse { get; private set; }
9393

9494
public HttpResponseMessage HttpResponse { get; private set; }
9595

9696
public BadRequestException(HttpResponseMessage httpResponse, string content)
9797
{
9898
HttpResponse = httpResponse;
99-
Error = JsonConvert.DeserializeObject<ErrorResponse>(content);
99+
ErrorResponse = JsonConvert.DeserializeObject<ErrorResponse>(content);
100100
}
101101
}
102102

Supabase/Storage/StorageBucketApi.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ public async Task<List<Bucket>> ListBuckets()
4242
/// <returns></returns>
4343
public async Task<Bucket> GetBucket(string id)
4444
{
45-
var result = await Helpers.MakeRequest<Bucket>(HttpMethod.Get, $"{Url}/bucket/{id}", null, Headers);
46-
return result;
45+
try
46+
{
47+
var result = await Helpers.MakeRequest<Bucket>(HttpMethod.Get, $"{Url}/bucket/{id}", null, Headers);
48+
return result;
49+
}
50+
catch (BadRequestException ex)
51+
{
52+
if (ex.ErrorResponse.Error == "Not found") return null;
53+
else throw ex;
54+
}
4755
}
4856

4957
/// <summary>

Supabase/Storage/StorageFileApi.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,18 @@ public async Task<string> Update(byte[] data, string supabasePath, FileOptions o
160160
/// <param name="fromPath">The original file path, including the current file name. For example `folder/image.png`.</param>
161161
/// <param name="toPath">The new file path, including the new file name. For example `folder/image-copy.png`.</param>
162162
/// <returns></returns>
163-
public async Task<string> Move(string fromPath, string toPath)
163+
public async Task<bool> Move(string fromPath, string toPath)
164164
{
165-
var body = new Dictionary<string, string> { { "bucketId", BucketId }, { "sourceKey", fromPath }, { "destinationKey", toPath } };
166-
var response = await Helpers.MakeRequest<GenericResponse>(HttpMethod.Post, $"{Url}/object/move", body, Headers);
167-
168-
return response.Message;
165+
try
166+
{
167+
var body = new Dictionary<string, string> { { "bucketId", BucketId }, { "sourceKey", fromPath }, { "destinationKey", toPath } };
168+
await Helpers.MakeRequest<GenericResponse>(HttpMethod.Post, $"{Url}/object/move", body, Headers);
169+
return true;
170+
}
171+
catch
172+
{
173+
return false;
174+
}
169175
}
170176

171177
/// <summary>

Supabase/Supabase.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<PackOnBuild>true</PackOnBuild>
66
<PackageId>supabase-csharp</PackageId>
77
<PackageVersion>0.1.9</PackageVersion>
@@ -24,9 +24,9 @@
2424
</PropertyGroup>
2525
<ItemGroup>
2626
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
27-
<PackageReference Include="postgrest-csharp" Version="1.0.8" />
28-
<PackageReference Include="realtime-csharp" Version="1.0.6" />
29-
<PackageReference Include="gotrue-csharp" Version="2.1.3" />
27+
<PackageReference Include="postgrest-csharp" Version="2.0.0" />
28+
<PackageReference Include="realtime-csharp" Version="2.0.0" />
29+
<PackageReference Include="gotrue-csharp" Version="2.1.4" />
3030
<PackageReference Include="MimeMapping" Version="1.0.1.37" />
3131
</ItemGroup>
3232
<ItemGroup>
40.2 KB
Loading

SupabaseTests/Storage.cs renamed to SupabaseTests/StorageBucket.cs

+55-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace SupabaseTests
1414
{
1515
[TestClass]
16-
public class Storage
16+
public class StorageBucket
1717
{
1818
private static string SERVICE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIiwiaWF0IjoxNjEzNTMxOTg1LCJleHAiOjE5MjkxMDc5ODV9.th84OKK0Iz8QchDyXZRrojmKSEZ-OuitQm_5DvLiSIc";
1919

@@ -37,24 +37,24 @@ public async Task InitializeTest()
3737
});
3838
}
3939

40-
[TestMethod("Can List Buckets")]
41-
public async Task CanListBuckets()
40+
[TestMethod("Bucket: List")]
41+
public async Task List()
4242
{
4343
var buckets = await storage.ListBuckets();
4444

4545
Assert.IsTrue(buckets.Count > 0);
4646
Assert.IsInstanceOfType(buckets, typeof(List<Bucket>));
4747
}
4848

49-
[TestMethod("Can Get Bucket")]
50-
public async Task CanGetPublicBucket()
49+
[TestMethod("Bucket: Get")]
50+
public async Task Get()
5151
{
5252
var bucket = await storage.GetBucket("public-bucket");
5353
Assert.IsInstanceOfType(bucket, typeof(Bucket));
5454
}
5555

56-
[TestMethod("Can Create Bucket")]
57-
public async Task CanCreateBucket()
56+
[TestMethod("Bucket: Create, Private")]
57+
public async Task CreatePrivate()
5858
{
5959
var id = Guid.NewGuid().ToString();
6060
var insertId = await storage.CreateBucket(id);
@@ -65,8 +65,8 @@ public async Task CanCreateBucket()
6565
Assert.IsFalse(bucket.Public);
6666
}
6767

68-
[TestMethod("Can Create Public Bucket")]
69-
public async Task CanCreatePublicBucket()
68+
[TestMethod("Bucket: Create, Public")]
69+
public async Task CreatePublic()
7070
{
7171
var id = Guid.NewGuid().ToString();
7272
await storage.CreateBucket(id, new BucketUpsertOptions { Public = true }); ;
@@ -76,8 +76,8 @@ public async Task CanCreatePublicBucket()
7676
Assert.IsTrue(bucket.Public);
7777
}
7878

79-
[TestMethod("Can Update Bucket")]
80-
public async Task CanUpdateBucket()
79+
[TestMethod("Bucket: Update")]
80+
public async Task Update()
8181
{
8282
var id = Guid.NewGuid().ToString();
8383
await storage.CreateBucket(id);
@@ -91,10 +91,17 @@ public async Task CanUpdateBucket()
9191
Assert.IsTrue(nowPublicBucket.Public);
9292
}
9393

94-
[TestMethod("Can Empty Bucket")]
95-
public async Task CanEmptyBucket()
94+
[TestMethod("Bucket: Empty")]
95+
public async Task Empty()
9696
{
97-
var id = "bucket-3";
97+
var id = Guid.NewGuid().ToString();
98+
await storage.CreateBucket(id);
99+
100+
for (var i = 0; i < 5; i++)
101+
{
102+
await storage.From(id).Upload(new Byte[] { 0x0, 0x0, 0x0 }, $"test-{i}.bin");
103+
}
104+
98105
var initialList = await storage.From(id).List();
99106

100107
Assert.IsTrue(initialList.Count > 0);
@@ -105,5 +112,39 @@ public async Task CanEmptyBucket()
105112

106113
Assert.IsTrue(listAfterEmpty.Count == 0);
107114
}
115+
116+
[TestMethod("Bucket: Delete, Throws Error if Not Empty")]
117+
public async Task DeleteThrows()
118+
{
119+
var id = Guid.NewGuid().ToString();
120+
await storage.CreateBucket(id);
121+
122+
for (var i = 0; i < 5; i++)
123+
{
124+
await storage.From(id).Upload(new Byte[] { 0x0, 0x0, 0x0 }, $"test-{i}.bin");
125+
}
126+
127+
await Assert.ThrowsExceptionAsync<BadRequestException>(async () =>
128+
{
129+
await storage.DeleteBucket(id);
130+
});
131+
}
132+
133+
[TestMethod("Bucket: Delete")]
134+
public async Task Delete()
135+
{
136+
var id = Guid.NewGuid().ToString();
137+
await storage.CreateBucket(id);
138+
139+
for (var i = 0; i < 5; i++)
140+
{
141+
await storage.From(id).Upload(new Byte[] { 0x0, 0x0, 0x0 }, $"test-{i}.bin");
142+
}
143+
144+
await storage.EmptyBucket(id);
145+
await storage.DeleteBucket(id);
146+
147+
Assert.IsNull(await storage.GetBucket(id));
148+
}
108149
}
109150
}

0 commit comments

Comments
 (0)