Exemplo n.º 1
0
    def test_periodic_func(self):
        with self.app.app_context(), freeze_time(
                "2016-01-01 00:00:00") as frozen_time:
            # We need to define the function in here to make sure the first
            # call is scheduled to the frozen time
            @periodic(timedelta(minutes=5), "arg")
            def periodic_function(arg):
                CronTest.run_count += 1
                CronTest.received_arg = arg

            self.assertEqual(CronTest.run_count, 0)
            run_scheduled_tasks()

            # Check the function has run and got the correct argument
            self.assertEqual(CronTest.run_count, 1)
            self.assertEqual(CronTest.received_arg, "arg")

            # Now check that it is called at correct intervals
            for _ in range(0, 32):
                frozen_time.tick(delta=timedelta(minutes=1))
                run_scheduled_tasks()

            self.assertEqual(CronTest.run_count, 7)

            # Do some cleanup! Else this will get called in other tests..
            cron.periodic_functions.remove(periodic_function)
Exemplo n.º 2
0
    def test_periodic_func(self):
        with self.app.app_context(), freeze_time(
                "2016-01-01 00:00:00") as frozen_time:
            # We need to define the function in here to make sure the first
            # call is scheduled to the frozen time
            @periodic(timedelta(minutes=5), "arg")
            def periodic_function(arg):
                CronTest.run_count += 1
                CronTest.received_arg = arg

            self.assertEqual(CronTest.run_count, 0)
            run_scheduled_tasks()

            # Check the function has run and got the correct argument
            self.assertEqual(CronTest.run_count, 1)
            self.assertEqual(CronTest.received_arg, "arg")

            # Now check that it is called at correct intervals
            for _ in range(0, 32):
                frozen_time.tick(delta=timedelta(minutes=1))
                run_scheduled_tasks()

            self.assertEqual(CronTest.run_count, 7)

            # Do some cleanup! Else this will get called in other tests..
            cron.periodic_functions.remove(periodic_function)
Exemplo n.º 3
0
    def test_schedule_once_soon_works(self):
        with self.app.app_context():
            CronTest.run_count = 0

            @schedulable
            def inc():
                CronTest.run_count += 1

            schedule_once_soon(inc)
            print("nmow")
            schedule_once_soon(inc)

            run_scheduled_tasks()

            self.assertEqual(CronTest.run_count, 1)
Exemplo n.º 4
0
    def test_schedule_once_soon_works(self):
        with self.app.app_context():
            CronTest.run_count = 0

            @schedulable
            def inc():
                CronTest.run_count += 1

            schedule_once_soon(inc)
            print("nmow")
            schedule_once_soon(inc)

            run_scheduled_tasks()

            self.assertEqual(CronTest.run_count, 1)
Exemplo n.º 5
0
    def test_session_expiry(self):
        with self.app.app_context(), freeze_time() as frozen_time:
            self.new_object("users", nethz="pablo", password="******")
            self.api.post('/sessions',
                          data={"username": "******", "password": "******"},
                          status_code=201)

            frozen_time.tick(delta=self.app.config['SESSION_TIMEOUT'] -
                             timedelta(days=1))
            run_scheduled_tasks()

            self.assertEqual(self.app.data.driver.db['sessions'].find().count(),
                             1)

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

            self.assertEqual(self.app.data.driver.db['sessions'].find().count(),
                             0)
Exemplo n.º 6
0
    def test_session_expiry(self):
        with self.app.app_context(), freeze_time() as frozen_time:
            self.new_object("users", nethz="pablo", password="******")
            self.api.post('/sessions',
                          data={"username": "******", "password": "******"},
                          status_code=201)

            frozen_time.tick(delta=self.app.config['SESSION_TIMEOUT'] -
                             timedelta(days=1))
            run_scheduled_tasks()

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

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

            self.assertEqual(
                self.app.data.driver.db['sessions'].count_documents({}), 0)
