Coverage for compiler_admin / commands / user / offboard.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-28 05:48 +0000

1import click 

2 

3from compiler_admin import Result 

4from compiler_admin.commands.user.deactivate import deactivate 

5from compiler_admin.commands.user.delete import delete 

6from compiler_admin.services.google import GoogleAccount, GoogleArchive, GoogleUsers 

7 

8 

9@click.command() 

10@click.option("-a", "--alias", help="Another account to assign username as an alias.") 

11@click.option("-d", "--delete", "_delete", is_flag=True, help="Also delete the account.") 

12@click.option("-f", "--force", is_flag=True, help="Don't ask for confirmation.") 

13@click.option("-n", "--notify", help="An email address to send the new password notification.") 

14@click.argument("username") 

15@click.pass_context 

16def offboard(ctx: click.Context, username: str, alias: str = "", _delete: bool = False, force: bool = False, **kwargs): 

17 """Fully offboard a user from Compiler. 

18 

19 Deactivate, back up email, transfer Calendar/Drive, and optionally delete. 

20 """ 

21 account = GoogleAccount(username) 

22 google_users = GoogleUsers() 

23 google_archive = GoogleArchive() 

24 

25 if not account.exists(): 

26 click.echo(f"User does not exist: {account}") 

27 raise SystemExit(Result.FAILURE) 

28 

29 alias_account = None 

30 if alias: 

31 alias_account = GoogleAccount(alias) 

32 if not alias_account.exists(): 

33 click.echo(f"Alias target user does not exist: {alias_account}") 

34 raise SystemExit(Result.FAILURE) 

35 

36 if not force: 

37 cont = input(f"Offboard account {account} {' (assigning alias to ' + alias_account + ')' if alias else ''}? (Y/n): ") 

38 if not cont.lower().startswith("y"): 

39 click.echo("Aborting offboard.") 

40 raise SystemExit(Result.SUCCESS) 

41 

42 click.echo(f"User exists, offboarding: {account}") 

43 

44 # call the deactivate command 

45 ctx.forward(deactivate) 

46 

47 click.echo("Backing up email") 

48 google_archive.create_email_backup(account) 

49 

50 click.echo("Starting Drive and Calendar transfer") 

51 google_archive.archive_content(account) 

52 

53 google_archive.await_archive_completion(account, lambda: click.echo("Transfer in progress")) 

54 click.echo("Transfer complete") 

55 

56 click.echo("Deprovisioning POP/IMAP") 

57 google_users.deprovision_popimap(account) 

58 

59 # call the delete command 

60 if _delete: 

61 ctx.forward(delete) 

62 

63 if alias_account: 

64 click.echo(f"Adding an alias to account: {alias_account}") 

65 alias_account.add_email_alias(account) 

66 

67 click.echo(f"Offboarding for user complete: {account}")