Example #1
0
    def setUp(self):
        self.domain = "test-domain"
        create_domain(self.domain)
        self.post_xml(xform_xml, self.domain)

        self.repeater = CaseRepeater(domain=self.domain,
                                     url='case-repeater-url',
                                     version=V1,
                                     format='new_format')
        self.repeater.save()
Example #2
0
 def setUp(self):
     self.client = Client()
     self.domain = "test-domain"
     self.case_repeater = CaseRepeater(domain=self.domain,
                                       url='case-repeater-url',
                                       version=V1)
     self.case_repeater.save()
     self.form_repeater = FormRepeater(domain=self.domain,
                                       url='form-repeater-url')
     self.form_repeater.save()
     self.log = []
     self.post_xml(xform_xml)
    def handle(self, *args, **options):
        domain = 'care-bihar'

        # forward all cases that were last modified between these dates
        def should_forward_case(case):
            min_date = datetime(2013, 9, 10)
            max_date = datetime(2013, 11, 7)
            return (case.server_modified_on
                    and min_date < case.server_modified_on < max_date)

        prod_repeater = CaseRepeater.get('a478a5a3d8964338cb3124de77e3ec58')
        success_count = 0
        fail_count = 0
        for case in get_cases_in_domain(domain):
            try:
                if should_forward_case(case):
                    prod_repeater.register(case)
                    success_count += 1
            except Exception:
                fail_count += 1
                logging.exception(
                    'problem creating repeater stub for case %s' % case._id)

        print 'successfully forwarded %s cases. %s were not processed' % (
            success_count, fail_count)
Example #4
0
    def setUp(self):

        self.domain = "test-domain"
        create_domain(self.domain)
        self.case_repeater = CaseRepeater(
            domain=self.domain,
            url='case-repeater-url',
            version=V1,
        )
        self.case_repeater.save()
        self.form_repeater = FormRepeater(
            domain=self.domain,
            url='form-repeater-url',
        )
        self.form_repeater.save()
        self.log = []
        self.post_xml(xform_xml, self.domain)
Example #5
0
    def setUpClass(cls):
        cls.domain_name = "test-domain"
        cls.domain = create_domain(cls.domain_name)

        cls.repeater = CaseRepeater(
            domain=cls.domain_name,
            url="case-repeater-url",
        )
        cls.repeater.save()
 def setUp(self):
     self.client = Client()
     self.domain = "test-domain"
     self.case_repeater = CaseRepeater(domain=self.domain, url='case-repeater-url', version=V1)
     self.case_repeater.save()
     self.form_repeater = FormRepeater(domain=self.domain, url='form-repeater-url')
     self.form_repeater.save()
     self.log = []
     self.post_xml(xform_xml)
Example #7
0
 def setUp(self):
     self.client = Client()
     self.domain = "test-domain"
     create_domain(self.domain)
     self.case_repeater = CaseRepeater(domain=self.domain, url='case-repeater-url', version=V1, next_check=datetime.utcnow() - timedelta(seconds=5))
     self.case_repeater.save()
     self.form_repeater = FormRepeater(domain=self.domain, url='form-repeater-url', next_check=datetime.utcnow() - timedelta(seconds=10))
     self.form_repeater.save()
     self.log = []
     self.post_xml(xform_xml)
Example #8
0
def domain_forwarding(request, domain):
    form_repeaters = FormRepeater.by_domain(domain)
    case_repeaters = CaseRepeater.by_domain(domain)
    short_form_repeaters = ShortFormRepeater.by_domain(domain)
    return render(request, "domain/admin/domain_forwarding.html", {
        "domain": domain,
        "repeaters": (
            ("FormRepeater", form_repeaters),
            ("CaseRepeater", case_repeaters),
            ("ShortFormRepeater", short_form_repeaters)
        ),
    })
Example #9
0
    def setUp(self):
        self.domain = "test-domain"
        create_domain(self.domain)
        self.post_xml(xform_xml, self.domain)

        self.repeater = CaseRepeater(
            domain=self.domain,
            url='case-repeater-url',
            version=V1,
            format='new_format'
        )
        self.repeater.save()
