Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Sources/Mailozaurr.Tests/GmailMailboxBrowserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,24 @@ public async System.Threading.Tasks.Task MoveMessageAsync_ToTrash_UsesTrashEndpo
Assert.Contains("/users/me/messages/m1/trash", handler.Requests[0].RequestUri!.ToString());
}

[Fact]
public async System.Threading.Tasks.Task MoveMessageAsync_WithNullSourceFolder_DoesNotRemoveInbox() {
var labelsJson = "{\"labels\":[{\"id\":\"Label_Target\",\"name\":\"Archive\"}]}";
var modifyJson = "{\"id\":\"m1\",\"threadId\":\"t1\"}";
var handler = new RecordingHandler(
new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(labelsJson) },
new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(modifyJson) });
var browser = CreateBrowser(handler);

await browser.MoveMessageAsync("m1", sourceFolder: null, targetFolder: "Archive");

Assert.Equal(2, handler.Requests.Count);
var body = await handler.Requests[1].Content!.ReadAsStringAsync();
Assert.Contains("\"addLabelIds\":[\"Label_Target\"]", body, StringComparison.Ordinal);
Assert.Contains("\"removeLabelIds\":[\"TRASH\"]", body, StringComparison.Ordinal);
Assert.DoesNotContain("INBOX", body, StringComparison.Ordinal);
}

[Fact]
public async System.Threading.Tasks.Task ArchiveMessagesAsync_UsesBatchModify_AndReturnsPerMessageResults() {
var handler = new RecordingHandler(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) });
Expand All @@ -400,6 +418,26 @@ public async System.Threading.Tasks.Task ArchiveMessagesAsync_UsesBatchModify_An
Assert.Contains("\"removeLabelIds\":[\"INBOX\",\"TRASH\"]", body, StringComparison.Ordinal);
}

[Fact]
public async System.Threading.Tasks.Task MoveMessagesAsync_WithNullSourceFolder_DoesNotRemoveInbox() {
var labelsJson = "{\"labels\":[{\"id\":\"Label_Target\",\"name\":\"Archive\"}]}";
var handler = new RecordingHandler(
new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(labelsJson) },
new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) });
var browser = CreateBrowser(handler);

var results = await browser.MoveMessagesAsync(new[] { "m1", "m2" }, sourceFolder: null, targetFolder: "Archive");

Assert.Equal(2, results.Count);
Assert.All(results, x => Assert.True(x.Ok, x.Error));
Assert.Equal(2, handler.Requests.Count);
var body = await handler.Requests[1].Content!.ReadAsStringAsync();
Assert.Contains("\"ids\":[\"m1\",\"m2\"]", body, StringComparison.Ordinal);
Assert.Contains("\"addLabelIds\":[\"Label_Target\"]", body, StringComparison.Ordinal);
Assert.Contains("\"removeLabelIds\":[\"TRASH\"]", body, StringComparison.Ordinal);
Assert.DoesNotContain("INBOX", body, StringComparison.Ordinal);
}

[Fact]
public async System.Threading.Tasks.Task DeleteThreadsAsync_MapsPerThreadFailures() {
var handler = new RecordingHandler(
Expand Down
14 changes: 12 additions & 2 deletions Sources/Mailozaurr/Gmail/GmailMailboxBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ public async Task MoveMessageAsync(
}

var remove = new List<string>();
var sourceLabelId = NormalizeOptional(await ResolveLabelIdAsync(sourceFolder, cancellationToken).ConfigureAwait(false));
var sourceLabelId = await ResolveSourceLabelIdAsync(sourceFolder, cancellationToken).ConfigureAwait(false);
if (sourceLabelId != null) {
remove.Add(sourceLabelId);
}
Expand Down Expand Up @@ -806,7 +806,7 @@ public async Task<IReadOnlyList<GmailMailboxBulkOperationResult>> MoveMessagesAs
}

var remove = new List<string>();
var sourceLabelId = NormalizeOptional(await ResolveLabelIdAsync(sourceFolder, cancellationToken).ConfigureAwait(false));
var sourceLabelId = await ResolveSourceLabelIdAsync(sourceFolder, cancellationToken).ConfigureAwait(false);
if (sourceLabelId != null) {
remove.Add(sourceLabelId);
}
Expand Down Expand Up @@ -1207,6 +1207,16 @@ private async Task<IReadOnlyList<GmailMailboxBulkOperationResult>> ExecuteThread
return results;
}

private async Task<string?> ResolveSourceLabelIdAsync(
string? sourceFolder,
CancellationToken cancellationToken) {
if (string.IsNullOrWhiteSpace(sourceFolder)) {
return null;
}

return NormalizeOptional(await ResolveLabelIdAsync(sourceFolder, cancellationToken).ConfigureAwait(false));
}

private static List<string> NormalizeIds(IEnumerable<string> ids) {
var output = new List<string>();
if (ids == null) {
Expand Down
Loading