File size: 29,109 Bytes
97f32ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import requests
import json
import uuid
import sys
import os
import time
from getpass import getpass
from tabulate import tabulate
from colorama import init, Fore, Style

# Initialize colorama
init()

BASE_URL = 'http://localhost:8000/api/'

class APITester:
    def __init__(self, base_url):
        self.base_url = base_url
        self.token = None
        self.headers = {}
        self.user_role = None
        self.username = None
        self.last_response = None
    
    def login(self, username, password):
        """Login to get authentication token"""
        try:
            print(f"{Fore.CYAN}Attempting to login...{Style.RESET_ALL}")
            response = requests.post(
                f"{self.base_url.replace('/api/', '/auth/')}login/",
                data={'username': username, 'password': password},
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 200:
                data = response.json()
                self.token = data.get('token')
                self.headers = {'Authorization': f'Token {self.token}'}
                self.username = username
                
                # Get user info to determine role
                user_info = self.get_user_info()
                if user_info:
                    self.user_role = user_info.get('role')
                    role_display = f"{Fore.GREEN}{self.user_role}{Style.RESET_ALL}"
                    print(f"{Fore.GREEN}βœ… Logged in as {Fore.YELLOW}{username}{Fore.GREEN} (Role: {role_display}){Style.RESET_ALL}")
                    return True
                return False
            else:
                print(f"{Fore.RED}❌ Login failed: {response.text}{Style.RESET_ALL}")
                return False
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server. Is the server running?{Style.RESET_ALL}")
            return False
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return False
        except Exception as e:
            print(f"{Fore.RED}❌ Error during login: {str(e)}{Style.RESET_ALL}")
            return False
            
    def get_user_info(self):
        """Get current user information"""
        try:
            response = requests.get(
                f"{self.base_url.replace('/api/', '/auth/')}me/",
                headers=self.headers,
                timeout=10
            )
            
            if response.status_code == 200:
                return response.json()
            return None
        except Exception:
            return None
    
    def test_paperworks_list(self):
        """Test getting list of paperworks"""
        try:
            print(f"{Fore.CYAN}Fetching paperworks...{Style.RESET_ALL}")
            response = requests.get(
                f"{self.base_url}paperworks/",
                headers=self.headers,
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 200:
                paperworks = response.json()
                print(f"{Fore.GREEN}βœ… Retrieved {len(paperworks)} paperworks{Style.RESET_ALL}")
                
                if paperworks:
                    # Display paperworks in a table
                    table_data = []
                    for pw in paperworks:
                        table_data.append([
                            pw.get('id'),
                            pw.get('title'),
                            pw.get('researcher', {}).get('username'),
                            pw.get('status'),
                            pw.get('assigned_at')
                        ])
                    
                    headers = ["ID", "Title", "Researcher", "Status", "Assigned At"]
                    print(tabulate(table_data, headers=headers, tablefmt="grid"))
                
                return paperworks
            else:
                print(f"{Fore.RED}❌ Failed to get paperworks: {response.text}{Style.RESET_ALL}")
                return None
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server.{Style.RESET_ALL}")
            return None
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return None
        except Exception as e:
            print(f"{Fore.RED}❌ Error retrieving paperworks: {str(e)}{Style.RESET_ALL}")
            return None
    
    def test_paperwork_detail(self, paperwork_id):
        """Test getting paperwork details"""
        try:
            print(f"{Fore.CYAN}Fetching paperwork details...{Style.RESET_ALL}")
            response = requests.get(
                f"{self.base_url}paperworks/{paperwork_id}/", 
                headers=self.headers,
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 200:
                paperwork = response.json()
                print(f"{Fore.GREEN}βœ… Retrieved paperwork: {paperwork['title']}{Style.RESET_ALL}")
                
                # Display paperwork details in a formatted way
                print(f"\n{Fore.YELLOW}Paperwork Details:{Style.RESET_ALL}")
                print(f"  {Fore.CYAN}ID:{Style.RESET_ALL} {paperwork.get('id')}")
                print(f"  {Fore.CYAN}Title:{Style.RESET_ALL} {paperwork.get('title')}")
                print(f"  {Fore.CYAN}Researcher:{Style.RESET_ALL} {paperwork.get('researcher', {}).get('username')}")
                print(f"  {Fore.CYAN}Status:{Style.RESET_ALL} {paperwork.get('status')}")
                print(f"  {Fore.CYAN}Assigned At:{Style.RESET_ALL} {paperwork.get('assigned_at')}")
                print(f"  {Fore.CYAN}Updated At:{Style.RESET_ALL} {paperwork.get('updated_at')}")
                
                return paperwork
            else:
                print(f"{Fore.RED}❌ Failed to get paperwork details: {response.text}{Style.RESET_ALL}")
                return None
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server.{Style.RESET_ALL}")
            return None
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return None
        except Exception as e:
            print(f"{Fore.RED}❌ Error retrieving paperwork details: {str(e)}{Style.RESET_ALL}")
            return None
    
    def test_versions_list(self, paperwork_id):
        """Test getting versions for a paperwork"""
        try:
            print(f"{Fore.CYAN}Fetching versions for paperwork {paperwork_id}...{Style.RESET_ALL}")
            response = requests.get(
                f"{self.base_url}paperworks/{paperwork_id}/versions/", 
                headers=self.headers,
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 200:
                versions = response.json()
                print(f"{Fore.GREEN}βœ… Retrieved {len(versions)} versions for paperwork{Style.RESET_ALL}")
                
                if versions:
                    # Display versions in a table
                    table_data = []
                    for v in versions:
                        table_data.append([
                            v.get('version_no'),
                            v.get('submitted_at'),
                            f"{v.get('ai_percent_self', 'N/A')}%",
                            "βœ“" if v.get('pdf_path') else "βœ—",
                            "βœ“" if v.get('latex_path') else "βœ—",
                            "βœ“" if v.get('python_path') else "βœ—"
                        ])
                    
                    headers = ["Version", "Submitted At", "AI %", "PDF", "LaTeX", "Python"]
                    print(tabulate(table_data, headers=headers, tablefmt="grid"))
                
                return versions
            else:
                print(f"{Fore.RED}❌ Failed to get versions: {response.text}{Style.RESET_ALL}")
                return None
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server.{Style.RESET_ALL}")
            return None
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return None
        except Exception as e:
            print(f"{Fore.RED}❌ Error retrieving versions: {str(e)}{Style.RESET_ALL}")
            return None
    
    def test_submit_version(self, paperwork_id, version_data):
        """Test submitting a new version"""
        try:
            print(f"{Fore.CYAN}Submitting new version for paperwork {paperwork_id}...{Style.RESET_ALL}")
            response = requests.post(
                f"{self.base_url}paperworks/{paperwork_id}/versions/", 
                headers=self.headers,
                json=version_data,
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 201:
                version = response.json()
                print(f"{Fore.GREEN}βœ… Submitted new version {version['version_no']}{Style.RESET_ALL}")
                
                # Display version details
                print(f"\n{Fore.YELLOW}Version Details:{Style.RESET_ALL}")
                print(f"  {Fore.CYAN}Version Number:{Style.RESET_ALL} {version.get('version_no')}")
                print(f"  {Fore.CYAN}Submitted At:{Style.RESET_ALL} {version.get('submitted_at')}")
                print(f"  {Fore.CYAN}AI Percentage:{Style.RESET_ALL} {version.get('ai_percent_self')}%")
                print(f"  {Fore.CYAN}PDF Path:{Style.RESET_ALL} {version.get('pdf_path')}")
                print(f"  {Fore.CYAN}LaTeX Path:{Style.RESET_ALL} {version.get('latex_path')}")
                print(f"  {Fore.CYAN}Python Path:{Style.RESET_ALL} {version.get('python_path')}")
                
                return version
            else:
                print(f"{Fore.RED}❌ Failed to submit version: {response.text}{Style.RESET_ALL}")
                return None
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server.{Style.RESET_ALL}")
            return None
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return None
        except Exception as e:
            print(f"{Fore.RED}❌ Error submitting version: {str(e)}{Style.RESET_ALL}")
            return None
    
    def test_version_detail(self, paperwork_id, version_no):
        """Test getting version details"""
        try:
            print(f"{Fore.CYAN}Fetching version {version_no} details for paperwork {paperwork_id}...{Style.RESET_ALL}")
            response = requests.get(
                f"{self.base_url}paperworks/{paperwork_id}/versions/{version_no}/", 
                headers=self.headers,
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 200:
                version = response.json()
                print(f"{Fore.GREEN}βœ… Retrieved version {version['version_no']} details{Style.RESET_ALL}")
                
                # Display version details
                print(f"\n{Fore.YELLOW}Version Details:{Style.RESET_ALL}")
                print(f"  {Fore.CYAN}Version Number:{Style.RESET_ALL} {version.get('version_no')}")
                print(f"  {Fore.CYAN}Submitted At:{Style.RESET_ALL} {version.get('submitted_at')}")
                print(f"  {Fore.CYAN}AI Percentage:{Style.RESET_ALL} {version.get('ai_percent_self')}%")
                print(f"  {Fore.CYAN}PDF Path:{Style.RESET_ALL} {version.get('pdf_path')}")
                print(f"  {Fore.CYAN}LaTeX Path:{Style.RESET_ALL} {version.get('latex_path')}")
                print(f"  {Fore.CYAN}Python Path:{Style.RESET_ALL} {version.get('python_path')}")
                
                if 'paperwork' in version:
                    print(f"\n{Fore.YELLOW}Associated Paperwork:{Style.RESET_ALL}")
                    print(f"  {Fore.CYAN}Title:{Style.RESET_ALL} {version['paperwork'].get('title')}")
                    print(f"  {Fore.CYAN}Status:{Style.RESET_ALL} {version['paperwork'].get('status')}")
                    print(f"  {Fore.CYAN}Researcher:{Style.RESET_ALL} {version['paperwork'].get('researcher_name')}")
                
                return version
            else:
                print(f"{Fore.RED}❌ Failed to get version details: {response.text}{Style.RESET_ALL}")
                return None
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server.{Style.RESET_ALL}")
            return None
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return None
        except Exception as e:
            print(f"{Fore.RED}❌ Error retrieving version details: {str(e)}{Style.RESET_ALL}")
            return None
    
    def test_review_paperwork(self, paperwork_id, review_data):
        """Test reviewing a paperwork"""
        try:
            print(f"{Fore.CYAN}Reviewing paperwork {paperwork_id}...{Style.RESET_ALL}")
            response = requests.post(
                f"{self.base_url}paperworks/{paperwork_id}/review/", 
                headers=self.headers,
                json=review_data,
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 200:
                paperwork = response.json()
                print(f"{Fore.GREEN}βœ… Reviewed paperwork: {paperwork['title']} - Status: {paperwork['status']}{Style.RESET_ALL}")
                
                # Display paperwork details
                print(f"\n{Fore.YELLOW}Updated Paperwork Details:{Style.RESET_ALL}")
                print(f"  {Fore.CYAN}Title:{Style.RESET_ALL} {paperwork.get('title')}")
                print(f"  {Fore.CYAN}Status:{Style.RESET_ALL} {paperwork.get('status')}")
                print(f"  {Fore.CYAN}Researcher:{Style.RESET_ALL} {paperwork.get('researcher', {}).get('username')}")
                print(f"  {Fore.CYAN}Updated At:{Style.RESET_ALL} {paperwork.get('updated_at')}")
                
                if 'comments' in review_data and review_data['comments']:
                    print(f"  {Fore.CYAN}Comments:{Style.RESET_ALL} {review_data['comments']}")
                
                return paperwork
            else:
                print(f"{Fore.RED}❌ Failed to review paperwork: {response.text}{Style.RESET_ALL}")
                return None
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server.{Style.RESET_ALL}")
            return None
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return None
        except Exception as e:
            print(f"{Fore.RED}❌ Error reviewing paperwork: {str(e)}{Style.RESET_ALL}")
            return None
    
    def test_reports_summary(self):
        """Test getting reports summary"""
        try:
            print(f"{Fore.CYAN}Fetching reports summary...{Style.RESET_ALL}")
            response = requests.get(
                f"{self.base_url}reports/summary/", 
                headers=self.headers,
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 200:
                summary = response.json()
                print(f"{Fore.GREEN}βœ… Retrieved reports summary - Total papers: {summary['total_papers']}{Style.RESET_ALL}")
                
                # Display summary details
                print(f"\n{Fore.YELLOW}Summary Statistics:{Style.RESET_ALL}")
                print(f"  {Fore.CYAN}Total Papers:{Style.RESET_ALL} {summary.get('total_papers')}")
                print(f"  {Fore.CYAN}Average AI Percentage:{Style.RESET_ALL} {summary.get('average_ai_percentage', 'N/A')}%")
                
                if 'papers_by_status' in summary:
                    print(f"\n{Fore.YELLOW}Papers by Status:{Style.RESET_ALL}")
                    status_data = []
                    for status, count in summary['papers_by_status'].items():
                        status_data.append([status, count])
                    print(tabulate(status_data, headers=["Status", "Count"], tablefmt="grid"))
                
                return summary
            else:
                print(f"{Fore.RED}❌ Failed to get reports summary: {response.text}{Style.RESET_ALL}")
                return None
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server.{Style.RESET_ALL}")
            return None
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return None
        except Exception as e:
            print(f"{Fore.RED}❌ Error retrieving reports summary: {str(e)}{Style.RESET_ALL}")
            return None
    
    def test_notifications_list(self):
        """Test getting notifications"""
        try:
            print(f"{Fore.CYAN}Fetching notifications...{Style.RESET_ALL}")
            response = requests.get(
                f"{self.base_url}notifications/", 
                headers=self.headers,
                timeout=10
            )
            
            self.last_response = response
            
            if response.status_code == 200:
                notifications = response.json()
                print(f"{Fore.GREEN}βœ… Retrieved {len(notifications)} notifications{Style.RESET_ALL}")
                
                if notifications:
                    # Display notifications in a table
                    table_data = []
                    for notif in notifications:
                        table_data.append([
                            notif.get('id'),
                            notif.get('event'),
                            notif.get('paper', {}).get('title', 'N/A'),
                            notif.get('timestamp')
                        ])
                    
                    headers = ["ID", "Event", "Paper", "Timestamp"]
                    print(tabulate(table_data, headers=headers, tablefmt="grid"))
                else:
                    print(f"{Fore.YELLOW}No notifications found.{Style.RESET_ALL}")
                
                return notifications
            else:
                print(f"{Fore.RED}❌ Failed to get notifications: {response.text}{Style.RESET_ALL}")
                return None
        except requests.exceptions.ConnectionError:
            print(f"{Fore.RED}❌ Connection error: Could not connect to the server.{Style.RESET_ALL}")
            return None
        except requests.exceptions.Timeout:
            print(f"{Fore.RED}❌ Timeout: The server took too long to respond.{Style.RESET_ALL}")
            return None
        except Exception as e:
            print(f"{Fore.RED}❌ Error retrieving notifications: {str(e)}{Style.RESET_ALL}")
            return None

def clear_screen():
    """Clear the terminal screen"""
    os.system('cls' if os.name == 'nt' else 'clear')

def print_header():
    """Print application header"""
    clear_screen()
    print(f"{Fore.CYAN}{'=' * 80}{Style.RESET_ALL}")
    print(f"{Fore.YELLOW}{'PMS API TESTING TOOL':^80}{Style.RESET_ALL}")
    print(f"{Fore.CYAN}{'=' * 80}{Style.RESET_ALL}")

def main():
    print_header()
    print(f"{Fore.GREEN}Welcome to the PMS API Testing Tool!{Style.RESET_ALL}")
    print(f"This tool allows you to test the API endpoints of the PMS-Backend system.\n")
    
    # Check if server is running
    try:
        requests.get(BASE_URL.rstrip('/'), timeout=2)
    except requests.exceptions.ConnectionError:
        print(f"{Fore.RED}ERROR: Cannot connect to the server at {BASE_URL}{Style.RESET_ALL}")
        print(f"{Fore.YELLOW}Please make sure the server is running before using this tool.{Style.RESET_ALL}")
        print(f"\nTo start the server, run: {Fore.CYAN}python manage.py runserver{Style.RESET_ALL}")
        sys.exit(1)
    except Exception:
        pass
    
    tester = APITester(BASE_URL)
    
    # Get login credentials
    print(f"\n{Fore.YELLOW}Please login to continue:{Style.RESET_ALL}")
    username = input(f"{Fore.CYAN}Username: {Style.RESET_ALL}")
    password = getpass(f"{Fore.CYAN}Password: {Style.RESET_ALL}")
    
    if not tester.login(username, password):
        print(f"\n{Fore.RED}Login failed. Exiting...{Style.RESET_ALL}")
        time.sleep(2)
        sys.exit(1)
    
    # Store last used paperwork ID for convenience
    last_paperwork_id = None
    
    # Test menu
    while True:
        print(f"\n{Fore.CYAN}{'=' * 80}{Style.RESET_ALL}")
        print(f"{Fore.YELLOW}{'API TESTING MENU':^80}{Style.RESET_ALL}")
        print(f"{Fore.CYAN}{'=' * 80}{Style.RESET_ALL}")
        
        # Show user info
        if tester.username and tester.user_role:
            print(f"Logged in as: {Fore.YELLOW}{tester.username}{Style.RESET_ALL} | Role: {Fore.GREEN}{tester.user_role}{Style.RESET_ALL}")
        
        print(f"\n{Fore.CYAN}Available Actions:{Style.RESET_ALL}")
        print(f"{Fore.WHITE}1. List all paperworks{Style.RESET_ALL}")
        print(f"{Fore.WHITE}2. Get paperwork details{Style.RESET_ALL}")
        print(f"{Fore.WHITE}3. List versions for a paperwork{Style.RESET_ALL}")
        print(f"{Fore.WHITE}4. Submit a new version{Style.RESET_ALL}")
        print(f"{Fore.WHITE}5. Get version details{Style.RESET_ALL}")
        print(f"{Fore.WHITE}6. Review a paperwork{Style.RESET_ALL}")
        print(f"{Fore.WHITE}7. Get reports summary{Style.RESET_ALL}")
        print(f"{Fore.WHITE}8. List notifications{Style.RESET_ALL}")
        print(f"{Fore.WHITE}9. Clear screen{Style.RESET_ALL}")
        print(f"{Fore.RED}0. Exit{Style.RESET_ALL}")
        
        choice = input(f"\n{Fore.YELLOW}Enter your choice: {Style.RESET_ALL}")
        
        if choice == '1':
            paperworks = tester.test_paperworks_list()
            if paperworks and len(paperworks) > 0:
                # Store the first paperwork ID for convenience
                last_paperwork_id = paperworks[0].get('id')
        
        elif choice == '2':
            if last_paperwork_id:
                print(f"Last used paperwork ID: {Fore.YELLOW}{last_paperwork_id}{Style.RESET_ALL}")
                use_last = input(f"Use this ID? (y/n, default: y): ").lower() != 'n'
                if use_last:
                    paperwork_id = last_paperwork_id
                else:
                    paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            else:
                paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            
            paperwork = tester.test_paperwork_detail(paperwork_id)
            if paperwork:
                last_paperwork_id = paperwork_id
        
        elif choice == '3':
            if last_paperwork_id:
                print(f"Last used paperwork ID: {Fore.YELLOW}{last_paperwork_id}{Style.RESET_ALL}")
                use_last = input(f"Use this ID? (y/n, default: y): ").lower() != 'n'
                if use_last:
                    paperwork_id = last_paperwork_id
                else:
                    paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            else:
                paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            
            versions = tester.test_versions_list(paperwork_id)
            if versions is not None:
                last_paperwork_id = paperwork_id
        
        elif choice == '4':
            if last_paperwork_id:
                print(f"Last used paperwork ID: {Fore.YELLOW}{last_paperwork_id}{Style.RESET_ALL}")
                use_last = input(f"Use this ID? (y/n, default: y): ").lower() != 'n'
                if use_last:
                    paperwork_id = last_paperwork_id
                else:
                    paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            else:
                paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            
            try:
                version_no = int(input(f"{Fore.CYAN}Enter version number: {Style.RESET_ALL}"))
                ai_percent = float(input(f"{Fore.CYAN}Enter AI percentage (0-100): {Style.RESET_ALL}"))
                
                version_data = {
                    'paperwork_id': paperwork_id,
                    'version_no': version_no,
                    'pdf_path': f"uploads/papers/{paperwork_id}/v{version_no}.pdf",
                    'latex_path': f"uploads/papers/{paperwork_id}/v{version_no}.tex",
                    'python_path': f"uploads/papers/{paperwork_id}/v{version_no}.py",
                    'ai_percent_self': ai_percent
                }
                
                version = tester.test_submit_version(paperwork_id, version_data)
                if version is not None:
                    last_paperwork_id = paperwork_id
            except ValueError:
                print(f"{Fore.RED}❌ Invalid input. Please enter numeric values.{Style.RESET_ALL}")
        
        elif choice == '5':
            if last_paperwork_id:
                print(f"Last used paperwork ID: {Fore.YELLOW}{last_paperwork_id}{Style.RESET_ALL}")
                use_last = input(f"Use this ID? (y/n, default: y): ").lower() != 'n'
                if use_last:
                    paperwork_id = last_paperwork_id
                else:
                    paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            else:
                paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            
            version_no = input(f"{Fore.CYAN}Enter version number: {Style.RESET_ALL}")
            version = tester.test_version_detail(paperwork_id, version_no)
            if version is not None:
                last_paperwork_id = paperwork_id
        
        elif choice == '6':
            if last_paperwork_id:
                print(f"Last used paperwork ID: {Fore.YELLOW}{last_paperwork_id}{Style.RESET_ALL}")
                use_last = input(f"Use this ID? (y/n, default: y): ").lower() != 'n'
                if use_last:
                    paperwork_id = last_paperwork_id
                else:
                    paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            else:
                paperwork_id = input(f"{Fore.CYAN}Enter paperwork ID: {Style.RESET_ALL}")
            
            print(f"\n{Fore.YELLOW}Available statuses:{Style.RESET_ALL}")
            print(f"  {Fore.CYAN}ASSIGNED{Style.RESET_ALL} - Paper is assigned to researcher")
            print(f"  {Fore.CYAN}SUBMITTED{Style.RESET_ALL} - Paper has been submitted")
            print(f"  {Fore.CYAN}CHANGES_REQUESTED{Style.RESET_ALL} - Changes have been requested")
            print(f"  {Fore.CYAN}APPROVED{Style.RESET_ALL} - Paper has been approved")
            
            status = input(f"\n{Fore.CYAN}Enter new status: {Style.RESET_ALL}").upper()
            comments = input(f"{Fore.CYAN}Enter comments (optional): {Style.RESET_ALL}")
            
            review_data = {
                'status': status,
                'comments': comments
            }
            
            paperwork = tester.test_review_paperwork(paperwork_id, review_data)
            if paperwork is not None:
                last_paperwork_id = paperwork_id
        
        elif choice == '7':
            tester.test_reports_summary()
        
        elif choice == '8':
            tester.test_notifications_list()
            
        elif choice == '9':
            print_header()
        
        elif choice == '0':
            print(f"{Fore.YELLOW}Exiting...{Style.RESET_ALL}")
            break
        
        else:
            print(f"{Fore.RED}Invalid choice. Please try again.{Style.RESET_ALL}")
            
        # Pause before showing menu again
        input(f"\n{Fore.CYAN}Press Enter to continue...{Style.RESET_ALL}")

if __name__ == "__main__":
    main()