Esempio n. 1
0
class TestUtilFunctions(TestCase):
    def setUp(self):
        self.case = CommCareCase(domain='test-domain', name='test-case')
        self.case.save()

        self.user = CommCareUser.create('test-domain', 'test-user', '123')

    def test_get_contact(self):
        contact = get_contact(self.case.get_id)
        self.assertEqual(contact.get_id, self.case.get_id)
        self.assertTrue(isinstance(contact, CommConnectCase))

        contact = get_contact(self.user.get_id)
        self.assertEqual(contact.get_id, self.user.get_id)
        self.assertTrue(isinstance(contact, CommCareUser))

        try:
            get_contact('this-id-should-not-be-found')
        except Exception:
            pass
        else:
            self.assertTrue(False)

    def test_apply_leniency(self):
        self.assertEqual('16175551234', apply_leniency(' 1 (617) 555-1234 '))
        self.assertEqual('16175551234', apply_leniency(' 1.617.555.1234 '))
        self.assertEqual('16175551234', apply_leniency(' +1 617 555 1234 '))

    def tearDown(self):
        self.case.delete()
        self.user.delete()
Esempio n. 2
0
    def get(self, case_id):
        if not case_id:
            raise IllegalCaseId('case_id must not be empty')
        if case_id in self.cache:
            return self.cache[case_id]

        try:
            if self.strip_history:
                case_doc = CommCareCase.get_lite(case_id, wrap=self.wrap)
            elif self.lock:
                try:
                    case_doc, lock = CommCareCase.get_locked_obj(_id=case_id)
                except redis.RedisError:
                    case_doc = CommCareCase.get(case_id)
                else:
                    self.locks.append(lock)
            else:
                if self.wrap:
                    case_doc = CommCareCase.get(case_id)
                else:
                    case_doc = CommCareCase.get_db().get(case_id)
        except ResourceNotFound:
            return None

        self.validate_doc(case_doc)
        self.cache[case_id] = case_doc
        return case_doc
Esempio n. 3
0
    def get_case_with_lock(case_id, lock=False, strip_history=False, wrap=False):

        def _get_case():
            if wrap:
                return CommCareCase.get(case_id)
            else:
                return CommCareCase.get_db().get(case_id)

        try:
            if strip_history:
                case_doc = CommCareCase.get_lite(case_id, wrap=wrap)
            elif lock:
                try:
                    case, lock = CommCareCase.get_locked_obj(_id=case_id)
                    if case and not wrap:
                        case = case.to_json()
                    return case, lock
                except redis.RedisError:
                    case_doc = _get_case()
            else:
                case_doc = _get_case()
        except ResourceNotFound:
            return None, None

        return case_doc, None
Esempio n. 4
0
    def test_edit_submissions_simple(self):
        initial_quantity = 100
        form_id = submit_case_blocks(
            case_blocks=get_single_balance_block(quantity=initial_quantity, **self._stock_state_key),
            domain=self.domain,
        )
        stock_state, latest_txn, all_txns = self._get_stats()
        self.assertEqual(stock_state.stock_on_hand, initial_quantity)
        self.assertEqual(latest_txn.stock_on_hand, initial_quantity)
        self.assertEqual(all_txns.count(), 1)
        case = CommCareCase.get(id=self.case.case_id)
        self.assertEqual(1, len(case.actions))
        self.assertEqual([form_id], case.xform_ids)

        # change the value to 50
        edit_quantity = 50
        submit_case_blocks(
            case_blocks=get_single_balance_block(quantity=edit_quantity, **self._stock_state_key),
            domain=self.domain,
            form_id=form_id,
        )
        case = CommCareCase.get(id=self.case.case_id)
        self.assertEqual(1, len(case.actions))
        stock_state, latest_txn, all_txns = self._get_stats()
        self.assertEqual(stock_state.stock_on_hand, edit_quantity)
        self.assertEqual(latest_txn.stock_on_hand, edit_quantity)
        self.assertEqual(all_txns.count(), 1)
        self.assertEqual([form_id], case.xform_ids)
Esempio n. 5
0
    def testArchivingOnlyForm(self):
        """
        Checks that archiving the only form associated with the case archives
        the case and unarchiving unarchives it.
        """
        case_id = _post_util(create=True, p1='p1-1', p2='p2-1')
        case = CommCareCase.get(case_id)

        self.assertEqual('CommCareCase', case._doc['doc_type'])
        self.assertEqual(2, len(case.actions))
        [form_id] = case.xform_ids
        form = XFormInstance.get(form_id)

        form.archive()
        case = CommCareCase.get(case_id)

        self.assertEqual('CommCareCase-Deleted', case._doc['doc_type'])
        # should just have the 'rebuild' action
        self.assertEqual(1, len(case.actions))
        self.assertEqual(const.CASE_ACTION_REBUILD, case.actions[0].action_type)

        form.unarchive()
        case = CommCareCase.get(case_id)
        self.assertEqual('CommCareCase', case._doc['doc_type'])
        self.assertEqual(3, len(case.actions))
        self.assertEqual(const.CASE_ACTION_REBUILD, case.actions[-1].action_type)
Esempio n. 6
0
 def test_rebuild_indicators(self, datetime_mock):
     datetime_mock.utcnow.return_value = self.fake_time_now
     self.config.save()
     sample_doc, _ = get_sample_doc_and_indicators(self.fake_time_now)
     CommCareCase.get_db().save_doc(sample_doc)
     rebuild_indicators(self.config._id)
     self._check_sample_doc_state()
    def _send_case_to_es(self,
            domain=None,
            owner_id=None,
            user_id=None,
            case_type=None,
            opened_on=None,
            closed_on=None):

        actions = [CommCareCaseAction(
            action_type=CASE_ACTION_CREATE,
            date=opened_on,
        )]

        case = CommCareCase(
            _id=uuid.uuid4().hex,
            domain=domain or self.domain,
            owner_id=owner_id or self.owner_id,
            user_id=user_id or self.user_id,
            type=case_type or self.case_type,
            opened_on=opened_on or datetime.now(),
            opened_by=user_id or self.user_id,
            closed_on=closed_on,
            closed_by=user_id or self.user_id,
            actions=actions,
        )
        self.pillow.change_transport(case.to_json())
        self.pillow.get_es_new().indices.refresh(self.pillow.es_index)
        return case
Esempio n. 8
0
 def test_already_sorted(self):
     case = CommCareCase(actions=[
         CommCareCaseAction(server_date=datetime(2001, 1, 1, 0, 0, 0)),
         CommCareCaseAction(server_date=datetime(2001, 1, 2, 0, 0, 0)),
         CommCareCaseAction(server_date=datetime(2001, 1, 3, 0, 0, 0)),
     ])
     self.assertTrue(case.check_action_order())
Esempio n. 9
0
    def testRecursiveUpdates(self):
        parent_case = self._make_case(self.other_user._id, self.other_user._id)
        case = self._make_case(self.other_user._id, self.other_user._id,
                               index={'parent': ('parent-case', parent_case._id)})
        subcase1 = self._make_case(self.other_user._id, self.other_user._id,
                                   index={'parent': ('parent-case', case._id)})
        subcase2 = self._make_case(self.other_user._id, self.other_user._id,
                                   index={'parent': ('parent-case', case._id)})
        subsub1 = self._make_case(self.other_user._id, self.other_user._id,
                                  index={'parent': ('parent-case', subcase1._id)})
        subsub2 = self._make_case(self.other_user._id, self.other_user._id,
                                  index={'parent': ('parent-case', subcase1._id)})
        cases = [case, subcase1, subcase2, subsub1, subsub2]
        for c in cases:
            self.assertEqual(self.other_user._id, c.owner_id)
        reconcile_ownership(case, self.user, recursive=True)
        case = CommCareCase.get(case._id)
        owner = get_wrapped_owner(get_owner_id(case))
        self.assertTrue(isinstance(owner, Group))
        self.assertTrue(self.other_user._id in owner.users)
        self.assertTrue(self.user._id in owner.users)
        self.assertTrue(owner.case_sharing)
        self.assertFalse(owner.reporting)
        for c in cases:
            c = CommCareCase.get(c._id)
            self.assertEqual(owner._id, c.owner_id)

        parent_case = CommCareCase.get(parent_case._id)
        self.assertEqual(self.other_user._id, parent_case.owner_id)
Esempio n. 10
0
 def setUp(self):
     # we have to set the fake database before any other calls
     self.orig_db = CommCareCase.get_db()
     self.database = FakeCouchDb()
     CommCareCase.set_db(self.database)
     self.spec = {
         "type": "related_doc",
         "related_doc_type": "CommCareCase",
         "doc_id_expression": {"type": "property_name", "property_name": "parent_id"},
         "value_expression": {"type": "property_name", "property_name": "related_property"},
     }
     self.expression = ExpressionFactory.from_spec(self.spec)
     self.nested_expression = ExpressionFactory.from_spec(
         {
             "type": "related_doc",
             "related_doc_type": "CommCareCase",
             "doc_id_expression": {"type": "property_name", "property_name": "parent_id"},
             "value_expression": {
                 "type": "related_doc",
                 "related_doc_type": "CommCareCase",
                 "doc_id_expression": {"type": "property_name", "property_name": "parent_id"},
                 "value_expression": {"type": "property_name", "property_name": "related_property"},
             },
         }
     )
Esempio n. 11
0
 def testRebuildEmpty(self):
     self.assertEqual(None, rebuild_case_from_forms('notarealid'))
     try:
         CommCareCase.get_with_rebuild('notarealid')
         self.fail('get with rebuild should still fail for unknown cases')
     except ResourceNotFound:
         pass
Esempio n. 12
0
 def get_cases(case_ids, ordered=False):
     return [
         CommCareCase.wrap(doc) for doc in iter_docs(
             CommCareCase.get_db(),
             case_ids
         )
     ]
Esempio n. 13
0
class UtilTestCase(TestCase):
    
    def setUp(self):
        self.case = CommCareCase(domain='test-domain', name='test-case')
        self.case.save()
        self.user = CommCareUser.create('test-domain', 'test-user', '123')

    def tearDown(self):
        self.case.delete()
        self.user.delete()

    def testCleanPhoneNumber(self):
        phone_number = "  324 23-23421241"
        cleaned = clean_phone_number(phone_number)
        self.assertEquals(cleaned, "+3242323421241")

    def test_get_contact(self):
        contact = get_contact(self.case.get_id)
        self.assertEqual(contact.get_id, self.case.get_id)
        self.assertTrue(isinstance(contact, CommConnectCase))

        contact = get_contact(self.user.get_id)
        self.assertEqual(contact.get_id, self.user.get_id)
        self.assertTrue(isinstance(contact, CommCareUser))

        with self.assertRaises(ContactNotFoundException):
            get_contact('this-id-should-not-be-found')

    def test_apply_leniency(self):
        self.assertEqual('16175551234', apply_leniency(' 1 (617) 555-1234 '))
        self.assertEqual('16175551234', apply_leniency(' 1.617.555.1234 '))
        self.assertEqual('16175551234', apply_leniency(' +1 617 555 1234 '))
