-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDocumentClientTests.cs
308 lines (246 loc) · 10.3 KB
/
DocumentClientTests.cs
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Mime;
using System.Threading.Tasks;
using NodaTime;
using VMelnalksnis.PaperlessDotNet.Documents;
using VMelnalksnis.PaperlessDotNet.Tags;
namespace VMelnalksnis.PaperlessDotNet.Tests.Integration.Documents;
public sealed class DocumentClientTests(PaperlessFixture paperlessFixture) : PaperlessTests(paperlessFixture)
{
[Test]
[Order(1)]
public async Task GetAll_ShouldReturnExpected()
{
var documents = await Client.Documents.GetAll().ToListAsync();
documents.Should().BeEmpty();
}
[Test]
public async Task GetAll_PageSizeShouldNotChangeResult()
{
var documents = await Client.Documents.GetAll().ToListAsync();
var pageSizeDocuments = await Client.Documents.GetAll(1).ToListAsync();
documents.Should().BeEquivalentTo(pageSizeDocuments);
}
[Test]
public async Task Create()
{
const string documentName = "Lorem Ipsum.txt";
var correspondent = await Client.Correspondents.Create(new("Foo"));
var tags = new List<Tag>();
foreach (var tag in new List<TagCreation> { new("Receipt"), new("Bill") })
{
tags.Add(await Client.Tags.Create(tag));
}
await using var documentStream = typeof(DocumentClientTests).GetResource(documentName);
var documentCreation = new DocumentCreation(documentStream, documentName)
{
Created = Clock.GetCurrentInstant(),
Title = "Lorem Ipsum",
CorrespondentId = correspondent.Id,
ArchiveSerialNumber = 1,
TagIds = tags.Select(tag => tag.Id).ToArray(),
};
var result = await Client.Documents.Create(documentCreation);
if (PaperlessVersion < new Version(2, 0))
{
result.Should().BeOfType<ImportStarted>();
return;
}
var id = result.Should().BeOfType<DocumentCreated>().Subject.Id;
var document = (await Client.Documents.Get(id))!;
var documents = await Client.Documents.GetAll().ToListAsync();
var metadata = await Client.Documents.GetMetadata(id);
using var scope = new AssertionScope();
var currentTime = SystemClock.Instance.GetCurrentInstant();
var content = await typeof(DocumentClientTests).ReadResource(documentName);
documents.Should().ContainSingle(d => d.Id == id).Which.Should().BeEquivalentTo(document);
document.Should().NotBeNull();
document.OriginalFileName.Should().Be(documentName);
document.Created.ToInstant().Should().Be(documentCreation.Created.Value);
document.Added.ToInstant().Should().BeInRange(currentTime - Duration.FromSeconds(10), currentTime);
document.Modified.ToInstant().Should().BeInRange(currentTime - Duration.FromSeconds(10), currentTime);
document.Title.Should().Be(documentCreation.Title);
document.ArchiveSerialNumber.Should().Be(documentCreation.ArchiveSerialNumber);
document.CorrespondentId.Should().Be(documentCreation.CorrespondentId);
document.TagIds.Should().BeEquivalentTo(tags.Select(tag => tag.Id));
#if NET6_0_OR_GREATER
document.Content.ReplaceLineEndings().Should().BeEquivalentTo(content);
#else
document.Content.Replace("\n", Environment.NewLine).Replace("\r\n", Environment.NewLine).Should().Be(content);
#endif
metadata.Should().BeEquivalentTo(new DocumentMetadata(
$"{id:0000000}.txt",
Environment.NewLine is "\r\n" ? "999853181bf31bb3f54be7c0bc20f6af" : "be37b4f97ce9f67970d878978e5db5eb",
Environment.NewLine is "\r\n" ? 2868 : 2859,
MediaTypeNames.Text.Plain,
documentName,
[],
"ca",
false));
var update = new DocumentUpdate { Title = $"{document.Title}1" };
var updatedDocument = await Client.Documents.Update(id, update);
updatedDocument.Title.Should().Be(update.Title);
await Client.Correspondents.Delete(correspondent.Id);
foreach (var tag in tags)
{
await Client.Tags.Delete(tag.Id);
}
await Client.Documents.Delete(id);
await FluentActions
.Awaiting(() => Client.Documents.Get(id))
.Should()
.ThrowExactlyAsync<HttpRequestException>()
.WithMessage("Response status code does not indicate success: 404 (Not Found).");
}
[Test]
public async Task CreateDuplicate()
{
if (PaperlessVersion < new Version(2, 0))
{
Assert.Ignore($"Paperless v{PaperlessVersion} does not directly allow downloading documents.");
}
const string documentName = "Lorem Ipsum 4.txt";
var tasks = Enumerable
.Range(1, 2)
.Select(_ =>
{
var stream = typeof(DocumentClientTests).GetResource(documentName);
var creation = new DocumentCreation(stream, documentName)
{
Created = Clock.GetCurrentInstant(),
Title = "Lorem Ipsum",
};
return Client.Documents.Create(creation);
});
var results = await Task.WhenAll(tasks);
using var scope = new AssertionScope();
var created = results.OfType<DocumentCreated>().Should().ContainSingle().Subject;
results
.OfType<ImportFailed>()
.Should()
.HaveCount(results.Length - 1)
.And.AllSatisfy(failed =>
failed.Result.Should().Be($"{documentName}: Not consuming {documentName}: It is a duplicate of Lorem Ipsum (#{created.Id})."));
await Client.Documents.Delete(created.Id);
}
[Test]
public async Task Download()
{
if (PaperlessVersion < new Version(2, 0))
{
Assert.Ignore($"Paperless v{PaperlessVersion} does not directly allow downloading documents.");
}
const string documentName = "Lorem Ipsum 3.txt";
await using var documentStream = typeof(DocumentClientTests).GetResource(documentName);
var documentCreation = new DocumentCreation(documentStream, documentName);
var createResult = await Client.Documents.Create(documentCreation);
var id = createResult.Should().BeOfType<DocumentCreated>().Subject.Id;
var expectedDocumentContent = await typeof(DocumentClientTests).ReadResource(documentName);
var expectedPartOfFileName = "Lorem Ipsum 3";
// Download
var documentDownload = await Client.Documents.Download(id);
documentDownload.MediaTypeHeaderValue.MediaType.Should().Be("text/plain");
documentDownload.ContentDisposition!.FileName.Should().Contain(expectedPartOfFileName);
var downloadContent = await ReadStreamContentAsString(documentDownload.Content);
downloadContent.Should().BeEquivalentTo(expectedDocumentContent);
// Download Original
var documentOriginalDownload = await Client.Documents.DownloadOriginal(id);
documentOriginalDownload.MediaTypeHeaderValue.MediaType.Should().Be("text/plain");
documentOriginalDownload.ContentDisposition!.FileName.Should().Contain(expectedPartOfFileName);
var downloadOriginalContent = await ReadStreamContentAsString(documentOriginalDownload.Content);
downloadOriginalContent.Should().BeEquivalentTo(expectedDocumentContent);
// Download Preview
var downloadPreview = await Client.Documents.DownloadPreview(id);
downloadPreview.MediaTypeHeaderValue.MediaType.Should().Be("text/plain");
downloadPreview.ContentDisposition!.FileName.Should().Contain(expectedPartOfFileName);
var downloadPreviewContent = await ReadStreamContentAsString(downloadPreview.Content);
downloadPreviewContent.Should().BeEquivalentTo(expectedDocumentContent);
// Download Preview Original
var downloadPreviewOriginal = await Client.Documents.DownloadPreview(id);
downloadPreviewOriginal.MediaTypeHeaderValue.MediaType.Should().Be("text/plain");
downloadPreviewOriginal.ContentDisposition!.FileName.Should().Contain(expectedPartOfFileName);
var downloadPreviewOrignalContent = await ReadStreamContentAsString(downloadPreviewOriginal.Content);
downloadPreviewOrignalContent.Should().BeEquivalentTo(expectedDocumentContent);
// Download thumbnail
var downloadThumbnail = await Client.Documents.DownloadThumbnail(id);
downloadThumbnail.MediaTypeHeaderValue.MediaType.Should().Be("image/webp");
}
[Test]
public async Task CustomFields()
{
if (PaperlessVersion < new Version(2, 0))
{
Assert.Ignore($"Paperless v{PaperlessVersion} does not support custom fields");
}
const string documentName = "Lorem Ipsum 2.txt";
var fieldCreations = new List<CustomFieldCreation>
{
new("field1", CustomFieldType.String),
new("field2", CustomFieldType.Url),
new("field3", CustomFieldType.Date),
new("field4", CustomFieldType.Boolean),
new("field5", CustomFieldType.Integer),
new("field6", CustomFieldType.Float),
new("field7", CustomFieldType.Monetary),
new("field8", CustomFieldType.DocumentLink),
};
if (PaperlessVersion >= new Version(2, 11, 0))
{
fieldCreations.Add(new SelectCustomFieldCreation<SelectOptions>("field9"));
}
foreach (var customFieldCreation in fieldCreations)
{
await Client.Documents.CreateCustomField(customFieldCreation);
}
var customFields = await Client.Documents.GetCustomFields().ToListAsync();
customFields.Should().HaveCount(fieldCreations.Count);
var paginatedCustomFields = await Client.Documents.GetCustomFields(1).ToListAsync();
paginatedCustomFields.Should().BeEquivalentTo(customFields);
await using var documentStream = typeof(DocumentClientTests).GetResource(documentName);
var documentCreation = new DocumentCreation(documentStream, documentName);
var result = await Client.Documents.Create(documentCreation);
SerializerOptions.CustomFields.Clear();
var id = result.Should().BeOfType<DocumentCreated>().Subject.Id;
var document = (await Client.Documents.Get<CustomFields>(id))!;
document.CustomFields.Should().BeNull("cannot create document with custom fields");
var update = new DocumentUpdate<CustomFields>
{
CustomFields = new()
{
Field1 = "foo",
Field2 = new("https://example.com/"),
Field3 = new LocalDate(2024, 01, 19),
Field4 = true,
Field5 = 12,
Field6 = 12.34567f,
Field7 = 12.34f,
Field8 = [id],
},
};
if (PaperlessVersion >= new Version(2, 11, 0))
{
update.CustomFields.Field9 = SelectOptions.Option1;
}
SerializerOptions.CustomFields.Clear();
document = await Client.Documents.Update(id, update);
document.CustomFields.Should().BeEquivalentTo(update.CustomFields);
SerializerOptions.CustomFields.Clear();
var documents = await Client.Documents.GetAll<CustomFields>().ToListAsync();
documents.Should().ContainSingle(d => d.Id == id).Which.Should().BeEquivalentTo(document);
}
private async Task<string> ReadStreamContentAsString(Stream stream)
{
await using (stream)
{
var reader = new StreamReader(stream);
return await reader.ReadToEndAsync();
}
}
}