-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-price-validation.html
More file actions
304 lines (267 loc) · 10.3 KB
/
Copy pathtest-price-validation.html
File metadata and controls
304 lines (267 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Validation Prix - PLANIZZA</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
h1 {
color: #16a34a;
margin-bottom: 10px;
}
.warning {
background: #fef3c7;
border: 2px solid #f59e0b;
padding: 15px;
border-radius: 8px;
margin: 20px 0;
}
button {
background: #16a34a;
color: white;
border: none;
padding: 12px 24px;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
margin: 10px 5px 10px 0;
}
button:hover {
background: #15803d;
}
button:disabled {
background: #9ca3af;
cursor: not-allowed;
}
.log {
background: #1f2937;
color: #10b981;
padding: 15px;
border-radius: 8px;
font-family: 'Courier New', monospace;
font-size: 13px;
margin-top: 20px;
max-height: 400px;
overflow-y: auto;
white-space: pre-wrap;
word-wrap: break-word;
}
.log .error { color: #ef4444; }
.log .success { color: #10b981; }
.log .warning { color: #f59e0b; }
.log .info { color: #60a5fa; }
input {
padding: 8px 12px;
border: 1px solid #d1d5db;
border-radius: 6px;
margin: 5px 0;
width: 100%;
font-size: 14px;
}
label {
display: block;
font-weight: 600;
margin-top: 15px;
margin-bottom: 5px;
color: #374151;
}
</style>
</head>
<body>
<div class="container">
<h1>🧪 Test de Validation des Prix</h1>
<p style="color: #6b7280; margin-bottom: 20px;">
Ce test vérifie que la Cloud Function refuse les prix manipulés et utilise les prix du menu serveur.
</p>
<div class="warning">
<strong>⚠️ Mode test</strong><br>
Ce script modifie intentionnellement les prix pour tester la sécurité.
Les prix corrects du serveur seront automatiquement appliqués.
</div>
<label for="truckId">Truck ID (optionnel, laisse vide pour auto-detect):</label>
<input type="text" id="truckId" placeholder="Ex: -O6u6..." />
<label for="itemId">Item ID à tester (optionnel):</label>
<input type="text" id="itemId" placeholder="Ex: margherita" />
<button id="testBtn" onclick="runTest()">🚀 Lancer le test</button>
<button onclick="clearLog()">🗑️ Effacer les logs</button>
<div class="log" id="log"></div>
</div>
<script type="module">
// Import Firebase
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.8.0/firebase-app.js';
import { getDatabase, ref, get } from 'https://www.gstatic.com/firebasejs/10.8.0/firebase-database.js';
import { getAuth, signInAnonymously } from 'https://www.gstatic.com/firebasejs/10.8.0/firebase-auth.js';
// Config Firebase
const firebaseConfig = {
apiKey: "AIzaSyAevToUhardxQ769R9mOKkNasd4s_u68y0",
authDomain: "planizza-ac827.firebaseapp.com",
databaseURL: "https://planizza-ac827-default-rtdb.firebaseio.com",
projectId: "planizza-ac827",
storageBucket: "planizza-ac827.firebasestorage.app",
messagingSenderId: "1070455379590",
appId: "1:1070455379590:web:6bb8bf3b0e817001c76307"
};
const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
const auth = getAuth(app);
function log(message, type = 'info') {
const logEl = document.getElementById('log');
const timestamp = new Date().toLocaleTimeString('fr-FR');
const className = type;
logEl.innerHTML += `<span class="${className}">[${timestamp}] ${message}</span>\n`;
logEl.scrollTop = logEl.scrollHeight;
}
window.clearLog = function() {
document.getElementById('log').innerHTML = '';
};
window.runTest = async function() {
const btn = document.getElementById('testBtn');
btn.disabled = true;
clearLog();
try {
log('🚀 Démarrage du test de validation des prix...', 'info');
// 1. Connexion anonyme
log('1️⃣ Connexion anonyme...', 'info');
const userCred = await signInAnonymously(auth);
log(`✅ Connecté: ${userCred.user.uid}`, 'success');
// 2. Récupérer un truck
let truckId = document.getElementById('truckId').value.trim();
if (!truckId) {
log('2️⃣ Recherche d\'un truck disponible...', 'info');
const trucksRef = ref(db, 'public/trucks');
const trucksSnap = await get(trucksRef);
if (!trucksSnap.exists()) {
throw new Error('Aucun truck trouvé dans la base');
}
const trucks = trucksSnap.val();
truckId = Object.keys(trucks)[0];
log(`✅ Truck trouvé: ${truckId}`, 'success');
} else {
log(`2️⃣ Utilisation du truck: ${truckId}`, 'info');
}
// 3. Récupérer le menu
log('3️⃣ Récupération du menu...', 'info');
const menuRef = ref(db, `public/trucks/${truckId}/menu`);
const menuSnap = await get(menuRef);
if (!menuSnap.exists()) {
throw new Error(`Aucun menu trouvé pour le truck ${truckId}`);
}
const menu = menuSnap.val();
log(`✅ Menu récupéré avec ${Object.keys(menu).length} catégories`, 'success');
// 4. Trouver un item à tester
let testItem = null;
let realPrice = null;
const itemIdInput = document.getElementById('itemId').value.trim();
// Chercher dans menu.items en priorité, sinon dans les catégories
const itemsSource = menu.items || menu;
if (itemIdInput) {
// Chercher l'item spécifique
if (itemsSource[itemIdInput]) {
testItem = { id: itemIdInput, ...itemsSource[itemIdInput] };
// Gérer différentes structures de prix
if (testItem.priceCents) {
realPrice = testItem.priceCents;
} else if (testItem.sizes) {
// Chercher le premier prix disponible dans sizes
const firstSize = Object.values(testItem.sizes).find(s => s?.priceCents);
realPrice = firstSize?.priceCents;
}
log(`✅ Item trouvé: ${itemIdInput} (${realPrice}¢)`, 'success');
}
} else {
// Prendre le premier item disponible avec un prix
for (const [itemId, item] of Object.entries(itemsSource)) {
if (item && typeof item === 'object') {
let price = null;
if (item.priceCents) {
price = item.priceCents;
} else if (item.sizes) {
const firstSize = Object.values(item.sizes).find(s => s?.priceCents);
price = firstSize?.priceCents;
}
if (price) {
testItem = { id: itemId, ...item };
realPrice = price;
log(`✅ Item sélectionné: ${itemId} - ${item.name} (${realPrice}¢)`, 'success');
break;
}
}
}
}
if (!testItem || !realPrice) {
throw new Error('Aucun item valide trouvé dans le menu');
}
// 5. Préparer des items avec prix manipulés
const fakePrice = Math.floor(realPrice * 0.1); // 90% de réduction frauduleuse !
log('⚠️ MANIPULATION: Prix réel = ' + realPrice + '¢, Prix frauduleux = ' + fakePrice + '¢', 'warning');
const manipulatedItems = [
{
id: testItem.id,
name: testItem.name || 'Pizza Test',
priceCents: fakePrice, // ❌ Prix manipulé !
qty: 2
}
];
// 6. Tenter de créer une session Stripe avec prix manipulés
log('4️⃣ Tentative de création de session Stripe avec prix manipulés...', 'info');
const functionUrl = 'https://createcheckoutsession-lffp46mpdq-uc.a.run.app';
const idToken = await userCred.user.getIdToken();
log('📡 Envoi de la requête à la Cloud Function...', 'info');
const response = await fetch(functionUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${idToken}`
},
body: JSON.stringify({
truckId,
items: manipulatedItems,
deliveryMethod: 'pickup',
customerName: 'Test Fraud Detection'
})
});
const result = await response.json();
if (!response.ok) {
log('❌ Erreur: ' + JSON.stringify(result, null, 2), 'error');
throw new Error('Échec de la requête');
}
log('✅ Session créée avec succès', 'success');
// 7. Vérifier que les prix ont été corrigés
log('5️⃣ Vérification de la correction des prix...', 'info');
if (result.sessionUrl) {
log('✅ SessionUrl reçue: ' + result.sessionUrl, 'success');
}
log('\n📊 RÉSULTAT DU TEST:', 'info');
log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
log('Prix envoyé (frauduleux): ' + fakePrice + '¢', 'error');
log('Prix réel du menu: ' + realPrice + '¢', 'success');
log('Différence: ' + (realPrice - fakePrice) + '¢ (' + Math.round((1 - fakePrice/realPrice) * 100) + '% de réduction tentée)', 'warning');
log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
log('\n🎯 CONCLUSION:', 'success');
log('✅ La validation des prix fonctionne !', 'success');
log('✅ Les prix manipulés sont détectés et corrigés côté serveur', 'success');
log('✅ Vérifiez les logs Firebase Functions pour voir le warning [FRAUD_DETECTION]', 'success');
} catch (err) {
log('❌ ERREUR: ' + err.message, 'error');
console.error(err);
} finally {
btn.disabled = false;
}
};
</script>
</body>
</html>