Esempio n. 14
0
    def testDuplicateCasePropertiesBug(self):
        """
        How do we do when submitting multiple values for the same property
        in an update block
        """
        file_path = os.path.join(os.path.dirname(__file__), "data", "bugs",
                                 "duplicate_case_properties.xml")
        with open(file_path, "rb") as f:
            xml_data = f.read()
        form = post_xform_to_couch(xml_data)
        # before the bug was fixed this call failed
        process_cases(form)
        case = CommCareCase.get(form.xpath("form/case/@case_id"))
        # make sure the property is there, but empty
        self.assertEqual("", case.foo)

        file_path = os.path.join(os.path.dirname(__file__), "data", "bugs",
                                 "duplicate_case_properties_2.xml")
        with open(file_path, "rb") as f:
            xml_data = f.read()
        form = post_xform_to_couch(xml_data)
        process_cases(form)
        case = CommCareCase.get(form.xpath("form/case/@case_id"))
        # make sure the property takes the last defined value
        self.assertEqual("2", case.bar)
Esempio n. 15
0
    def handle(self, *args, **options):
        domain, group_name = args
        group = Group.by_name(domain, name=group_name)
        owner_ids = get_all_owner_ids_from_group(group)
        pillow = CareBiharFluffPillow()
        db = CommCareCase.get_db()

        greenlets = []

        def process_case(case):
            pillow.change_transport(pillow.change_transform(case))


        for i, owner_id in enumerate(owner_ids):
            print '{0}/{1} owner_ids'.format(i, len(owner_ids))
            rows = CommCareCase.view(
                'hqcase/by_owner',
                startkey=[domain, owner_id],
                endkey=[domain, owner_id, {}],
                reduce=False,
            ).all()
            case_ids = [row['id'] for row in rows]
            print '{0} case_ids'.format(len(case_ids))
            for case in iter_docs(db, case_ids):
                g = gevent.Greenlet.spawn(process_case, case)
                greenlets.append(g)
        gevent.joinall(greenlets)
Esempio n. 16
0
 def testAttachInCreate(self):
     self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all()))
     
     file_path = os.path.join(os.path.dirname(__file__), "data", "attachments", "create_with_attach.xml")
     with open(file_path, "rb") as f:
         xml_data = f.read()
     
     attach_name = "fruity.jpg"
     attachment_path = os.path.join(os.path.dirname(__file__), "data", "attachments", attach_name)
     with open(attachment_path, "rb") as attachment:
         uf = UploadedFile(attachment, attach_name)
         form = post_xform_to_couch(xml_data, {attach_name: uf})
         self.assertEqual(1, len(form.attachments))
         fileback = form.fetch_attachment(attach_name)
         # rewind the pointer before comparing
         attachment.seek(0) 
         self.assertEqual(hashlib.md5(fileback).hexdigest(), 
                          hashlib.md5(attachment.read()).hexdigest())
         
     
     process_cases(sender="testharness", xform=form)
     case = CommCareCase.get(form.xpath("form/case/case_id"))
     self.assertEqual(1, len(case.attachments))
     self.assertEqual(form.get_id, case.attachments[0][0])
     self.assertEqual(attach_name, case.attachments[0][1])
Esempio n. 17
0
 def test_rebuild_indicators(self, datetime_mock):
     datetime_mock.utcnow.return_value = self.fake_time_now
     sample_doc, expected_indicators = get_sample_doc_and_indicators(self.fake_time_now)
     CommCareCase.get_db().save_doc(sample_doc)
     self.addCleanup(lambda id: CommCareCase.get_db().delete_doc(id), sample_doc['_id'])
     rebuild_indicators(self.config._id)
     self._check_sample_doc_state(expected_indicators)
    def setUp(self):
        self.now = datetime(2015, 10, 22, 0, 0)
        self.domain = 'auto-update-test'
        self.rule = AutomaticUpdateRule(
            domain=self.domain,
            name='test-rule',
            case_type='test-case-type',
            active=True,
            server_modified_boundary=30,
        )
        self.rule.save()
        self.rule.automaticupdaterulecriteria_set = [
            AutomaticUpdateRuleCriteria(
                property_name='last_visit_date',
                property_value='30',
                match_type=AutomaticUpdateRuleCriteria.MATCH_DAYS_SINCE,
            ),
        ]
        self.rule.automaticupdateaction_set = [
            AutomaticUpdateAction(
                action=AutomaticUpdateAction.ACTION_UPDATE,
                property_name='update_flag',
                property_value='Y',
            ),
            AutomaticUpdateAction(
                action=AutomaticUpdateAction.ACTION_CLOSE,
            ),
        ]

        self.rule2 = AutomaticUpdateRule(
            domain=self.domain,
            name='test-rule-2',
            case_type='test-case-type-2',
            active=True,
            server_modified_boundary=30,
        )
        self.rule2.save()
        self.rule2.automaticupdateaction_set = [
            AutomaticUpdateAction(
                action=AutomaticUpdateAction.ACTION_CLOSE,
            ),
        ]

        self.rule3 = AutomaticUpdateRule(
            domain=self.domain,
            name='test-rule-3',
            case_type='test-case-type-2',
            active=True,
            server_modified_boundary=50,
        )
        self.rule3.save()
        self.rule3.automaticupdateaction_set = [
            AutomaticUpdateAction(
                action=AutomaticUpdateAction.ACTION_CLOSE,
            ),
        ]

        case = CommCareCase(domain=self.domain, type='test-case-type')
        case.save()
        self.case_id = case.get_id
Esempio n. 19
0
    def testAttachments(self):
        self.assertEqual(len(CommCareCase.get(self.case_id).case_attachments), 0)
        scan_path = os.path.join(os.path.dirname(__file__), "data", "vscan", "VH014466XK_000016_20130722T175057")
        files = self.pack_directory(scan_path)
        utils.attach_images_to_case(self.case_id, files)

        self.assertEqual(len(CommCareCase.get(self.case_id).case_attachments), 3)
Esempio n. 20
0
    def _send_case_to_es(self,
            domain=None,
            owner_id=None,
            user_id=None,
            case_type=None,
            opened_on=None,
            closed_on=None):

        actions = [CommCareCaseAction(
            action_type=CASE_ACTION_CREATE,
            date=opened_on,
        )]

        case = CommCareCase(
            _id=uuid.uuid4().hex,
            domain=domain or self.domain,
            owner_id=owner_id or self.owner_id,
            user_id=user_id or self.user_id,
            type=case_type or self.case_type,
            opened_on=opened_on or datetime.now(),
            opened_by=user_id or self.user_id,
            closed_on=closed_on,
            closed_by=user_id or self.user_id,
            actions=actions,
        )
        send_to_elasticsearch('cases', case.to_json())
        self.es.indices.refresh(CASE_INDEX_INFO.index)
        return case
Esempio n. 21
0
 def testRebuildEmpty(self):
     self.assertEqual(None, rebuild_case("notarealid"))
     try:
         CommCareCase.get_with_rebuild("notarealid")
         self.fail("get with rebuild should still fail for unknown cases")
     except ResourceNotFound:
         pass
Esempio n. 22
0
class IndexSimpleTest(SimpleTestCase):
    def setUp(self):
        self.i1 = CommCareCaseIndex(identifier="i1", referenced_type="t1", referenced_id="id1")
        self.i2 = CommCareCaseIndex(identifier="i2", referenced_type="t2", referenced_id="id2")
        self.case = CommCareCase(indices=[self.i1, self.i2])

    def testHasIndex(self):
        self.assertEqual(True, self.case.has_index("i1"))
        self.assertEqual(True, self.case.has_index("i2"))
        self.assertEqual(False, self.case.has_index("i3"))

    def testGetIndex(self):
        self.assertEqual(self.i1, self.case.get_index("i1"))
        self.assertEqual(self.i2, self.case.get_index("i2"))
        self.assertEqual(None, self.case.get_index("i3"))
        self.assertEqual(None, self.case.get_index("id1"))

    def testGetIndexByRef(self):
        self.assertEqual(self.i1, self.case.get_index_by_ref_id("id1"))
        self.assertEqual(self.i2, self.case.get_index_by_ref_id("id2"))
        self.assertEqual(None, self.case.get_index_by_ref_id("id3"))
        self.assertEqual(None, self.case.get_index_by_ref_id("i1"))

    def testRemoveIndexByRef(self):
        self.assertEqual(2, len(self.case.indices))
        self.case.remove_index_by_ref_id("id1")
        self.assertEqual(1, len(self.case.indices))
        self.assertRaises(ValueError, self.case.remove_index_by_ref_id, "id3")
        self.assertRaises(ValueError, self.case.remove_index_by_ref_id, "i2")
Esempio n. 23
0
    def cases(self):
        if "debug_case" in self.request.GET:
            case = CommCareCase.get(self.request.GET["debug_case"])
            if case.domain != DOMAIN:
                raise Http404()
            return [case]

        query = (
            case_es.CaseES()
            .domain(self.domain)
            .exclude_source()
            .opened_range(lte=self.datespan.enddate_utc)
            .case_type(self.default_case_type)
        )
        query.index = "report_cases"

        if self.case_status == "open":
            query = query.filter(
                es_filters.OR(case_es.is_closed(False), case_es.closed_range(gte=self.datespan.enddate_utc))
            )
        elif self.case_status == "closed":
            query = query.filter(case_es.closed_range(lte=self.datespan.enddate_utc))

        query = query.owner([user["doc_id"] for user in self.users_matching_filter])

        result = query.run()

        return [CommCareCase.wrap(doc) for doc in iter_docs(CommCareCase.get_db(), result.doc_ids)]
Esempio n. 24
0
 def test_out_of_order(self):
     case = CommCareCase(actions=[
         CommCareCaseAction(server_date=datetime(2001, 1, 1, 0, 0, 0)),
         CommCareCaseAction(server_date=datetime(2001, 1, 3, 0, 0, 0)),
         CommCareCaseAction(server_date=datetime(2001, 1, 2, 0, 0, 0)),
     ])
     self.assertFalse(case.check_action_order())
Esempio n. 25
0
    def test_get_list(self):
        """
        Any case in the appropriate domain should be in the list from the API.
        """

        # The actual infrastructure involves saving to CouchDB, having PillowTop
        # read the changes and write it to ElasticSearch.

        # the pillow is set to offline mode - elasticsearch not needed to validate
        fake_case_es = FakeXFormES()
        v0_4.MOCK_CASE_ES = fake_case_es

        modify_date = datetime.utcnow()

        backend_case = CommCareCase(server_modified_on=modify_date, domain=self.domain.name)
        backend_case.save()
        self.addCleanup(backend_case.delete)

        translated_doc = transform_case_for_elasticsearch(backend_case.to_json())

        fake_case_es.add_doc(translated_doc['_id'], translated_doc)

        response = self._assert_auth_get_resource(self.list_endpoint)
        self.assertEqual(response.status_code, 200)

        api_cases = json.loads(response.content)['objects']
        self.assertEqual(len(api_cases), 1)

        api_case = api_cases[0]
        self.assertEqual(api_case['server_date_modified'], json_format_datetime(backend_case.server_modified_on))