Example #10
0
def domain_forwarding(request, domain):
    form_repeaters = FormRepeater.by_domain(domain)
    case_repeaters = CaseRepeater.by_domain(domain)
    short_form_repeaters = ShortFormRepeater.by_domain(domain)
    return render(
        request, "domain/admin/domain_forwarding.html", {
            "domain":
            domain,
            "repeaters": (("FormRepeater", form_repeaters),
                          ("CaseRepeater", case_repeaters),
                          ("ShortFormRepeater", short_form_repeaters)),
        })
Example #11
0
    def test_get_list(self):
        self.client.login(username=self.username, password=self.password)

        # Add a form repeater and check that it comes back
        form_repeater = FormRepeater(domain=self.domain.name, url='http://example.com/forwarding/form')
        form_repeater.save()
        backend_id = form_repeater._id

        response = self.client.get(self.list_endpoint)
        self.assertEqual(response.status_code, 200)

        api_repeaters = simplejson.loads(response.content)['objects']
        self.assertEqual(len(api_repeaters), 1)
        self.assertEqual(api_repeaters[0]['id'], backend_id)
        self.assertEqual(api_repeaters[0]['url'], form_repeater.url)
        self.assertEqual(api_repeaters[0]['domain'], form_repeater.domain)
        self.assertEqual(api_repeaters[0]['type'], 'FormRepeater')

        # Add a case repeater and check that both come back
        case_repeater = CaseRepeater(domain=self.domain.name, url='http://example.com/forwarding/case')
        case_repeater.save()
        backend_id = case_repeater._id

        response = self.client.get(self.list_endpoint)
        self.assertEqual(response.status_code, 200)

        api_repeaters = simplejson.loads(response.content)['objects']
        self.assertEqual(len(api_repeaters), 2)

        api_case_repeater = filter(lambda r: r['type'] == 'CaseRepeater', api_repeaters)[0]
        self.assertEqual(api_case_repeater['id'], case_repeater._id)
        self.assertEqual(api_case_repeater['url'], case_repeater.url)    
        self.assertEqual(api_case_repeater['domain'], case_repeater.domain)    

        form_repeater.delete()
        case_repeater.delete()
Example #12
0
 def setUp(self):
     self.client = Client()
     self.domain = "test-domain"
     self.case_repeater = CaseRepeater(domain=self.domain, url='case-repeater-url', version=V1)
     self.case_repeater.save()
     self.form_repeater = FormRepeater(domain=self.domain, url='form-repeater-url')
     self.form_repeater.save()
     f = StringIO(xform_xml)
     f.name = 'register.xml'
     self.client.post(
         reverse('receiver_post', args=[self.domain]), {
             'xml_submission_file': f
         }
     )
     self.log = []
Example #13
0
    def test_get_list(self):
        self.client.login(username=self.username, password=self.password)

        # Add a form repeater and check that it comes back
        form_repeater = FormRepeater(domain=self.domain.name, url="http://example.com/forwarding/form")
        form_repeater.save()
        backend_id = form_repeater._id

        response = self.client.get(self.list_endpoint)
        self.assertEqual(response.status_code, 200)

        api_repeaters = json.loads(response.content)["objects"]
        self.assertEqual(len(api_repeaters), 1)
        self.assertEqual(api_repeaters[0]["id"], backend_id)
        self.assertEqual(api_repeaters[0]["url"], form_repeater.url)
        self.assertEqual(api_repeaters[0]["domain"], form_repeater.domain)
        self.assertEqual(api_repeaters[0]["type"], "FormRepeater")

        # Add a case repeater and check that both come back
        case_repeater = CaseRepeater(domain=self.domain.name, url="http://example.com/forwarding/case")
        case_repeater.save()
        backend_id = case_repeater._id

        response = self.client.get(self.list_endpoint)
        self.assertEqual(response.status_code, 200)

        api_repeaters = json.loads(response.content)["objects"]
        self.assertEqual(len(api_repeaters), 2)

        api_case_repeater = filter(lambda r: r["type"] == "CaseRepeater", api_repeaters)[0]
        self.assertEqual(api_case_repeater["id"], case_repeater._id)
        self.assertEqual(api_case_repeater["url"], case_repeater.url)
        self.assertEqual(api_case_repeater["domain"], case_repeater.domain)

        form_repeater.delete()
        case_repeater.delete()
