Coverage for src/auth/plugin.py: 34%

32 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-25 22:09 +0300

1import click 

2from click import Group 

3from litestar.plugins import CLIPlugin 

4 

5from core.cli import coro 

6from core.database import session_maker 

7from core.mail import MailClient 

8from core.settings import settings 

9 

10from .factories import UserFactory 

11from .services import provide_auth_service 

12 

13 

14class AuthPlugin(CLIPlugin): 

15 def on_cli_init(self, cli: Group) -> None: 

16 @cli.group(help="Manage auth, load data with ``load`` command") 

17 @click.version_option(prog_name="auth") 

18 def auth(): ... 

19 

20 @auth.command(help="load auth data") 

21 @coro 

22 async def load(): 

23 async with session_maker() as session: 

24 click.echo("Loading auth data...") 

25 

26 for _ in range(10): 

27 session.add(UserFactory.build()) 

28 await session.commit() 

29 

30 @auth.command(help="load auth data") 

31 @coro 

32 async def reset(): 

33 async with session_maker() as session: 

34 auth_service = provide_auth_service(session) 

35 await auth_service.delete_where() 

36 await auth_service.create_many( 

37 [UserFactory.build() for _ in range(10)], auto_commit=True 

38 ) 

39 click.echo("Reset auth data successfully") 

40 

41 @auth.command(help="Send test mail") 

42 @click.argument("recipient") 

43 def send_mail(recipient): 

44 mail_controller = MailClient(settings.mail_config) 

45 mail_controller.send([recipient], "test", "test")