Beispiel #1
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)
Beispiel #2
0
    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)
Beispiel #3
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)
Beispiel #4
0
def create_app_structure_repeat_records(sender, application, **kwargs):
    from corehq.motech.repeaters.models import AppStructureRepeater
    domain = application.domain
    if domain:
        repeaters = AppStructureRepeater.by_domain(domain)
        for repeater in repeaters:
            repeater.register(application)
Beispiel #5
0
def create_app_structure_repeat_records(sender, application, **kwargs):
    from corehq.motech.repeaters.models import AppStructureRepeater
    domain = application.domain
    if domain:
        repeaters = AppStructureRepeater.by_domain(domain)
        for repeater in repeaters:
            repeater.register(application)
Beispiel #6
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()
Beispiel #7
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()
Beispiel #8
0
    def test_add_repeater(self):
        forwarding_url = 'https://example.com/forwarding'

        self.client.login(username=self.username, password=self.password)

        post_url = reverse('add_repeater', kwargs={'domain': self.domain.name, 'repeater_type': 'AppStructureRepeater'})
        response = self.client.post(post_url, {'url': forwarding_url}, follow=True)
        self.assertEqual(response.status_code, 200)

        self.client.logout()
        
        app_structure_repeaters = AppStructureRepeater.by_domain(self.domain.name)
        self.assertEqual(len(app_structure_repeaters), 1)

        for app_structure_repeater in app_structure_repeaters:
            app_structure_repeater.delete()
Beispiel #9
0
    def test_add_repeater(self):
        forwarding_url = 'https://example.com/forwarding'

        self.client.login(username=self.username, password=self.password)

        post_url = reverse('add_repeater', kwargs={'domain': self.domain.name, 'repeater_type': 'AppStructureRepeater'})
        response = self.client.post(post_url, {'url': forwarding_url}, follow=True)
        self.assertEqual(response.status_code, 200)

        self.client.logout()
        
        app_structure_repeaters = AppStructureRepeater.by_domain(self.domain.name)
        self.assertEqual(len(app_structure_repeaters), 1)

        for app_structure_repeater in app_structure_repeaters:
            app_structure_repeater.delete()
Beispiel #10
0
    def test_add_repeater(self):
        self.client.login(username=self.username, password=self.password)

        with connection_fixture(self.domain.name) as connx:
            post_url = reverse('add_repeater',
                               kwargs={
                                   'domain': self.domain.name,
                                   'repeater_type': 'AppStructureRepeater'
                               })
            response = self.client.post(post_url,
                                        {'connection_settings_id': connx.id},
                                        follow=True)
            self.assertEqual(response.status_code, 200)

            app_structure_repeaters = AppStructureRepeater.by_domain(
                self.domain.name)
            self.assertEqual(len(app_structure_repeaters), 1)

            for app_structure_repeater in app_structure_repeaters:
                app_structure_repeater.delete()

        self.client.logout()
Beispiel #11
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)