Example #14
0
    def setUp(self):

        self.domain = "test-domain"
        create_domain(self.domain)
        self.case_repeater = CaseRepeater(
            domain=self.domain,
            url='case-repeater-url',
            version=V1,
        )
        self.case_repeater.save()
        self.form_repeater = FormRepeater(
            domain=self.domain,
            url='form-repeater-url',
        )
        self.form_repeater.save()
        self.log = []
        self.post_xml(xform_xml, self.domain)
Example #15
0
class TestRepeaterFormat(BaseRepeaterTest):
    def setUp(self):
        self.domain = "test-domain"
        create_domain(self.domain)
        self.post_xml(xform_xml, self.domain)

        self.repeater = CaseRepeater(domain=self.domain,
                                     url='case-repeater-url',
                                     version=V1,
                                     format='new_format')
        self.repeater.save()

    def tearDown(self):
        self.repeater.delete()
        XFormInstance.get(instance_id).delete()
        repeat_records = RepeatRecord.all()
        for repeat_record in repeat_records:
            repeat_record.delete()

    def test_new_format_same_name(self):
        with self.assertRaises(DuplicateFormatException):

            @RegisterGenerator(CaseRepeater,
                               'case_xml',
                               'XML',
                               is_default=False)
            class NewCaseGenerator(BasePayloadGenerator):
                def get_payload(self, repeat_record, payload_doc):
                    return "some random case"

    def test_new_format_second_default(self):
        with self.assertRaises(DuplicateFormatException):

            @RegisterGenerator(CaseRepeater, 'rubbish', 'XML', is_default=True)
            class NewCaseGenerator(BasePayloadGenerator):
                def get_payload(self, repeat_record, payload_doc):
                    return "some random case"

    def test_new_format_payload(self):
        payload = "some random case"

        @RegisterGenerator(CaseRepeater, 'new_format', 'XML')
        class NewCaseGenerator(BasePayloadGenerator):
            def get_payload(self, repeat_record, payload_doc):
                return payload

        repeat_record = self.repeater.register(case_id)
        post_fn = MagicMock()
        repeat_record.fire(post_fn=post_fn)
        headers = self.repeater.get_headers(repeat_record)
        post_fn.assert_called_with(payload, self.repeater.url, headers=headers)
Example #16
0
class TestRepeaterFormat(BaseRepeaterTest):
    def setUp(self):
        self.domain = "test-domain"
        create_domain(self.domain)
        self.post_xml(xform_xml, self.domain)

        self.repeater = CaseRepeater(
            domain=self.domain,
            url='case-repeater-url',
            version=V1,
            format='new_format'
        )
        self.repeater.save()

    def tearDown(self):
        self.repeater.delete()
        XFormInstance.get(instance_id).delete()
        repeat_records = RepeatRecord.all()
        for repeat_record in repeat_records:
            repeat_record.delete()

    def test_new_format_same_name(self):
        with self.assertRaises(DuplicateFormatException):
            @RegisterGenerator(CaseRepeater, 'case_xml', 'XML', is_default=False)
            class NewCaseGenerator(BasePayloadGenerator):
                def get_payload(self, repeat_record, payload_doc):
                    return "some random case"

    def test_new_format_second_default(self):
        with self.assertRaises(DuplicateFormatException):
            @RegisterGenerator(CaseRepeater, 'rubbish', 'XML', is_default=True)
            class NewCaseGenerator(BasePayloadGenerator):
                def get_payload(self, repeat_record, payload_doc):
                    return "some random case"

    def test_new_format_payload(self):
        payload = "some random case"

        @RegisterGenerator(CaseRepeater, 'new_format', 'XML')
        class NewCaseGenerator(BasePayloadGenerator):
            def get_payload(self, repeat_record, payload_doc):
                return payload

        repeat_record = self.repeater.register(case_id)
        post_fn = MagicMock()
        repeat_record.fire(post_fn=post_fn)
        headers = self.repeater.get_headers(repeat_record)
        post_fn.assert_called_with(payload, self.repeater.url, headers=headers)
    def handle(self, *args, **options):
        domain = 'care-bihar'

        # forward all cases that were last modified between these dates
        def should_forward_case(case):
            min_date = datetime(2013, 9, 10)
            max_date = datetime(2013, 11, 7)
            return (case.server_modified_on
                    and min_date < case.server_modified_on < max_date)

        prod_repeater = CaseRepeater.get('a478a5a3d8964338cb3124de77e3ec58')
        success_count = 0
        fail_count = 0
        for case in get_cases_in_domain(domain):
            try:
                if should_forward_case(case):
                    prod_repeater.register(case)
                    success_count += 1
            except Exception:
                fail_count += 1
                logging.exception('problem creating repeater stub for case %s' % case._id)

        print 'successfully forwarded %s cases. %s were not processed' % (success_count, fail_count)
