171 lines
4.9 KiB
Python
171 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Test script for organization member support via GraphQL API."""
|
||
|
|
|
||
|
|
import httpx
|
||
|
|
import json
|
||
|
|
|
||
|
|
API_URL = "http://127.0.0.1:8000/graphql"
|
||
|
|
|
||
|
|
def execute_query(query: str, variables: dict = None):
|
||
|
|
"""Execute a GraphQL query."""
|
||
|
|
payload = {"query": query}
|
||
|
|
if variables:
|
||
|
|
payload["variables"] = variables
|
||
|
|
|
||
|
|
response = httpx.post(API_URL, json=payload)
|
||
|
|
response.raise_for_status()
|
||
|
|
result = response.json()
|
||
|
|
|
||
|
|
if "errors" in result:
|
||
|
|
print(f"GraphQL Errors: {result['errors']}")
|
||
|
|
raise Exception(f"GraphQL query failed: {result['errors']}")
|
||
|
|
|
||
|
|
return result["data"]
|
||
|
|
|
||
|
|
# Test 1: Create individual member
|
||
|
|
print("Test 1: Creating individual member...")
|
||
|
|
create_individual_query = """
|
||
|
|
mutation CreateMember($input: CreateMemberInput!) {
|
||
|
|
createMember(input: $input) {
|
||
|
|
id
|
||
|
|
memberType
|
||
|
|
firstName
|
||
|
|
lastName
|
||
|
|
companyName
|
||
|
|
}
|
||
|
|
}
|
||
|
|
"""
|
||
|
|
individual_result = execute_query(create_individual_query, {
|
||
|
|
"input": {
|
||
|
|
"firstName": "Charlie",
|
||
|
|
"lastName": "Brown",
|
||
|
|
"email": "charlie@example.com"
|
||
|
|
}
|
||
|
|
})
|
||
|
|
print(f"✓ Created individual member: {individual_result['createMember']}")
|
||
|
|
assert individual_result['createMember']['memberType'] == 'INDIVIDUAL'
|
||
|
|
assert individual_result['createMember']['firstName'] == 'Charlie'
|
||
|
|
assert individual_result['createMember']['companyName'] is None
|
||
|
|
print()
|
||
|
|
|
||
|
|
# Test 2: Create organization member
|
||
|
|
print("Test 2: Creating organization member...")
|
||
|
|
create_org_query = """
|
||
|
|
mutation CreateMember($input: CreateMemberInput!) {
|
||
|
|
createMember(input: $input) {
|
||
|
|
id
|
||
|
|
memberType
|
||
|
|
firstName
|
||
|
|
companyName
|
||
|
|
}
|
||
|
|
}
|
||
|
|
"""
|
||
|
|
org_result = execute_query(create_org_query, {
|
||
|
|
"input": {
|
||
|
|
"memberType": "ORGANIZATION",
|
||
|
|
"companyName": "Tech Innovations Inc",
|
||
|
|
"email": "info@techinnovations.com"
|
||
|
|
}
|
||
|
|
})
|
||
|
|
print(f"✓ Created organization member: {org_result['createMember']}")
|
||
|
|
assert org_result['createMember']['memberType'] == 'ORGANIZATION'
|
||
|
|
assert org_result['createMember']['companyName'] == 'Tech Innovations Inc'
|
||
|
|
print()
|
||
|
|
|
||
|
|
# Test 3: Create organization with contact person
|
||
|
|
print("Test 3: Creating organization with contact person...")
|
||
|
|
org_with_contact_result = execute_query(create_org_query, {
|
||
|
|
"input": {
|
||
|
|
"memberType": "ORGANIZATION",
|
||
|
|
"companyName": "Global Solutions Ltd",
|
||
|
|
"firstName": "Jane",
|
||
|
|
"lastName": "Doe",
|
||
|
|
"email": "contact@globalsolutions.com"
|
||
|
|
}
|
||
|
|
})
|
||
|
|
print(f"✓ Created organization with contact: {org_with_contact_result['createMember']}")
|
||
|
|
assert org_with_contact_result['createMember']['memberType'] == 'ORGANIZATION'
|
||
|
|
assert org_with_contact_result['createMember']['companyName'] == 'Global Solutions Ltd'
|
||
|
|
assert org_with_contact_result['createMember']['firstName'] == 'Jane'
|
||
|
|
print()
|
||
|
|
|
||
|
|
# Test 4: List all members
|
||
|
|
print("Test 4: Listing all members...")
|
||
|
|
list_query = """
|
||
|
|
query {
|
||
|
|
members {
|
||
|
|
id
|
||
|
|
memberType
|
||
|
|
firstName
|
||
|
|
lastName
|
||
|
|
companyName
|
||
|
|
}
|
||
|
|
}
|
||
|
|
"""
|
||
|
|
list_result = execute_query(list_query)
|
||
|
|
members = list_result['members']
|
||
|
|
print(f"✓ Found {len(members)} members")
|
||
|
|
print(" Members:")
|
||
|
|
for member in members:
|
||
|
|
if member['memberType'] == 'INDIVIDUAL':
|
||
|
|
print(f" - Individual: {member['firstName']} {member['lastName']}")
|
||
|
|
else:
|
||
|
|
print(f" - Organization: {member['companyName']}")
|
||
|
|
print()
|
||
|
|
|
||
|
|
# Test 5: Verify validation - individual without firstName should fail
|
||
|
|
print("Test 5: Testing validation - individual without firstName (should fail)...")
|
||
|
|
try:
|
||
|
|
execute_query(create_individual_query, {
|
||
|
|
"input": {
|
||
|
|
"memberType": "INDIVIDUAL",
|
||
|
|
"email": "nofirstname@example.com"
|
||
|
|
}
|
||
|
|
})
|
||
|
|
print("✗ Should have failed but didn't!")
|
||
|
|
exit(1)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✓ Correctly rejected: {str(e)[:100]}")
|
||
|
|
print()
|
||
|
|
|
||
|
|
# Test 6: Verify validation - organization without companyName should fail
|
||
|
|
print("Test 6: Testing validation - organization without companyName (should fail)...")
|
||
|
|
try:
|
||
|
|
execute_query(create_org_query, {
|
||
|
|
"input": {
|
||
|
|
"memberType": "ORGANIZATION",
|
||
|
|
"email": "nocompany@example.com"
|
||
|
|
}
|
||
|
|
})
|
||
|
|
print("✗ Should have failed but didn't!")
|
||
|
|
exit(1)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✓ Correctly rejected: {str(e)[:100]}")
|
||
|
|
print()
|
||
|
|
|
||
|
|
# Test 7: Check introspection for MemberType enum
|
||
|
|
print("Test 7: Verifying MemberType enum in schema...")
|
||
|
|
introspection_query = """
|
||
|
|
query {
|
||
|
|
__type(name: "MemberType") {
|
||
|
|
name
|
||
|
|
kind
|
||
|
|
enumValues {
|
||
|
|
name
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
"""
|
||
|
|
introspection_result = execute_query(introspection_query)
|
||
|
|
member_type_enum = introspection_result['__type']
|
||
|
|
print(f"✓ Found MemberType enum: {member_type_enum['name']}")
|
||
|
|
enum_values = [ev['name'] for ev in member_type_enum['enumValues']]
|
||
|
|
print(f" Enum values: {enum_values}")
|
||
|
|
assert 'INDIVIDUAL' in enum_values
|
||
|
|
assert 'ORGANIZATION' in enum_values
|
||
|
|
print()
|
||
|
|
|
||
|
|
print("=" * 50)
|
||
|
|
print("All tests passed! ✓")
|
||
|
|
print("=" * 50)
|