File size: 5,271 Bytes
32ae427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Verification script to test user profile fields
"""
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from app.models.user import User
from datetime import datetime

def test_user_creation():
    """Test that User.create_document includes all new fields"""
    print("Testing User.create_document()...")
    
    user_doc = User.create_document(
        first_name="Test",
        last_name="User",
        email="test@example.com",
        phone=None,
        hashed_password="hashed_password",
        role="renter"
    )
    
    # Check new fields exist
    required_fields = ["reviews_count", "location", "languages", "isVerified", "createdAt"]
    
    for field in required_fields:
        if field in user_doc:
            print(f"βœ“ Field '{field}' exists with value: {user_doc[field]}")
        else:
            print(f"βœ— Field '{field}' is MISSING!")
            return False
    
    # Verify default values
    assert user_doc["reviews_count"] == 0, "reviews_count should default to 0"
    assert user_doc["location"] is None, "location should default to None"
    assert user_doc["languages"] == [], "languages should default to empty array"
    assert user_doc["isVerified"] is False, "isVerified should default to False"
    assert isinstance(user_doc["createdAt"], datetime), "createdAt should be a datetime"
    
    print("\nβœ“ All fields created correctly with proper defaults!\n")
    return True

def test_user_format_response():
    """Test that User.format_response includes all new fields"""
    print("Testing User.format_response()...")
    
    # Create a mock user document
    from bson import ObjectId
    mock_user = {
        "_id": ObjectId(),
        "firstName": "Test",
        "lastName": "User",
        "email": "test@example.com",
        "phone": None,
        "role": "renter",
        "subscriptionType": "free",
        "subscriptionExpiresAt": None,
        "isEmailVerified": True,
        "isPhoneVerified": False,
        "isActive": True,
        "totalListings": 0,
        "profilePicture": None,
        "bio": None,
        "location": "New York",
        "languages": ["English", "Spanish"],
        "isVerified": True,
        "reviews_count": 5,
        "lastLogin": None,
        "createdAt": datetime.utcnow(),
        "updatedAt": datetime.utcnow(),
    }
    
    formatted = User.format_response(mock_user)
    
    # Check new fields are in response
    required_fields = ["reviews_count", "location", "languages", "isVerified", "createdAt"]
    
    for field in required_fields:
        if field in formatted:
            print(f"βœ“ Field '{field}' in response with value: {formatted[field]}")
        else:
            print(f"βœ— Field '{field}' is MISSING from response!")
            return False
    
    # Verify values
    assert formatted["reviews_count"] == 5, "reviews_count should be 5"
    assert formatted["location"] == "New York", "location should be 'New York'"
    assert formatted["languages"] == ["English", "Spanish"], "languages should match"
    assert formatted["isVerified"] is True, "isVerified should be True"
    
    print("\nβœ“ All fields formatted correctly in response!\n")
    return True

def test_user_format_response_with_missing_fields():
    """Test that User.format_response handles missing fields with defaults"""
    print("Testing User.format_response() with missing fields...")
    
    from bson import ObjectId
    # Create a mock user document without new fields (old user)
    mock_old_user = {
        "_id": ObjectId(),
        "firstName": "Old",
        "lastName": "User",
        "email": "old@example.com",
        "phone": None,
        "role": "landlord",
        "subscriptionType": "free",
        "isEmailVerified": True,
        "isPhoneVerified": False,
        "isActive": True,
        "totalListings": 3,
        "createdAt": datetime.utcnow(),
        "updatedAt": datetime.utcnow(),
    }
    
    formatted = User.format_response(mock_old_user)
    
    # Check that defaults are applied
    assert formatted["reviews_count"] == 0, "reviews_count should default to 0"
    assert formatted["location"] is None, "location should default to None"
    assert formatted["languages"] == [], "languages should default to empty array"
    assert formatted["isVerified"] is False, "isVerified should default to False"
    
    print("βœ“ Missing fields handled correctly with defaults!\n")
    return True

if __name__ == "__main__":
    print("=" * 60)
    print("USER PROFILE FIELDS VERIFICATION")
    print("=" * 60)
    print()
    
    try:
        success = True
        success = test_user_creation() and success
        success = test_user_format_response() and success
        success = test_user_format_response_with_missing_fields() and success
        
        if success:
            print("=" * 60)
            print("βœ“ ALL TESTS PASSED!")
            print("=" * 60)
            sys.exit(0)
        else:
            print("=" * 60)
            print("βœ— SOME TESTS FAILED!")
            print("=" * 60)
            sys.exit(1)
    except Exception as e:
        print(f"\nβœ— ERROR: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)