Example #18
0
    def test_get_list(self):
        self.client.login(username=self.username, password=self.password)

        # Add a form repeater and check that it comes back
        form_repeater = FormRepeater(domain=self.domain.name,
                                     url='http://example.com/forwarding/form')
        form_repeater.save()
        backend_id = form_repeater._id

        response = self.client.get(self.list_endpoint)
        self.assertEqual(response.status_code, 200)

        api_repeaters = simplejson.loads(response.content)['objects']
        self.assertEqual(len(api_repeaters), 1)
        self.assertEqual(api_repeaters[0]['id'], backend_id)
        self.assertEqual(api_repeaters[0]['url'], form_repeater.url)
        self.assertEqual(api_repeaters[0]['domain'], form_repeater.domain)
        self.assertEqual(api_repeaters[0]['type'], 'FormRepeater')

        # Add a case repeater and check that both come back
        case_repeater = CaseRepeater(domain=self.domain.name,
                                     url='http://example.com/forwarding/case')
        case_repeater.save()
        backend_id = case_repeater._id

        response = self.client.get(self.list_endpoint)
        self.assertEqual(response.status_code, 200)

        api_repeaters = simplejson.loads(response.content)['objects']
        self.assertEqual(len(api_repeaters), 2)

        api_case_repeater = filter(lambda r: r['type'] == 'CaseRepeater',
                                   api_repeaters)[0]
        self.assertEqual(api_case_repeater['id'], case_repeater._id)
        self.assertEqual(api_case_repeater['url'], case_repeater.url)
        self.assertEqual(api_case_repeater['domain'], case_repeater.domain)

        form_repeater.delete()
        case_repeater.delete()
