def test_monitor_users_disable_user_gone_no_token_perms(app):
    """If the user is gone, disable even a token with no permissions."""
    with app.app_context():
        insert_usr(app, permissions=[])
        with mocked_perms({}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_disabled(app, js, [])
def test_monitor_users_disable_changed_perms(app):
    """If the user's permissions are a, b and the token's are b, c, disable"""
    with app.app_context():
        insert_usr(app, permissions=[B, C])
        with mocked_perms({'*****@*****.**': [A, B]}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_disabled(app, js, [B, C])
def test_monitor_users_disable_user_gone(app):
    """If the user is gone, disable"""
    with app.app_context():
        insert_usr(app, permissions=[A, B])
        with mocked_perms({}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_disabled(app, js, [A, B])
def test_monitor_users_reenable(app):
    """If the token is disabled and can be re-enabled, it is"""
    with app.app_context():
        insert_usr(app, permissions=[A, B], disabled=True)
        with mocked_perms({'*****@*****.**': [A, B, C]}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_reenabled(app, js, [A, B])
def test_monitor_users_disable_reduced_perms(app):
    """If the user's permissions are a subset of those for the token, disable"""
    with app.app_context():
        insert_usr(app, permissions=[A, B])
        with mocked_perms({'*****@*****.**': [A]}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_disabled(app, js, [A, B])
def test_monitor_users_same_permissions(app):
    """If the user's permissions match the token's, leave it enabled"""
    with app.app_context():
        insert_usr(app, permissions=[A, B])
        with mocked_perms({'*****@*****.**': [A, B]}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_enabled(app)
def test_monitor_users_ample_permissions(app):
    """If the user's permissions exceed the token's, leave it enabled"""
    with app.app_context():
        insert_usr(app, permissions=[A, B])
        with mocked_perms({'*****@*****.**': [A, B, C]}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_enabled(app)
def test_monitor_users_disable_user_gone_no_token_perms(app):
    """If the user is gone, disable even a token with no permissions."""
    with app.app_context():
        insert_usr(app, permissions=[])
        with mocked_perms({}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_disabled(app, js, [])
def test_monitor_users_disable_user_gone(app):
    """If the user is gone, disable"""
    with app.app_context():
        insert_usr(app, permissions=[A, B])
        with mocked_perms({}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_disabled(app, js, [A, B])
def test_monitor_users_disable_changed_perms(app):
    """If the user's permissions are a, b and the token's are b, c, disable"""
    with app.app_context():
        insert_usr(app, permissions=[B, C])
        with mocked_perms({'*****@*****.**': [A, B]}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_disabled(app, js, [B, C])
def test_monitor_users_disable_reduced_perms(app):
    """If the user's permissions are a subset of those for the token, disable"""
    with app.app_context():
        insert_usr(app, permissions=[A, B])
        with mocked_perms({'*****@*****.**': [A]}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_disabled(app, js, [A, B])
def test_monitor_users_reenable(app):
    """If the token is disabled and can be re-enabled, it is"""
    with app.app_context():
        insert_usr(app, permissions=[A, B], disabled=True)
        with mocked_perms({'*****@*****.**': [A, B, C]}):
            js = mock.Mock()
            usermonitor.monitor_users(js)
        assert_reenabled(app, js, [A, B])