File size: 9,121 Bytes
c024705
 
 
 
 
 
 
 
 
 
 
eeacc46
c024705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python3
"""
Complete Admin Dashboard Flow Test
Tests the entire professional management workflow
"""

import requests
import json
import time

# Configuration
API_BASE_URL = "https://prodevroger-ishingiro.hf.space"
ADMIN_EMAIL = "eliasfeza@gmail.com"
ADMIN_PASSWORD = "EliasFeza@12301"

def test_admin_login():
    """Test admin login"""
    print("πŸ” Testing admin login...")
    
    try:
        response = requests.post(f"{API_BASE_URL}/admin/login", json={
            "email": ADMIN_EMAIL,
            "password": ADMIN_PASSWORD
        })
        
        if response.status_code == 200:
            data = response.json()
            if data.get('success'):
                print("βœ… Admin login successful")
                return data.get('token')
            else:
                print(f"❌ Admin login failed: {data.get('error')}")
                return None
        else:
            print(f"❌ Admin login failed: {response.status_code}")
            return None
            
    except Exception as e:
        print(f"❌ Admin login error: {e}")
        return None

def test_add_professional():
    """Test adding a new professional"""
    print("\nβž• Testing add professional...")
    
    professional_data = {
        "username": "test_add_professional",
        "password": "password123",
        "first_name": "Test",
        "last_name": "Professional",
        "email": "test.professional@example.com",
        "phone": "+250788123456",
        "specialization": "counselor",
        "expertise_areas": ["depression", "anxiety"],
        "experience_years": 5,
        "district": "Gasabo",
        "consultation_fee": 50000,
        "bio": "Test professional for add functionality",
        "languages": ["english"],
        "qualifications": [],
        "availability_schedule": {}
    }
    
    try:
        response = requests.post(f"{API_BASE_URL}/admin/professionals", json=professional_data)
        
        if response.status_code == 200:
            data = response.json()
            if data.get('success'):
                print("βœ… Add professional successful")
                return data.get('professional', {}).get('id')
            else:
                print(f"❌ Add professional failed: {data.get('error')}")
                return None
        else:
            print(f"❌ Add professional failed: {response.status_code}")
            print(f"Response: {response.text}")
            return None
            
    except Exception as e:
        print(f"❌ Add professional error: {e}")
        return None

def test_edit_professional(professional_id):
    """Test editing a professional"""
    print(f"\n✏️ Testing edit professional {professional_id}...")
    
    update_data = {
        "first_name": "Updated",
        "last_name": "Professional",
        "email": "updated.professional@example.com",
        "phone": "+250788654321",
        "specialization": "psychologist",
        "expertise_areas": ["ptsd", "trauma"],
        "experience_years": 7,
        "district": "Kicukiro",
        "consultation_fee": 75000,
        "bio": "Updated professional for testing"
    }
    
    try:
        response = requests.put(f"{API_BASE_URL}/admin/professionals/{professional_id}", json=update_data)
        
        if response.status_code == 200:
            data = response.json()
            if data.get('success'):
                print("βœ… Edit professional successful")
                return True
            else:
                print(f"❌ Edit professional failed: {data.get('error')}")
                return False
        else:
            print(f"❌ Edit professional failed: {response.status_code}")
            return False
            
    except Exception as e:
        print(f"❌ Edit professional error: {e}")
        return False

def test_get_professionals():
    """Test getting all professionals"""
    print("\nπŸ“‹ Testing get professionals...")
    
    try:
        response = requests.get(f"{API_BASE_URL}/admin/professionals")
        
        if response.status_code == 200:
            data = response.json()
            if data.get('professionals'):
                print(f"βœ… Get professionals successful - found {len(data['professionals'])} professionals")
                return data.get('professionals')
            else:
                print("❌ Get professionals failed - no professionals found")
                return []
        else:
            print(f"❌ Get professionals failed: {response.status_code}")
            return []
            
    except Exception as e:
        print(f"❌ Get professionals error: {e}")
        return []