Example #19
0
class RepeaterTest(BaseRepeaterTest):
    def setUp(self):

        self.domain = "test-domain"
        create_domain(self.domain)
        self.case_repeater = CaseRepeater(
            domain=self.domain,
            url='case-repeater-url',
            version=V1,
        )
        self.case_repeater.save()
        self.form_repeater = FormRepeater(
            domain=self.domain,
            url='form-repeater-url',
        )
        self.form_repeater.save()
        self.log = []
        self.post_xml(xform_xml, self.domain)

    def clear_log(self):
        for i in range(len(self.log)):
            self.log.pop()

    def make_post_fn(self, status_codes):
        status_codes = iter(status_codes)

        def post_fn(data, url, headers=None):
            status_code = status_codes.next()
            self.log.append((url, status_code, data, headers))

            class resp:
                status = status_code
            return resp

        return post_fn

    def tearDown(self):
        self.case_repeater.delete()
        self.form_repeater.delete()
        XFormInstance.get(instance_id).delete()
        repeat_records = RepeatRecord.all()
        for repeat_record in repeat_records:
            repeat_record.delete()

    def test_repeater(self):
        #  this test should probably be divided into more units

        CommCareCase.get(case_id)

        def now():
            return datetime.utcnow()

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)

        self.clear_log()

        records_by_repeater_id = {}
        for repeat_record in repeat_records:
            repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404]))
            repeat_record.save()
            records_by_repeater_id[repeat_record.repeater_id] = repeat_record

        for (url, status, data, headers) in self.log:
            self.assertEqual(status, 404)

        self.clear_log()

        next_check_time = now() + timedelta(minutes=60)

        repeat_records = RepeatRecord.all(
            domain=self.domain,
            due_before=now() + timedelta(minutes=15),
        )
        self.assertEqual(len(repeat_records), 0)

        repeat_records = RepeatRecord.all(
            domain=self.domain,
            due_before=next_check_time + timedelta(seconds=2),
        )
        self.assertEqual(len(repeat_records), 2)

        for repeat_record in repeat_records:
            self.assertLess(abs(next_check_time - repeat_record.next_check),
                            timedelta(seconds=3))
            repeat_record.fire(post_fn=self.make_post_fn([404, 200]))
            repeat_record.save()

        self.assertEqual(len(self.log), 4)

        # The following is pretty fickle and depends on which of
        #   - corehq.apps.receiverwrapper.signals
        #   - casexml.apps.case.signals
        # gets loaded first.
        # This is deterministic but easily affected by minor code changes

        # check case stuff
        rec = records_by_repeater_id[self.case_repeater.get_id]
        self.assertEqual(self.log[1][:2], (self.case_repeater.get_url(rec), 200))
        self.assertIn('server-modified-on', self.log[1][3])
        check_xml_line_by_line(self, self.log[1][2], case_block)

        # check form stuff
        rec = records_by_repeater_id[self.form_repeater.get_id]
        self.assertEqual(self.log[3][:3],
                         (self.form_repeater.get_url(rec), 200, xform_xml))
        self.assertIn('received-on', self.log[3][3])

        repeat_records = RepeatRecord.all(
            domain=self.domain,
            due_before=next_check_time,
        )
        for repeat_record in repeat_records:
            self.assertEqual(repeat_record.succeeded, True)
            self.assertEqual(repeat_record.next_check, None)

        self.assertEqual(len(self.repeat_records(self.domain)), 0)

        self.post_xml(update_xform_xml, self.domain)
        self.assertEqual(len(self.repeat_records(self.domain)), 2)
Example #20
0
class RepeaterTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.domain = "test-domain"
        create_domain(self.domain)
        self.case_repeater = CaseRepeater(domain=self.domain, url="case-repeater-url", version=V1)
        self.case_repeater.save()
        self.form_repeater = FormRepeater(domain=self.domain, url="form-repeater-url")
        self.form_repeater.save()
        self.log = []
        self.post_xml(xform_xml)

    def post_xml(self, xml):
        f = StringIO(xml)
        f.name = "form.xml"
        self.client.post(reverse("receiver_post", args=[self.domain]), {"xml_submission_file": f})

    def clear_log(self):
        for i in range(len(self.log)):
            self.log.pop()

    def make_post_fn(self, status_codes):
        status_codes = iter(status_codes)

        def post_fn(data, url, headers=None):
            status_code = status_codes.next()
            self.log.append((url, status_code, data, headers))

            class resp:
                status = status_code

            return resp

        return post_fn

    def tearDown(self):
        self.case_repeater.delete()
        self.form_repeater.delete()
        XFormInstance.get(instance_id).delete()
        repeat_records = RepeatRecord.all()
        for repeat_record in repeat_records:
            repeat_record.delete()

    def test_repeater(self):

        CommCareCase.get(case_id)

        def now():
            return datetime.utcnow()

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)

        self.clear_log()

        for repeat_record in repeat_records:
            repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404]))
            repeat_record.save()

        for (url, status, data, headers) in self.log:
            self.assertEqual(status, 404)

        self.clear_log()

        next_check_time = now() + timedelta(minutes=60)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now() + timedelta(minutes=15))
        self.assertEqual(len(repeat_records), 0)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=next_check_time + timedelta(seconds=2))
        self.assertEqual(len(repeat_records), 2)

        for repeat_record in repeat_records:
            self.assertLess(abs(next_check_time - repeat_record.next_check), timedelta(seconds=2))
            repeat_record.fire(post_fn=self.make_post_fn([404, 200]))
            repeat_record.save()

        self.assertEqual(len(self.log), 4)

        # The following is pretty fickle and depends on which of
        #   - corehq.apps.receiverwrapper.signals
        #   - casexml.apps.case.signals
        # gets loaded first.
        # This is deterministic but easily affected by minor code changes

        # check case stuff
        self.assertEqual(self.log[1][:2], (self.case_repeater.url, 200))
        self.assertIn("server-modified-on", self.log[1][3])
        check_xml_line_by_line(self, self.log[1][2], case_block)

        # check form stuff
        self.assertEqual(self.log[3][:3], (self.form_repeater.url, 200, xform_xml))
        self.assertIn("received-on", self.log[3][3])

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=next_check_time)
        for repeat_record in repeat_records:
            self.assertEqual(repeat_record.succeeded, True)
            self.assertEqual(repeat_record.next_check, None)

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

        self.post_xml(update_xform_xml)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)
class RepeaterTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.domain = "test-domain"
        create_domain(self.domain)
        self.case_repeater = CaseRepeater(
            domain=self.domain,
            url='case-repeater-url',
            version=V1,
        )
        self.case_repeater.save()
        self.form_repeater = FormRepeater(
            domain=self.domain,
            url='form-repeater-url',
        )
        self.form_repeater.save()
        self.log = []
        self.post_xml(xform_xml)

    def post_xml(self, xml):
        f = StringIO(xml)
        f.name = 'form.xml'
        self.client.post(reverse('receiver_post', args=[self.domain]),
                         {'xml_submission_file': f})

    def clear_log(self):
        for i in range(len(self.log)):
            self.log.pop()

    def make_post_fn(self, status_codes):
        status_codes = iter(status_codes)

        def post_fn(data, url, headers=None):
            status_code = status_codes.next()
            self.log.append((url, status_code, data, headers))

            class resp:
                status = status_code

            return resp

        return post_fn

    def tearDown(self):
        self.case_repeater.delete()
        self.form_repeater.delete()
        XFormInstance.get(instance_id).delete()
        repeat_records = RepeatRecord.all()
        for repeat_record in repeat_records:
            repeat_record.delete()

    def test_repeater(self):

        CommCareCase.get(case_id)

        def now():
            return datetime.utcnow()

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)

        self.clear_log()

        for repeat_record in repeat_records:
            repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404]))
            repeat_record.save()

        for (url, status, data, headers) in self.log:
            self.assertEqual(status, 404)

        self.clear_log()

        next_check_time = now() + timedelta(minutes=60)

        repeat_records = RepeatRecord.all(
            domain=self.domain,
            due_before=now() + timedelta(minutes=15),
        )
        self.assertEqual(len(repeat_records), 0)

        repeat_records = RepeatRecord.all(
            domain=self.domain,
            due_before=next_check_time + timedelta(seconds=2),
        )
        self.assertEqual(len(repeat_records), 2)

        for repeat_record in repeat_records:
            self.assertLess(abs(next_check_time - repeat_record.next_check),
                            timedelta(seconds=2))
            repeat_record.fire(post_fn=self.make_post_fn([404, 200]))
            repeat_record.save()

        self.assertEqual(len(self.log), 4)

        # The following is pretty fickle and depends on which of
        #   - corehq.apps.receiverwrapper.signals
        #   - casexml.apps.case.signals
        # gets loaded first.
        # This is deterministic but easily affected by minor code changes

        # check case stuff
        self.assertEqual(self.log[1][:2], (self.case_repeater.url, 200))
        self.assertIn('server-modified-on', self.log[1][3])
        check_xml_line_by_line(self, self.log[1][2], case_block)

        # check form stuff
        self.assertEqual(self.log[3][:3],
                         (self.form_repeater.url, 200, xform_xml))
        self.assertIn('received-on', self.log[3][3])

        repeat_records = RepeatRecord.all(
            domain=self.domain,
            due_before=next_check_time,
        )
        for repeat_record in repeat_records:
            self.assertEqual(repeat_record.succeeded, True)
            self.assertEqual(repeat_record.next_check, None)

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

        self.post_xml(update_xform_xml)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)
class RepeaterTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.domain = "test-domain"
        self.case_repeater = CaseRepeater(domain=self.domain, url='case-repeater-url', version=V1)
        self.case_repeater.save()
        self.form_repeater = FormRepeater(domain=self.domain, url='form-repeater-url')
        self.form_repeater.save()
        self.log = []
        self.post_xml(xform_xml)

    def post_xml(self, xml):
        f = StringIO(xml)
        f.name = 'form.xml'
        self.client.post(
            reverse('receiver_post', args=[self.domain]), {
                'xml_submission_file': f
            }
        )

    def clear_log(self):
        for i in range(len(self.log)): self.log.pop()

    def make_post_fn(self, status_codes):
        status_codes = iter(status_codes)
        def post_fn(data, url):
            status_code = status_codes.next()
            self.log.append((url, status_code, data))
            class resp:
                status = status_code
            return resp
        return post_fn
    def tearDown(self):
        self.case_repeater.delete()
        self.form_repeater.delete()
        XFormInstance.get(instance_id).delete()
        repeat_records = RepeatRecord.all()
        for repeat_record in repeat_records:
            repeat_record.delete()


    def test_repeater(self):

        CommCareCase.get(case_id)

        def now():
            return datetime.utcnow()


        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)

        self.clear_log()


        for repeat_record in repeat_records:
            repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404]))
            repeat_record.save()

        for (url, status, data) in self.log:
            self.assertEqual(status, 404)

        self.clear_log()

        next_check_time = now() + timedelta(minutes=60)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now() + timedelta(minutes=15))
        self.assertEqual(len(repeat_records), 0)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=next_check_time + timedelta(seconds=2))
        self.assertEqual(len(repeat_records), 2)

        for repeat_record in repeat_records:
            self.assertLess(abs(next_check_time - repeat_record.next_check), timedelta(seconds=2))
            repeat_record.fire(post_fn=self.make_post_fn([404, 200]))
            repeat_record.save()

        self.assertEqual(len(self.log), 4)
        self.assertEqual(self.log[1], (self.form_repeater.url, 200, xform_xml))
        self.assertEqual(self.log[3][:2], (self.case_repeater.url, 200))
        check_xml_line_by_line(self, self.log[3][2], case_block)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=next_check_time)
        for repeat_record in repeat_records:
            self.assertEqual(repeat_record.succeeded, True)
            self.assertEqual(repeat_record.next_check, None)


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

        self.post_xml(update_xform_xml)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)
Example #23
0
class RepeaterTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.domain = "test-domain"
        self.case_repeater = CaseRepeater(domain=self.domain,
                                          url='case-repeater-url',
                                          version=V1)
        self.case_repeater.save()
        self.form_repeater = FormRepeater(domain=self.domain,
                                          url='form-repeater-url')
        self.form_repeater.save()
        self.log = []
        self.post_xml(xform_xml)

    def post_xml(self, xml):
        f = StringIO(xml)
        f.name = 'form.xml'
        self.client.post(reverse('receiver_post', args=[self.domain]),
                         {'xml_submission_file': f})

    def clear_log(self):
        for i in range(len(self.log)):
            self.log.pop()

    def make_post_fn(self, status_codes):
        status_codes = iter(status_codes)

        def post_fn(data, url):
            status_code = status_codes.next()
            self.log.append((url, status_code, data))

            class resp:
                status = status_code

            return resp

        return post_fn

    def tearDown(self):
        self.case_repeater.delete()
        self.form_repeater.delete()
        XFormInstance.get(instance_id).delete()
        repeat_records = RepeatRecord.all()
        for repeat_record in repeat_records:
            repeat_record.delete()

    def test_repeater(self):

        CommCareCase.get(case_id)

        def now():
            return datetime.utcnow()

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)

        self.clear_log()

        for repeat_record in repeat_records:
            repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404]))
            repeat_record.save()

        for (url, status, data) in self.log:
            self.assertEqual(status, 404)

        self.clear_log()

        next_check_time = now() + timedelta(minutes=60)

        repeat_records = RepeatRecord.all(domain=self.domain,
                                          due_before=now() +
                                          timedelta(minutes=15))
        self.assertEqual(len(repeat_records), 0)

        repeat_records = RepeatRecord.all(domain=self.domain,
                                          due_before=next_check_time +
                                          timedelta(seconds=2))
        self.assertEqual(len(repeat_records), 2)

        for repeat_record in repeat_records:
            self.assertLess(abs(next_check_time - repeat_record.next_check),
                            timedelta(seconds=2))
            repeat_record.fire(post_fn=self.make_post_fn([404, 200]))
            repeat_record.save()

        self.assertEqual(len(self.log), 4)
        self.assertEqual(self.log[1], (self.form_repeater.url, 200, xform_xml))
        self.assertEqual(self.log[3][:2], (self.case_repeater.url, 200))
        check_xml_line_by_line(self, self.log[3][2], case_block)

        repeat_records = RepeatRecord.all(domain=self.domain,
                                          due_before=next_check_time)
        for repeat_record in repeat_records:
            self.assertEqual(repeat_record.succeeded, True)
            self.assertEqual(repeat_record.next_check, None)

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

        self.post_xml(update_xform_xml)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now())
        self.assertEqual(len(repeat_records), 2)
