async def test_creating_roles(app, role, role_def, response_type, response, caplog):
    """Test raising errors and warnings when creating/modifying roles"""
    caplog.set_level(logging.INFO)

    db = app.db

    if response_type == 'error':
        with pytest.raises(response):
            roles.create_role(db, role_def)

    elif response_type == 'warning' or response_type == 'info':
        roles.create_role(db, role_def)
        if response:
            assert response in caplog.text
        # check the role has been created/modified
        role = orm.Role.find(db, role_def['name'])
        assert role is not None
        if 'description' in role_def.keys():
            assert role.description == role_def['description']
        if 'scopes' in role_def.keys():
            assert role.scopes == role_def['scopes']

    # make sure no warnings/info logged when the role exists and its definition hasn't been changed
    elif response_type == 'no-log':
        roles.create_role(db, role_def)
        assert caplog.text == ""
        role = orm.Role.find(db, role_def['name'])
        assert role is not None
Test raising errors and warnings when creating/modifying roles
jupyterhub/tests/test_roles.py