-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptifyClientTests.cs
More file actions
243 lines (201 loc) · 8.96 KB
/
Copy pathCryptifyClientTests.cs
File metadata and controls
243 lines (201 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System.Net;
using E4A.PostGuard.Api;
using E4A.PostGuard.Exceptions;
using E4A.PostGuard.Models;
using E4A.PostGuard.Tests.TestHelpers;
namespace E4A.PostGuard.Tests;
public class CryptifyClientTests
{
private const string BaseUrl = "https://cryptify.postguard.eu";
private const int ChunkSize = 1024 * 1024; // mirrors CryptifyClient.ChunkSize
private static readonly Dictionary<string, string> InitToken =
new() { ["cryptifytoken"] = "token-0" };
private static RecipientBuilder Email(string email) =>
new(email, RecipientBaseType.Email);
private static (CryptifyClient Client, RecordingHttpMessageHandler Handler) NewClient()
{
var handler = new RecordingHttpMessageHandler();
return (new CryptifyClient(new HttpClient(handler), BaseUrl), handler);
}
private static HttpResponseMessage TokenResponse(string token)
{
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(""),
};
response.Headers.TryAddWithoutValidation("cryptifytoken", token);
return response;
}
[Fact]
public async Task SingleChunk_WiresInitChunkFinalize()
{
var (client, handler) = NewClient();
handler
.EnqueueJson("""{"uuid":"abc-123"}""", headers: InitToken)
.Enqueue(TokenResponse("token-1"))
.Enqueue(TokenResponse("token-2"));
var data = new byte[100];
var uuid = await client.UploadAsync(data, [Email("alice@example.com")], notify: null);
Assert.Equal("abc-123", uuid);
Assert.Equal(3, handler.Requests.Count);
Assert.Equal(HttpMethod.Post, handler.Requests[0].Method);
Assert.Equal($"{BaseUrl}/fileupload/init", handler.Requests[0].Uri.ToString());
Assert.Equal(HttpMethod.Put, handler.Requests[1].Method);
Assert.Equal($"{BaseUrl}/fileupload/abc-123", handler.Requests[1].Uri.ToString());
Assert.Equal(HttpMethod.Post, handler.Requests[2].Method);
Assert.Equal($"{BaseUrl}/fileupload/finalize/abc-123", handler.Requests[2].Uri.ToString());
}
[Fact]
public async Task ChunkContentRange_IsFormattedPerChunk()
{
var (client, handler) = NewClient();
handler
.EnqueueJson("""{"uuid":"u"}""", headers: InitToken)
.Enqueue(TokenResponse("t1"))
.Enqueue(TokenResponse("t2"))
.Enqueue(TokenResponse("t3"));
// 1.5 chunks → two PUTs.
var total = ChunkSize + 100;
var uuid = await client.UploadAsync(new byte[total], [Email("a@b.com")], notify: null);
Assert.Equal("u", uuid);
var puts = handler.Requests.Where(r => r.Method == HttpMethod.Put).ToList();
Assert.Equal(2, puts.Count);
// NOTE: the current SDK emits an exclusive range end (`end = offset + len`).
// encryption4all/postguard-dotnet#34 changes this to an inclusive end; that
// PR must update these two assertions when it lands.
Assert.Equal($"bytes 0-{ChunkSize}/*", puts[0].ContentRange);
Assert.Equal($"bytes {ChunkSize}-{total}/*", puts[1].ContentRange);
}
[Fact]
public async Task Finalize_SendsTotalSizeContentRange()
{
var (client, handler) = NewClient();
handler
.EnqueueJson("""{"uuid":"u"}""", headers: InitToken)
.Enqueue(TokenResponse("t1"))
.Enqueue(TokenResponse("t2"));
var total = 4242;
await client.UploadAsync(new byte[total], [Email("a@b.com")], notify: null);
var finalize = handler.Requests.Single(r => r.Uri.ToString().Contains("/finalize/"));
Assert.Equal($"bytes */{total}", finalize.ContentRange);
}
[Fact]
public async Task TokenRotates_AcrossChunksAndFinalize()
{
var (client, handler) = NewClient();
handler
.EnqueueJson("""{"uuid":"u"}""", headers: InitToken) // init -> token-0
.Enqueue(TokenResponse("token-1")) // chunk 1 -> token-1
.Enqueue(TokenResponse("token-2")) // chunk 2 -> token-2
.Enqueue(TokenResponse("token-3")); // finalize ok
await client.UploadAsync(new byte[ChunkSize + 1], [Email("a@b.com")], notify: null);
// init carries no token; each subsequent request carries the token from
// the previous response.
Assert.Null(handler.Requests[0].CryptifyToken);
Assert.Equal("token-0", handler.Requests[1].CryptifyToken); // first chunk uses init token
Assert.Equal("token-1", handler.Requests[2].CryptifyToken); // second chunk uses chunk-1 token
Assert.Equal("token-2", handler.Requests[3].CryptifyToken); // finalize uses last chunk token
}
[Fact]
public async Task Init_JoinsRecipientEmailsWithComma()
{
var (client, handler) = NewClient();
handler
.EnqueueJson("""{"uuid":"u"}""", headers: InitToken)
.Enqueue(TokenResponse("t1"))
.Enqueue(TokenResponse("t2"));
await client.UploadAsync(
new byte[10],
[Email("alice@example.com"), Email("bob@example.com")],
notify: null);
Assert.Contains("alice@example.com,bob@example.com", handler.Requests[0].BodyText);
}
[Fact]
public async Task Notify_DefaultsToSilentUpload()
{
var (client, handler) = NewClient();
handler
.EnqueueJson("""{"uuid":"u"}""", headers: InitToken)
.Enqueue(TokenResponse("t1"))
.Enqueue(TokenResponse("t2"));
await client.UploadAsync(new byte[10], [Email("a@b.com")], notify: null);
var body = handler.Requests[0].BodyText;
Assert.Contains("\"confirm\":false", body);
Assert.Contains("\"notifyRecipients\":false", body);
Assert.Contains("\"mailLang\":\"EN\"", body);
}
[Fact]
public async Task Notify_PropagatesOptionsToInitBody()
{
var (client, handler) = NewClient();
handler
.EnqueueJson("""{"uuid":"u"}""", headers: InitToken)
.Enqueue(TokenResponse("t1"))
.Enqueue(TokenResponse("t2"));
var notify = new NotifyOptions
{
Recipients = true,
Sender = true,
Message = "hello",
Language = "NL",
};
await client.UploadAsync(new byte[10], [Email("a@b.com")], notify);
var body = handler.Requests[0].BodyText;
Assert.Contains("\"confirm\":true", body);
Assert.Contains("\"notifyRecipients\":true", body);
Assert.Contains("\"mailContent\":\"hello\"", body);
Assert.Contains("\"mailLang\":\"NL\"", body);
}
[Fact]
public async Task NullUuid_ThrowsPostGuardException()
{
var (client, handler) = NewClient();
handler.EnqueueJson("""{"uuid":null}""", headers: InitToken);
var ex = await Assert.ThrowsAsync<PostGuardException>(
() => client.UploadAsync(new byte[10], [Email("a@b.com")], notify: null));
Assert.Contains("uuid", ex.Message);
}
[Fact]
public async Task MissingInitToken_ThrowsPostGuardException()
{
var (client, handler) = NewClient();
handler.EnqueueJson("""{"uuid":"u"}"""); // no cryptifytoken header
var ex = await Assert.ThrowsAsync<PostGuardException>(
() => client.UploadAsync(new byte[10], [Email("a@b.com")], notify: null));
Assert.Contains("cryptifytoken", ex.Message);
}
[Fact]
public async Task MissingChunkToken_ThrowsPostGuardException()
{
var (client, handler) = NewClient();
handler
.EnqueueJson("""{"uuid":"u"}""", headers: InitToken)
.Enqueue(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("") }); // no token
var ex = await Assert.ThrowsAsync<PostGuardException>(
() => client.UploadAsync(new byte[10], [Email("a@b.com")], notify: null));
Assert.Contains("cryptifytoken", ex.Message);
}
[Fact]
public async Task NonSuccessInit_ThrowsNetworkExceptionWithUrl()
{
var (client, handler) = NewClient();
handler.EnqueueJson("nope", HttpStatusCode.InternalServerError);
var ex = await Assert.ThrowsAsync<NetworkException>(
() => client.UploadAsync(new byte[10], [Email("a@b.com")], notify: null));
Assert.Equal(500, ex.StatusCode);
Assert.Contains("/fileupload/init", ex.Url);
}
[Fact]
public async Task Ctor_TrimsTrailingSlashFromUrl()
{
var handler = new RecordingHttpMessageHandler();
handler
.EnqueueJson("""{"uuid":"u"}""", headers: InitToken)
.Enqueue(TokenResponse("t1"))
.Enqueue(TokenResponse("t2"));
var client = new CryptifyClient(new HttpClient(handler), BaseUrl + "/");
await client.UploadAsync(new byte[10], [Email("a@b.com")], notify: null);
// No double slash in the init URL.
Assert.Equal($"{BaseUrl}/fileupload/init", handler.Requests[0].Uri.ToString());
}
}