-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart
More file actions
1011 lines (839 loc) · 38 KB
/
start
File metadata and controls
1011 lines (839 loc) · 38 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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import os
import sys
import subprocess
import platform
import time
import shutil
import threading
from threading import RLock
import re
from urllib.parse import urlencode
import random
from typing import List, Optional
from concurrent.futures import ThreadPoolExecutor, as_completed
EMAIL_CODE_PATTERN = re.compile(r"Email Code:\s*(\d+)")
VERIFY_TOKEN_PATTERN = re.compile(r"user_id=([\w-]+)&expires=(\d+)&signature=([\w-]+)")
def get_verify_token(text: str, user_id: Optional[str] = None):
# Search for the pattern in captured output
if user_id is None:
match = re.search(VERIFY_TOKEN_PATTERN, text)
if not match:
raise ValueError("Verify token not found in captured output")
# Extracted components
user_id, expires, signature = match.groups()
return urlencode({"user_id": user_id, "expires": expires, "signature": signature})
else:
match = re.search(rf"user_id={user_id}&expires=(\d+)&signature=([\w-]+)", text)
if not match:
raise ValueError("Verify token not found in captured output")
expires, signature = match.groups()
return urlencode({"user_id": user_id, "expires": expires, "signature": signature})
def get_email_code(text: str, user_id: Optional[str] = None):
# Search for the pattern in captured output
if user_id is not None:
match = re.search(rf"UserID: {user_id},\s*Email Code:\s*(\d+)", text)
else:
match = re.search(EMAIL_CODE_PATTERN, text)
if not match:
raise ValueError("Email code not found in captured output")
# Extracted email code
email_code = match.group(1)
return email_code
# Pre-installation check
print("This script will install required python packages [rich, faker, requests, pandas, openpyxl, fastapi, pyotp, httpx] and start the test environment.\nPlease make sure you activate a virtual python environment. Continue? (y/n)")
response = input()
if response.lower() != "y":
sys.exit("Installation cancelled.")
print("Installing required packages...")
subprocess.run(["pip", "install", "rich", "faker", "requests", "pandas", "openpyxl", "fastapi", "pyotp", "httpx"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
import pandas as pd
from rich.console import Console
from rich.prompt import Prompt
from rich.progress import Progress, SpinnerColumn, BarColumn, TextColumn, TimeElapsedColumn
from rich.spinner import Spinner
from rich.table import Table
from faker import Faker
import requests
from urllib.parse import urljoin
from requests import Session
from server.tests.v1 import testclasses
from faker.providers import BaseProvider
import openpyxl
from openpyxl.utils import get_column_letter
import json
from faker import Faker
from faker.providers import BaseProvider
class ProgramProvider(BaseProvider):
def __init__(self, generator, data):
super().__init__(generator)
self.adjectives = data.get("adjectives", [])
self.subjects = data.get("subjects", [])
def program_name(self):
adjective = self.random_element(self.adjectives)
subject = self.random_element(self.subjects)
return f"{adjective} {subject}"
class MembershipProvider(BaseProvider):
def __init__(self, generator, data):
super().__init__(generator)
self.adjectives = data.get("adjectives", [])
self.subjects = data.get("subjects", [])
def membership_name(self):
adjective = self.random_element(self.adjectives)
subject = self.random_element(self.subjects)
return f"{adjective} {subject}"
with open("start_data", "r", encoding="utf-8") as f:
start_data = json.load(f)
console = Console()
faker = Faker()
faker.add_provider(ProgramProvider(faker, start_data["program"]))
faker.add_provider(MembershipProvider(faker, start_data["membership"]))
class DockerLogStreamer:
def __init__(self, server_dir, service_name="backend", encoding="utf-8", errors="replace"):
"""
Initializes the log streamer.
:param server_dir: Path to the directory where the docker compose file is located.
:param service_name: Name of the Docker Compose service (default: "backend").
:param encoding: Encoding used for decoding.
:param errors: Error handling strategy during decoding.
"""
self.server_dir = server_dir
self.service_name = service_name
self.encoding = encoding
self.errors = errors
self.buffer = [] # Buffer for log lines
self.all_logs = [] # All log lines
self.process = None
self.thread = None
self._running = False
self.lock = RLock()
def _stream_logs(self):
self.process = subprocess.Popen(
["docker", "compose", "logs", "-f", "--tail=0", self.service_name],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=self.server_dir,
encoding=self.encoding,
errors=self.errors,
bufsize=1,
)
while self._running:
line = self.process.stdout.readline() # type: ignore
if line:
with self.lock:
self.buffer.append(line)
self.all_logs.append(line)
else:
time.sleep(0.001)
self.process.terminate()
self.process.wait()
def start(self):
"""Starts the log streaming process in a background thread."""
if not self._running:
self._running = True
self.thread = threading.Thread(target=self._stream_logs, daemon=True)
self.thread.start()
def get_all_logs(self):
"""Returns all log lines that have occurred since the log streaming process started."""
time.sleep(0.05)
with self.lock:
return "".join(self.all_logs)
def clear_buffer(self):
"""Clears the buffer containing the log lines."""
with self.lock:
self.buffer = []
def clear_all_logs(self):
"""Clears the list with all log lines."""
with self.lock:
self.all_logs = []
def get_new_logs(self):
"""
Returns the new log lines since the last query and clears the buffer.
:return: A string that contains all newly added log lines.
"""
time.sleep(0.05)
with self.lock:
new_logs = "".join(self.buffer)
self.buffer = []
return new_logs
def stop(self):
"""Stops the log streaming process."""
self._running = False
if self.thread:
self.thread.join()
class TestUser(testclasses.TestUser):
def __init__(self, client, log_thread: DockerLogStreamer):
super().__init__(client)
self.log_thread = log_thread
def login_email(self):
response = self.client.post(
"/api/v1/auth/login",
headers={"application-id": self.application_id},
json={"ident": self.email, "password": self.password},
)
if response.status_code != 200:
raise ValueError(f"Login failed: {response.json()}")
security_token = response.json()["security_token"]
for _ in range(3):
logs = self.log_thread.get_all_logs() # type: ignore
email_code = get_email_code(logs, self.user_id)
response = self.client.post(
"/api/v1/auth/verify-code-2fa",
headers={"application-id": self.application_id, "Authorization": f"Bearer {security_token}"},
json={"code": email_code, "is_totp": False},
)
if response.status_code == 200:
break
time.sleep(1)
else:
raise ValueError(f"Email verification failed: {response.json()}")
self.tokens = response.json()
class AdminUser(testclasses.AdminUser):
def __init__(self, client, log_thread: DockerLogStreamer):
super().__init__(client)
self.log_thread = log_thread
def login_email(self):
response = self.client.post(
"/api/v1/auth/login",
headers={"application-id": self.application_id},
json={"ident": self.email, "password": self.password},
)
if response.status_code != 200:
raise ValueError(f"Login failed: {response.json()}")
security_token = response.json()["security_token"]
logs = self.log_thread.get_all_logs() # type: ignore
email_code = get_email_code(logs, self.user_id)
response = self.client.post(
"/api/v1/auth/verify-code-2fa",
headers={"application-id": self.application_id, "Authorization": f"Bearer {security_token}"},
json={"code": email_code, "is_totp": False},
)
if response.status_code != 200:
raise ValueError(f"Email verification failed: {response.json()}")
self.tokens = response.json()
class LiveServerSession(Session):
def __init__(self, base_url=None):
super().__init__()
self.base_url = base_url
def request(self, method, url, *args, **kwargs):
joined_url = urljoin(self.base_url, url) # type: ignore
return super().request(method, joined_url, *args, **kwargs)
def check_prerequisites():
console.print("[bold]Checking prerequisites...[/bold]")
try:
subprocess.run(["docker", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
console.print("[green]Docker found[/green]")
except Exception:
console.print("[bold red]Docker not found. Please install Docker.[/bold red]")
sys.exit(1)
try:
subprocess.run(["node", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
console.print("[green]Node found[/green]")
except Exception:
console.print("[bold red]Node not found. Please install Node.js.[/bold red]")
sys.exit(1)
try:
if os.name == "nt":
subprocess.run(["npm", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
else:
subprocess.run(["npm", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
console.print("[green]npm found[/green]")
except Exception:
console.print("[bold red]npm not found. Please install npm.[/bold red]")
sys.exit(1)
def docker_cleanup(server_dir):
console.print(f"Running Docker cleanup in directory: [yellow]{server_dir}[/yellow]")
try:
subprocess.run(["docker", "compose", "down", "-v", "--rmi", "all"], check=True, cwd=server_dir)
console.print("[bold green]Docker cleanup completed successfully.[/bold green]")
except Exception as e:
console.print(f"[bold red]Docker cleanup failed: {e}[/bold red]")
def start_docker_stack(server_dir):
console.print("Building Docker stack for the backend...")
try:
console.print("Pulling Docker images...")
subprocess.run(["docker", "compose", "pull"], check=True, cwd=server_dir)
console.print("Starting Docker stack...")
subprocess.run(["docker", "compose", "up", "-d", "--build"], check=True, cwd=server_dir)
console.print("[bold green]Docker stack started successfully.[/bold green]")
except Exception as e:
console.print(f"[bold red]Error starting Docker stack: {e}[/bold red]")
sys.exit(1)
def restart_docker_stack(server_dir):
console.print("Restarting Docker stack...")
try:
subprocess.run(["docker", "compose", "restart"], check=True, cwd=server_dir)
console.print("[bold green]Docker stack restarted successfully.[/bold green]")
except Exception as e:
console.print(f"[bold red]Error restarting Docker stack: {e}[/bold red]")
def stop_docker_stack(server_dir):
console.print("[bold red]Stopping Docker containers...[/bold red]")
try:
subprocess.run(["docker", "compose", "stop"], check=True, cwd=server_dir)
console.print("[bold green]Docker stack stopped successfully.[/bold green]")
except Exception as e:
console.print(f"[bold red]Error stopping Docker stack: {e}[/bold red]")
def open_new_terminal(command, cwd):
"""
Opens a new terminal window and executes the given command in the desired directory.
Supports Windows, macOS, and Linux.
"""
system = platform.system()
if system == "Windows":
subprocess.Popen(
["cmd", "/c", "start", "cmd", "/k", f"cd /d {cwd} && {command}"],
shell=True
)
elif system == "Darwin": # macOS
apple_script = f'''
tell application "Terminal"
do script "cd {cwd} && {command}"
activate
end tell
'''
subprocess.Popen(["osascript", "-e", apple_script])
elif system == "Linux":
# Search for an available terminal emulator
terminal = None
for term in ["gnome-terminal", "konsole", "xfce4-terminal", "x-terminal-emulator"]:
if shutil.which(term):
terminal = term
break
if terminal:
# Use the currently configured shell or default to bash
shell = os.environ.get("SHELL", "/bin/bash")
shell_name = os.path.basename(shell)
# Build the command with the desired working directory
full_command = f'cd "{cwd}" && {command}; exec {shell_name}'
print(terminal)
# For gnome-terminal and xfce4-terminal, the parameter -e is often needed; for others, maybe "--"
if terminal in ["gnome-terminal", "xfce4-terminal"]:
subprocess.Popen([terminal, "-e", f'{shell} -i -c "{full_command}"'])
elif terminal == "konsole":
subprocess.Popen([terminal, "--noclose", "-e", shell, "-i", "-c", full_command])
else:
subprocess.Popen([terminal, "--", shell, "-i", "-c", full_command])
else:
console.print("[bold red]No terminal emulator found. Please run the following command manually:[/bold red]")
console.print(command)
else:
console.print(f"[bold red]Operating system {system} is not supported.[/bold red]")
###########################################################################
################################# Testdata ################################
###########################################################################
def export_test_data(all_users, verified_users, clubs):
# Create the Users table
user_rows = []
for user in all_users:
employee_in_clubs = []
# Check in which clubs the user appears as an employee
for club in clubs:
for employee in club.employees:
if employee.ident == user.email:
employee_in_clubs.append(club.name)
user_rows.append(
{
"User ID": user.user_id,
"Username": user.username,
"Email": user.email,
"Password": user.password,
"First Name": user.first_name,
"Last Name": user.last_name,
"Employee in Clubs": ", ".join(employee_in_clubs) if employee_in_clubs else "",
"Verified": "Yes" if user in verified_users else "No",
}
)
df_users = pd.DataFrame(user_rows)
# Create the Clubs table
club_rows = []
for club in clubs:
club_rows.append(
{
"Club ID": club.club_id,
"Name": club.name,
"Description": club.description,
"Address": json.dumps(club.address, ensure_ascii=False),
"Programs": ", ".join([program.name for program in club.programs]),
"Memberships": ", ".join([membership.name for membership in club.memberships]),
"Employees": ", ".join([employee.ident for employee in club.employees]),
"Club Roles": ", ".join([role.name for role in club.roles]),
}
)
df_clubs = pd.DataFrame(club_rows)
# Create the Programs table
program_rows = []
for club in clubs:
for program in club.programs:
program_rows.append(
{
"Program ID": program.program_id,
"Club ID": club.club_id,
"Club Name": club.name,
"Name": program.name,
"Description": program.description,
"Price Model": program.price_model.value,
"Price": program.price,
"Currency": program.currency,
"Capacity": program.capacity,
"Membership Required": "Yes" if program.membership_required else "No",
"Status": program.status.value,
}
)
df_programs = pd.DataFrame(program_rows)
os.makedirs("test_data", exist_ok=True)
# Export as separate CSV files
df_users.to_csv("test_data/users.csv", index=False)
df_clubs.to_csv("test_data/clubs.csv", index=False)
df_programs.to_csv("test_data/programs.csv", index=False)
# Export all tables into one Excel file with separate sheets
excel_filename = "test_data/test_data.xlsx"
with pd.ExcelWriter(excel_filename, engine="openpyxl") as writer:
df_users.to_excel(writer, sheet_name="Users", index=False)
df_clubs.to_excel(writer, sheet_name="Clubs", index=False)
df_programs.to_excel(writer, sheet_name="Programs", index=False)
# Post-processing: Set AutoFilter for each sheet in the Excel file
wb = openpyxl.load_workbook(excel_filename)
for ws in wb.worksheets:
max_col = ws.max_column
max_row = ws.max_row
ws.auto_filter.ref = f"A1:{get_column_letter(max_col)}{max_row}"
wb.save(excel_filename)
def create_users(log_thread: DockerLogStreamer, progress: Progress, task, total_users: int) -> List[TestUser]:
client = LiveServerSession("http://localhost:8001")
users = []
for _ in range(total_users):
user = TestUser(client, log_thread) # type: ignore
user.first_name = faker.first_name()
user.last_name = faker.last_name()
user.password = faker.password(length=12)
email_sufix = faker.free_email_domain()
unique_word = os.urandom(4).hex()
user.email = random.choice(
[
f"{user.first_name}_{user.last_name}-{unique_word}@{email_sufix}",
f"{unique_word}@{email_sufix}",
f"{user.first_name}_{unique_word}@{email_sufix}",
]
)
user.username = f"{faker.user_name()}-{random.randint(0, 9999)}"
try:
user.register_user(False)
logs = log_thread.get_all_logs()
verify_token = get_verify_token(logs, user.user_id)
user.verify_email(verify_token)
users.append(user)
except Exception as e:
console.print(f"[bold red]Error creating user: {e}[/bold red]")
continue
progress.advance(task)
return users
def verify_users(users: List[testclasses.TestUser], admin: testclasses.AdminUser, progress: Progress, task) -> int:
count = 0
for user in users:
try:
user.submit_identity_verification()
except Exception:
try:
time.sleep(1)
user.submit_identity_verification()
except Exception:
continue
try:
admin.verify_identity(user.user_id) # type: ignore
except Exception:
try:
time.sleep(1)
admin.verify_identity(user.user_id) # type: ignore
except Exception:
continue
progress.advance(task)
count += 1
return count
def create_clubs(
club_owners: List[testclasses.TestUser],
employees: List[testclasses.TestUser],
admin: testclasses.AdminUser,
progress: Progress,
task,
) -> List[testclasses.Club]:
clubs = []
for club_owner in club_owners:
club = testclasses.Club(club_owner)
club.name = f"{faker.company()} Club {faker.random_lowercase_letter()}"
club.description = faker.text(max_nb_chars=50)
club.address = {
"street": faker.street_address(),
"city": faker.city(),
"state": faker.state(),
"postal_code": faker.zipcode(),
"country": random.choice(["Germany", "France", "Italy", "Spain", "United Kingdom", "United States"]),
}
try:
try:
club.create()
except Exception:
time.sleep(1)
club.create()
except Exception:
console.print(f"[bold red]Error creating club: {club.name}[/bold red]")
continue
club.refresh_roles()
admin.put(
f"/api/v1/clubs/{club.club_id}/stripe",
json={"stripe_account_id": f"acct_{os.urandom(16).hex()}"},
)
club_employees = employees[:3]
for employee in club_employees:
club_role = None
while True:
club_role = random.choice(club.roles)
if club_role.name != "Owner":
break
club_employee = testclasses.Employee(employee.email, club, club_role, employee)
club_employee.create()
club.employees.append(club_employee)
progress.advance(task)
clubs.append(club)
return clubs
def create_programs(
clubs: List[testclasses.Club], min_programs: int, max_programs: int, max_sessions: int, progress: Progress, task
):
for club in clubs:
program_count = random.randint(min_programs, max_programs)
rand_programs_active = testclasses.get_random_programs(
club, (program_count // 2 + program_count % 2), random.randint(2, max_sessions)
)
for program in rand_programs_active:
try:
program.name = f"{faker.program_name()} - {faker.random_lowercase_letter()}"
program.status = testclasses.ProgramStatusPublic.ACTIVE
desc = faker.text(max_nb_chars=100)
while len(desc) < 10:
desc += " " + faker.text(max_nb_chars=20)
program.description = desc
program.create()
progress.advance(task)
except Exception as e:
console.print(f"[bold red]Error creating program[/bold red] {e}")
continue
rand_programs_draft = testclasses.get_random_programs(club, program_count // 2, random.randint(2, max_sessions))
for program in rand_programs_draft:
try:
program.name = f"{faker.program_name()} ({faker.random_lowercase_letter()})"
program.status = testclasses.ProgramStatusPublic.DRAFT
desc = faker.text(max_nb_chars=20)
while len(desc) < 10:
desc += " " + faker.text(max_nb_chars=5)
program.description = desc
program.create()
progress.advance(task)
except Exception as e:
console.print(f"[bold red]Error creating program[/bold red] {e}")
continue
club.refresh_programs()
def create_memberships(clubs: List[testclasses.Club], total_memberships: int, progress: Progress, task):
for club in clubs:
for _ in range(total_memberships):
membership = testclasses.Membership(
club,
f"{faker.membership_name()} {faker.random_lowercase_letter()}",
faker.text(max_nb_chars=50),
random.randint(10, 100),
random.choice(["EUR", "USD"]),
random.randint(5, 12),
random.choice([testclasses.DurationUnit.DAY, testclasses.DurationUnit.MONTH]),
)
membership.create()
progress.advance(task)
club.refresh_memberships()
def create_bookings(users: List[TestUser], clubs: List[testclasses.Club], max_bookings: int, progress: Progress, task):
programs_per_session = []
programs_package = []
for club in clubs:
for program in club.programs:
if program.status != testclasses.ProgramStatusPublic.ACTIVE:
continue
if program.price_model == testclasses.PriceType.PACKAGE:
programs_package.append(program)
else:
programs_per_session.extend(program.session_courses)
programs_per_session.extend(program.session_events)
random.shuffle(programs_per_session)
random.shuffle(programs_package)
if not programs_per_session:
console.print("[bold red]No session programs available for booking.[/bold red]")
return
if not programs_package:
console.print("[bold red]No package programs available for booking.[/bold red]")
return
count = 0
for user in users:
if count > max_bookings:
break
try:
sessions_to_book = random.sample(
programs_per_session, random.randint(1, min(10, len(programs_per_session)))
)
programs_to_book = random.sample(programs_package, random.randint(1, min(5, len(programs_package))))
user.book(
[program.program_id for program in programs_to_book],
[session.session_id for session in sessions_to_book],
)
except Exception:
continue
count += 1
progress.advance(task)
if count < max_bookings:
console.print("[bold yellow]Could not book for all users due backend.[/bold yellow]")
def create_test_data(server_dir):
TOTAL_USERS = 50
TOTAL_CLUBS = 10
MIN_PROGRAMS_PER_CLUB = 10
MAX_PROGRAMS_PER_CLUB = 20
MAX_BOOKINGS = 20
MAX_SESSIONS_PER_PROGRAM = 20
TOTAL_MEMBERSHIPS = 3
THREADS = 10
if TOTAL_CLUBS * 4 > TOTAL_USERS:
console.print("[bold red]Error: Not enough users to assign to clubs.[/bold red]")
sys.exit(1)
if MAX_BOOKINGS > TOTAL_USERS:
console.print("[bold red]Error: Not enough users to create bookings.[/bold red]")
sys.exit(1)
console.print("[bold]Starting Backend Log Stream...[/bold]")
log_thread = DockerLogStreamer(server_dir)
log_thread.start()
time.sleep(1)
progress = Progress(
SpinnerColumn(),
"[progress.description]{task.description}",
BarColumn(),
TextColumn("{task.completed}/{task.total}"),
TimeElapsedColumn(),
console=console,
)
progress.start()
console.print("[bold]Creating test data...[/bold]")
request_client = LiveServerSession("http://localhost:8001")
admin = AdminUser(request_client, log_thread)
# ======================================================== #
# ========================= Users ======================== #
# ======================================================== #
task_users = progress.add_task("Creating users...", total=TOTAL_USERS)
users: List[TestUser] = []
num_threads = min(THREADS, TOTAL_USERS)
user_batch_size = TOTAL_USERS // num_threads
# Create separate progress tasks for each thread
batches = []
for i in range(num_threads):
batches.append((log_thread, progress, task_users, user_batch_size))
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(create_users, *batch) for batch in batches]
for future in as_completed(futures):
users.extend(future.result())
progress.update(task_users, description="Users created.", completed=TOTAL_USERS)
progress.stop_task(task_users)
verified_users = users[: TOTAL_CLUBS * 4]
unverified_users = users[TOTAL_CLUBS * 4 :]
log_thread.clear_all_logs()
admin.login_email()
# ======================================================== #
# ================= IdentityVerification ================= #
# ======================================================== #
task_verify = progress.add_task("Verifying users...", total=len(verified_users))
num_threads = min(THREADS, len(verified_users))
batch_size = len(verified_users) // num_threads
batches = []
for i in range(num_threads):
batches.append((verified_users[i * batch_size : (i + 1) * batch_size], admin, progress, task_verify))
count = 0
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(verify_users, *batch) for batch in batches]
for future in as_completed(futures):
count += future.result()
if count != len(verified_users):
progress.stop()
console.print(f"[bold red]Error verifying user. Please rerun the script.[/bold red]")
purge_data = Prompt.ask("Do you want to purge the data?", choices=["yes", "no"], default="yes")
if purge_data == "yes":
docker_cleanup(server_dir)
sys.exit(1)
progress.update(task_verify, description="Users verified.", completed=len(verified_users))
progress.stop_task(task_verify)
# ======================================================== #
# ========================= Clubs ======================== #
# ======================================================== #
club_owners = verified_users[:TOTAL_CLUBS]
employees = verified_users[TOTAL_CLUBS : TOTAL_CLUBS * 2]
clubs = []
task_clubs = progress.add_task("Creating clubs...", total=TOTAL_CLUBS)
num_threads = min(THREADS, TOTAL_CLUBS)
club_batch_size = TOTAL_CLUBS // num_threads
batches = []
for i in range(num_threads):
batches.append(
(club_owners[i * club_batch_size : (i + 1) * club_batch_size], employees, admin, progress, task_clubs)
)
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(create_clubs, *batch) for batch in batches]
for future in as_completed(futures):
clubs.extend(future.result())
progress.update(task_clubs, description="Clubs created.", completed=TOTAL_CLUBS)
progress.stop_task(task_clubs)
# ======================================================== #
# ========================= Programs ===================== #
# ======================================================== #
task_programs = progress.add_task("Creating programs...", total=TOTAL_CLUBS * MAX_PROGRAMS_PER_CLUB)
num_threads = min(THREADS, TOTAL_CLUBS)
program_batch_size = TOTAL_CLUBS // num_threads
batches = []
for i in range(num_threads):
batches.append(
(
clubs[i * program_batch_size : (i + 1) * program_batch_size],
MIN_PROGRAMS_PER_CLUB,
MAX_PROGRAMS_PER_CLUB,
MAX_SESSIONS_PER_PROGRAM,
progress,
task_programs,
)
)
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(create_programs, *batch) for batch in batches]
for future in as_completed(futures):
pass
progress.update(task_programs, description="Programs created.", completed=TOTAL_CLUBS * MAX_PROGRAMS_PER_CLUB)
progress.stop_task(task_programs)
# ======================================================== #
# ======================= Bookings ======================= #
# ======================================================== #
task_bookings = progress.add_task("Creating bookings...", total=len(unverified_users))
num_threads = min(THREADS, MAX_BOOKINGS)
user_batch_size = len(unverified_users) // num_threads
batches = []
users_for_booking = unverified_users + random.sample(verified_users, max(MAX_BOOKINGS - len(unverified_users), 0))
for i in range(num_threads):
batches.append(
(
users_for_booking[i * user_batch_size : (i + 1) * user_batch_size],
clubs,
MAX_BOOKINGS // num_threads,
progress,
task_bookings,
)
)
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(create_bookings, *batch) for batch in batches]
for future in as_completed(futures):
pass
progress.update(task_bookings, description="Bookings created.", completed=len(unverified_users))
progress.stop_task(task_bookings)
# ======================================================== #
# ====================== Memberships ===================== #
# ======================================================== #
task_memberships = progress.add_task("Creating memberships...", total=TOTAL_CLUBS * TOTAL_MEMBERSHIPS)
num_threads = min(THREADS, TOTAL_CLUBS)
membership_batch_size = TOTAL_CLUBS // num_threads
batches = []
for i in range(num_threads):
batches.append(
(clubs[i * membership_batch_size : (i + 1) * membership_batch_size], progress, task_memberships)
)
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(create_memberships, *batch) for batch in batches]
for future in as_completed(futures):
pass
progress.update(task_memberships, description="Memberships created.", completed=TOTAL_CLUBS * TOTAL_MEMBERSHIPS)
progress.stop_task(task_memberships)
progress.stop()
console.print("[bold green]Test data created successfully.[/bold green]")
export_test_data(users, verified_users, clubs)
console.print("[bold green]Test data saved to Excel and CSV files.[/bold green]")
def start_frontend(client_dir, expo_platform):
console.print("Starting Expo frontend...")
try:
if os.name == "nt":
subprocess.run(["npm", "install"], check=True, cwd=client_dir, shell=True)
else:
subprocess.run(["npm", "install"], check=True, cwd=client_dir)
console.print("[bold green]npm install completed successfully.[/bold green]")
except Exception as e:
console.print(f"[bold red]Error during npm install: {e}[/bold red]")
return
expo_command = f"npx expo run:{expo_platform}"
open_new_terminal(expo_command, client_dir)
console.print(f"[bold green]Expo frontend is starting for {expo_platform}.[/bold green]")
def start_backend_logs(server_dir):
console.print("Starting backend logs...")
command = "docker compose logs -f backend"
open_new_terminal(command, server_dir)
def show_meta_info():
table = Table(title="Installation Complete")
table.add_column("Service", style="cyan", no_wrap=True)
table.add_column("URL", style="magenta")
table.add_row("Mailhog", "http://localhost:8025")
table.add_row("Swagger Docs", "http://localhost:8001/docs")
console.print(table)
def backend_is_ready():
try:
response = requests.get("http://localhost:8001/ping")
return response.status_code == 200
except requests.exceptions.ConnectionError:
return False
def main():
check_prerequisites()
# Choose whether to perform cleanup or to start the process.
option = Prompt.ask(
"Please choose: cleanup = Docker Cleanup, start = Start test environment",
choices=["cleanup", "start"],
default="start",
)
# Assumption: The script is located at the project root with subdirectories /server and /client
current_dir = os.getcwd()
server_dir = os.path.join(current_dir, "server")
client_dir = os.path.join(current_dir, "client")
if option == "cleanup":
console.print("[bold red]Starting Docker cleanup...[/bold red]")
docker_cleanup(server_dir)
console.print("[bold green]Cleanup complete.[/bold green]")
sys.exit(0)
# If starting the environment:
mode = Prompt.ask(
"Choose deployment mode (full = full deployment, start = just start services, prepare = setup test environment)",
choices=["full", "start", "prepare"],
default="start",
)
expo_platform = None
if mode != "prepare":
expo_platform = Prompt.ask("Choose Expo platform", choices=["android", "ios"], default="ios")
if mode == "full" or mode == "prepare":
console.print("[bold green]Starting full deployment...[/bold green]")
start_docker_stack(server_dir)
with console.status("[bold]Waiting for backend to be ready...") as status:
while not backend_is_ready():
time.sleep(1)
console.print("[bold green]Backend is ready![/bold green]")
create_test_data(server_dir)
time.sleep(1)
if mode == "full":
restart_docker_stack(server_dir)
else:
console.print("[bold green]Starting just backend and frontend...[/bold green]")
start_docker_stack(server_dir)
if mode == "prepare":
stop_docker_stack(server_dir)
console.print("[bold green]Test environment setup complete.[/bold green]")
sys.exit(0)
start_backend_logs(server_dir)
start_frontend(client_dir, expo_platform)
show_meta_info()
console.print("[bold green]Installation complete.[/bold green]")
console.print("[bold]Press Ctrl+C to stop the docker services.[/bold]")