def _send_forms(self, forms): # list of form tuples [(xmlns, received_on)] to_ret = [] for xmlns, received_on in forms: backend_form = XFormInstance( xmlns=xmlns or 'fake-xmlns', domain=self.domain.name, received_on=received_on or datetime.utcnow(), edited_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': xmlns or 'fake-xmlns', 'meta': { 'userID': 'metadata-user-id' }, }, auth_context={ 'user_id': 'auth-user-id', 'domain': self.domain.name, 'authenticated': True, }, ) backend_form.save() to_ret.append(backend_form) self.addCleanup(backend_form.delete) send_to_elasticsearch( 'forms', transform_xform_for_elasticsearch(backend_form.to_json())) self.es.indices.refresh(XFORM_INDEX_INFO.index) return to_ret
def test_get_list(self): """ Any form 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. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. # the pillow is set to offline mode - elasticsearch not needed to validate fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance( xmlns='fake-xmlns', domain=self.domain.name, received_on=datetime.utcnow(), edited_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': 'fake-xmlns', 'meta': { 'userID': 'metadata-user-id' }, }, auth_context={ 'user_id': 'auth-user-id', 'domain': self.domain.name, 'authenticated': True, }, ) backend_form.save() self.addCleanup(backend_form.delete) translated_doc = transform_xform_for_elasticsearch( backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) response = self._assert_auth_get_resource(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = json.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], json_format_datetime(backend_form.received_on)) self.assertEqual(api_form['metadata']['userID'], 'metadata-user-id') self.assertEqual(api_form['edited_by_user_id'], 'auth-user-id')
def test_get_list(self): """ Any form 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. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. # the pillow is set to offline mode - elasticsearch not needed to validate fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance( xmlns='fake-xmlns', domain=self.domain.name, received_on=datetime.utcnow(), edited_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': 'fake-xmlns', 'meta': {'userID': 'metadata-user-id'}, }, auth_context={ 'user_id': 'auth-user-id', 'domain': self.domain.name, 'authenticated': True, }, ) backend_form.save() self.addCleanup(backend_form.delete) translated_doc = transform_xform_for_elasticsearch(backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) response = self._assert_auth_get_resource(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = json.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], json_format_datetime(backend_form.received_on)) self.assertEqual(api_form['metadata']['userID'], 'metadata-user-id') self.assertEqual(api_form['edited_by_user_id'], 'auth-user-id')
def test_get_list(self): """ Any form 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. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. #the pillow is set to offline mode - elasticsearch not needed to validate pillow = XFormPillow(online=False) fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance(xmlns='fake-xmlns', domain=self.domain.name, received_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': 'fake-xmlns' }) backend_form.save() translated_doc = pillow.change_transform(backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) self.client.login(username=self.username, password=self.password) response = self.client.get(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = simplejson.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], backend_form.received_on.isoformat()) backend_form.delete()
def test_get_list(self): """ Any form 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. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. #the pillow is set to offline mode - elasticsearch not needed to validate pillow = XFormPillow(online=False) fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance(xmlns = 'fake-xmlns', domain = self.domain.name, received_on = datetime.utcnow(), form = { '#type': 'fake-type', '@xmlns': 'fake-xmlns' }) backend_form.save() translated_doc = pillow.change_transform(backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) self.client.login(username=self.username, password=self.password) response = self.client.get(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = simplejson.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], backend_form.received_on.isoformat()) backend_form.delete()