Пример #1
0
    def test_repeat_record_created(self):
        '''
        Tests that whenever an application with a repeater is saved that a repeat record is created.
        '''
        application = Application(domain=self.domain)
        application.save()

        repeat_records = RepeatRecord.all(domain=self.domain,
                                          due_before=datetime.utcnow())
        self.assertEqual(len(repeat_records), 0)

        app_structure_repeater = AppStructureRepeater(domain=self.domain,
                                                      url=self.forwarding_url)
        app_structure_repeater.save()

        application.save()
        repeat_records = RepeatRecord.all(domain=self.domain,
                                          due_before=datetime.utcnow())

        self.assertEqual(len(repeat_records), 1)
        for repeat_record in repeat_records:
            self.assertEqual(repeat_record.url, self.forwarding_url)
            self.assertEqual(repeat_record.get_payload(), application.get_id)
            repeat_record.delete()

        application.delete()
        app_structure_repeater.delete()
Пример #2
0
class TestAppStructureRepeater(TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestAppStructureRepeater, cls).setUpClass()
        cls.client = Client()
        cls.domain = 'bedazzled'
        cls.forwarding_url = 'http://not-a-real-url-at-all'

    def tearDown(self):
        delete_all_repeat_records()

    def test_repeat_record_not_created(self):
        """
        When an application without a repeater is saved, then a repeat record should not be created
        """
        self.application = Application(domain=self.domain)
        self.application.save()
        self.addCleanup(self.application.delete)

        # Enqueued repeat records have next_check set 48 hours in the future.
        later = datetime.utcnow() + timedelta(hours=48 + 1)
        repeat_records = RepeatRecord.all(domain=self.domain, due_before=later)
        self.assertEqual(len(repeat_records), 0)

    def test_repeat_record_created(self):
        """
        When an application with a repeater is saved, then a repeat record should be created
        """
        self.app_structure_repeater = AppStructureRepeater(
            domain=self.domain, url=self.forwarding_url)
        self.app_structure_repeater.save()
        self.addCleanup(self.app_structure_repeater.delete)

        self.application = Application(domain=self.domain)
        self.application.save()
        self.addCleanup(self.application.delete)

        later = datetime.utcnow() + timedelta(hours=48 + 1)
        repeat_records = RepeatRecord.all(domain=self.domain, due_before=later)
        self.assertEqual(len(repeat_records), 1)

    def test_repeat_record_forwarded(self):
        """
        When an application with a repeater is saved, then HQ should try to forward the repeat record
        """
        self.app_structure_repeater = AppStructureRepeater(
            domain=self.domain, url=self.forwarding_url)
        self.app_structure_repeater.save()
        self.addCleanup(self.app_structure_repeater.delete)

        with patch('corehq.motech.repeaters.models.simple_post') as mock_fire:
            self.application = Application(domain=self.domain)
            self.application.save()
            self.addCleanup(self.application.delete)

            self.assertEqual(mock_fire.call_count, 1)
Пример #3
0
    def test_repeat_record_created(self):
        '''
        Tests that whenever an application with a repeater is saved that a repeat record is created.
        '''
        application = Application(domain=self.domain)
        application.save()

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=datetime.utcnow())
        self.assertEqual(len(repeat_records), 0)

        app_structure_repeater = AppStructureRepeater(domain=self.domain, url=self.forwarding_url)
        app_structure_repeater.save()

        application.save()
        repeat_records = RepeatRecord.all(domain=self.domain, due_before=datetime.utcnow())

        self.assertEqual(len(repeat_records), 1)
        for repeat_record in repeat_records:
                self.assertEqual(repeat_record.repeater.get_url(repeat_record), self.forwarding_url)
                self.assertEqual(repeat_record.get_payload(), application.get_id)
                repeat_record.delete()

        application.delete()
        app_structure_repeater.delete()
Пример #4
0
class TestAppStructureRepeater(TestCase, DomainSubscriptionMixin):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.client = Client()
        cls.forwarding_url = 'http://not-a-real-url-at-all'

        cls.domain = 'bedazzled'
        cls.domain_obj = Domain.get_or_create_with_name(cls.domain)

        # DATA_FORWARDING is on PRO and above
        cls.setup_subscription(cls.domain, SoftwarePlanEdition.PRO)

    @classmethod
    def tearDownClass(cls):
        cls.teardown_subscriptions()
        cls.domain_obj.delete()
        clear_plan_version_cache()
        super().tearDownClass()

    def tearDown(self):
        delete_all_repeat_records()
        super().tearDown()

    def test_repeat_record_not_created(self):
        """
        When an application without a repeater is saved, then a repeat record should not be created
        """
        self.application = Application(domain=self.domain)
        self.application.save()
        self.addCleanup(self.application.delete)

        # Enqueued repeat records have next_check set 48 hours in the future.
        later = datetime.utcnow() + timedelta(hours=48 + 1)
        repeat_records = RepeatRecord.all(domain=self.domain, due_before=later)
        self.assertEqual(len(repeat_records), 0)

    def test_repeat_record_created(self):
        """
        When an application with a repeater is saved, then a repeat record should be created
        """
        self.app_structure_repeater = AppStructureRepeater(
            domain=self.domain, url=self.forwarding_url)
        self.app_structure_repeater.save()
        self.addCleanup(self.app_structure_repeater.delete)

        self.application = Application(domain=self.domain)
        self.application.save()
        self.addCleanup(self.application.delete)

        later = datetime.utcnow() + timedelta(hours=48 + 1)
        repeat_records = RepeatRecord.all(domain=self.domain, due_before=later)
        self.assertEqual(len(repeat_records), 1)

    def test_repeat_record_forwarded(self):
        """
        When an application with a repeater is saved, then HQ should try to forward the repeat record
        """
        self.app_structure_repeater = AppStructureRepeater(
            domain=self.domain, url=self.forwarding_url)
        self.app_structure_repeater.save()
        self.addCleanup(self.app_structure_repeater.delete)

        with patch(
                'corehq.motech.repeaters.models.simple_request') as mock_fire:
            self.application = Application(domain=self.domain)
            self.application.save()
            self.addCleanup(self.application.delete)

            self.assertEqual(mock_fire.call_count, 1)