Exemplo n.º 7
0
    def test_scheduled_email_on_delete(self):
        """Test that a user receives only one an email if an entry is deleted"""
        with self.app.app_context(), freeze_time(
                "2017-01-01 00:00:00") as frozen_time:
            user_id = 24 * '0'
            blacklist_id = 24 * '1'

            # Create user and blacklist entry
            self.load_fixture(
                {'users': [{
                    '_id': user_id,
                    'email': "*****@*****.**"
                }]})
            r = self.load_fixture({
                'blacklist': [{
                    '_id': blacklist_id,
                    'user': user_id,
                    'reason': "Test1",
                    'end_time': "2017-01-02T00:00:00Z",
                }]
            })

            etag = r[0]['_etag']

            header = {'If-Match': etag}
            with freeze_time(datetime(2017, 1, 1)):
                r = self.api.delete("/blacklist/%s" % blacklist_id,
                                    headers=header,
                                    token=self.get_root_token(),
                                    status_code=204)

            run_scheduled_tasks()

            # Only the creation email should be sent
            self.assertEqual(len(self.app.test_mails), 2)

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

            run_scheduled_tasks()

            # Since the entry was deleted no mail should be sent
            self.assertEqual(len(self.app.test_mails), 2)
Exemplo n.º 8
0
    def test_scheduled_function_gets_called(self):
        with self.app.app_context(), freeze_time(
                "2016-01-01 00:00:00") as frozen_time:
            @schedulable
            def scheduled_function(arg):
                CronTest.has_run = True
                CronTest.received_arg = arg

            schedule_task(datetime(2016, 1, 1, 1, 0, 0),
                          scheduled_function,
                          "arg")

            run_scheduled_tasks()

            self.assertFalse(CronTest.has_run)

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

            self.assertTrue(CronTest.has_run)
            self.assertEqual(CronTest.received_arg, "arg")
Exemplo n.º 9
0
    def test_scheduled_function_gets_called(self):
        with self.app.app_context(), freeze_time(
                "2016-01-01 00:00:00") as frozen_time:

            @schedulable
            def scheduled_function(arg):
                CronTest.has_run = True
                CronTest.received_arg = arg

            schedule_task(datetime(2016, 1, 1, 1, 0, 0), scheduled_function,
                          "arg")

            run_scheduled_tasks()

            self.assertFalse(CronTest.has_run)

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

            self.assertTrue(CronTest.has_run)
            self.assertEqual(CronTest.received_arg, "arg")
Exemplo n.º 10
0
    def test_update_scheduled_task(self):
        with self.app.app_context(), freeze_time(
                "2016-01-01 00:00:00") as frozen_time:

            @schedulable
            def tester(arg):
                CronTest.has_run = True
                CronTest.received_arg = arg

            schedule_task(datetime(2016, 1, 1, 1, 0, 0),
                          tester,
                          "arg")

            run_scheduled_tasks()

            self.assertFalse(CronTest.has_run)

            update_scheduled_task(datetime(2016, 1, 1, 3, 20, 0),
                                  tester,
                                  "new-arg")

            frozen_time.tick(delta=timedelta(hours=2))
            run_scheduled_tasks()

            self.assertFalse(CronTest.has_run)

            frozen_time.tick(delta=timedelta(hours=3))
            run_scheduled_tasks()

            self.assertTrue(CronTest.has_run)
            self.assertEqual(CronTest.received_arg, "new-arg")
Exemplo n.º 11
0
    def test_update_scheduled_task(self):
        with self.app.app_context(), freeze_time(
                "2016-01-01 00:00:00") as frozen_time:

            @schedulable
            def tester(arg):
                CronTest.has_run = True
                CronTest.received_arg = arg

            schedule_task(datetime(2016, 1, 1, 1, 0, 0), tester, "arg")

            run_scheduled_tasks()

            self.assertFalse(CronTest.has_run)

            update_scheduled_task(datetime(2016, 1, 1, 3, 20, 0), tester,
                                  "new-arg")

            frozen_time.tick(delta=timedelta(hours=2))
            run_scheduled_tasks()

            self.assertFalse(CronTest.has_run)

            frozen_time.tick(delta=timedelta(hours=3))
            run_scheduled_tasks()

            self.assertTrue(CronTest.has_run)
            self.assertEqual(CronTest.received_arg, "new-arg")