Esempio n. 26
0
 def test_normal(self):
     case = CommCareCase(foo='some property', bar='some other property').to_generic()
     props = case.dynamic_case_properties()
     self.assertEqual(2, len(props))
     props_dict = dict(props)
     self.assertEqual('some property', props_dict['foo'])
     self.assertEqual('some other property', props_dict['bar'])
Esempio n. 27
0
 def get_cases(case_ids, ordered=False, prefetched_indices=None):
     # prefetched_indices is ignored sinces cases already have them
     return [
         CommCareCase.wrap(doc) for doc in iter_docs(
             CommCareCase.get_db(),
             case_ids
         )
     ]
Esempio n. 28
0
 def _iter_raw_cases(case_ids):
     if self.strip_history:
         for ids in chunked(case_ids, 100):
             for row in CommCareCase.get_db().view("case/get_lite", keys=ids, include_docs=False):
                 yield row["value"]
     else:
         for raw_case in iter_docs(CommCareCase.get_db(), case_ids):
             yield raw_case
Esempio n. 29
0
def iter_cases(case_ids, strip_history=False, wrap=True):
    from casexml.apps.case.models import CommCareCase
    if not strip_history:
        for doc in iter_docs(CommCareCase.get_db(), case_ids):
            yield CommCareCase.wrap(doc) if wrap else doc
    else:
        for case in CommCareCase.bulk_get_lite(case_ids, wrap=wrap):
            yield case
Esempio n. 30
0
def get_indexed_cases(domain, case_ids):
    """
    Given a base list of cases, gets all wrapped cases that they reference
    (parent cases).
    """
    from casexml.apps.case.models import CommCareCase
    return [CommCareCase.wrap(doc) for doc in iter_docs(CommCareCase.get_db(),
                                                        get_indexed_case_ids(domain, case_ids))]
Esempio n. 31
0
    def setUp(self):
        super(KooKooTestCase, self).setUp()
        self.ivr_backend = MobileBackend(
            _id="MOBILE_BACKEND_KOOKOO",
            outbound_module="corehq.apps.kookoo.api",
            outbound_params={
                "is_test": True,
                "api_key": "xyz"
            },
        )
        self.ivr_backend.save()

        self.user1 = self.create_mobile_worker("user1",
                                               "123",
                                               "91001",
                                               save_vn=False)
        self.user2 = self.create_mobile_worker("user2",
                                               "123",
                                               "91002",
                                               save_vn=False)
        self.create_group("group1", [self.user1, self.user2])

        self.case = CommCareCase(
            domain=self.domain,
            type="participant",
            owner_id=self.groups[0]._id,
        )
        self.case.set_case_property("contact_phone_number", "91000")
        self.case.set_case_property("contact_phone_number_is_verified", "1")
        self.case.set_case_property("contact_ivr_backend_id",
                                    "MOBILE_BACKEND_KOOKOO")
        self.case.save()

        dirname = os.path.dirname(os.path.abspath(__file__))
        self.load_app("app1.json", dirname)
        self.load_app("app2.json", dirname)

        self.reminder1 = CaseReminderHandler(
            domain=self.domain,
            active=True,
            case_type="participant",
            method=METHOD_IVR_SURVEY,
            recipient=RECIPIENT_CASE,
            sample_id=None,
            user_group_id=None,
            user_id=None,
            case_id=None,
            reminder_type=REMINDER_TYPE_DEFAULT,
            submit_partial_forms=True,
            include_case_side_effects=False,
            max_question_retries=5,
            start_condition_type=CASE_CRITERIA,
            start_property="name",
            start_value="case1",
            start_date=None,
            start_offset=0,
            start_match_type=MATCH_EXACT,
            events=[
                CaseReminderEvent(
                    day_num=0,
                    fire_time=time(12, 0),
                    fire_time_type=FIRE_TIME_DEFAULT,
                    callback_timeout_intervals=[30],
                    form_unique_id=self.apps[0].modules[0].forms[0].unique_id,
                ),
                CaseReminderEvent(
                    day_num=0,
                    fire_time=time(13, 0),
                    fire_time_type=FIRE_TIME_DEFAULT,
                    callback_timeout_intervals=[30],
                    form_unique_id=self.apps[0].modules[0].forms[1].unique_id,
                ),
            ],
            schedule_length=1,
            event_interpretation=EVENT_AS_SCHEDULE,
            max_iteration_count=7,
            until=None,
            force_surveys_to_use_triggered_case=False,
        )
        self.reminder1.save()

        self.reminder2 = CaseReminderHandler(
            domain=self.domain,
            active=True,
            case_type="participant",
            method=METHOD_IVR_SURVEY,
            recipient=RECIPIENT_OWNER,
            sample_id=None,
            user_group_id=None,
            user_id=None,
            case_id=None,
            reminder_type=REMINDER_TYPE_DEFAULT,
            submit_partial_forms=True,
            include_case_side_effects=True,
            max_question_retries=5,
            start_condition_type=CASE_CRITERIA,
            start_property="name",
            start_value="case2",
            start_date=None,
            start_offset=0,
            start_match_type=MATCH_EXACT,
            events=[
                CaseReminderEvent(
                    day_num=0,
                    fire_time=time(12, 0),
                    fire_time_type=FIRE_TIME_DEFAULT,
                    callback_timeout_intervals=[30, 30],
                    form_unique_id=self.apps[1].modules[0].forms[0].unique_id,
                ),
            ],
            schedule_length=1,
            event_interpretation=EVENT_AS_SCHEDULE,
            max_iteration_count=7,
            until=None,
            force_surveys_to_use_triggered_case=False,
        )
        self.reminder2.save()
Esempio n. 32
0
 def tearDownClass(cls):
     CommCareCase.get_db().bulk_delete(cls.cases)
     XFormInstance.get_db().bulk_delete(cls.forms)
     super(DBAccessorsTest, cls).tearDownClass()
Esempio n. 33
0
 def tearDown(self):
     CommCareCase.set_db(self.case_orig_db)
     XFormInstance.set_db(self.form_orig_db)
     CommCareUser.set_db(self.user_orig_db)
Esempio n. 34
0
    def setUp(self):
        super(BackendTestCase, self).setUp()

        self.domain = "test-domain"
        self.domain2 = "test-domain2"

        self.site, self.site_created = self.get_or_create_site()

        self.domain_obj = Domain(name=self.domain)
        self.domain_obj.save()

        generator.instantiate_accounting_for_tests()
        self.account = BillingAccount.get_or_create_account_by_domain(
            self.domain_obj.name,
            created_by="automated-test",
        )[0]
        plan = DefaultProductPlan.get_default_plan_by_domain(
            self.domain_obj, edition=SoftwarePlanEdition.ADVANCED)
        self.subscription = Subscription.new_domain_subscription(
            self.account, self.domain_obj.name, plan)
        self.subscription.is_active = True
        self.subscription.save()
        self.domain_obj = Domain.get(
            self.domain_obj._id)  # Prevent resource conflict

        self.backend1 = TestCaseBackend(name="BACKEND1", is_global=True)
        self.backend1.save()

        self.backend2 = TestCaseBackend(name="BACKEND2", is_global=True)
        self.backend2.save()

        self.backend3 = TestCaseBackend(name="BACKEND3", is_global=True)
        self.backend3.save()

        self.backend4 = TestCaseBackend(name="BACKEND4", is_global=True)
        self.backend4.save()

        self.backend5 = TestCaseBackend(name="BACKEND5",
                                        domain=self.domain,
                                        is_global=False,
                                        authorized_domains=[])
        self.backend5.save()

        self.backend6 = TestCaseBackend(name="BACKEND6",
                                        domain=self.domain2,
                                        is_global=False,
                                        authorized_domains=[self.domain])
        self.backend6.save()

        self.backend7 = TestCaseBackend(name="BACKEND7",
                                        domain=self.domain2,
                                        is_global=False,
                                        authorized_domains=[])
        self.backend7.save()

        self.backend8 = TestCaseBackend(name="BACKEND",
                                        domain=self.domain,
                                        is_global=False,
                                        authorized_domains=[])
        self.backend8.save()

        self.backend9 = TestCaseBackend(name="BACKEND",
                                        domain=self.domain2,
                                        is_global=False,
                                        authorized_domains=[self.domain])
        self.backend9.save()

        self.backend10 = TestCaseBackend(name="BACKEND", is_global=True)
        self.backend10.save()

        self.backend_mapping1 = BackendMapping(is_global=True,
                                               prefix="*",
                                               backend_id=self.backend1._id)
        self.backend_mapping1.save()

        self.backend_mapping2 = BackendMapping(is_global=True,
                                               prefix="1",
                                               backend_id=self.backend2._id)
        self.backend_mapping2.save()

        self.backend_mapping3 = BackendMapping(is_global=True,
                                               prefix="91",
                                               backend_id=self.backend3._id)
        self.backend_mapping3.save()

        self.backend_mapping4 = BackendMapping(is_global=True,
                                               prefix="265",
                                               backend_id=self.backend4._id)
        self.backend_mapping4.save()

        self.backend_mapping5 = BackendMapping(is_global=True,
                                               prefix="256",
                                               backend_id=self.backend5._id)
        self.backend_mapping5.save()

        self.backend_mapping6 = BackendMapping(is_global=True,
                                               prefix="25670",
                                               backend_id=self.backend6._id)
        self.backend_mapping6.save()

        self.backend_mapping7 = BackendMapping(is_global=True,
                                               prefix="25675",
                                               backend_id=self.backend7._id)
        self.backend_mapping7.save()

        self.case = CommCareCase(domain=self.domain)
        self.case.set_case_property("contact_phone_number", "15551234567")
        self.case.set_case_property("contact_phone_number_is_verified", "1")
        self.case.save()

        self.contact = CommConnectCase.wrap(self.case.to_json())

        settings.SMS_LOADED_BACKENDS.append(
            "corehq.apps.sms.tests.TestCaseBackend")
Esempio n. 35
0
 def save(self, **kwargs):
     self.doc_type = 'IndicatorCase'
     assert self.get_db().uri != CommCareCase.get_db().uri
     super(IndicatorCase, self).save(**kwargs)
                        parent = CommCareCase.get(parent_id)
                    except ResourceNotFound:
                        parent = MissingParent(get_id=parent_id,
                                               owner_id='Parent Missing')

                case_row = CaseRow(case, parent)

                if case.type != 'task':
                    if case.user_id == MOTECH_ID:
                        case_row.update_type('task')

                if parent and not isinstance(
                        parent,
                        MissingParent) and parent.owner_id != case.owner_id:
                    case_row.update_owner(parent.owner_id)

                if case_row.save:
                    csv_file.writerow(case_row.to_row())
                    to_save.append(case_row.case)

                if len(to_save) > 100:
                    CommCareCase.get_db().bulk_save(to_save)
                    to_save = []

                if i % 100 == 0:
                    logger.info("{current}/{count} cases completed".format(
                        current=i, count=len(case_ids)))

            if to_save:
                CommCareCase.get_db().bulk_save(to_save)