Example #24
0
class RepeaterTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.domain = "test-domain"
        self.case_repeater = CaseRepeater(domain=self.domain, url='case-repeater-url', version=V1)
        self.case_repeater.save()
        self.form_repeater = FormRepeater(domain=self.domain, url='form-repeater-url')
        self.form_repeater.save()
        f = StringIO(xform_xml)
        f.name = 'register.xml'
        self.client.post(
            reverse('receiver_post', args=[self.domain]), {
                'xml_submission_file': f
            }
        )
        self.log = []

    def clear_log(self):
        for i in range(len(self.log)): self.log.pop()

    def make_post_fn(self, status_codes):
        status_codes = iter(status_codes)
        def post_fn(data, url):
            status_code = status_codes.next()
            self.log.append((url, status_code, data))
            class resp:
                status = status_code
            return resp
        return post_fn
    def tearDown(self):
        self.case_repeater.delete()
        self.form_repeater.delete()
        XFormInstance.get(instance_id).delete()
        repeat_records = RepeatRecord.all()
        for repeat_record in repeat_records:
            repeat_record.delete()


    def test_repeater(self):

        CommCareCase.get(case_id)

        now = datetime.utcnow()


        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now)
        self.assertEqual(len(repeat_records), 2)

        self.clear_log()


        for repeat_record in repeat_records:
            repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404]))
            repeat_record.save()

        for (url, status, data) in self.log:
            self.assertEqual(status, 404)

        self.clear_log()

        in30min = now + timedelta(minutes=30)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=now + timedelta(minutes=15))
        self.assertEqual(len(repeat_records), 0)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=in30min + timedelta(seconds=1))
        self.assertEqual(len(repeat_records), 2)

        for repeat_record in repeat_records:
            self.assertLess(abs(in30min - repeat_record.next_check), timedelta(seconds=1))
            repeat_record.fire(post_fn=self.make_post_fn([404, 200]))
            repeat_record.save()

        self.assertEqual(len(self.log), 4)
        self.assertEqual(self.log[1], (self.form_repeater.url, 200, xform_xml))
        self.assertEqual(self.log[3][:2], (self.case_repeater.url, 200))
        check_xml_line_by_line(self, self.log[3][2], case_block)

        repeat_records = RepeatRecord.all(domain=self.domain, due_before=in30min)
        for repeat_record in repeat_records:
            self.assertEqual(repeat_record.succeeded, True)
            self.assertEqual(repeat_record.next_check, None)

    def test_migration(self):
        now = datetime.utcnow()
        repeats = [
            {
                "url": "http://example.com/",
                "doc_type": "RepeatRecord",
                "next_check": now,
                "last_checked": None,
                "succeeded": False
            }
        ]
        xform = XFormInstance.get(instance_id)
        xform.repeats = repeats
        xform.save()

        def always_success():
            while True:
                yield 200

        self.clear_log()

        check_inline_form_repeaters(post_fn=self.make_post_fn(always_success()))

        self.assertEqual(len(self.log), 1)

        self.assertEqual(self.log[0], (repeats[0]['url'], 200, xform_xml))

        self.clear_log()

        check_inline_form_repeaters(post_fn=self.make_post_fn(always_success()))

        self.assertEqual(len(self.log), 0)