Exemplo n.º 12
0
    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'].find().count(), 1)

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

            self.assertEqual(
                self.app.data.driver.db['sessions'].find().count(), 0)
Exemplo n.º 13
0
    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()

            self.assertEqual(
                self.app.data.driver.db['sessions'].count_documents({}), 0)
Exemplo n.º 14
0
    def test_receive_scheduled_email_on_create(self):
        """Test if a user receives an email if the end_time is reached"""
        with self.app.app_context(), freeze_time(
                "2017-01-01 00:00:00") as frozen_time:
            user_id = 24 * '0'
            blacklist_id = 24 * '1'

            # Create user and blacklist entry
            self.load_fixture(
                {'users': [{
                    '_id': user_id,
                    'email': "*****@*****.**"
                }]})
            self.load_fixture({
                'blacklist': [{
                    '_id': blacklist_id,
                    'user': user_id,
                    'reason': "Test1",
                    'end_time': datetime(2017, 1, 2)
                }]
            })

            run_scheduled_tasks()

            self.assertEqual(len(self.app.test_mails), 1)

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

            run_scheduled_tasks()

            # Check mail
            mail = self.app.test_mails[1]
            self.assertEqual(mail['receivers'], '*****@*****.**')
            expected_text = (
                "Congratulations, your blacklist entry with the following "
                "reason has been removed:\n\nTest1\n\nBest Regards,\nAMIV")
            self.assertEqual(mail['text'], expected_text)
Exemplo n.º 15
0
    def test_receive_scheduled_email_on_patch(self):
        """Test if a user receives an email if the end_time is reached"""
        with self.app.app_context(), freeze_time(
                "2017-01-01 00:00:00") as frozen_time:
            user_id = 24 * '0'
            blacklist_id = 24 * '1'

            # Create user and blacklist entry
            self.load_fixture(
                {'users': [{
                    '_id': user_id,
                    'email': "*****@*****.**"
                }]})
            r = self.load_fixture({
                'blacklist': [{
                    '_id': blacklist_id,
                    'user': user_id,
                    'reason': "Test1",
                    'end_time': "2017-01-02T00:00:00Z",
                }]
            })

            etag = r[0]['_etag']

            patch = {
                'user': user_id,
                'reason': "Test1",
                'end_time': '2017-01-03T00:00:00Z'
            }

            header = {'If-Match': etag}
            with freeze_time(datetime(2017, 1, 1)):
                r = self.api.patch("/blacklist/%s" % blacklist_id,
                                   data=patch,
                                   headers=header,
                                   token=self.get_root_token(),
                                   status_code=200)

            run_scheduled_tasks()

            # Only the creation email should be sent
            self.assertEqual(len(self.app.test_mails), 1)

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

            run_scheduled_tasks()

            # Since the date was changed to the future, no email should be sent
            self.assertEqual(len(self.app.test_mails), 1)

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

            run_scheduled_tasks()

            # Check mail
            mail = self.app.test_mails[1]
            self.assertEqual(mail['receivers'], '*****@*****.**')
            expected_text = (
                "Congratulations, your blacklist entry with the following "
                "reason has been removed:\n\nTest1\n\nBest Regards,\nAMIV")
            self.assertEqual(mail['text'], expected_text)
Exemplo n.º 16
0
def run_cron(app):
    """Run scheduled tasks with the given app."""
    echo("Executing scheduled tasks...")
    with app.app_context():
        run_scheduled_tasks()
Exemplo n.º 17
0
def run_cron(app):
    """Run scheduled tasks with the given app."""
    echo("Executing scheduled tasks...")
    with app.app_context():
        run_scheduled_tasks()
Exemplo n.º 18
0
def cron(config):
    """Run scheduled tasks."""
    app = create_app(config_file=config)
    with app.app_context():
        run_scheduled_tasks()