Esempio n. 37
0
 def delete_all_cases(cls, domain=None):
     logger.debug("Deleting all Couch cases for domain %s", domain)
     assert CommCareCase.get_db().dbname.startswith('test_')
     cls._delete_all(CommCareCase.get_db(), ['CommCareCase', 'CommCareCase-Deleted'], domain)
     FormProcessorTestUtils.delete_all_sql_cases(domain)
Esempio n. 38
0
def build_form_multimedia_zip(domain, xmlns, startdate, enddate, app_id, export_id, zip_name, download_id):

    def find_question_id(form, value):
        for k, v in form.iteritems():
            if isinstance(v, dict):
                ret = find_question_id(v, value)
                if ret:
                    return [k] + ret
            else:
                if v == value:
                    return [k]

        return None

    def filename(form_info, question_id, extension):
        fname = u"%s-%s-%s-%s%s"
        if form_info['cases']:
            fname = u'-'.join(form_info['cases']) + u'-' + fname
        return fname % (form_info['name'],
                        unidecode(question_id),
                        form_info['user'],
                        form_info['id'], extension)

    case_ids = set()

    def extract_form_info(form, properties=None, case_ids=case_ids):
        unknown_number = 0
        meta = form['form'].get('meta', dict())
        # get case ids
        case_blocks = extract_case_blocks(form)
        cases = {c['@case_id'] for c in case_blocks}
        case_ids |= cases

        form_info = {
            'form': form,
            'attachments': list(),
            'name': form['form'].get('@name', 'unknown form'),
            'user': meta.get('username', 'unknown_user'),
            'cases': cases,
            'id': form['_id']
        }
        for k, v in form['_attachments'].iteritems():
            if v['content_type'] == 'text/xml':
                continue
            try:
                question_id = unicode(u'-'.join(find_question_id(form['form'], k)))
            except TypeError:
                question_id = unicode(u'unknown' + unicode(unknown_number))
                unknown_number += 1

            if not properties or question_id in properties:
                extension = unicode(os.path.splitext(k)[1])
                form_info['attachments'].append({
                    'size': v['length'],
                    'name': k,
                    'question_id': question_id,
                    'extension': extension,
                    'timestamp': parse(form['received_on']).timetuple(),
                })

        return form_info

    key = [domain, app_id, xmlns]
    form_ids = {f['id'] for f in XFormInstance.get_db().view("attachments/attachments",
                                                             start_key=key + [startdate],
                                                             end_key=key + [enddate, {}],
                                                             reduce=False)}

    properties = set()
    if export_id:
        schema = FormExportSchema.get(export_id)
        for table in schema.tables:
            # - in question id is replaced by . in excel exports
            properties |= {c.display.replace('.', '-') for c in table.columns}

    if not app_id:
        zip_name = 'Unrelated Form'
    forms_info = list()
    for form in iter_docs(XFormInstance.get_db(), form_ids):
        if not zip_name:
            zip_name = unidecode(form['form'].get('@name', 'unknown form'))
        forms_info.append(extract_form_info(form, properties))

    num_forms = len(forms_info)
    DownloadBase.set_progress(build_form_multimedia_zip, 0, num_forms)

    # get case names
    case_id_to_name = {c: c for c in case_ids}
    for case in iter_docs(CommCareCase.get_db(), case_ids):
        if case['name']:
            case_id_to_name[case['_id']] = case['name']

    use_transfer = settings.SHARED_DRIVE_CONF.transfer_enabled
    if use_transfer:
        params = '_'.join(map(str, [xmlns, startdate, enddate, export_id, num_forms]))
        fname = '{}-{}'.format(app_id, hashlib.md5(params).hexdigest())
        fpath = os.path.join(settings.SHARED_DRIVE_CONF.transfer_dir, fname)
    else:
        _, fpath = tempfile.mkstemp()

    if not (os.path.isfile(fpath) and use_transfer):  # Don't rebuild the file if it is already there
        with open(fpath, 'wb') as zfile:
            with zipfile.ZipFile(zfile, 'w') as z:
                for form_number, form_info in enumerate(forms_info):
                    f = XFormInstance.wrap(form_info['form'])
                    form_info['cases'] = {case_id_to_name[case_id] for case_id in form_info['cases']}
                    for a in form_info['attachments']:
                        fname = filename(form_info, a['question_id'], a['extension'])
                        zi = zipfile.ZipInfo(fname, a['timestamp'])
                        z.writestr(zi, f.fetch_attachment(a['name'], stream=True).read(), zipfile.ZIP_STORED)
                    DownloadBase.set_progress(build_form_multimedia_zip, form_number + 1, num_forms)

    common_kwargs = dict(
        mimetype='application/zip',
        content_disposition='attachment; filename="{fname}.zip"'.format(fname=zip_name),
        download_id=download_id,
    )

    if use_transfer:
        expose_file_download(
            fpath,
            use_transfer=use_transfer,
            **common_kwargs
        )
    else:
        expose_cached_download(
            FileWrapper(open(fpath)),
            expiry=(1 * 60 * 60),
            **common_kwargs
        )

    DownloadBase.set_progress(build_form_multimedia_zip, num_forms, num_forms)
Esempio n. 39
0
def _get_couch_forms_by_doc_type(domain):
    return _get_couch_doc_counts(CommCareCase.get_db(), domain,
                                 all_known_formlike_doc_types())
Esempio n. 40
0
def _get_couch_cases_by_doc_type(domain):
    return _get_couch_doc_counts(CommCareCase.get_db(), domain,
                                 ('CommCareCase', 'CommCareCase-Deleted'))
Esempio n. 41
0
    def rows(self):
        startdate = json_format_datetime(self.datespan.startdate_utc)
        enddate = json_format_datetime(self.datespan.enddate_utc)
        data = CallLog.by_domain_date(self.domain, startdate, enddate)
        result = []

        # Store the results of lookups for faster loading
        username_map = {}
        form_map = {}

        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }

        # Retrieve message log options
        message_log_options = getattr(settings, "MESSAGE_LOG_OPTIONS", {})
        abbreviated_phone_number_domains = message_log_options.get(
            "abbreviated_phone_number_domains", [])
        abbreviate_phone_number = (self.domain
                                   in abbreviated_phone_number_domains)

        for call in data:
            recipient_id = call.couch_recipient
            if recipient_id in [None, ""]:
                username = "******"
            elif recipient_id in username_map:
                username = username_map.get(recipient_id)
            else:
                username = "******"
                try:
                    if call.couch_recipient_doc_type == "CommCareCase":
                        username = CommCareCase.get(recipient_id).name
                    else:
                        username = CouchUser.get_by_user_id(
                            recipient_id).username
                except Exception:
                    pass

                username_map[recipient_id] = username

            form_unique_id = call.form_unique_id
            if form_unique_id in [None, ""]:
                form_name = "-"
            elif form_unique_id in form_map:
                form_name = form_map.get(form_unique_id)
            else:
                form_name = get_form_name(form_unique_id)
                form_map[form_unique_id] = form_name

            phone_number = call.phone_number
            if abbreviate_phone_number and phone_number is not None:
                phone_number = phone_number[0:7] if phone_number[
                    0:1] == "+" else phone_number[0:6]

            timestamp = tz_utils.adjust_datetime_to_timezone(
                call.date, pytz.utc.zone, self.timezone.zone)

            if call.direction == INCOMING:
                answered = "-"
            else:
                answered = _("Yes") if call.answered else _("No")

            if call.xforms_session_id is None:
                submission_id = None
            else:
                session = XFormsSession.latest_by_session_id(
                    call.xforms_session_id)
                submission_id = session.submission_id

            row = [
                self._fmt_timestamp(timestamp),
                self._fmt(username),
                self._fmt(phone_number),
                self._fmt(direction_map.get(call.direction, "-")),
                self._fmt(form_name),
                self._fmt("-") if submission_id is None else
                self._fmt_submission_link(submission_id),
                self._fmt(answered),
                self._fmt(call.duration),
                self._fmt(_("Yes") if call.error else _("No")),
                self._fmt(call.error_message),
            ]

            if self.request.couch_user.is_previewer():
                row.append(self._fmt(call.gateway_session_id))

            result.append(row)

        return result
Esempio n. 42
0
def wrapped_case(case):
    json = case.to_json()
    case_class = CommCareCase.get_wrap_class(json)
    return case_class.wrap(case.to_json())
Esempio n. 43
0
 def setUpClass(cls):
     super(OverrideDBTest, cls).setUpClass()
     cls.other_db_1 = Database(settings.COUCH_DATABASE + '_foo-test', create=True)
     cls.other_db_2 = Database(settings.COUCH_DATABASE + '_foo-boo-test', create=True)
     cls.normal_db = CommCareCase.get_db()
     cls.normal_get_db = CommCareCase.get_db
