integration.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Integration tests that hit Cloudflare's real siteverify endpoint using the
  3. * documented test sitekeys + test secrets.
  4. *
  5. * Test keys reference:
  6. * https://developers.cloudflare.com/turnstile/troubleshooting/testing/
  7. *
  8. * Test secrets:
  9. * 1x0000000000000000000000000000000AA — always passes
  10. * 2x0000000000000000000000000000000AA — always fails
  11. * 3x0000000000000000000000000000000AA — already-spent token (timeout-or-duplicate)
  12. *
  13. * Test sitekeys:
  14. * 1x00000000000000000000AA — visible, always passes
  15. * 2x00000000000000000000AB — visible, always blocks
  16. * 1x00000000000000000000BB — invisible, always passes
  17. * 2x00000000000000000000BB — invisible, always blocks
  18. * 3x00000000000000000000FF — forces an interactive challenge
  19. *
  20. * These tests make real network calls. Run with `npm run test:integration`.
  21. */
  22. import { describe, expect, it } from 'vitest';
  23. import worker from '../src/index.js';
  24. import type { Env } from '../src/types.js';
  25. const PASSING_SECRET = '1x0000000000000000000000000000000AA';
  26. const FAILING_SECRET = '2x0000000000000000000000000000000AA';
  27. const SPENT_TOKEN_SECRET = '3x0000000000000000000000000000000AA';
  28. // The widget produces some opaque token string. For test secrets the actual
  29. // content doesn't matter; siteverify decides based on the secret.
  30. const FAKE_TOKEN = 'XXXX.DUMMY.TOKEN.XXXX';
  31. function envWith(secret: string): Env {
  32. return {
  33. TURNSTILE_SECRET_KEY: secret,
  34. ALLOWED_ORIGIN: '*',
  35. };
  36. }
  37. describe('integration: real siteverify with test secrets', () => {
  38. it('passing-secret returns success=true', async () => {
  39. const res = await worker.fetch(
  40. new Request('https://w/', {
  41. method: 'POST',
  42. headers: { 'Content-Type': 'application/json' },
  43. body: JSON.stringify({ token: FAKE_TOKEN }),
  44. }),
  45. envWith(PASSING_SECRET),
  46. );
  47. expect(res.status).toBe(200);
  48. const body = (await res.json()) as { success: boolean; _worker?: { duration_ms: number } };
  49. expect(body.success).toBe(true);
  50. expect(body._worker?.duration_ms).toBeGreaterThan(0);
  51. }, 15_000);
  52. it('failing-secret returns success=false', async () => {
  53. const res = await worker.fetch(
  54. new Request('https://w/', {
  55. method: 'POST',
  56. headers: { 'Content-Type': 'application/json' },
  57. body: JSON.stringify({ token: FAKE_TOKEN }),
  58. }),
  59. envWith(FAILING_SECRET),
  60. );
  61. expect(res.status).toBe(200);
  62. const body = (await res.json()) as { success: boolean; 'error-codes': string[] };
  63. expect(body.success).toBe(false);
  64. expect(body['error-codes'].length).toBeGreaterThan(0);
  65. }, 15_000);
  66. it('spent-token secret returns timeout-or-duplicate', async () => {
  67. const res = await worker.fetch(
  68. new Request('https://w/', {
  69. method: 'POST',
  70. headers: { 'Content-Type': 'application/json' },
  71. body: JSON.stringify({ token: FAKE_TOKEN }),
  72. }),
  73. envWith(SPENT_TOKEN_SECRET),
  74. );
  75. expect(res.status).toBe(200);
  76. const body = (await res.json()) as { success: boolean; 'error-codes': string[] };
  77. expect(body.success).toBe(false);
  78. expect(body['error-codes']).toContain('timeout-or-duplicate');
  79. }, 15_000);
  80. });