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

41 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.backupcodes import backupcodes 

5from compiler_admin.commands.user.reset import reset 

6from compiler_admin.services.google import GoogleAccount, GoogleGroups, GoogleOrgs, GoogleUsers 

7 

8 

9@click.command() 

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

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

12@click.option( 

13 "-e", 

14 "--recovery-email", 

15 default="", 

16 help="An email address to use as the new recovery email.", 

17) 

18@click.option( 

19 "-p", 

20 "--recovery-phone", 

21 default="", 

22 help="A phone number to use as the new recovery phone number.", 

23) 

24@click.option("-s", "--staff", is_flag=True, help="Reactivate the user as a staff member. The default is contractor.") 

25@click.argument("username") 

26@click.pass_context 

27def reactivate( 

28 ctx: click.Context, 

29 username: str, 

30 force: bool = False, 

31 recovery_email: str = "", 

32 recovery_phone: str = "", 

33 staff: bool = False, 

34 **kwargs, 

35): 

36 """Reactivate a previously deactivated user.""" 

37 account = GoogleAccount(username) 

38 

39 if not account.exists(): 

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

41 raise SystemExit(Result.FAILURE) 

42 

43 if not account.is_deactivated(): 

44 click.echo("User is not deactivated, cannot reactivate") 

45 raise SystemExit(Result.FAILURE) 

46 

47 if not force: 

48 cont = input(f"Reactivate account {account}? (Y/n): ") 

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

50 click.echo("Aborting reactivation") 

51 raise SystemExit(Result.SUCCESS) 

52 

53 click.echo(f"User exists, reactivating: {account}") 

54 

55 click.echo(f"Adding to group: {GoogleGroups.GROUP_TEAM}") 

56 GoogleGroups(GoogleGroups.GROUP_TEAM).add_user(account) 

57 

58 if staff: 

59 click.echo(f"Moving to OU: {GoogleOrgs.OU_STAFF}") 

60 GoogleOrgs(GoogleOrgs.OU_STAFF).move_user(account) 

61 click.echo(f"Adding to group: {GoogleGroups.GROUP_STAFF}") 

62 GoogleGroups(GoogleGroups.GROUP_STAFF).add_user(account) 

63 else: 

64 click.echo(f"Moving to OU: {GoogleOrgs.OU_CONTRACTORS}") 

65 GoogleOrgs(GoogleOrgs.OU_CONTRACTORS).move_user(account) 

66 

67 # reset password, sign out 

68 ctx.forward(reset) 

69 

70 click.echo("Update user profile info") 

71 GoogleUsers().reset_recovery_info(account=account, recovery_email=recovery_email, recovery_phone=recovery_phone) 

72 

73 # get the user's backup codes 

74 ctx.forward(backupcodes) 

75 

76 click.echo(f"User is reactivated: {account}")