|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script de test pour les notifications Slack |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python scripts/test_slack_notification.py |
| 7 | +
|
| 8 | +Configuration: |
| 9 | + Définir SLACK_WEBHOOK_URL et SLACK_NOTIFICATIONS_ENABLED=true dans .env |
| 10 | +""" |
| 11 | + |
| 12 | +import asyncio |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +# Add src to path |
| 17 | +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) |
| 18 | + |
| 19 | +from services.slack import slack_service |
| 20 | +from models.energy_provider import OfferContribution |
| 21 | +from models.user import User |
| 22 | +from unittest.mock import MagicMock |
| 23 | + |
| 24 | + |
| 25 | +async def test_notification(): |
| 26 | + """Test sending a Slack notification""" |
| 27 | + print("Testing Slack notification service...") |
| 28 | + print(f"Enabled: {slack_service.enabled}") |
| 29 | + print(f"Webhook URL configured: {bool(slack_service.webhook_url)}") |
| 30 | + |
| 31 | + if not slack_service.enabled: |
| 32 | + print("\n⚠️ Slack notifications are disabled.") |
| 33 | + print("Set SLACK_NOTIFICATIONS_ENABLED=true in .env to enable.") |
| 34 | + return |
| 35 | + |
| 36 | + if not slack_service.webhook_url: |
| 37 | + print("\n⚠️ Slack webhook URL is not configured.") |
| 38 | + print("Set SLACK_WEBHOOK_URL in .env with your webhook URL.") |
| 39 | + return |
| 40 | + |
| 41 | + # Create mock objects |
| 42 | + print("\nCreating mock contribution and user...") |
| 43 | + |
| 44 | + user = MagicMock(spec=User) |
| 45 | + user.id = "test-user-123" |
| 46 | + |
| 47 | + |
| 48 | + contribution = MagicMock(spec=OfferContribution) |
| 49 | + contribution.id = "test-contrib-456" |
| 50 | + contribution.contribution_type = "NEW_OFFER" |
| 51 | + contribution.offer_name = "Test Offer - EDF Tempo 2025" |
| 52 | + contribution.offer_type = "TEMPO" |
| 53 | + contribution.provider_name = "EDF" |
| 54 | + contribution.power_kva = 6 |
| 55 | + contribution.pricing_data = { |
| 56 | + "subscription_price": 12.50, |
| 57 | + "tempo_blue_hc": 0.12340, |
| 58 | + "tempo_blue_hp": 0.23450, |
| 59 | + "tempo_white_hc": 0.34560, |
| 60 | + "tempo_white_hp": 0.45670, |
| 61 | + "tempo_red_hc": 0.56780, |
| 62 | + "tempo_red_hp": 0.67890, |
| 63 | + } |
| 64 | + contribution.price_sheet_url = "https://example.com/edf-tempo-2025.pdf" |
| 65 | + |
| 66 | + print("\nSending notification to Slack...") |
| 67 | + result = await slack_service.send_contribution_notification(contribution, user) |
| 68 | + |
| 69 | + if result: |
| 70 | + print("✅ Notification sent successfully!") |
| 71 | + else: |
| 72 | + print("❌ Failed to send notification. Check logs for details.") |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + asyncio.run(test_notification()) |
0 commit comments