mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-05-06 20:32:55 +08:00
test: add E2E tests for webhook settings (#871)
* test: add E2E tests for webhook settings Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: verify headers and body fields in webhook roundtrip test Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Dream Hunter <dreamhunter2333@gmail.com>
This commit is contained in:
@@ -16,11 +16,16 @@ ENABLE_AUTO_REPLY = true
|
|||||||
DEFAULT_SEND_BALANCE = 10
|
DEFAULT_SEND_BALANCE = 10
|
||||||
ENABLE_ADDRESS_PASSWORD = true
|
ENABLE_ADDRESS_PASSWORD = true
|
||||||
DISABLE_ADMIN_PASSWORD_CHECK = true
|
DISABLE_ADMIN_PASSWORD_CHECK = true
|
||||||
|
ENABLE_WEBHOOK = true
|
||||||
E2E_TEST_MODE = true
|
E2E_TEST_MODE = true
|
||||||
SMTP_CONFIG = """
|
SMTP_CONFIG = """
|
||||||
{"test.example.com":{"host":"mailpit","port":1025,"secure":false}}
|
{"test.example.com":{"host":"mailpit","port":1025,"secure":false}}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
[[kv_namespaces]]
|
||||||
|
binding = "KV"
|
||||||
|
id = "e2e-test-kv-00000000-0000-0000-0000-000000000000"
|
||||||
|
|
||||||
[[d1_databases]]
|
[[d1_databases]]
|
||||||
binding = "DB"
|
binding = "DB"
|
||||||
database_name = "e2e-temp-email"
|
database_name = "e2e-temp-email"
|
||||||
|
|||||||
88
e2e/tests/api/webhook-settings.spec.ts
Normal file
88
e2e/tests/api/webhook-settings.spec.ts
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import {
|
||||||
|
WORKER_URL,
|
||||||
|
createTestAddress,
|
||||||
|
seedTestMail,
|
||||||
|
deleteAddress,
|
||||||
|
} from '../../fixtures/test-helpers';
|
||||||
|
|
||||||
|
test.describe('Webhook Settings', () => {
|
||||||
|
test('get default webhook settings returns empty/disabled', async ({ request }) => {
|
||||||
|
const { jwt } = await createTestAddress(request, 'webhook-get');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await request.get(`${WORKER_URL}/api/webhook/settings`, {
|
||||||
|
headers: { Authorization: `Bearer ${jwt}` },
|
||||||
|
});
|
||||||
|
expect(res.ok()).toBe(true);
|
||||||
|
const settings = await res.json();
|
||||||
|
expect(settings.enabled).toBeFalsy();
|
||||||
|
expect(settings.url).toBe('');
|
||||||
|
} finally {
|
||||||
|
await deleteAddress(request, jwt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('save and retrieve webhook settings', async ({ request }) => {
|
||||||
|
const { jwt } = await createTestAddress(request, 'webhook-save');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Save webhook settings
|
||||||
|
const saveRes = await request.post(`${WORKER_URL}/api/webhook/settings`, {
|
||||||
|
headers: { Authorization: `Bearer ${jwt}` },
|
||||||
|
data: {
|
||||||
|
enabled: true,
|
||||||
|
url: 'https://example.com/webhook',
|
||||||
|
method: 'POST',
|
||||||
|
headers: JSON.stringify({ 'Content-Type': 'application/json' }),
|
||||||
|
body: JSON.stringify({ from: '${from}', subject: '${subject}' }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(saveRes.ok()).toBe(true);
|
||||||
|
const saveBody = await saveRes.json();
|
||||||
|
expect(saveBody.success).toBe(true);
|
||||||
|
|
||||||
|
// Retrieve and verify
|
||||||
|
const getRes = await request.get(`${WORKER_URL}/api/webhook/settings`, {
|
||||||
|
headers: { Authorization: `Bearer ${jwt}` },
|
||||||
|
});
|
||||||
|
expect(getRes.ok()).toBe(true);
|
||||||
|
const settings = await getRes.json();
|
||||||
|
expect(settings.enabled).toBe(true);
|
||||||
|
expect(settings.url).toBe('https://example.com/webhook');
|
||||||
|
expect(settings.method).toBe('POST');
|
||||||
|
expect(settings.headers).toBe(JSON.stringify({ 'Content-Type': 'application/json' }));
|
||||||
|
expect(settings.body).toBe(JSON.stringify({ from: '${from}', subject: '${subject}' }));
|
||||||
|
} finally {
|
||||||
|
await deleteAddress(request, jwt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('test webhook with unreachable URL returns error', async ({ request }) => {
|
||||||
|
const { jwt, address } = await createTestAddress(request, 'webhook-fail');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Seed a mail so the test endpoint has raw data
|
||||||
|
await seedTestMail(request, address, {
|
||||||
|
subject: 'Webhook Fail Test',
|
||||||
|
from: 'sender@test.example.com',
|
||||||
|
text: 'This webhook should fail',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test webhook with unreachable URL — expect non-2xx response
|
||||||
|
const testRes = await request.post(`${WORKER_URL}/api/webhook/test`, {
|
||||||
|
headers: { Authorization: `Bearer ${jwt}` },
|
||||||
|
data: {
|
||||||
|
enabled: true,
|
||||||
|
url: 'http://unreachable.invalid/webhook',
|
||||||
|
method: 'POST',
|
||||||
|
headers: JSON.stringify({ 'Content-Type': 'application/json' }),
|
||||||
|
body: JSON.stringify({ from: '${from}' }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(testRes.ok()).toBe(false);
|
||||||
|
} finally {
|
||||||
|
await deleteAddress(request, jwt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user