@remit/backend 0.0.119 → 0.0.121
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -1
- package/src/handlers/filter.ts +24 -0
- package/src/handlers/folder-role-labels.ts +63 -0
- package/src/handlers/folder-role.test.ts +100 -75
- package/src/handlers/folder-role.ts +1 -28
- package/src/handlers/mailbox-bindings.ts +111 -0
- package/src/handlers/mailbox-rename-refusal.test.ts +247 -0
- package/src/handlers/mailbox-settled-target.test.ts +239 -0
- package/src/handlers/mailbox.test.ts +164 -30
- package/src/handlers/mailbox.ts +163 -84
- package/src/handlers/message.ts +11 -2
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import type { MailboxItem } from "@remit/data-ports";
|
|
4
|
+
import { MailboxSyncStatus } from "@remit/domain-enums";
|
|
5
|
+
import { applyMailboxPatch, type MailboxPatchClient } from "./mailbox.js";
|
|
6
|
+
|
|
7
|
+
const ACCOUNT_ID = "acc-1";
|
|
8
|
+
const CONFIG_ID = "cfg-1";
|
|
9
|
+
|
|
10
|
+
const mailbox = (over: Partial<MailboxItem> = {}): MailboxItem =>
|
|
11
|
+
({
|
|
12
|
+
mailboxId: "mbx-1",
|
|
13
|
+
accountId: ACCOUNT_ID,
|
|
14
|
+
fullPath: "Work",
|
|
15
|
+
hierarchyDelimiter: "/",
|
|
16
|
+
syncStatus: MailboxSyncStatus.synced,
|
|
17
|
+
...over,
|
|
18
|
+
}) as MailboxItem;
|
|
19
|
+
|
|
20
|
+
const clientOver = (
|
|
21
|
+
row: MailboxItem,
|
|
22
|
+
others: MailboxItem[] = [],
|
|
23
|
+
): {
|
|
24
|
+
client: MailboxPatchClient;
|
|
25
|
+
renames: { mailboxId: string; newPath: string }[];
|
|
26
|
+
dismissals: string[];
|
|
27
|
+
settings: string[];
|
|
28
|
+
} => {
|
|
29
|
+
const renames: { mailboxId: string; newPath: string }[] = [];
|
|
30
|
+
const dismissals: string[] = [];
|
|
31
|
+
const settings: string[] = [];
|
|
32
|
+
const client: MailboxPatchClient = {
|
|
33
|
+
mailbox: {
|
|
34
|
+
get: async () => row,
|
|
35
|
+
listAllByAccount: async () => [row, ...others],
|
|
36
|
+
},
|
|
37
|
+
mailboxQueue: {
|
|
38
|
+
renameMailbox: async (mailboxId, newPath) => {
|
|
39
|
+
renames.push({ mailboxId, newPath });
|
|
40
|
+
return mailbox({ pendingPath: newPath });
|
|
41
|
+
},
|
|
42
|
+
dismissMailboxIntent: async (mailboxId) => {
|
|
43
|
+
dismissals.push(mailboxId);
|
|
44
|
+
return mailbox();
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
accountSetting: {
|
|
48
|
+
get: async () => null,
|
|
49
|
+
upsert: async (item) => {
|
|
50
|
+
settings.push(item.name);
|
|
51
|
+
return item as never;
|
|
52
|
+
},
|
|
53
|
+
delete: async (_configId, name) => {
|
|
54
|
+
settings.push(name);
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
return { client, renames, dismissals, settings };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const patch = (
|
|
62
|
+
client: MailboxPatchClient,
|
|
63
|
+
body: Record<string, unknown>,
|
|
64
|
+
): Promise<MailboxItem> =>
|
|
65
|
+
applyMailboxPatch(client, CONFIG_ID, "mbx-1", ACCOUNT_ID, body);
|
|
66
|
+
|
|
67
|
+
const refusedWith = (message: string) => (error: unknown) =>
|
|
68
|
+
(error as { statusCode?: number }).statusCode === 400 &&
|
|
69
|
+
(error as Error).message === message;
|
|
70
|
+
|
|
71
|
+
describe("applyMailboxPatch — renames that can never succeed (D4, D5)", () => {
|
|
72
|
+
it("refuses to rename the inbox", async () => {
|
|
73
|
+
const { client, renames } = clientOver(mailbox({ fullPath: "INBOX" }));
|
|
74
|
+
|
|
75
|
+
await assert.rejects(
|
|
76
|
+
patch(client, { fullPath: "Postvak" }),
|
|
77
|
+
refusedWith("The inbox can't be renamed."),
|
|
78
|
+
);
|
|
79
|
+
assert.equal(renames.length, 0);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("refuses a target that canonicalizes to the inbox", async () => {
|
|
83
|
+
const { client, renames } = clientOver(mailbox());
|
|
84
|
+
|
|
85
|
+
await assert.rejects(
|
|
86
|
+
patch(client, { fullPath: "inbox" }),
|
|
87
|
+
refusedWith("“INBOX” is reserved for the inbox."),
|
|
88
|
+
);
|
|
89
|
+
assert.equal(renames.length, 0);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("refuses an empty target", async () => {
|
|
93
|
+
const { client, renames } = clientOver(mailbox());
|
|
94
|
+
|
|
95
|
+
await assert.rejects(
|
|
96
|
+
patch(client, { fullPath: " " }),
|
|
97
|
+
refusedWith("A folder needs a name."),
|
|
98
|
+
);
|
|
99
|
+
assert.equal(renames.length, 0);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("refuses a target another folder already holds", async () => {
|
|
103
|
+
const { client, renames } = clientOver(mailbox(), [
|
|
104
|
+
mailbox({ mailboxId: "mbx-2", fullPath: "Projects" }),
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
await assert.rejects(
|
|
108
|
+
patch(client, { fullPath: "Projects" }),
|
|
109
|
+
refusedWith("A folder named “Projects” is already there."),
|
|
110
|
+
);
|
|
111
|
+
assert.equal(renames.length, 0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The sweep reads a folder whose leaf name is a role's conventional name but
|
|
116
|
+
* which lacks the server's flag as a duplicate, and deletes its row — under
|
|
117
|
+
* D8, with the folder's mail — as soon as another folder holds the flag. A
|
|
118
|
+
* rename to one of those settles `synced` and is then reaped.
|
|
119
|
+
*/
|
|
120
|
+
for (const [name, leaf] of [
|
|
121
|
+
["Archive", "Archive"],
|
|
122
|
+
["Trash", "Trash"],
|
|
123
|
+
["Drafts", "Drafts"],
|
|
124
|
+
["Sent", "Sent"],
|
|
125
|
+
["Junk", "Spam"],
|
|
126
|
+
] as const) {
|
|
127
|
+
it(`refuses a rename to the reserved ${name} name`, async () => {
|
|
128
|
+
const { client, renames } = clientOver(mailbox());
|
|
129
|
+
|
|
130
|
+
await assert.rejects(
|
|
131
|
+
patch(client, { fullPath: `Work/${leaf}` }),
|
|
132
|
+
refusedWith(
|
|
133
|
+
`“${leaf}” is reserved for a system folder. Pick another name.`,
|
|
134
|
+
),
|
|
135
|
+
);
|
|
136
|
+
assert.equal(renames.length, 0);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
it("refuses a target another rename is already on its way to", async () => {
|
|
141
|
+
// A path a rename is aiming at is as taken as one a folder sits at (D2).
|
|
142
|
+
// Two renames onto one target both settle, both write it, and the sweep
|
|
143
|
+
// then reaps one row — with its mail — and inserts a duplicate.
|
|
144
|
+
const { client, renames } = clientOver(mailbox(), [
|
|
145
|
+
mailbox({
|
|
146
|
+
mailboxId: "mbx-2",
|
|
147
|
+
fullPath: "Admin",
|
|
148
|
+
syncStatus: MailboxSyncStatus.pending,
|
|
149
|
+
pendingPath: "Projects",
|
|
150
|
+
}),
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
await assert.rejects(
|
|
154
|
+
patch(client, { fullPath: "Projects" }),
|
|
155
|
+
refusedWith("“Admin” is already being renamed to “Projects”."),
|
|
156
|
+
);
|
|
157
|
+
assert.equal(renames.length, 0);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("lets a failed rename's stale target go to somebody else", async () => {
|
|
161
|
+
// `failed` keeps its target so the client can name what the rename was
|
|
162
|
+
// aiming at and offer a retry (T6), and nothing clears it until the user
|
|
163
|
+
// retries or dismisses. Honouring that claim would tell everyone else the
|
|
164
|
+
// path is taken by a rename that is not happening, with nothing to wait
|
|
165
|
+
// for and no way to find out.
|
|
166
|
+
const { client, renames } = clientOver(mailbox(), [
|
|
167
|
+
mailbox({
|
|
168
|
+
mailboxId: "mbx-2",
|
|
169
|
+
fullPath: "Admin",
|
|
170
|
+
syncStatus: MailboxSyncStatus.failed,
|
|
171
|
+
pendingPath: "Projects",
|
|
172
|
+
}),
|
|
173
|
+
]);
|
|
174
|
+
|
|
175
|
+
await patch(client, { fullPath: "Projects" });
|
|
176
|
+
|
|
177
|
+
assert.deepEqual(renames, [{ mailboxId: "mbx-1", newPath: "Projects" }]);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("lets a retry aim at the target its own failed rename recorded", async () => {
|
|
181
|
+
const { client, renames } = clientOver(
|
|
182
|
+
mailbox({
|
|
183
|
+
syncStatus: MailboxSyncStatus.failed,
|
|
184
|
+
pendingPath: "Projects",
|
|
185
|
+
}),
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
await patch(client, { fullPath: "Projects" });
|
|
189
|
+
|
|
190
|
+
assert.deepEqual(renames, [{ mailboxId: "mbx-1", newPath: "Projects" }]);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("records the intent for an ordinary target", async () => {
|
|
194
|
+
const { client, renames } = clientOver(mailbox());
|
|
195
|
+
|
|
196
|
+
const renamed = await patch(client, { fullPath: "Projects" });
|
|
197
|
+
|
|
198
|
+
assert.deepEqual(renames, [{ mailboxId: "mbx-1", newPath: "Projects" }]);
|
|
199
|
+
assert.equal(renamed.pendingPath, "Projects");
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe("applyMailboxPatch — dismissing a failed intent (T10, T11)", () => {
|
|
204
|
+
it("reads a target equal to the confirmed path as a dismissal, not a rename", async () => {
|
|
205
|
+
const { client, renames, dismissals } = clientOver(
|
|
206
|
+
mailbox({
|
|
207
|
+
syncStatus: MailboxSyncStatus.failed,
|
|
208
|
+
pendingPath: "Projects",
|
|
209
|
+
}),
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
const dismissed = await patch(client, { fullPath: "Work" });
|
|
213
|
+
|
|
214
|
+
assert.deepEqual(dismissals, ["mbx-1"]);
|
|
215
|
+
assert.equal(renames.length, 0, "nothing is enqueued for a dismissal");
|
|
216
|
+
assert.equal(dismissed.syncStatus, MailboxSyncStatus.synced);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("dismisses even where the folder's own name is reserved", async () => {
|
|
220
|
+
// The refusals gate a rename's target. Keeping the name a folder already
|
|
221
|
+
// has is not one, and a folder called `Archive` must still be able to
|
|
222
|
+
// dismiss a rename that failed.
|
|
223
|
+
const { client, dismissals } = clientOver(
|
|
224
|
+
mailbox({ fullPath: "Archive", syncStatus: MailboxSyncStatus.failed }),
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
await patch(client, { fullPath: "Archive" });
|
|
228
|
+
|
|
229
|
+
assert.deepEqual(dismissals, ["mbx-1"]);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe("applyMailboxPatch — an override-only PATCH", () => {
|
|
234
|
+
it("touches no mutation state", async () => {
|
|
235
|
+
const { client, renames, dismissals, settings } = clientOver(mailbox());
|
|
236
|
+
|
|
237
|
+
await patch(client, { muted: true });
|
|
238
|
+
|
|
239
|
+
assert.equal(renames.length, 0);
|
|
240
|
+
assert.equal(dismissals.length, 0);
|
|
241
|
+
assert.equal(
|
|
242
|
+
settings.length > 0,
|
|
243
|
+
true,
|
|
244
|
+
"the override row is still written",
|
|
245
|
+
);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every API write that binds a durable reference to a folder requires that
|
|
3
|
+
* folder to be settled (D12, first row; imap-mutations R2: wait). Driven
|
|
4
|
+
* through the real handlers rather than the helper, because the claim is that
|
|
5
|
+
* each call site is wired to it — a helper that nothing calls refuses nothing.
|
|
6
|
+
*
|
|
7
|
+
* The other half of the same invariant is the delete refusal (D16), in
|
|
8
|
+
* `mailbox.test.ts`: a binding is only created against a settled folder and
|
|
9
|
+
* only removed by the user, so a `mailboxId` that names nothing never exists.
|
|
10
|
+
*/
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
import { afterEach, describe, it } from "node:test";
|
|
13
|
+
import type { MailboxItem } from "@remit/data-ports";
|
|
14
|
+
import { CanonicalMailboxRole, MailboxSyncStatus } from "@remit/domain-enums";
|
|
15
|
+
import type { APIGatewayProxyEvent } from "aws-lambda";
|
|
16
|
+
import type { Context } from "openapi-backend";
|
|
17
|
+
import { deriveAccountConfigId } from "../auth.js";
|
|
18
|
+
import {
|
|
19
|
+
_resetForTest,
|
|
20
|
+
type RemitClient,
|
|
21
|
+
setClient,
|
|
22
|
+
} from "../service/data-client.js";
|
|
23
|
+
import { FilterDetailOperations, FilterOperations } from "./filter.js";
|
|
24
|
+
import { FolderRoleOperations } from "./folder-role.js";
|
|
25
|
+
import { MessageBulkOperations } from "./message.js";
|
|
26
|
+
|
|
27
|
+
const SUB = "cognito-sub-365";
|
|
28
|
+
const ACCOUNT_ID = "acc-365";
|
|
29
|
+
const CONFIG_ID = deriveAccountConfigId(SUB);
|
|
30
|
+
const TARGET = "mbx-target";
|
|
31
|
+
|
|
32
|
+
const target = (syncStatus: MailboxItem["syncStatus"]): MailboxItem =>
|
|
33
|
+
({
|
|
34
|
+
mailboxId: TARGET,
|
|
35
|
+
accountId: ACCOUNT_ID,
|
|
36
|
+
fullPath: "Receipts",
|
|
37
|
+
hierarchyDelimiter: "/",
|
|
38
|
+
syncStatus,
|
|
39
|
+
}) as MailboxItem;
|
|
40
|
+
|
|
41
|
+
interface Writes {
|
|
42
|
+
filters: unknown[];
|
|
43
|
+
filterPatches: unknown[];
|
|
44
|
+
appointments: unknown[];
|
|
45
|
+
moves: unknown[];
|
|
46
|
+
copies: unknown[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const world = (syncStatus: MailboxItem["syncStatus"]): Writes => {
|
|
50
|
+
const writes: Writes = {
|
|
51
|
+
filters: [],
|
|
52
|
+
filterPatches: [],
|
|
53
|
+
appointments: [],
|
|
54
|
+
moves: [],
|
|
55
|
+
copies: [],
|
|
56
|
+
};
|
|
57
|
+
const row = target(syncStatus);
|
|
58
|
+
|
|
59
|
+
setClient({
|
|
60
|
+
account: {
|
|
61
|
+
get: async () => ({ accountId: ACCOUNT_ID, accountConfigId: CONFIG_ID }),
|
|
62
|
+
listAllByAccountConfig: async () => [{ accountId: ACCOUNT_ID }],
|
|
63
|
+
},
|
|
64
|
+
mailbox: {
|
|
65
|
+
get: async (_accountId: string, mailboxId: string | string[]) =>
|
|
66
|
+
Array.isArray(mailboxId)
|
|
67
|
+
? mailboxId.map((id) => ({ mailboxId: id, accountId: ACCOUNT_ID }))
|
|
68
|
+
: row,
|
|
69
|
+
listAllByAccount: async () => [row],
|
|
70
|
+
},
|
|
71
|
+
message: {
|
|
72
|
+
get: async () => ({ mailboxId: "mbx-source" }),
|
|
73
|
+
},
|
|
74
|
+
filter: {
|
|
75
|
+
get: async () => ({ filterId: "flt-1", scope: "Standing" }),
|
|
76
|
+
update: async (...args: unknown[]) => {
|
|
77
|
+
writes.filterPatches.push(args);
|
|
78
|
+
return { filterId: "flt-1" };
|
|
79
|
+
},
|
|
80
|
+
listByAccountConfig: async () => [],
|
|
81
|
+
},
|
|
82
|
+
filterAnchorTransaction: {
|
|
83
|
+
createWithAnchor: async (input: unknown) => {
|
|
84
|
+
writes.filters.push(input);
|
|
85
|
+
return { filterId: "flt-1" };
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
accountSetting: {
|
|
89
|
+
get: async () => null,
|
|
90
|
+
upsert: async (item: unknown) => {
|
|
91
|
+
writes.appointments.push(item);
|
|
92
|
+
return item;
|
|
93
|
+
},
|
|
94
|
+
delete: async () => undefined,
|
|
95
|
+
},
|
|
96
|
+
messageMove: {
|
|
97
|
+
moveMessages: async (...args: unknown[]) => {
|
|
98
|
+
writes.moves.push(args);
|
|
99
|
+
},
|
|
100
|
+
copyMessages: async (...args: unknown[]) => {
|
|
101
|
+
writes.copies.push(args);
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
} as unknown as RemitClient);
|
|
105
|
+
|
|
106
|
+
return writes;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const event = {
|
|
110
|
+
requestContext: { authorizer: { claims: { sub: SUB } } },
|
|
111
|
+
} as unknown as APIGatewayProxyEvent;
|
|
112
|
+
|
|
113
|
+
const call = <T>(
|
|
114
|
+
handler: unknown,
|
|
115
|
+
request: Record<string, unknown>,
|
|
116
|
+
): Promise<T> =>
|
|
117
|
+
(handler as (context: Context, event: APIGatewayProxyEvent) => Promise<T>)(
|
|
118
|
+
{ request } as unknown as Context,
|
|
119
|
+
event,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const appoint = () =>
|
|
123
|
+
call(FolderRoleOperations.FolderRoleOperations_appointFolderRole, {
|
|
124
|
+
params: { accountId: ACCOUNT_ID, role: CanonicalMailboxRole.Archive },
|
|
125
|
+
requestBody: { mailboxId: TARGET },
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const createFilter = () =>
|
|
129
|
+
call(FilterOperations.FilterOperations_createFilter, {
|
|
130
|
+
params: { accountId: ACCOUNT_ID },
|
|
131
|
+
requestBody: {
|
|
132
|
+
name: "Invoices",
|
|
133
|
+
scope: "Standing",
|
|
134
|
+
actionMailboxId: TARGET,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const updateFilter = () =>
|
|
139
|
+
call(FilterDetailOperations.FilterDetailOperations_updateFilter, {
|
|
140
|
+
params: { accountId: ACCOUNT_ID, filterId: "flt-1" },
|
|
141
|
+
requestBody: { actionMailboxId: TARGET },
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const moveMessages = () =>
|
|
145
|
+
call(MessageBulkOperations.MessageBulkOperations_moveMessages, {
|
|
146
|
+
params: {},
|
|
147
|
+
requestBody: { messageIds: ["msg-1"], destinationMailboxId: TARGET },
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const copyMessages = () =>
|
|
151
|
+
call(MessageBulkOperations.MessageBulkOperations_copyMessages, {
|
|
152
|
+
params: {},
|
|
153
|
+
requestBody: { messageIds: ["msg-1"], destinationMailboxId: TARGET },
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const unready = (error: unknown): boolean =>
|
|
157
|
+
(error as { statusCode?: number }).statusCode === 422 &&
|
|
158
|
+
(error as { publicApiError?: { code?: string } }).publicApiError?.code ===
|
|
159
|
+
"mailbox_not_settled";
|
|
160
|
+
|
|
161
|
+
const SITES = [
|
|
162
|
+
["appointing a role", appoint, (w: Writes) => w.appointments],
|
|
163
|
+
["creating a filter", createFilter, (w: Writes) => w.filters],
|
|
164
|
+
["updating a filter", updateFilter, (w: Writes) => w.filterPatches],
|
|
165
|
+
["moving messages", moveMessages, (w: Writes) => w.moves],
|
|
166
|
+
["copying messages", copyMessages, (w: Writes) => w.copies],
|
|
167
|
+
] as const;
|
|
168
|
+
|
|
169
|
+
describe("a write that binds to a folder requires a settled folder", () => {
|
|
170
|
+
afterEach(() => {
|
|
171
|
+
_resetForTest();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
for (const [what, run, written] of SITES) {
|
|
175
|
+
for (const state of [
|
|
176
|
+
MailboxSyncStatus.pending,
|
|
177
|
+
MailboxSyncStatus.deleting,
|
|
178
|
+
MailboxSyncStatus.failed,
|
|
179
|
+
]) {
|
|
180
|
+
it(`refuses ${what} against a ${state} folder, writing nothing`, async () => {
|
|
181
|
+
const writes = world(state);
|
|
182
|
+
|
|
183
|
+
await assert.rejects(run(), unready);
|
|
184
|
+
assert.deepEqual(written(writes), []);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
it(`accepts ${what} against a settled folder`, async () => {
|
|
189
|
+
const writes = world(MailboxSyncStatus.synced);
|
|
190
|
+
|
|
191
|
+
await run();
|
|
192
|
+
assert.equal(written(writes).length > 0, true);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
it("never answers 409 from these paths", async () => {
|
|
197
|
+
// 409 belongs to the folder's own mutation losing a compare-and-set
|
|
198
|
+
// (#362, #363). Reusing it here would make "somebody else is changing this
|
|
199
|
+
// folder" and "the folder you are pointing at is not ready" the same
|
|
200
|
+
// answer, with different copy and different remedies behind it.
|
|
201
|
+
world(MailboxSyncStatus.pending);
|
|
202
|
+
|
|
203
|
+
await assert.rejects(
|
|
204
|
+
appoint(),
|
|
205
|
+
(error: unknown) => (error as { statusCode?: number }).statusCode !== 409,
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("does not gate clearing a role whose folder is on its way out", async () => {
|
|
210
|
+
const writes = world(MailboxSyncStatus.deleting);
|
|
211
|
+
|
|
212
|
+
await call(FolderRoleOperations.FolderRoleOperations_appointFolderRole, {
|
|
213
|
+
params: { accountId: ACCOUNT_ID, role: CanonicalMailboxRole.Archive },
|
|
214
|
+
requestBody: { mailboxId: null },
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
assert.deepEqual(
|
|
218
|
+
writes.appointments,
|
|
219
|
+
[],
|
|
220
|
+
"the row is deleted, not upserted",
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("does not gate a filter that applies no move action", async () => {
|
|
225
|
+
// `None` is the sentinel for "no move action", so it binds to nothing.
|
|
226
|
+
const writes = world(MailboxSyncStatus.deleting);
|
|
227
|
+
|
|
228
|
+
await call(FilterOperations.FilterOperations_createFilter, {
|
|
229
|
+
params: { accountId: ACCOUNT_ID },
|
|
230
|
+
requestBody: {
|
|
231
|
+
name: "Just a label",
|
|
232
|
+
scope: "Standing",
|
|
233
|
+
actionMailboxId: "None",
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
assert.equal(writes.filters.length, 1);
|
|
238
|
+
});
|
|
239
|
+
});
|
|
@@ -11,36 +11,7 @@ import {
|
|
|
11
11
|
type RemitClient,
|
|
12
12
|
setClient,
|
|
13
13
|
} from "../service/data-client.js";
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
const mailbox = (
|
|
17
|
-
over: Partial<MailboxItem> & { mailboxId: string },
|
|
18
|
-
): MailboxItem =>
|
|
19
|
-
({ fullPath: over.mailboxId, ...over }) as unknown as MailboxItem;
|
|
20
|
-
|
|
21
|
-
describe("excludeDeletingMailboxes", () => {
|
|
22
|
-
it("drops a folder being deleted so it leaves the list before the worker reaps it", () => {
|
|
23
|
-
const items = [
|
|
24
|
-
mailbox({ mailboxId: "inbox", syncStatus: MailboxSyncStatus.synced }),
|
|
25
|
-
mailbox({ mailboxId: "gone", syncStatus: MailboxSyncStatus.deleting }),
|
|
26
|
-
mailbox({ mailboxId: "new", syncStatus: MailboxSyncStatus.pending }),
|
|
27
|
-
];
|
|
28
|
-
assert.deepEqual(
|
|
29
|
-
excludeDeletingMailboxes(items).map((m) => m.mailboxId),
|
|
30
|
-
["inbox", "new"],
|
|
31
|
-
);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("keeps a folder whose delete failed and was restored off `deleting`", () => {
|
|
35
|
-
const items = [
|
|
36
|
-
mailbox({ mailboxId: "restored", syncStatus: MailboxSyncStatus.failed }),
|
|
37
|
-
];
|
|
38
|
-
assert.deepEqual(
|
|
39
|
-
excludeDeletingMailboxes(items).map((m) => m.mailboxId),
|
|
40
|
-
["restored"],
|
|
41
|
-
);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
14
|
+
import { MailboxDetailOperations, TrashOperations } from "./mailbox.js";
|
|
44
15
|
|
|
45
16
|
const SUB = "cognito-sub-887";
|
|
46
17
|
const ACCOUNT_ID = "acc-887";
|
|
@@ -96,3 +67,166 @@ describe("TrashOperations_emptyTrash", () => {
|
|
|
96
67
|
);
|
|
97
68
|
});
|
|
98
69
|
});
|
|
70
|
+
|
|
71
|
+
const deleteMailbox =
|
|
72
|
+
MailboxDetailOperations.MailboxDetailOperations_deleteMailbox as unknown as (
|
|
73
|
+
context: Context,
|
|
74
|
+
event: APIGatewayProxyEvent,
|
|
75
|
+
) => Promise<{ statusCode: number }>;
|
|
76
|
+
|
|
77
|
+
const folder = (over: Partial<MailboxItem> = {}): MailboxItem =>
|
|
78
|
+
({
|
|
79
|
+
mailboxId: "mbx-1",
|
|
80
|
+
accountId: ACCOUNT_ID,
|
|
81
|
+
fullPath: "Receipts",
|
|
82
|
+
hierarchyDelimiter: "/",
|
|
83
|
+
syncStatus: MailboxSyncStatus.synced,
|
|
84
|
+
...over,
|
|
85
|
+
}) as MailboxItem;
|
|
86
|
+
|
|
87
|
+
interface DeleteWorld {
|
|
88
|
+
mailbox?: MailboxItem;
|
|
89
|
+
children?: MailboxItem[];
|
|
90
|
+
filters?: { name: string; actionMailboxId: string }[];
|
|
91
|
+
appointments?: Record<string, string>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const callDelete = (
|
|
95
|
+
world: DeleteWorld,
|
|
96
|
+
): { run: () => Promise<{ statusCode: number }>; deleted: string[] } => {
|
|
97
|
+
const deleted: string[] = [];
|
|
98
|
+
setClient({
|
|
99
|
+
account: {
|
|
100
|
+
get: async () => ({
|
|
101
|
+
accountId: ACCOUNT_ID,
|
|
102
|
+
accountConfigId: deriveAccountConfigId(SUB),
|
|
103
|
+
}),
|
|
104
|
+
},
|
|
105
|
+
mailbox: {
|
|
106
|
+
get: async () => world.mailbox ?? folder(),
|
|
107
|
+
findByPathPrefix: async () => world.children ?? [],
|
|
108
|
+
},
|
|
109
|
+
filter: {
|
|
110
|
+
listByAccountConfig: async () => world.filters ?? [],
|
|
111
|
+
},
|
|
112
|
+
accountSetting: {
|
|
113
|
+
get: async (_configId: string, name: string) => {
|
|
114
|
+
for (const [role, mailboxId] of Object.entries(
|
|
115
|
+
world.appointments ?? {},
|
|
116
|
+
)) {
|
|
117
|
+
if (name === `FolderRoleAppointment#${ACCOUNT_ID}#${role}`) {
|
|
118
|
+
return { value: { kind: "String", value: mailboxId } };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
mailboxQueue: {
|
|
125
|
+
deleteMailbox: async (mailboxId: string) => {
|
|
126
|
+
deleted.push(mailboxId);
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
} as unknown as RemitClient);
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
deleted,
|
|
133
|
+
run: () =>
|
|
134
|
+
deleteMailbox(
|
|
135
|
+
{
|
|
136
|
+
request: { params: { accountId: ACCOUNT_ID, mailboxId: "mbx-1" } },
|
|
137
|
+
} as unknown as Context,
|
|
138
|
+
{
|
|
139
|
+
requestContext: { authorizer: { claims: { sub: SUB } } },
|
|
140
|
+
} as unknown as APIGatewayProxyEvent,
|
|
141
|
+
),
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const refusal = (match: RegExp) => (error: unknown) =>
|
|
146
|
+
(error as { statusCode?: number }).statusCode === 400 &&
|
|
147
|
+
match.test((error as Error).message);
|
|
148
|
+
|
|
149
|
+
describe("MailboxDetailOperations_deleteMailbox — what it refuses (D4, D16)", () => {
|
|
150
|
+
afterEach(() => {
|
|
151
|
+
_resetForTest();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("refuses the inbox", async () => {
|
|
155
|
+
const { run, deleted } = callDelete({
|
|
156
|
+
mailbox: folder({ fullPath: "INBOX" }),
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
await assert.rejects(run(), refusal(/inbox can't be deleted/));
|
|
160
|
+
assert.deepEqual(deleted, []);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("refuses a folder that has folders inside it", async () => {
|
|
164
|
+
// The server would keep the parent's name as a placeholder and leave the
|
|
165
|
+
// children untouched — an outcome no local row describes.
|
|
166
|
+
const { run, deleted } = callDelete({
|
|
167
|
+
children: [folder({ mailboxId: "mbx-2", fullPath: "Receipts/2026" })],
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
await assert.rejects(run(), refusal(/has folders inside it/));
|
|
171
|
+
assert.deepEqual(deleted, []);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* D16. Refusing is what makes the settled-target rule total: a binding is
|
|
176
|
+
* only created against a settled folder and only removed by the user, so a
|
|
177
|
+
* mailboxId that names nothing never exists and no reader needs a
|
|
178
|
+
* missing-target branch.
|
|
179
|
+
*/
|
|
180
|
+
it("refuses a folder a filter moves mail into, naming the filter", async () => {
|
|
181
|
+
const { run, deleted } = callDelete({
|
|
182
|
+
filters: [{ name: "Invoices → Receipts", actionMailboxId: "mbx-1" }],
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
await assert.rejects(run(), refusal(/the filter “Invoices → Receipts”/));
|
|
186
|
+
assert.deepEqual(deleted, []);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("refuses a folder appointed to a role, naming the role", async () => {
|
|
190
|
+
const { run, deleted } = callDelete({ appointments: { Archive: "mbx-1" } });
|
|
191
|
+
|
|
192
|
+
await assert.rejects(run(), refusal(/the Archive role/));
|
|
193
|
+
assert.deepEqual(deleted, []);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("names both when both are bound", async () => {
|
|
197
|
+
const { run } = callDelete({
|
|
198
|
+
filters: [{ name: "Invoices", actionMailboxId: "mbx-1" }],
|
|
199
|
+
appointments: { Archive: "mbx-1" },
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
await assert.rejects(
|
|
203
|
+
run(),
|
|
204
|
+
refusal(/the filter “Invoices” and the Archive role/),
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("ignores a filter that moves mail into a different folder", async () => {
|
|
209
|
+
const { run, deleted } = callDelete({
|
|
210
|
+
filters: [{ name: "Elsewhere", actionMailboxId: "mbx-9" }],
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
assert.deepEqual(await run(), { statusCode: 204 });
|
|
214
|
+
assert.deepEqual(deleted, ["mbx-1"]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("ignores a filter with no move action at all", async () => {
|
|
218
|
+
const { run, deleted } = callDelete({
|
|
219
|
+
filters: [{ name: "Just a label", actionMailboxId: "None" }],
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
assert.deepEqual(await run(), { statusCode: 204 });
|
|
223
|
+
assert.deepEqual(deleted, ["mbx-1"]);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("records the intent for an ordinary folder", async () => {
|
|
227
|
+
const { run, deleted } = callDelete({});
|
|
228
|
+
|
|
229
|
+
assert.deepEqual(await run(), { statusCode: 204 });
|
|
230
|
+
assert.deepEqual(deleted, ["mbx-1"]);
|
|
231
|
+
});
|
|
232
|
+
});
|