mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-09-04 23:17:34 +08:00
refactor: serve mail states from backend
This commit is contained in:
@@ -12,6 +12,13 @@ test.describe('Mail Read Status', () => {
|
||||
const second = await createTestAddress(request, 'mail-flags-second');
|
||||
|
||||
try {
|
||||
const statesRes = await request.get(`${WORKER_URL}/api/mail-states`, {
|
||||
headers: { Authorization: `Bearer ${first.jwt}` },
|
||||
});
|
||||
expect(statesRes.ok()).toBe(true);
|
||||
expect((await statesRes.json()).results.map((state: { value: string }) => state.value))
|
||||
.toEqual(['all', 'unread', 'read']);
|
||||
|
||||
await seedTestMail(request, first.address, { subject: 'Unread mail' });
|
||||
const listRes = await request.get(`${WORKER_URL}/api/mails?limit=10&offset=0`, {
|
||||
headers: { Authorization: `Bearer ${first.jwt}` },
|
||||
@@ -23,21 +30,21 @@ test.describe('Mail Read Status', () => {
|
||||
expect(results[0].unread).toBe(true);
|
||||
|
||||
const unreadRes = await request.get(
|
||||
`${WORKER_URL}/api/mails?limit=10&offset=0&read_status=unread`,
|
||||
`${WORKER_URL}/api/mails?limit=10&offset=0&mail_state=unread`,
|
||||
{ headers: { Authorization: `Bearer ${first.jwt}` } },
|
||||
);
|
||||
expect((await unreadRes.json()).results).toHaveLength(1);
|
||||
|
||||
const deniedRes = await request.patch(`${WORKER_URL}/api/mails/read-status`, {
|
||||
const deniedRes = await request.patch(`${WORKER_URL}/api/mails/state`, {
|
||||
headers: { Authorization: `Bearer ${second.jwt}` },
|
||||
data: { ids: [results[0].id], action: 'read' },
|
||||
data: { ids: [results[0].id], state: 'read' },
|
||||
});
|
||||
expect(deniedRes.ok()).toBe(true);
|
||||
expect((await deniedRes.json()).changes).toBe(0);
|
||||
|
||||
const updateRes = await request.patch(`${WORKER_URL}/api/mails/read-status`, {
|
||||
const updateRes = await request.patch(`${WORKER_URL}/api/mails/state`, {
|
||||
headers: { Authorization: `Bearer ${first.jwt}` },
|
||||
data: { ids: [results[0].id], action: 'read' },
|
||||
data: { ids: [results[0].id], state: 'read' },
|
||||
});
|
||||
expect(updateRes.ok()).toBe(true);
|
||||
const updateResult = await updateRes.json();
|
||||
@@ -50,31 +57,31 @@ test.describe('Mail Read Status', () => {
|
||||
expect((await updatedListRes.json()).results[0].unread).toBe(false);
|
||||
|
||||
const unreadAfterUpdateRes = await request.get(
|
||||
`${WORKER_URL}/api/mails?limit=10&offset=0&read_status=unread`,
|
||||
`${WORKER_URL}/api/mails?limit=10&offset=0&mail_state=unread`,
|
||||
{ headers: { Authorization: `Bearer ${first.jwt}` } },
|
||||
);
|
||||
expect((await unreadAfterUpdateRes.json()).results).toHaveLength(0);
|
||||
|
||||
const toggleRes = await request.patch(`${WORKER_URL}/api/mails/read-status`, {
|
||||
const unreadStateRes = await request.patch(`${WORKER_URL}/api/mails/state`, {
|
||||
headers: { Authorization: `Bearer ${first.jwt}` },
|
||||
data: { ids: [results[0].id], action: 'toggle' },
|
||||
data: { ids: [results[0].id], state: 'unread' },
|
||||
});
|
||||
expect(toggleRes.ok()).toBe(true);
|
||||
expect((await toggleRes.json()).results[0].unread).toBe(true);
|
||||
expect(unreadStateRes.ok()).toBe(true);
|
||||
expect((await unreadStateRes.json()).results[0].unread).toBe(true);
|
||||
} finally {
|
||||
await deleteAddress(request, first.jwt);
|
||||
await deleteAddress(request, second.jwt);
|
||||
}
|
||||
});
|
||||
|
||||
test('rejects unsupported read-status actions', async ({ request }) => {
|
||||
test('rejects unsupported mail states', async ({ request }) => {
|
||||
const { jwt } = await createTestAddress(request, 'mail-flags-invalid');
|
||||
try {
|
||||
for (const data of [
|
||||
{ ids: [1], action: 'invalid' },
|
||||
{ ids: [1], state: 'invalid' },
|
||||
{ ids: [1] },
|
||||
]) {
|
||||
const res = await request.patch(`${WORKER_URL}/api/mails/read-status`, {
|
||||
const res = await request.patch(`${WORKER_URL}/api/mails/state`, {
|
||||
headers: { Authorization: `Bearer ${jwt}` },
|
||||
data,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user