-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_profile_fixes.py
More file actions
139 lines (120 loc) Β· 5.58 KB
/
Copy pathtest_profile_fixes.py
File metadata and controls
139 lines (120 loc) Β· 5.58 KB
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
#!/usr/bin/env python3
"""
Test script to validate profile page and cart-data fixes
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from app import app
from models import db, User
from flask import session
def test_profile_routes():
"""Test the profile routes we just added"""
with app.test_client() as client:
with app.app_context():
print("π§ͺ Testing Profile Routes and Cart Data...")
# Test 1: Profile page without login (should redirect)
print("\n1οΈβ£ Testing profile access without login...")
response = client.get('/user/profile')
if response.status_code in [302, 401]:
print("β
Profile correctly requires login (redirects)")
else:
print(f"β Expected redirect, got {response.status_code}")
return False
# Test 2: Test cart-data endpoint
print("\n2οΈβ£ Testing cart-data endpoint...")
response = client.get('/cart-data')
if response.status_code == 200:
print("β
Cart-data endpoint accessible")
try:
data = response.get_json()
if 'items' in data:
print("β
Cart-data returns proper JSON structure")
else:
print("β Cart-data missing 'items' key")
return False
except Exception as e:
print(f"β Cart-data JSON parsing error: {e}")
return False
else:
print(f"β Cart-data endpoint failed: {response.status_code}")
return False
# Test 3: Create a test user and test profile access
print("\n3οΈβ£ Testing profile access with login simulation...")
try:
# Create test user
test_user = User(
username="profile_test_user",
email="profile.test@example.com",
password="hashedpassword123",
first_name="Test",
last_name="User",
phone="+254701234567"
)
db.session.add(test_user)
db.session.commit()
# Simulate login by setting session
with client.session_transaction() as sess:
sess['user_id'] = test_user.id
# Test profile page access
response = client.get('/user/profile')
if response.status_code == 200:
print("β
Profile page accessible with login")
# Check if page contains user data
page_content = response.get_data(as_text=True)
if 'profile_test_user' in page_content and 'Test User' in page_content:
print("β
Profile page displays user information")
else:
print("β οΈ Profile page accessible but may not display all user data")
else:
print(f"β Profile page failed with login: {response.status_code}")
return False
# Cleanup
db.session.delete(test_user)
db.session.commit()
except Exception as e:
print(f"β Profile test with login failed: {e}")
return False
# Test 4: Test User model methods
print("\n4οΈβ£ Testing User model enhanced methods...")
try:
test_user = User(
username="method_test_user",
email="method.test@example.com",
password="hashedpassword123"
)
# Test fallback behavior
fallback_name = test_user.get_full_name()
if fallback_name == "method_test_user":
print("β
get_full_name() fallback works correctly")
else:
print(f"β get_full_name() fallback failed: {fallback_name}")
return False
# Test with actual names
test_user.first_name = "Method"
test_user.last_name = "Test"
full_name = test_user.get_full_name()
if full_name == "Method Test":
print("β
get_full_name() with names works correctly")
else:
print(f"β get_full_name() with names failed: {full_name}")
return False
# Test profile completion
incomplete = test_user.has_complete_profile()
if not incomplete:
print("β
has_complete_profile() correctly identifies incomplete profile")
else:
print("β has_complete_profile() should return False for incomplete profile")
return False
except Exception as e:
print(f"β User model method testing failed: {e}")
return False
print("\nπ All profile and cart-data tests passed!")
return True
if __name__ == "__main__":
if test_profile_routes():
print("\nβ
Profile routes and fixes are working correctly!")
sys.exit(0)
else:
print("\nβ Some tests failed!")
sys.exit(1)