def test_toggle_professional_status(professional_id):
    """Test toggling professional status"""
    print(f"\nπŸ”„ Testing toggle professional status {professional_id}...")
    
    try:
        response = requests.post(f"{API_BASE_URL}/admin/professionals/{professional_id}/status", json={
            "is_active": False
        })
        
        if response.status_code == 200:
            data = response.json()
            if data.get('success'):
                print("βœ… Toggle professional status successful")
                return True
            else:
                print(f"❌ Toggle professional status failed: {data.get('error')}")
                return False
        else:
            print(f"❌ Toggle professional status failed: {response.status_code}")
            return False
            
    except Exception as e:
        print(f"❌ Toggle professional status error: {e}")
        return False

def test_delete_professional(professional_id):
    """Test deleting a professional"""
    print(f"\nπŸ—‘οΈ Testing delete professional {professional_id}...")
    
    try:
        response = requests.delete(f"{API_BASE_URL}/admin/professionals/{professional_id}")
        
        if response.status_code == 200:
            data = response.json()
            if data.get('success'):
                print("βœ… Delete professional successful")
                return True
            else:
                print(f"❌ Delete professional failed: {data.get('error')}")
                return False
        else:
            print(f"❌ Delete professional failed: {response.status_code}")
            return False
            
    except Exception as e:
        print(f"❌ Delete professional error: {e}")
        return False

def cleanup_test_data():
    """Clean up test data"""
    print("\n🧹 Cleaning up test data...")
    
    try:
        # Get all professionals
        response = requests.get(f"{API_BASE_URL}/admin/professionals")
        if response.status_code == 200:
            data = response.json()
            professionals = data.get('professionals', [])
            
            for prof in professionals:
                if prof.get('username') == 'test_add_professional':
                    delete_response = requests.delete(f"{API_BASE_URL}/admin/professionals/{prof['id']}")
                    if delete_response.status_code == 200:
                        print(f"βœ… Cleaned up {prof['username']}")
                    else:
                        print(f"❌ Failed to clean up {prof['username']}")
                        
    except Exception as e:
        print(f"❌ Cleanup error: {e}")

def main():
    """Run complete admin dashboard flow test"""
    print("πŸ§ͺ Testing Complete Admin Dashboard Flow")
    print("=" * 60)
    
    # Test admin login
    token = test_admin_login()
    if not token:
        print("❌ Cannot proceed without admin authentication")
        return
    
    # Test get professionals (should work even if empty)
    professionals = test_get_professionals()
    
    # Test add professional
    professional_id = test_add_professional()
    if not professional_id:
        print("❌ Cannot proceed without successful professional creation")
        return
    
    # Test edit professional
    edit_success = test_edit_professional(professional_id)
    
    # Test toggle status
    toggle_success = test_toggle_professional_status(professional_id)
    
    # Test delete professional
    delete_success = test_delete_professional(professional_id)
    
    # Cleanup
    cleanup_test_data()
    
    print("\n" + "=" * 60)
    print("πŸŽ‰ Admin Dashboard Flow Test Complete!")
    print("\nπŸ“‹ Results:")
    print(f"βœ… Login: {'PASS' if token else 'FAIL'}")
    print(f"βœ… Get Professionals: {'PASS' if professionals is not None else 'FAIL'}")
    print(f"βœ… Add Professional: {'PASS' if professional_id else 'FAIL'}")
    print(f"βœ… Edit Professional: {'PASS' if edit_success else 'FAIL'}")
    print(f"βœ… Toggle Status: {'PASS' if toggle_success else 'FAIL'}")
    print(f"βœ… Delete Professional: {'PASS' if delete_success else 'FAIL'}")
    
    if all([token, professional_id, edit_success, toggle_success, delete_success]):
        print("\nπŸš€ All tests passed! The admin dashboard backend is working perfectly!")
    else:
        print("\n⚠️ Some tests failed. Check the backend API endpoints.")

if __name__ == "__main__":
    main()