parent
831ebf17aa
commit
51737585bd
@ -0,0 +1,35 @@ |
||||
import click |
||||
|
||||
from ..utils import http_post |
||||
from .utils import handle_sync_response, print_colon_kv |
||||
|
||||
|
||||
@click.command(short_help="Sync the 'program' attribute with UWLDAP") |
||||
@click.option('--dry-run', is_flag=True, default=False) |
||||
@click.option('--members', required=False) |
||||
def updateprograms(dry_run, members): |
||||
body = {} |
||||
if dry_run: |
||||
body['dry_run'] = True |
||||
if members is not None: |
||||
body['members'] = ','.split(members) |
||||
|
||||
if not dry_run: |
||||
click.confirm('Are you sure that you want to sync programs with UWLDAP?', abort=True) |
||||
|
||||
resp = http_post('/api/uwldap/updateprograms', json=body) |
||||
result = handle_sync_response(resp) |
||||
if len(result) == 0: |
||||
click.echo('All programs are up-to-date.') |
||||
return |
||||
if dry_run: |
||||
click.echo('Members whose program would be changed:') |
||||
else: |
||||
click.echo('Members whose program was changed:') |
||||
lines = [] |
||||
for uid, csc_program, uw_program in result: |
||||
csc_program = csc_program or 'Unknown' |
||||
csc_program = click.style(csc_program, fg='yellow') |
||||
uw_program = click.style(uw_program, fg='green') |
||||
lines.append((uid, csc_program + ' -> ' + uw_program)) |
||||
print_colon_kv(lines) |
@ -0,0 +1,43 @@ |
||||
from click.testing import CliRunner |
||||
import ldap3 |
||||
|
||||
from ceo.cli import cli |
||||
|
||||
|
||||
def test_updatemembers(cli_setup, cfg, ldap_conn, ldap_user, uwldap_user): |
||||
# sanity check |
||||
assert ldap_user.uid == uwldap_user.uid |
||||
# modify the user's program in UWLDAP |
||||
conn = ldap_conn |
||||
base_dn = cfg.get('uwldap_base') |
||||
dn = f'uid={uwldap_user.uid},{base_dn}' |
||||
changes = {'ou': [(ldap3.MODIFY_REPLACE, ['New Program'])]} |
||||
conn.modify(dn, changes) |
||||
|
||||
runner = CliRunner() |
||||
result = runner.invoke(cli, ['updateprograms', '--dry-run']) |
||||
expected = ( |
||||
"Members whose program would be changed:\n" |
||||
f"{ldap_user.uid}: {ldap_user.program} -> New Program\n" |
||||
) |
||||
assert result.exit_code == 0 |
||||
assert result.output == expected |
||||
|
||||
runner = CliRunner() |
||||
result = runner.invoke(cli, ['updateprograms'], input='y\n') |
||||
expected = ( |
||||
"Are you sure that you want to sync programs with UWLDAP? [y/N]: y\n" |
||||
"Members whose program was changed:\n" |
||||
f"{ldap_user.uid}: {ldap_user.program} -> New Program\n" |
||||
) |
||||
assert result.exit_code == 0 |
||||
assert result.output == expected |
||||
|
||||
runner = CliRunner() |
||||
result = runner.invoke(cli, ['updateprograms'], input='y\n') |
||||
expected = ( |
||||
"Are you sure that you want to sync programs with UWLDAP? [y/N]: y\n" |
||||
"All programs are up-to-date.\n" |
||||
) |
||||
assert result.exit_code == 0 |
||||
assert result.output == expected |
Loading…
Reference in new issue