Coverage for compiler_admin / commands / user / convert.py: 98%

39 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.services.google import GoogleAccount, GoogleGroups, GoogleOrgs 

5 

6 

7@click.command() 

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

9@click.argument("username") 

10@click.argument("account_type", type=click.Choice(GoogleOrgs.ORG_UNITS.keys(), case_sensitive=False)) 

11@click.pass_context 

12def convert(ctx: click.Context, username: str, account_type: str, **kwargs): 

13 """Convert a user of one type to another.""" 

14 account = GoogleAccount(username) 

15 groups = GoogleGroups() 

16 orgs = GoogleOrgs() 

17 

18 if not account.exists(): 

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

20 raise SystemExit(Result.FAILURE) 

21 

22 ou = orgs[account_type] 

23 click.echo(f"User exists, converting to: {ou} for {account}") 

24 

25 if ou == orgs.OU_CONTRACTORS: 

26 if account.is_partner(): 

27 groups.remove_user(account, groups.GROUP_PARTNERS) 

28 groups.remove_user(account, groups.GROUP_STAFF) 

29 elif account.is_staff(): 

30 groups.remove_user(account, groups.GROUP_STAFF) 

31 

32 elif ou == orgs.OU_STAFF: 

33 if account.is_partner(): 

34 groups.remove_user(account, groups.GROUP_PARTNERS) 

35 elif account.is_staff(): 

36 click.echo(f"User is already staff: {account}") 

37 raise SystemExit(Result.FAILURE) 

38 groups.add_user(account, groups.GROUP_STAFF) 

39 

40 elif ou == orgs.OU_PARTNERS: 40 ↛ 48line 40 didn't jump to line 48 because the condition on line 40 was always true

41 if account.is_partner(): 

42 click.echo(f"User is already partner: {account}") 

43 raise SystemExit(Result.FAILURE) 

44 if not account.is_staff(): 

45 groups.add_user(account, groups.GROUP_STAFF) 

46 groups.add_user(account, groups.GROUP_PARTNERS) 

47 

48 orgs.move_user(account, ou) 

49 

50 click.echo(f"Account conversion complete for: {account}")