Skip to content

Commit 7ab641b

Browse files
committed
add an e2e example
1 parent d7739b4 commit 7ab641b

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: Exclude Turnstile from E2E tests
3+
pcx_content_type: tutorial
4+
updated: 2025-01-24
5+
difficulty: Intermediate
6+
content_type: 📝 Tutorial
7+
languages:
8+
- TypeScript
9+
tags:
10+
- Testing
11+
- Node.js
12+
sidebar:
13+
order: 6
14+
---
15+
16+
This tutorial explains how to handle Turnstile in your end-to-end (E2E) tests by using Turnstile's dedicated testing keys.
17+
18+
## Overview
19+
20+
When running E2E tests, you often want to bypass or simplify the Turnstile verification process. Cloudflare provides official test credentials that always pass verification, making them perfect for testing environments:
21+
22+
- Test Sitekey: `1x00000000000000000000AA`
23+
- Test Secret Key: `1x0000000000000000000000000000000AA`
24+
25+
For more details, see the [testing documentation](https://developers.cloudflare.com/turnstile/troubleshooting/testing/).
26+
27+
## ⚠️ Important Security Warning
28+
29+
Never use test credentials in production! Always ensure:
30+
1. Test credentials are only used in test environments
31+
2. Production credentials are properly protected
32+
3. Your deployment process prevents test credentials from reaching production
33+
34+
## Implementation Strategy
35+
36+
The key to implementing test-environment detection is identifying test requests server-side. Here's a simple approach:
37+
38+
```typescript
39+
// Detect test environments using IP addresses or headers
40+
function isTestEnvironment(request) {
41+
const testIPs = ['127.0.0.1', '::1'];
42+
const isTestIP = testIPs.includes(request.ip);
43+
const hasTestHeader = request.headers['x-test-environment'] === 'secret-token';
44+
45+
return isTestIP || hasTestHeader;
46+
}
47+
48+
// Use the appropriate credentials based on the environment
49+
function getTurnstileCredentials(request) {
50+
if (isTestEnvironment(request)) {
51+
return {
52+
sitekey: '1x00000000000000000000AA',
53+
secretKey: '1x0000000000000000000000000000000AA'
54+
};
55+
}
56+
57+
return {
58+
sitekey: process.env.TURNSTILE_SITE_KEY,
59+
secretKey: process.env.TURNSTILE_SECRET_KEY
60+
};
61+
}
62+
```
63+
64+
## Server-side Integration
65+
66+
When rendering your page, inject the appropriate sitekey based on the environment:
67+
68+
```typescript
69+
app.get('/your-form', (req, res) => {
70+
const { sitekey } = getTurnstileCredentials(req);
71+
res.render('form', { sitekey });
72+
});
73+
```
74+
75+
## Client-side Integration
76+
77+
Your template can then use the injected sitekey:
78+
79+
```html
80+
<div class="turnstile" data-sitekey="<%= sitekey %>"></div>
81+
```
82+
83+
## Best Practices
84+
85+
1. **Environment Detection**
86+
- Use multiple factors to identify test environments (IP, headers, etc.)
87+
- Keep test environment identifiers secure
88+
- Document your test environment setup
89+
90+
2. **Credential Management**
91+
- Store production credentials securely (e.g., environment variables)
92+
- Never commit credentials to version control
93+
- Use different credentials for each environment
94+
95+
3. **Deployment Safety**
96+
- Add checks to prevent test credentials in production
97+
- Include credential validation in your CI/CD pipeline
98+
- Monitor for accidental test credential usage
99+
100+
## Testing Considerations
101+
102+
- Test credentials will always pass verification
103+
- They're perfect for automated testing environments
104+
- They help avoid rate limiting during testing
105+
- They make tests more predictable and faster
106+
107+
## Example Test Setup
108+
109+
For Cypress or similar E2E testing frameworks:
110+
111+
```typescript
112+
// Set test header for all test requests
113+
beforeEach(() => {
114+
cy.intercept('*', (req) => {
115+
req.headers['x-test-environment'] = 'secret-token';
116+
});
117+
});
118+
119+
// Your test can now interact with the form normally
120+
it('submits form successfully', () => {
121+
cy.visit('/your-form');
122+
cy.get('form').submit();
123+
// Turnstile will automatically pass verification
124+
});
125+
```
126+
127+
## Conclusion
128+
129+
By using Turnstile's test credentials and proper environment detection, you can create reliable E2E tests while maintaining security in production. Remember to always keep test credentials separate from production and implement proper safeguards in your deployment process.

0 commit comments

Comments
 (0)