Esempio n. 44
0
class KooKooTestCase(TouchformsTestCase):
    """
    Must be run manually (see corehq.apps.sms.tests.util.TouchformsTestCase)
    """
    def setUp(self):
        super(KooKooTestCase, self).setUp()
        self.ivr_backend = MobileBackend(
            _id="MOBILE_BACKEND_KOOKOO",
            outbound_module="corehq.apps.kookoo.api",
            outbound_params={
                "is_test": True,
                "api_key": "xyz"
            },
        )
        self.ivr_backend.save()

        self.user1 = self.create_mobile_worker("user1",
                                               "123",
                                               "91001",
                                               save_vn=False)
        self.user2 = self.create_mobile_worker("user2",
                                               "123",
                                               "91002",
                                               save_vn=False)
        self.create_group("group1", [self.user1, self.user2])

        self.case = CommCareCase(
            domain=self.domain,
            type="participant",
            owner_id=self.groups[0]._id,
        )
        self.case.set_case_property("contact_phone_number", "91000")
        self.case.set_case_property("contact_phone_number_is_verified", "1")
        self.case.set_case_property("contact_ivr_backend_id",
                                    "MOBILE_BACKEND_KOOKOO")
        self.case.save()

        dirname = os.path.dirname(os.path.abspath(__file__))
        self.load_app("app1.json", dirname)
        self.load_app("app2.json", dirname)

        self.reminder1 = CaseReminderHandler(
            domain=self.domain,
            active=True,
            case_type="participant",
            method=METHOD_IVR_SURVEY,
            recipient=RECIPIENT_CASE,
            sample_id=None,
            user_group_id=None,
            user_id=None,
            case_id=None,
            reminder_type=REMINDER_TYPE_DEFAULT,
            submit_partial_forms=True,
            include_case_side_effects=False,
            max_question_retries=5,
            start_condition_type=CASE_CRITERIA,
            start_property="name",
            start_value="case1",
            start_date=None,
            start_offset=0,
            start_match_type=MATCH_EXACT,
            events=[
                CaseReminderEvent(
                    day_num=0,
                    fire_time=time(12, 0),
                    fire_time_type=FIRE_TIME_DEFAULT,
                    callback_timeout_intervals=[30],
                    form_unique_id=self.apps[0].modules[0].forms[0].unique_id,
                ),
                CaseReminderEvent(
                    day_num=0,
                    fire_time=time(13, 0),
                    fire_time_type=FIRE_TIME_DEFAULT,
                    callback_timeout_intervals=[30],
                    form_unique_id=self.apps[0].modules[0].forms[1].unique_id,
                ),
            ],
            schedule_length=1,
            event_interpretation=EVENT_AS_SCHEDULE,
            max_iteration_count=7,
            until=None,
            force_surveys_to_use_triggered_case=False,
        )
        self.reminder1.save()

        self.reminder2 = CaseReminderHandler(
            domain=self.domain,
            active=True,
            case_type="participant",
            method=METHOD_IVR_SURVEY,
            recipient=RECIPIENT_OWNER,
            sample_id=None,
            user_group_id=None,
            user_id=None,
            case_id=None,
            reminder_type=REMINDER_TYPE_DEFAULT,
            submit_partial_forms=True,
            include_case_side_effects=True,
            max_question_retries=5,
            start_condition_type=CASE_CRITERIA,
            start_property="name",
            start_value="case2",
            start_date=None,
            start_offset=0,
            start_match_type=MATCH_EXACT,
            events=[
                CaseReminderEvent(
                    day_num=0,
                    fire_time=time(12, 0),
                    fire_time_type=FIRE_TIME_DEFAULT,
                    callback_timeout_intervals=[30, 30],
                    form_unique_id=self.apps[1].modules[0].forms[0].unique_id,
                ),
            ],
            schedule_length=1,
            event_interpretation=EVENT_AS_SCHEDULE,
            max_iteration_count=7,
            until=None,
            force_surveys_to_use_triggered_case=False,
        )
        self.reminder2.save()

    def kookoo_in(self, params):
        """
        params should be a dictionary containing:
        event, cid, sid, and (optionally) data
        """
        params = urllib.urlencode(params)
        url = "%s/kookoo/ivr/" % self.live_server_url
        return urllib2.urlopen("%s?%s" % (url, params)).read()

    def kookoo_finished(self, params):
        """
        params should be a dictionary containing:
        sid, status, and duration
        """
        params = urllib.urlencode(params)
        url = "%s/kookoo/ivr_finished/" % self.live_server_url
        return urllib2.urlopen(url, params).read()

    def testOutbound(self):
        # Send an outbound call using self.reminder1 to self.case
        # and answer it
        CaseReminderHandler.now = datetime(2014, 6, 23, 10, 0)
        self.case.name = "case1"
        self.case.save()
        CaseReminderHandler.now = datetime(2014, 6, 23, 12, 0)
        CaseReminderHandler.fire_reminders()
        reminder = self.reminder1.get_reminder(self.case)
        self.assertEquals(reminder.next_fire, datetime(2014, 6, 23, 12, 30))

        call = self.get_last_outbound_call(self.case)
        self.assertTrue(call.use_precached_first_response)

        kookoo_session_id = call.gateway_session_id[7:]
        resp = self.kookoo_in({
            "cid": "0000",
            "sid": kookoo_session_id,
            "event": "NewCall",
        })
        self.assertEqual(
            resp, '<response sid="%s"><collectdtmf l="1" o="3000">'
            '<playtext>How do you feel today? Press 1 for good, 2 for bad.'
            '</playtext></collectdtmf></response>' % kookoo_session_id)

        resp = self.kookoo_in({
            "cid": "0000",
            "sid": kookoo_session_id,
            "event": "GotDTMF",
            "data": "1",
        })
        self.assertEqual(
            resp, '<response sid="%s"><collectdtmf l="1" o="3000">'
            '<playtext>Did you remember to take your meds today? Press 1 for yes, 2 for no.'
            '</playtext></collectdtmf></response>' % kookoo_session_id)

        resp = self.kookoo_in({
            "cid": "0000",
            "sid": kookoo_session_id,
            "event": "GotDTMF",
            "data": "2",
        })
        self.assertEqual(
            resp,
            '<response sid="%s"><hangup/></response>' % kookoo_session_id)

        self.kookoo_finished({
            "sid": kookoo_session_id,
            "status": "answered",
            "duration": "20",
        })

        call = CallLog.get(call._id)
        self.assertTrue(call.answered)
        self.assertEqual(call.duration, 20)

        form = self.get_last_form_submission()
        self.assertFormQuestionEquals(form, "how_feel", "1")
        self.assertFormQuestionEquals(form, "take_meds", "2")
        case = CommCareCase.get(self.case._id)
        self.assertCasePropertyEquals(case, "how_feel", "1")
        self.assertCasePropertyEquals(case, "take_meds", "2")

        CaseReminderHandler.now = datetime(2014, 6, 23, 12, 30)
        CaseReminderHandler.fire_reminders()

        reminder = self.reminder1.get_reminder(self.case)
        self.assertEquals(reminder.next_fire, datetime(2014, 6, 23, 13, 0))

        last_call = self.get_last_outbound_call(self.case)
        self.assertEqual(call._id, last_call._id)

        # Move on to the second event which now uses an all-label form and
        # should not precache the first ivr response
        CaseReminderHandler.now = datetime(2014, 6, 23, 13, 0)
        CaseReminderHandler.fire_reminders()

        reminder = self.reminder1.get_reminder(self.case)
        self.assertEquals(reminder.next_fire, datetime(2014, 6, 23, 13, 30))

        call = self.get_last_outbound_call(self.case)
        self.assertFalse(call.use_precached_first_response)

        kookoo_session_id = call.gateway_session_id[7:]
        resp = self.kookoo_in({
            "cid": "0000",
            "sid": kookoo_session_id,
            "event": "NewCall",
        })
        self.assertEqual(
            resp, '<response sid="%s">'
            '<playtext>This is just a reminder to take your meds.'
            '</playtext><hangup/></response>' % kookoo_session_id)

        self.kookoo_finished({
            "sid": kookoo_session_id,
            "status": "answered",
            "duration": "5",
        })

        call = CallLog.get(call._id)
        self.assertTrue(call.answered)
        self.assertEqual(call.duration, 5)

        form = self.get_last_form_submission()
        self.assertFormQuestionEquals(form, "label", "ok")

        CaseReminderHandler.now = datetime(2014, 6, 23, 13, 30)
        CaseReminderHandler.fire_reminders()

        reminder = self.reminder1.get_reminder(self.case)
        self.assertEquals(reminder.next_fire, datetime(2014, 6, 24, 12, 0))

        last_call = self.get_last_outbound_call(self.case)
        self.assertEqual(call._id, last_call._id)

        # Now test sending outbound calls to a group of users (the owners
        # of the case)

        # Allow sending to unverified numbers
        self.domain_obj = Domain.get(self.domain_obj._id)
        self.domain_obj.send_to_duplicated_case_numbers = True
        self.domain_obj.save()

        CaseReminderHandler.now = datetime(2014, 6, 24, 10, 0)
        self.case = CommCareCase.get(self.case._id)
        self.case.name = "case2"
        self.case.save()
        reminder = self.reminder2.get_reminder(self.case)
        self.assertEquals(reminder.next_fire, datetime(2014, 6, 24, 12, 0))

        CaseReminderHandler.now = datetime(2014, 6, 24, 12, 0)
        CaseReminderHandler.fire_reminders()
        reminder = self.reminder2.get_reminder(self.case)
        self.assertEquals(reminder.next_fire, datetime(2014, 6, 24, 12, 30))

        call1 = self.get_last_outbound_call(self.user1)
        self.assertTrue(call1.use_precached_first_response)
        self.assertFalse(call1.answered)

        call2 = self.get_last_outbound_call(self.user2)
        self.assertTrue(call2.use_precached_first_response)
        self.assertFalse(call2.answered)

        old_call1 = call1
        old_call2 = call2

        CaseReminderHandler.now = datetime(2014, 6, 24, 12, 30)
        CaseReminderHandler.fire_reminders()
        reminder = self.reminder2.get_reminder(self.case)
        self.assertEquals(reminder.next_fire, datetime(2014, 6, 24, 13, 0))

        call1 = self.get_last_outbound_call(self.user1)
        self.assertTrue(call1.use_precached_first_response)
        self.assertNotEqual(call1._id, old_call1._id)

        call2 = self.get_last_outbound_call(self.user2)
        self.assertTrue(call2.use_precached_first_response)
        self.assertFalse(call2.answered)
        self.assertNotEqual(call2._id, old_call2._id)

        kookoo_session_id = call1.gateway_session_id[7:]
        resp = self.kookoo_in({
            "cid": "0001",
            "sid": kookoo_session_id,
            "event": "NewCall",
        })
        self.assertEqual(
            resp, '<response sid="%s"><collectdtmf l="1" o="3000">'
            '<playtext>How do you feel today? Press 1 for good, 2 for bad.'
            '</playtext></collectdtmf></response>' % kookoo_session_id)

        resp = self.kookoo_in({
            "cid": "0001",
            "sid": kookoo_session_id,
            "event": "GotDTMF",
            "data": "2",
        })
        self.assertEqual(
            resp, '<response sid="%s"><collectdtmf l="1" o="3000">'
            '<playtext>Did you remember to take your meds today? Press 1 for yes, 2 for no.'
            '</playtext></collectdtmf></response>' % kookoo_session_id)

        resp = self.kookoo_in({
            "cid": "0001",
            "sid": kookoo_session_id,
            "event": "GotDTMF",
            "data": "1",
        })
        self.assertEqual(
            resp,
            '<response sid="%s"><hangup/></response>' % kookoo_session_id)

        self.kookoo_finished({
            "sid": kookoo_session_id,
            "status": "answered",
            "duration": "20",
        })
        call1 = CallLog.get(call1._id)
        self.assertTrue(call1.answered)
        self.assertEqual(call1.duration, 20)

        form = self.get_last_form_submission()
        self.assertFormQuestionEquals(form, "how_feel", "2")
        self.assertFormQuestionEquals(form, "take_meds", "1")
        self.assertEqual(form.form["meta"]["userID"], self.user1._id)
        case = CommCareCase.get(self.case._id)
        self.assertCasePropertyEquals(case, "how_feel", "2")
        self.assertCasePropertyEquals(case, "take_meds", "1")
        self.assertEqual(case.user_id, self.user1._id)

        old_call1 = call1
        old_call2 = call2

        CaseReminderHandler.now = datetime(2014, 6, 24, 13, 0)
        CaseReminderHandler.fire_reminders()
        reminder = self.reminder2.get_reminder(self.case)
        self.assertEquals(reminder.next_fire, datetime(2014, 6, 25, 12, 0))

        call1 = self.get_last_outbound_call(self.user1)
        # No new call for user1 since it was already answered
        self.assertEqual(call1._id, old_call1._id)

        call2 = self.get_last_outbound_call(self.user2)
        self.assertTrue(call2.use_precached_first_response)
        self.assertNotEqual(call2._id, old_call2._id)

        kookoo_session_id = call2.gateway_session_id[7:]
        resp = self.kookoo_in({
            "cid": "0002",
            "sid": kookoo_session_id,
            "event": "NewCall",
        })
        self.assertEqual(
            resp, '<response sid="%s"><collectdtmf l="1" o="3000">'
            '<playtext>How do you feel today? Press 1 for good, 2 for bad.'
            '</playtext></collectdtmf></response>' % kookoo_session_id)

        resp = self.kookoo_in({
            "cid": "0002",
            "sid": kookoo_session_id,
            "event": "GotDTMF",
            "data": "1",
        })
        self.assertEqual(
            resp, '<response sid="%s"><collectdtmf l="1" o="3000">'
            '<playtext>Did you remember to take your meds today? Press 1 for yes, 2 for no.'
            '</playtext></collectdtmf></response>' % kookoo_session_id)

        resp = self.kookoo_in({
            "cid": "0002",
            "sid": kookoo_session_id,
            "event": "GotDTMF",
            "data": "2",
        })
        self.assertEqual(
            resp,
            '<response sid="%s"><hangup/></response>' % kookoo_session_id)

        self.kookoo_finished({
            "sid": kookoo_session_id,
            "status": "answered",
            "duration": "20",
        })
        call2 = CallLog.get(call2._id)
        self.assertTrue(call2.answered)
        self.assertEqual(call2.duration, 20)

        form = self.get_last_form_submission()
        self.assertFormQuestionEquals(form, "how_feel", "1")
        self.assertFormQuestionEquals(form, "take_meds", "2")
        self.assertEqual(form.form["meta"]["userID"], self.user2._id)
        case = CommCareCase.get(self.case._id)
        self.assertCasePropertyEquals(case, "how_feel", "1")
        self.assertCasePropertyEquals(case, "take_meds", "2")
        self.assertEqual(case.user_id, self.user2._id)

    def tearDown(self):
        self.ivr_backend.delete()
        super(KooKooTestCase, self).tearDown()
    def test_simple_case_management(self, datetime_mock):
        fake_time_now = datetime(2015, 4, 24, 12, 30, 8, 24886)
        datetime_mock.utcnow.return_value = fake_time_now
        index_column_id = 'indices.app_data_parent'
        app = self.app
        data_sources = get_case_data_sources(app)
        self.assertEqual(2, len(data_sources))
        data_source = data_sources[self.case_type]
        self.assertEqual(self.domain, data_source.domain)
        self.assertEqual('CommCareCase', data_source.referenced_doc_type)
        self.assertEqual(self.case_type, data_source.table_id)
        self.assertEqual(self.case_type, data_source.display_name)

        # test the filter
        self.assertTrue(
            data_source.filter({
                'doc_type': 'CommCareCase',
                'domain': self.domain,
                'type': self.case_type
            }))
        self.assertFalse(
            data_source.filter({
                'doc_type': 'CommCareCase',
                'domain': 'wrong-domain',
                'type': self.case_type
            }))
        self.assertFalse(
            data_source.filter({
                'doc_type': 'NotCommCareCase',
                'domain': self.domain,
                'type': self.case_type
            }))
        self.assertFalse(
            data_source.filter({
                'doc_type': 'CommCareCase',
                'domain': self.domain,
                'type': 'wrong-type'
            }))

        # check the indicators
        datetime_columns = [
            "last_modified_date", "opened_date", "closed_date", "inserted_at",
            "server_last_modified_date"
        ]
        expected_columns = set(datetime_columns + [
            "doc_id",
            "case_type",
            "last_modified_by_user_id",
            "opened_by_user_id",
            "closed",
            "closed_by_user_id",
            "owner_id",
            "name",
            "state",
            "external_id",
            "count",
        ] + list(self.case_properties.keys()) + [index_column_id])
        self.assertEqual(
            expected_columns,
            set(col_back.id for col_back in data_source.get_columns()))

        modified_on = datetime(2014, 11, 12, 15, 37, 49)
        opened_on = datetime(2014, 11, 11, 23, 34, 34, 25)
        parent_id = 'fake-parent-id'
        sample_doc = CommCareCase(_id='some-doc-id',
                                  modified_on=modified_on,
                                  opened_on=opened_on,
                                  user_id="23407238074",
                                  owner_id="0923409230948",
                                  name="priority ticket",
                                  domain=app.domain,
                                  type=self.case_type,
                                  first_name='test first',
                                  last_name='test last',
                                  children='3',
                                  dob='2001-01-01',
                                  indices=[
                                      CommCareCaseIndex(
                                          identifier='parent',
                                          referenced_type=self.parent_type,
                                          referenced_id=parent_id)
                                  ]).to_json()

        def _get_column_property(column):
            # this is the mapping of column id to case property path
            property_map = {
                c.label: c.item.path[0].name
                for c in MAIN_CASE_TABLE_PROPERTIES
            }
            property_map.update({
                'doc_id': '_id',
            })
            return property_map.get(column.id, column.id)

        default_case_property_datatypes = DEFAULT_CASE_PROPERTY_DATATYPES
        [row] = data_source.get_all_values(sample_doc)
        for result in row:
            if result.column.id in datetime_columns:
                self.assertEqual(result.column.datatype, 'datetime')
            if result.column.id == "inserted_at":
                self.assertEqual(fake_time_now, result.value)
            if result.column.id == index_column_id:
                self.assertEqual(parent_id, result.value)
            elif result.column.id == "last_modified_date":
                self.assertEqual(modified_on, result.value)
            elif result.column.id == "opened_date":
                self.assertEqual(opened_on, result.value)
            elif result.column.id == "count":
                self.assertEqual(1, result.value)
            elif result.column.id not in [
                    "repeat_iteration", "inserted_at", 'closed'
            ]:
                self.assertEqual(
                    sample_doc[_get_column_property(result.column)],
                    result.value)
                if result.column.id in default_case_property_datatypes:
                    self.assertEqual(
                        result.column.datatype,
                        default_case_property_datatypes[result.column.id])
