|
| 1 | +""" |
| 2 | +Test script for Kissflow API integration |
| 3 | +This script helps verify that the Kissflow API integration is working correctly |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +from datetime import datetime |
| 9 | +from random import Random |
| 10 | +from dotenv import load_dotenv |
| 11 | + |
| 12 | +# Load environment variables |
| 13 | +load_dotenv() |
| 14 | + |
| 15 | +# Add the current directory to the Python path |
| 16 | +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 17 | + |
| 18 | +# Import the functions we want to test |
| 19 | +from server import ( |
| 20 | + get_identifier, |
| 21 | + find_aog_item_by_grant_id, |
| 22 | + update_aog_kyc_comments, |
| 23 | + send_identifier_to_kissflow |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def test_environment_variables(): |
| 28 | + """Test that all required Kissflow environment variables are set""" |
| 29 | + print("Testing environment variables...") |
| 30 | + |
| 31 | + required_vars = ['KISSFLOW_API_KEY', 'KISSFLOW_ACCOUNT_ID', 'KISSFLOW_PROCESS_ID'] |
| 32 | + missing_vars = [] |
| 33 | + |
| 34 | + for var in required_vars: |
| 35 | + value = os.getenv(var) |
| 36 | + if value: |
| 37 | + # Mask the value for security |
| 38 | + masked_value = value[:4] + '...' + value[-4:] if len(value) > 8 else '***' |
| 39 | + print(f"✓ {var}: {masked_value}") |
| 40 | + else: |
| 41 | + print(f"✗ {var}: NOT SET") |
| 42 | + missing_vars.append(var) |
| 43 | + |
| 44 | + if missing_vars: |
| 45 | + print(f"\nERROR: Missing environment variables: {', '.join(missing_vars)}") |
| 46 | + print("Please set these variables in your .env file") |
| 47 | + return False |
| 48 | + |
| 49 | + print("\nAll environment variables are set!") |
| 50 | + return True |
| 51 | + |
| 52 | + |
| 53 | +def test_identifier_generation(): |
| 54 | + """Test the identifier generation function""" |
| 55 | + print("\nTesting identifier generation...") |
| 56 | + |
| 57 | + # Test with default parameters |
| 58 | + identifier1 = get_identifier('legal') |
| 59 | + print(f"Generated identifier: {identifier1}") |
| 60 | + |
| 61 | + # Test with custom parameters |
| 62 | + custom_date = datetime(2025, 1, 15, 14, 30, 45) |
| 63 | + identifier2 = get_identifier('legal', now=custom_date, randint=1234) |
| 64 | + expected = 'legal:2025:01:15:14:30:45:1234' |
| 65 | + |
| 66 | + if identifier2 == expected: |
| 67 | + print(f"✓ Custom identifier matches expected: {identifier2}") |
| 68 | + else: |
| 69 | + print(f"✗ Custom identifier mismatch!") |
| 70 | + print(f" Expected: {expected}") |
| 71 | + print(f" Got: {identifier2}") |
| 72 | + |
| 73 | + return True |
| 74 | + |
| 75 | + |
| 76 | +def test_find_aog_item(grant_id): |
| 77 | + """Test finding an AOG item by Grant ID""" |
| 78 | + print(f"\nTesting AOG item lookup for Grant ID: {grant_id}") |
| 79 | + |
| 80 | + try: |
| 81 | + item_id = find_aog_item_by_grant_id(grant_id) |
| 82 | + |
| 83 | + if item_id: |
| 84 | + print(f"✓ Found AOG item with ID: {item_id}") |
| 85 | + return item_id |
| 86 | + else: |
| 87 | + print(f"✗ No AOG item found for Grant ID: {grant_id}") |
| 88 | + return None |
| 89 | + |
| 90 | + except Exception as e: |
| 91 | + print(f"✗ Error finding AOG item: {str(e)}") |
| 92 | + return None |
| 93 | + |
| 94 | + |
| 95 | +def test_update_kyc_comments(item_id, identifier): |
| 96 | + """Test updating the KYC_Comments field""" |
| 97 | + print(f"\nTesting KYC_Comments update...") |
| 98 | + print(f"Item ID: {item_id}") |
| 99 | + print(f"Identifier: {identifier}") |
| 100 | + |
| 101 | + try: |
| 102 | + success = update_aog_kyc_comments(item_id, identifier) |
| 103 | + |
| 104 | + if success: |
| 105 | + print("✓ Successfully updated KYC_Comments field") |
| 106 | + return True |
| 107 | + else: |
| 108 | + print("✗ Failed to update KYC_Comments field") |
| 109 | + return False |
| 110 | + |
| 111 | + except Exception as e: |
| 112 | + print(f"✗ Error updating KYC_Comments: {str(e)}") |
| 113 | + return False |
| 114 | + |
| 115 | + |
| 116 | +def test_full_integration(grant_id): |
| 117 | + """Test the full integration flow""" |
| 118 | + print(f"\nTesting full integration flow for Grant ID: {grant_id}") |
| 119 | + |
| 120 | + # Generate a test identifier |
| 121 | + identifier = get_identifier('legal') |
| 122 | + print(f"Generated identifier: {identifier}") |
| 123 | + |
| 124 | + try: |
| 125 | + success = send_identifier_to_kissflow(grant_id, identifier) |
| 126 | + |
| 127 | + if success: |
| 128 | + print("✓ Full integration test passed!") |
| 129 | + print(f" - Grant ID: {grant_id}") |
| 130 | + print(f" - Identifier: {identifier}") |
| 131 | + print(" - Successfully updated in Kissflow") |
| 132 | + return True |
| 133 | + else: |
| 134 | + print("✗ Full integration test failed") |
| 135 | + return False |
| 136 | + |
| 137 | + except Exception as e: |
| 138 | + print(f"✗ Error in full integration: {str(e)}") |
| 139 | + return False |
| 140 | + |
| 141 | + |
| 142 | +def main(): |
| 143 | + """Main test function""" |
| 144 | + print("=" * 60) |
| 145 | + print("Kissflow Integration Test Suite") |
| 146 | + print("=" * 60) |
| 147 | + |
| 148 | + # Test 1: Environment variables |
| 149 | + if not test_environment_variables(): |
| 150 | + print("\nCannot proceed without proper environment variables.") |
| 151 | + return |
| 152 | + |
| 153 | + # Test 2: Identifier generation |
| 154 | + test_identifier_generation() |
| 155 | + |
| 156 | + # Get Grant ID for testing |
| 157 | + print("\n" + "-" * 60) |
| 158 | + grant_id = input("Enter a Grant ID to test (or press Enter to skip API tests): ").strip() |
| 159 | + |
| 160 | + if not grant_id: |
| 161 | + print("Skipping API tests.") |
| 162 | + return |
| 163 | + |
| 164 | + # Test 3: Find AOG item |
| 165 | + item_id = test_find_aog_item(grant_id) |
| 166 | + |
| 167 | + if item_id: |
| 168 | + # Test 4: Update KYC Comments |
| 169 | + test_identifier = f"TEST:{get_identifier('legal')}" |
| 170 | + test_update_kyc_comments(item_id, test_identifier) |
| 171 | + |
| 172 | + # Test 5: Full integration |
| 173 | + print("\n" + "-" * 60) |
| 174 | + test_full_integration(grant_id) |
| 175 | + |
| 176 | + print("\n" + "=" * 60) |
| 177 | + print("Test suite completed!") |
| 178 | + print("=" * 60) |
| 179 | + |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + main() |
0 commit comments