# Path: amivapi/cron.py
# def run_scheduled_tasks():
#     """ Check for scheduled task, which have passed the deadline and run them.
#     This needs an app context.
#     """
#     while True:
#         task = (current_app.data.driver.db['scheduled_tasks']
#                 .find_one_and_delete(
#                     {'time': {'$lte': datetime.utcnow()}}))
# 
#         if task is None:
#             return
# 
#         args = pickle.loads(task['args'])
#         func = schedulable_functions[task['function']]
#         func(*args)
# 
# Path: amivapi/tests/utils.py
# class WebTestNoAuth(WebTest):
#     """WebTest without authentification."""
# 
#     def setUp(self, **extra_config):
#         """Use auth hook to always authenticate as root for every request."""
#         super().setUp(**extra_config)
# 
#         def authenticate_root(resource):
#             g.resource_admin = True
# 
#         self.app.after_auth += authenticate_root


from datetime import timedelta
from freezegun import freeze_time
from amivapi.cron import run_scheduled_tasks
from amivapi.tests.utils import WebTestNoAuth
# license: AGPLv3, see LICENSE for details. In addition we strongly encourage
#          you to buy us beer if we meet and you like the software.
"""Test that expired group memberships are deleted."""




class GroupMembershipExpiry(WebTestNoAuth):
    """Test that members are removed from groups, when the membership has
    expired."""

    def test_expired_groupmembership_gets_removed(self):
        user = self.new_object('users')
        group = self.new_object('groups')

        with self.app.app_context(), freeze_time(
                "2016-01-01 00:00:00") as frozen_time:
            self.new_object('groupmemberships',
                            user=str(user['_id']),
                            group=str(group['_id']),
                            expiry='2016-01-03T00:00:00Z')

            frozen_time.tick(delta=timedelta(days=1))
            run_scheduled_tasks()

            self.assertEqual(
                self.app.data.driver.db['groupmemberships'].count_documents({}),
                1)

            frozen_time.tick(delta=timedelta(days=2))

# Path: amivapi/cron.py
# def run_scheduled_tasks():
#     """ Check for scheduled task, which have passed the deadline and run them.
#     This needs an app context.
#     """
#     while True:
#         task = (current_app.data.driver.db['scheduled_tasks']
#                 .find_one_and_delete(
#                     {'time': {'$lte': datetime.utcnow()}}))
# 
#         if task is None:
#             return
# 
#         args = pickle.loads(task['args'])
#         func = schedulable_functions[task['function']]
#         func(*args)
# 
# Path: amivapi/tests/utils.py
# class WebTestNoAuth(WebTest):
#     """WebTest without authentification."""
# 
#     def setUp(self, **extra_config):
#         """Use auth hook to always authenticate as root for every request."""
#         super().setUp(**extra_config)
# 
#         def authenticate_root(resource):
#             g.resource_admin = True
# 
#         self.app.after_auth += authenticate_root


# Path: amivapi/tests/groups/test_expiry.py
from datetime import timedelta
from freezegun import freeze_time
from amivapi.cron import run_scheduled_tasks
from amivapi.tests.utils import WebTestNoAuth

# license: AGPLv3, see LICENSE for details. In addition we strongly encourage
#          you to buy us beer if we meet and you like the software.
"""Test that expired group memberships are deleted."""




class GroupMembershipExpiry(WebTestNoAuth):
    """Test that members are removed from groups, when the membership has
    expired."""

    def test_expired_groupmembership_gets_removed(self):
        user = self.new_object('users')
        group = self.new_object('groups')

        with self.app.app_context(), freeze_time(
                "2016-01-01 00:00:00") as frozen_time:
            self.new_object('groupmemberships',
                            user=str(user['_id']),
                            group=str(group['_id']),
                            expiry='2016-01-03T00:00:00Z')

            frozen_time.tick(delta=timedelta(days=1))
            run_scheduled_tasks()

            self.assertEqual(
                self.app.data.driver.db['groupmemberships'].count_documents({}),
                1)

            frozen_time.tick(delta=timedelta(days=2))

            run_scheduled_tasks()