Esempio n. 46
0
    def rows(self):
        group_id = None
        if self.request.couch_user.is_commcare_user():
            group_ids = self.request.couch_user.get_group_ids()
            if len(group_ids) > 0:
                group_id = group_ids[0]

        cases = CommCareCase.view("hqcase/types_by_domain",
                                  key=[self.domain, "participant"],
                                  reduce=False,
                                  include_docs=True).all()

        data = {}

        for case in cases:
            # If a site coordinator is viewing the report, only show participants from that site (group)
            if group_id is None or group_id == case.owner_id:
                data[case._id] = {
                    "name": case.name,
                    "time_zone": case.get_case_property("time_zone"),
                    "dates": [None for x in range(14)],
                }

        dates = self.get_past_two_weeks()
        date_strings = [date.strftime("%Y-%m-%d") for date in dates]

        start_date = dates[0] - timedelta(days=1)
        end_date = dates[-1] + timedelta(days=2)

        start_utc_timestamp = json_format_datetime(start_date)
        end_utc_timestamp = json_format_datetime(end_date)

        expected_callback_events = ExpectedCallbackEventLog.view(
            "sms/expected_callback_event",
            startkey=[self.domain, start_utc_timestamp],
            endkey=[self.domain, end_utc_timestamp],
            include_docs=True).all()

        for event in expected_callback_events:
            if event.couch_recipient in data:
                event_date = tz_utils.adjust_datetime_to_timezone(
                    event.date, pytz.utc.zone,
                    data[event.couch_recipient]["time_zone"]).date()
                event_date = event_date.strftime("%Y-%m-%d")
                if event_date in date_strings:
                    data[event.couch_recipient]["dates"][date_strings.index(
                        event_date)] = event.status

        result = []
        for case_id, data_dict in data.items():
            row = [
                self._fmt(data_dict["name"]),
                None,
                None,
                None,
            ]

            total_no_response = 0
            total_indicated = 0
            total_pending = 0

            for date_status in data_dict["dates"]:
                if date_status == CALLBACK_PENDING:
                    total_indicated += 1
                    total_pending += 1
                    row.append(self._fmt(_("pending")))
                elif date_status == CALLBACK_RECEIVED:
                    total_indicated += 1
                    row.append(self._fmt(_("OK")))
                elif date_status == CALLBACK_MISSED:
                    total_indicated += 1
                    total_no_response += 1
                    row.append(self._fmt_highlight(_("No Response")))
                else:
                    row.append(self._fmt(_("not indicated")))

            if total_no_response > 0:
                row[1] = self._fmt_highlight(total_no_response)
            else:
                row[1] = self._fmt(total_no_response)
            row[2] = self._fmt(total_indicated)
            row[3] = self._fmt(total_pending)

            result.append(row)

        return result
Esempio n. 47
0
 def case(self):
     return CommCareCase.get(self.case_id)
Esempio n. 48
0
def commit_plan(domain, planning_db):
    assert get_planning_db_filepath(domain) == planning_db.db_filepath
    for form in planning_db.get_forms():
        XFormInstance.get_db().save_doc(form)
    for case in planning_db.get_cases():
        CommCareCase.get_db().save_doc(case)
Esempio n. 49
0
 def test_generate_xml_with_no_date_modified(self):
     # before this test was added both of these calls failed
     for version in (V1, V2):
         CommCareCase(_id='test').to_xml(version)
Esempio n. 50
0
    def test_nested(self):
        self.assertEqual(CommCareCase.get_db(), self.normal_db)
        self.assertEqual(CommCareCase.get_db, self.normal_get_db)

        with OverrideDB(CommCareCase, self.other_db_1):
            self.assertEqual(CommCareCase.get_db(), self.other_db_1)
            self.assertNotEqual(CommCareCase.get_db(), self.normal_db)
            self.assertNotEqual(CommCareCase.get_db(), self.normal_get_db)

            with OverrideDB(CommCareCase, self.other_db_2):
                self.assertEqual(CommCareCase.get_db(), self.other_db_2)
                self.assertNotEqual(CommCareCase.get_db(), self.normal_db)
                self.assertNotEqual(CommCareCase.get_db(), self.normal_get_db)

            self.assertEqual(CommCareCase.get_db(), self.other_db_1)
            self.assertNotEqual(CommCareCase.get_db(), self.normal_db)
            self.assertNotEqual(CommCareCase.get_db(), self.normal_get_db)

        self.assertEqual(CommCareCase.get_db(), self.normal_db)
        self.assertEqual(CommCareCase.get_db, self.normal_get_db)
Esempio n. 51
0
def get_open_case_docs_in_domain(domain, type=None, owner_id=None):
    from casexml.apps.case.models import CommCareCase
    case_ids = get_open_case_ids_in_domain(domain, type, owner_id)
    for doc in iter_docs(CommCareCase.get_db(), case_ids):
        yield doc
