#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 測試通知 API """ import requests import json # API 基礎 URL BASE_URL = 'http://127.0.0.1:5000/api/v1' def test_notification_api(): """測試通知 API 端點""" # 首先需要登入獲取 JWT Token # 這裡使用預設的管理員帳號 login_data = { 'username': 'ymirliu', 'password': 'password123' # LDAP 系統預設密碼 } try: print("Testing notification API...") # 登入 print("1. Testing login...") login_response = requests.post(f'{BASE_URL}/auth/login', json=login_data, timeout=10) print(f"Login status: {login_response.status_code}") if login_response.status_code == 200: token = login_response.json()['data']['access_token'] headers = {'Authorization': f'Bearer {token}'} # 測試獲取通知列表 print("2. Testing get notifications...") notifications_response = requests.get(f'{BASE_URL}/notifications', headers=headers, timeout=10) print(f"Get notifications status: {notifications_response.status_code}") if notifications_response.status_code == 200: data = notifications_response.json() print(f"Response: {json.dumps(data, indent=2, ensure_ascii=False)}") else: print(f"Error response: {notifications_response.text}") # 測試創建測試通知 print("3. Testing create test notification...") test_notification_response = requests.post(f'{BASE_URL}/notifications/test', headers=headers, timeout=10) print(f"Create test notification status: {test_notification_response.status_code}") if test_notification_response.status_code == 200: data = test_notification_response.json() print(f"Test notification created: {json.dumps(data, indent=2, ensure_ascii=False)}") else: print(f"Error response: {test_notification_response.text}") # 再次獲取通知列表,應該能看到測試通知 print("4. Testing get notifications again...") notifications_response = requests.get(f'{BASE_URL}/notifications', headers=headers, timeout=10) print(f"Get notifications status: {notifications_response.status_code}") if notifications_response.status_code == 200: data = notifications_response.json() print(f"Updated notifications: {json.dumps(data, indent=2, ensure_ascii=False)}") else: print(f"Error response: {notifications_response.text}") else: print(f"Login failed: {login_response.text}") except requests.exceptions.ConnectionError: print("Error: Could not connect to server. Make sure the Flask app is running on http://127.0.0.1:5000") except Exception as e: print(f"Error testing notification API: {e}") if __name__ == '__main__': test_notification_api()