add tests for UWLDAP API
This commit is contained in:
parent
448692018a
commit
28c55b2fed
|
@ -16,11 +16,10 @@ def register_error_handlers(app: Flask):
|
|||
|
||||
|
||||
def generic_error_handler(err: Exception):
|
||||
"""Return JSON for internal server errors."""
|
||||
# pass through HTTP errors
|
||||
"""Return JSON for all errors."""
|
||||
if isinstance(err, HTTPException):
|
||||
return err
|
||||
if isinstance(err, UserNotFoundError) or isinstance(err, GroupNotFoundError):
|
||||
status_code = err.code
|
||||
elif isinstance(err, UserNotFoundError) or isinstance(err, GroupNotFoundError):
|
||||
status_code = 404
|
||||
else:
|
||||
status_code = 500
|
||||
|
|
|
@ -2,7 +2,7 @@ from flask import Blueprint, request
|
|||
from flask.json import jsonify
|
||||
from zope import component
|
||||
|
||||
from .utils import authz_restrict_to_syscom
|
||||
from .utils import authz_restrict_to_syscom, is_truthy
|
||||
from ceo_common.interfaces import IUWLDAPService, ILDAPService
|
||||
|
||||
bp = Blueprint('uwldap', __name__)
|
||||
|
@ -23,11 +23,13 @@ def get_user(username: str):
|
|||
@authz_restrict_to_syscom
|
||||
def update_programs():
|
||||
ldap_srv = component.getUtility(ILDAPService)
|
||||
body = request.get_json(force=True)
|
||||
members = body.get('members')
|
||||
kwargs = {'members': members}
|
||||
if body.get('dry_run'):
|
||||
members['dry_run'] = True
|
||||
if request.headers.get('content-type') == 'application/json':
|
||||
body = request.get_json()
|
||||
else:
|
||||
body = {}
|
||||
kwargs = {'members': body.get('members')}
|
||||
if body.get('dry_run') or is_truthy(request.args.get('dry_run', 'false')):
|
||||
kwargs['dry_run'] = True
|
||||
return jsonify(
|
||||
ldap_srv.update_programs(**kwargs)
|
||||
)
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
import ldap3
|
||||
|
||||
|
||||
def test_get_user(client, uwldap_user):
|
||||
uid = uwldap_user.uid
|
||||
status, data = client.get(f'/api/uwldap/{uid}')
|
||||
assert status == 200
|
||||
expected = {
|
||||
"cn": uwldap_user.cn,
|
||||
"given_name": uwldap_user.given_name,
|
||||
"mail_local_addresses": uwldap_user.mail_local_addresses,
|
||||
"program": uwldap_user.program,
|
||||
"sn": uwldap_user.sn,
|
||||
"uid": uwldap_user.uid,
|
||||
}
|
||||
assert data == expected
|
||||
|
||||
|
||||
def test_updateprograms(
|
||||
cfg, ldap_conn, g_admin, client, 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)
|
||||
|
||||
status, data = client.post('/api/uwldap/updateprograms?dry_run=True')
|
||||
assert status == 200
|
||||
expected = [
|
||||
[uwldap_user.uid, uwldap_user.program, 'New Program'],
|
||||
]
|
||||
assert data == expected
|
||||
|
||||
# make sure that the user wasn't changed
|
||||
status, data = client.get(f'/api/members/{uwldap_user.uid}')
|
||||
assert status == 200
|
||||
assert data['program'] == ldap_user.program
|
||||
|
||||
status, data = client.post(
|
||||
'/api/uwldap/updateprograms', json={'members': ['no_such_user']})
|
||||
assert status == 200
|
||||
assert data == []
|
||||
|
||||
status, data = client.post('/api/uwldap/updateprograms')
|
||||
assert status == 200
|
||||
|
||||
# make sure that the user was changed
|
||||
status, data = client.get(f'/api/members/{uwldap_user.uid}')
|
||||
assert data['program'] == 'New Program'
|
Loading…
Reference in New Issue