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

1 import click

2 from click import Group

3 from litestar.plugins import CLIPlugin

4

5 from core.cli import coro

6 from core.database import session_maker

7 from core.mail import MailClient

8 from core.settings import settings

9

10 from .factories import UserFactory

11 from .services import provide_auth_service

12

13

14 class 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")