-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_enhanced_registration.py
More file actions
154 lines (135 loc) Β· 6.04 KB
/
Copy pathtest_enhanced_registration.py
File metadata and controls
154 lines (135 loc) Β· 6.04 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
"""
Test script for enhanced registration form
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from app import app
from models import db, User
def test_enhanced_registration():
"""Test the enhanced registration form with profile fields"""
with app.test_client() as client:
with app.app_context():
print("π§ͺ Testing Enhanced Registration Form...")
# Test 1: Basic registration (required fields only)
print("\n1οΈβ£ Testing basic registration...")
response = client.post('/user/register', data={
'username': 'testuser_basic',
'email': 'basic@test.com',
'password': 'TestPass123'
})
if response.status_code in [200, 302]: # Success or redirect
user = User.query.filter_by(username='testuser_basic').first()
if user:
print("β
Basic registration successful")
print(f" Profile completed: {user.profile_completed}")
print(f" Full name: {user.get_full_name()}")
db.session.delete(user)
db.session.commit()
else:
print("β User not created")
return False
else:
print(f"β Registration failed with status {response.status_code}")
return False
# Test 2: Complete registration (all fields)
print("\n2οΈβ£ Testing complete registration...")
response = client.post('/user/register', data={
'username': 'testuser_complete',
'email': 'complete@test.com',
'password': 'TestPass123',
'first_name': 'John',
'last_name': 'Doe',
'phone': '+254701234567',
'address': '123 Test Street',
'city': 'Nairobi',
'state': 'Nairobi County',
'postal_code': '00100',
'country': 'Kenya'
})
if response.status_code in [200, 302]:
user = User.query.filter_by(username='testuser_complete').first()
if user:
print("β
Complete registration successful")
print(f" Profile completed: {user.profile_completed}")
print(f" Full name: {user.get_full_name()}")
print(f" Full address: {user.get_full_address()}")
db.session.delete(user)
db.session.commit()
else:
print("β Complete user not created")
return False
else:
print(f"β Complete registration failed with status {response.status_code}")
return False
# Test 3: Partial registration (some optional fields)
print("\n3οΈβ£ Testing partial registration...")
response = client.post('/user/register', data={
'username': 'testuser_partial',
'email': 'partial@test.com',
'password': 'TestPass123',
'first_name': 'Jane',
'last_name': 'Smith',
'phone': '+254700000000'
# No address fields
})
if response.status_code in [200, 302]:
user = User.query.filter_by(username='testuser_partial').first()
if user:
print("β
Partial registration successful")
print(f" Profile completed: {user.profile_completed}")
print(f" Full name: {user.get_full_name()}")
print(f" Has address: {bool(user.address)}")
db.session.delete(user)
db.session.commit()
else:
print("β Partial user not created")
return False
else:
print(f"β Partial registration failed with status {response.status_code}")
return False
# Test 4: Validation errors
print("\n4οΈβ£ Testing validation errors...")
# Missing required fields
response = client.post('/user/register', data={
'username': '',
'email': 'test@test.com',
'password': 'TestPass123'
})
if response.status_code == 400:
print("β
Missing username validation works")
else:
print(f"β Missing username validation failed: {response.status_code}")
return False
# Invalid email
response = client.post('/user/register', data={
'username': 'testuser',
'email': 'invalid-email',
'password': 'TestPass123'
})
if response.status_code == 400:
print("β
Invalid email validation works")
else:
print(f"β Invalid email validation failed: {response.status_code}")
return False
# Weak password
response = client.post('/user/register', data={
'username': 'testuser',
'email': 'test@test.com',
'password': 'weak'
})
if response.status_code == 400:
print("β
Weak password validation works")
else:
print(f"β Weak password validation failed: {response.status_code}")
return False
print("\nπ All enhanced registration tests passed!")
return True
if __name__ == "__main__":
if test_enhanced_registration():
print("\nβ
Enhanced registration form is working correctly!")
sys.exit(0)
else:
print("\nβ Enhanced registration tests failed!")
sys.exit(1)