Esempio n. 52
0
 def get_case(self):
     if self.request.GET.get('patient_id', None) is None:
         return None
     return CommCareCase.get(self.request.GET['patient_id'])
Esempio n. 53
0
def _get_cases_by_domain_hq_user_id(domain, user_id, case_type, include_docs):
    return CommCareCase.view('case_by_domain_hq_user_id_type/view',
                             key=[domain, user_id, case_type],
                             reduce=False,
                             include_docs=include_docs).all()
Esempio n. 54
0
class BackendTestCase(BaseAccountingTest):
    def get_or_create_site(self):
        site, created = Site.objects.get_or_create(id=settings.SITE_ID)
        if created:
            site.domain = 'localhost'
            site.name = 'localhost'
            site.save()
        return (site, created)

    def setUp(self):
        super(BackendTestCase, self).setUp()

        self.domain = "test-domain"
        self.domain2 = "test-domain2"

        self.site, self.site_created = self.get_or_create_site()

        self.domain_obj = Domain(name=self.domain)
        self.domain_obj.save()

        generator.instantiate_accounting_for_tests()
        self.account = BillingAccount.get_or_create_account_by_domain(
            self.domain_obj.name,
            created_by="automated-test",
        )[0]
        plan = DefaultProductPlan.get_default_plan_by_domain(
            self.domain_obj, edition=SoftwarePlanEdition.ADVANCED)
        self.subscription = Subscription.new_domain_subscription(
            self.account, self.domain_obj.name, plan)
        self.subscription.is_active = True
        self.subscription.save()
        self.domain_obj = Domain.get(
            self.domain_obj._id)  # Prevent resource conflict

        self.backend1 = TestCaseBackend(name="BACKEND1", is_global=True)
        self.backend1.save()

        self.backend2 = TestCaseBackend(name="BACKEND2", is_global=True)
        self.backend2.save()

        self.backend3 = TestCaseBackend(name="BACKEND3", is_global=True)
        self.backend3.save()

        self.backend4 = TestCaseBackend(name="BACKEND4", is_global=True)
        self.backend4.save()

        self.backend5 = TestCaseBackend(name="BACKEND5",
                                        domain=self.domain,
                                        is_global=False,
                                        authorized_domains=[])
        self.backend5.save()

        self.backend6 = TestCaseBackend(name="BACKEND6",
                                        domain=self.domain2,
                                        is_global=False,
                                        authorized_domains=[self.domain])
        self.backend6.save()

        self.backend7 = TestCaseBackend(name="BACKEND7",
                                        domain=self.domain2,
                                        is_global=False,
                                        authorized_domains=[])
        self.backend7.save()

        self.backend8 = TestCaseBackend(name="BACKEND",
                                        domain=self.domain,
                                        is_global=False,
                                        authorized_domains=[])
        self.backend8.save()

        self.backend9 = TestCaseBackend(name="BACKEND",
                                        domain=self.domain2,
                                        is_global=False,
                                        authorized_domains=[self.domain])
        self.backend9.save()

        self.backend10 = TestCaseBackend(name="BACKEND", is_global=True)
        self.backend10.save()

        self.backend_mapping1 = BackendMapping(is_global=True,
                                               prefix="*",
                                               backend_id=self.backend1._id)
        self.backend_mapping1.save()

        self.backend_mapping2 = BackendMapping(is_global=True,
                                               prefix="1",
                                               backend_id=self.backend2._id)
        self.backend_mapping2.save()

        self.backend_mapping3 = BackendMapping(is_global=True,
                                               prefix="91",
                                               backend_id=self.backend3._id)
        self.backend_mapping3.save()

        self.backend_mapping4 = BackendMapping(is_global=True,
                                               prefix="265",
                                               backend_id=self.backend4._id)
        self.backend_mapping4.save()

        self.backend_mapping5 = BackendMapping(is_global=True,
                                               prefix="256",
                                               backend_id=self.backend5._id)
        self.backend_mapping5.save()

        self.backend_mapping6 = BackendMapping(is_global=True,
                                               prefix="25670",
                                               backend_id=self.backend6._id)
        self.backend_mapping6.save()

        self.backend_mapping7 = BackendMapping(is_global=True,
                                               prefix="25675",
                                               backend_id=self.backend7._id)
        self.backend_mapping7.save()

        self.case = CommCareCase(domain=self.domain)
        self.case.set_case_property("contact_phone_number", "15551234567")
        self.case.set_case_property("contact_phone_number_is_verified", "1")
        self.case.save()

        self.contact = CommConnectCase.wrap(self.case.to_json())

        settings.SMS_LOADED_BACKENDS.append(
            "corehq.apps.sms.tests.TestCaseBackend")

    def tearDown(self):
        self.backend1.delete_invoke_doc()
        self.backend1.delete()
        self.backend_mapping1.delete()

        self.backend2.delete_invoke_doc()
        self.backend2.delete()
        self.backend_mapping2.delete()

        self.backend3.delete_invoke_doc()
        self.backend3.delete()
        self.backend_mapping3.delete()

        self.backend4.delete_invoke_doc()
        self.backend4.delete()
        self.backend_mapping4.delete()

        self.backend5.delete_invoke_doc()
        self.backend5.delete()
        self.backend_mapping5.delete()

        self.backend6.delete_invoke_doc()
        self.backend6.delete()
        self.backend_mapping6.delete()

        self.backend7.delete_invoke_doc()
        self.backend7.delete()
        self.backend_mapping7.delete()

        self.contact.delete_verified_number()
        self.case.delete()

        SubscriptionAdjustment.objects.all().delete()
        self.subscription.delete()
        self.account.delete()

        self.domain_obj.delete()

        if self.site_created:
            self.site.delete()

        settings.SMS_LOADED_BACKENDS.pop()

    def test_multiple_country_prefixes(self):
        self.assertEqual(
            MobileBackend.auto_load('256800000000')._id, self.backend5._id)
        self.assertEqual(
            MobileBackend.auto_load('256700000000')._id, self.backend6._id)
        self.assertEqual(
            MobileBackend.auto_load('256750000000')._id, self.backend7._id)

    def test_backend(self):
        # Test the backend map

        self.assertTrue(
            send_sms(self.domain, None, "15551234567", "Test for BACKEND2"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertTrue(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend2.delete_invoke_doc()
        self.assertFalse(self.backend2.invoke_doc_exists())

        self.assertTrue(
            send_sms(self.domain, None, "9100000000", "Test for BACKEND3"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertTrue(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend3.delete_invoke_doc()
        self.assertFalse(self.backend3.invoke_doc_exists())

        self.assertTrue(
            send_sms(self.domain, None, "26500000000", "Test for BACKEND4"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertTrue(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend4.delete_invoke_doc()
        self.assertFalse(self.backend4.invoke_doc_exists())

        self.assertTrue(
            send_sms(self.domain, None, "25800000000", "Test for BACKEND1"))
        self.assertTrue(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend1.delete_invoke_doc()
        self.assertFalse(self.backend1.invoke_doc_exists())

        # Test overriding with a domain-level backend

        self.domain_obj = Domain.get(
            self.domain_obj._id)  # Prevent resource conflict
        self.domain_obj.default_sms_backend_id = self.backend5._id
        self.domain_obj.save()

        self.assertTrue(
            send_sms(self.domain, None, "15551234567", "Test for BACKEND5"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertTrue(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend5.delete_invoke_doc()
        self.assertFalse(self.backend5.invoke_doc_exists())

        # Test use of backend that another domain owns but has granted access

        self.domain_obj.default_sms_backend_id = self.backend6._id
        self.domain_obj.save()

        self.assertTrue(
            send_sms(self.domain, None, "25800000000", "Test for BACKEND6"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertTrue(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend6.delete_invoke_doc()
        self.assertFalse(self.backend6.invoke_doc_exists())

        # Test backend access control

        self.domain_obj.default_sms_backend_id = self.backend7._id
        self.domain_obj.save()

        self.assertFalse(
            send_sms(self.domain, None, "25800000000", "Test for BACKEND7"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())

        # Test sending to verified number with backend map

        self.domain_obj.default_sms_backend_id = None
        self.domain_obj.save()

        verified_number = self.contact.get_verified_number()
        self.assertTrue(verified_number is not None)
        self.assertTrue(verified_number.backend_id is None)
        self.assertEqual(verified_number.phone_number, "15551234567")

        self.assertTrue(
            send_sms_to_verified_number(verified_number, "Test for BACKEND2"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertTrue(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend2.delete_invoke_doc()
        self.assertFalse(self.backend2.invoke_doc_exists())

        # Test sending to verified number with default domain backend

        self.domain_obj.default_sms_backend_id = self.backend5._id
        self.domain_obj.save()

        self.assertTrue(
            send_sms_to_verified_number(verified_number, "Test for BACKEND5"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertTrue(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertFalse(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend5.delete_invoke_doc()
        self.assertFalse(self.backend5.invoke_doc_exists())

        # Test sending to verified number with a contact-level backend owned by the domain

        self.case.set_case_property("contact_backend_id", "BACKEND")
        self.case.save()
        self.contact = CommConnectCase.wrap(self.case.to_json())
        verified_number = self.contact.get_verified_number()
        self.assertTrue(verified_number is not None)
        self.assertEqual(verified_number.backend_id, "BACKEND")
        self.assertEqual(verified_number.phone_number, "15551234567")

        self.assertTrue(
            send_sms_to_verified_number(verified_number, "Test for BACKEND"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertTrue(self.backend8.invoke_doc_exists())
        self.assertFalse(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend8.delete_invoke_doc()
        self.assertFalse(self.backend8.invoke_doc_exists())

        # Test sending to verified number with a contact-level backend granted to the domain by another domain

        self.backend8.delete()

        self.assertTrue(
            send_sms_to_verified_number(verified_number, "Test for BACKEND"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertTrue(self.backend9.invoke_doc_exists())
        self.assertFalse(self.backend10.invoke_doc_exists())
        self.backend9.delete_invoke_doc()
        self.assertFalse(self.backend9.invoke_doc_exists())

        # Test sending to verified number with a contact-level global backend

        self.backend9.delete()

        self.assertTrue(
            send_sms_to_verified_number(verified_number, "Test for BACKEND"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertFalse(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.assertTrue(self.backend10.invoke_doc_exists())
        self.backend10.delete_invoke_doc()
        self.assertFalse(self.backend10.invoke_doc_exists())

        # Test raising exception if contact-level backend is not found

        self.backend10.delete()

        try:
            self.assertTrue(
                send_sms_to_verified_number(verified_number,
                                            "Test for BACKEND"))
        except BadSMSConfigException:
            pass
        else:
            self.assertTrue(False)

        # Test send_sms_with_backend

        self.assertTrue(
            send_sms_with_backend(self.domain, "+15551234567",
                                  "Test for BACKEND3", self.backend3._id))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertTrue(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.backend3.delete_invoke_doc()
        self.assertFalse(self.backend3.invoke_doc_exists())

        # Test send_sms_with_backend_name

        self.assertTrue(
            send_sms_with_backend_name(self.domain, "+15551234567",
                                       "Test for BACKEND3", "BACKEND3"))
        self.assertFalse(self.backend1.invoke_doc_exists())
        self.assertFalse(self.backend2.invoke_doc_exists())
        self.assertTrue(self.backend3.invoke_doc_exists())
        self.assertFalse(self.backend4.invoke_doc_exists())
        self.assertFalse(self.backend5.invoke_doc_exists())
        self.assertFalse(self.backend6.invoke_doc_exists())
        self.assertFalse(self.backend7.invoke_doc_exists())
        self.backend3.delete_invoke_doc()
        self.assertFalse(self.backend3.invoke_doc_exists())

    def test_sms_registration(self):
        formatted_username = format_username("tester", self.domain)

        incoming("+9991234567", "JOIN {} WORKER tester".format(self.domain),
                 "TEST_CASE_BACKEND")
        # Test without mobile worker registration enabled
        self.assertIsNone(CommCareUser.get_by_username(formatted_username))

        # Enable mobile worker registration
        setattr(self.domain_obj, "sms_mobile_worker_registration_enabled",
                True)
        self.domain_obj.save()

        incoming("+9991234567", "JOIN {} WORKER tester".format(self.domain),
                 "TEST_CASE_BACKEND")
        self.assertIsNotNone(CommCareUser.get_by_username(formatted_username))

        # Test a duplicate registration
        prev_num_users = num_mobile_users(self.domain)
        incoming("+9991234568", "JOIN {} WORKER tester".format(self.domain),
                 "TEST_CASE_BACKEND")
        current_num_users = num_mobile_users(self.domain)
        self.assertEqual(prev_num_users, current_num_users)
Esempio n. 55
0
    for case_id, case_properties, close in case_changes:
        case_block = _get_update_or_close_case_block(case_id, case_properties,
                                                     close)
        case_blocks.append(case_block.as_text())
    return submit_case_blocks(case_blocks, domain, device_id=device_id)


def resave_case(domain, case, send_post_save_signal=True):
    from corehq.form_processor.change_publishers import publish_case_saved
    if should_use_sql_backend(domain):
        publish_case_saved(case, send_post_save_signal)
    else:
        if send_post_save_signal:
            case.save()
        else:
            CommCareCase.get_db().save_doc(
                case._doc)  # don't just call save to avoid signals


def get_last_non_blank_value(case, case_property):
    case_transactions = sorted(case.actions,
                               key=lambda t: t.server_date,
                               reverse=True)
    for case_transaction in case_transactions:
        try:
            property_changed_info = property_changed_in_action(
                case.domain, case_transaction, case.case_id, case_property)
        except MissingFormXml:
            property_changed_info = None
        if property_changed_info and property_changed_info.new_value:
            return property_changed_info.new_value
Esempio n. 56
0
                'other_caste': 'c207073d-3f10-11e4-adec-0800271c1b75',
                'st': 'c20478b6-3f10-11e4-adec-0800271c1b75'
            }
        },
    },
}


@mock.patch.object(CaseAccessors,
                   'get_cases',
                   lambda self, case_ids, ordered=False: [{
                       '65e55473-e83b-4d78-9dde-eaf949758997':
                       CommCareCase(
                           case_id='65e55473-e83b-4d78-9dde-eaf949758997',
                           type='paciente',
                           name='Elsa',
                           estado_tarv='1',
                           tb='0',
                       )
                   }[case_id] for case_id in case_ids])
class OpenmrsRepeaterTest(SimpleTestCase, TestFileMixin):
    file_path = ('data', )
    root = os.path.dirname(__file__)

    def test_get_case_updates_for_registration(self):
        """
        get_relevant_case_updates_from_form_json should fetch case
        updates from teh given form JSON
        """
        self.assertEqual(
            get_relevant_case_updates_from_form_json(
Esempio n. 57
0
                else:
                    logging.error(
                        "XForm %s had a case block that wasn't able to create a case! "
                        "This usually means it had a missing ID" %
                        xform.get_id)

        return touched_cases

    @staticmethod
    def hard_rebuild_case(domain, case_id, detail, save=True, lock=True):
        case, lock_obj = FormProcessorCouch.get_case_with_lock(case_id,
                                                               lock=lock,
                                                               wrap=True)
        found = bool(case)
        if not found:
            case = CommCareCase()
            case.case_id = case_id
            case.domain = domain
            if lock:
                lock_obj = CommCareCase.get_obj_lock_by_id(case_id)
                acquire_lock(lock_obj, degrade_gracefully=False)

        try:
            assert case.domain == domain, (case.domain, domain)
            forms = FormProcessorCouch.get_case_forms(case_id)
            filtered_forms = [f for f in forms if f.is_normal]
            sorted_forms = sorted(filtered_forms, key=lambda f: f.received_on)

            actions = _get_actions_from_forms(domain, sorted_forms, case_id)

            if not found and case.domain is None:
Esempio n. 58
0
 def db(self):
     return CommCareCase.get_db()
Esempio n. 59
0
 def _get_case():
     if wrap:
         return CommCareCase.get(case_id)
     else:
         return CommCareCase.get_db().get(case_id)
Esempio n. 60
0
class CouchCaseRebuildTest(TestCase, CaseRebuildTestMixin):
    @classmethod
    def tearDownClass(cls):
        delete_all_cases()
        super(CouchCaseRebuildTest, cls).tearDownClass()

    def test_couch_action_equality(self):
        case_id = _post_util(create=True)
        _post_util(case_id=case_id, p1='p1', p2='p2')

        case = CommCareCase.get(case_id)
        self.assertEqual(
            3, len(case.actions)
        )  # (1) create & (2) update date opened (3) update properties
        self.assertTrue(case.actions[0] != case.actions[1])
        self.assertTrue(case.actions[1] == case.actions[1])

        orig = case.actions[2]
        copy = CommCareCaseAction.wrap(deepcopy(orig._doc))
        self.assertTrue(copy != case.actions[0])
        self.assertTrue(copy == orig)

        copy.server_date = copy.server_date + timedelta(seconds=1)
        self.assertTrue(copy != orig)
        copy.server_date = orig.server_date
        self.assertTrue(copy == orig)

        copy.updated_unknown_properties['p1'] = 'not-p1'
        self.assertTrue(copy != orig)
        copy.updated_unknown_properties['p1'] = 'p1'
        self.assertTrue(copy == orig)
        copy.updated_unknown_properties['pnew'] = ''
        self.assertTrue(copy != orig)

    def test_couch_action_not_equals(self):
        orig = CommCareCaseAction()
        copy = CommCareCaseAction.wrap(deepcopy(orig._doc))
        self.assertTrue(orig == copy)
        self.assertFalse(orig != copy)

    def test_couch_soft_rebuild(self):
        user_id = 'test-basic-rebuild-user'
        now = datetime.utcnow()
        case_id = _post_util(create=True, user_id=user_id, date_modified=now)
        _post_util(case_id=case_id,
                   p1='p1-1',
                   p2='p2-1',
                   user_id=user_id,
                   date_modified=now)
        _post_util(case_id=case_id,
                   p2='p2-2',
                   p3='p3-2',
                   user_id=user_id,
                   date_modified=now)

        # check initial state
        case = CommCareCase.get(case_id)
        self.assertEqual(case.p1, 'p1-1')  # original
        self.assertEqual(case.p2, 'p2-2')  # updated
        self.assertEqual(case.p3, 'p3-2')  # new
        self.assertEqual(4, len(
            case.actions))  # create + update (2 actions) + 2 updates
        a0 = case.actions[1]
        self.assertEqual(a0.updated_known_properties['opened_on'],
                         case.opened_on.date())
        a1 = case.actions[2]
        self.assertEqual(a1.updated_unknown_properties['p1'], 'p1-1')
        self.assertEqual(a1.updated_unknown_properties['p2'], 'p2-1')
        a2 = case.actions[3]
        self.assertEqual(a2.updated_unknown_properties['p2'], 'p2-2')
        self.assertEqual(a2.updated_unknown_properties['p3'], 'p3-2')

        # rebuild by flipping the actions
        case.actions = [case.actions[0], a2, a1]
        case.xform_ids = [
            case.xform_ids[0], case.xform_ids[2], case.xform_ids[1]
        ]
        CouchCaseUpdateStrategy(case).soft_rebuild_case()
        self.assertEqual(case.p1, 'p1-1')  # original
        self.assertEqual(case.p2, 'p2-1')  # updated (back!)
        self.assertEqual(case.p3, 'p3-2')  # new

    def test_couch_action_comparison(self):
        user_id = 'test-action-comparison-user'
        case_id = _post_util(create=True, property='a1 wins', user_id=user_id)
        _post_util(case_id=case_id, property='a2 wins', user_id=user_id)
        _post_util(case_id=case_id, property='a3 wins', user_id=user_id)

        # check initial state
        case = CommCareCase.get(case_id)
        create, a1, a2, a3 = deepcopy(list(case.actions))
        self.assertEqual('a3 wins', case.property)
        self.assertEqual(a1.updated_unknown_properties['property'], 'a1 wins')
        self.assertEqual(a2.updated_unknown_properties['property'], 'a2 wins')
        self.assertEqual(a3.updated_unknown_properties['property'], 'a3 wins')

        def _confirm_action_order(case, expected_actions):
            actual_actions = case.actions[
                1:]  # always assume create is first and removed
            for expected, actual in zip(expected_actions, actual_actions):
                self.assertEqual(
                    expected.updated_unknown_properties['property'],
                    actual.updated_unknown_properties['property'])

        _confirm_action_order(case, [a1, a2, a3])

        # test initial rebuild does nothing
        update_strategy = CouchCaseUpdateStrategy(case)
        update_strategy.soft_rebuild_case()
        _confirm_action_order(case, [a1, a2, a3])

        # test sorting by server date
        case.actions[2].server_date = case.actions[2].server_date + timedelta(
            days=1)
        update_strategy.soft_rebuild_case()
        _confirm_action_order(case, [a1, a3, a2])

        # test sorting by date within the same day
        case = CommCareCase.get(case_id)
        _confirm_action_order(case, [a1, a2, a3])
        case.actions[2].date = case.actions[3].date + timedelta(minutes=1)
        CouchCaseUpdateStrategy(case).soft_rebuild_case()
        _confirm_action_order(case, [a1, a3, a2])

        # test original form order
        case = CommCareCase.get(case_id)
        case.actions[3].server_date = case.actions[2].server_date
        case.actions[3].date = case.actions[2].date
        case.xform_ids = [a1.xform_id, a3.xform_id, a2.xform_id]
        CouchCaseUpdateStrategy(case).soft_rebuild_case()
        _confirm_action_order(case, [a1, a3, a2])

        # test create comes before update
        case = CommCareCase.get(case_id)
        case.actions = [a1, create, a2, a3]
        CouchCaseUpdateStrategy(case).soft_rebuild_case()
        _confirm_action_order(case, [a1, a2, a3])