本文整理汇总了Python中django_digest.test.Client类的典型用法代码示例。如果您正苦于以下问题:Python Client类的具体用法?Python Client怎么用?Python Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _download_xform
def _download_xform(self):
client = DigestClient()
client.set_authorization('bob', 'bob')
response = client.get(self.download_url)
response_doc = minidom.parseString(response.content)
xml_path = os.path.join(self.this_directory, "fixtures",
"transportation", "transportation.xml")
with open(xml_path) as xml_file:
expected_doc = minidom.parse(xml_file)
model_node = [
n for n in
response_doc.getElementsByTagName("h:head")[0].childNodes
if n.nodeType == Node.ELEMENT_NODE and
n.tagName == "model"][0]
# check for UUID and remove
uuid_nodes = [node for node in model_node.childNodes
if node.nodeType == Node.ELEMENT_NODE and
node.getAttribute("nodeset") ==
"/transportation/formhub/uuid"]
self.assertEqual(len(uuid_nodes), 1)
uuid_node = uuid_nodes[0]
uuid_node.setAttribute("calculate", "''")
response_xml = response_doc.toxml().replace(
self.xform.version, u"201411120717")
# check content without UUID
self.assertEqual(response_xml, expected_doc.toxml())
示例2: test_submission_to_require_auth_without_perm
def test_submission_to_require_auth_without_perm(self):
"""
test submission to a private form by non-owner without perm is
forbidden.
"""
view = XFormViewSet.as_view({
'patch': 'partial_update'
})
data = {'require_auth': True}
self.assertFalse(self.xform.require_auth)
request = self.factory.patch('/', data=data, **{
'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token})
view(request, pk=self.xform.id)
self.xform.reload()
self.assertTrue(self.xform.require_auth)
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml"
)
# create a new user
username = 'alice'
self._create_user(username, username)
client = DigestClient()
client.set_authorization(username, username, 'Digest')
self._make_submission(xml_submission_file_path, client=client)
self.assertEqual(self.response.status_code, 403)
示例3: _get_authenticated_client
def _get_authenticated_client(self, url, username="bob", password="bob", extra={}):
client = DigestClient()
# request with no credentials
req = client.get(url, {}, **extra)
self.assertEqual(req.status_code, 401)
# apply credentials
client.set_authorization(username, password, "Digest")
return client
示例4: testOtaRestore
def testOtaRestore(self, password=None):
client = Client()
client.set_authorization(self.couch_user.username, password if password else self.password, method='Digest')
resp = client.get('/a/%s/phone/restore' % self.domain, follow=True)
self.assertEqual(resp.status_code, 200)
self.assertTrue(resp.content.count("Successfully restored account %s!" % self.username) > 0)
示例5: _get_digest_client
def _get_digest_client(self):
self.user.profile.require_auth = True
self.user.profile.save()
client = DigestClient()
client.set_authorization(self.profile_data['username'],
self.profile_data['password1'],
'Digest')
return client
示例6: test_edited_submission
def test_edited_submission(self):
"""
Test submissions that have been edited
"""
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..", "fixtures", "tutorial", "instances",
"tutorial_2012-06-27_11-27-53_w_uuid.xml"
)
num_instances_history = InstanceHistory.objects.count()
num_instances = Instance.objects.count()
query_args = {
'username': self.user.username,
'id_string': self.xform.id_string,
'query': '{}',
'fields': '[]',
'sort': '[]',
'count': True
}
cursor = ParsedInstance.query_mongo(**query_args)
num_mongo_instances = cursor[0]['count']
# make first submission
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
self.assertEqual(Instance.objects.count(), num_instances + 1)
# no new record in instances history
self.assertEqual(
InstanceHistory.objects.count(), num_instances_history)
# check count of mongo instances after first submission
cursor = ParsedInstance.query_mongo(**query_args)
self.assertEqual(cursor[0]['count'], num_mongo_instances + 1)
# edited submission
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..", "fixtures", "tutorial", "instances",
"tutorial_2012-06-27_11-27-53_w_uuid_edited.xml"
)
client = DigestClient()
client.set_authorization('bob', 'bob', 'Digest')
self._make_submission(xml_submission_file_path, client=client)
self.assertEqual(self.response.status_code, 201)
# we must have the same number of instances
self.assertEqual(Instance.objects.count(), num_instances + 1)
# should be a new record in instances history
self.assertEqual(
InstanceHistory.objects.count(), num_instances_history + 1)
cursor = ParsedInstance.query_mongo(**query_args)
self.assertEqual(cursor[0]['count'], num_mongo_instances + 1)
# make sure we edited the mongo db record and NOT added a new row
query_args['count'] = False
cursor = ParsedInstance.query_mongo(**query_args)
record = cursor[0]
with open(xml_submission_file_path, "r") as f:
xml_str = f.read()
xml_str = clean_and_parse_xml(xml_str).toxml()
edited_name = re.match(ur"^.+?<name>(.+?)</name>", xml_str).groups()[0]
self.assertEqual(record['name'], edited_name)
示例7: _authenticated_client
def _authenticated_client(
self, url, username='bob', password='bob', extra={}):
client = DigestClient()
# request with no credentials
req = client.get(url, {}, **extra)
self.assertEqual(req.status_code, 401)
# apply credentials
client.set_authorization(username, password, 'Digest')
req = client.get(url, {}, **extra)
# if 204 authorization successfull, proceed
self.assertEqual(req.status_code, 204)
# submissions should use this authenticated client
return client
示例8: instances_xml
def instances_xml(url, request, **kwargs):
response = requests.Response()
client = DigestClient()
client.set_authorization('bob', 'bob', 'Digest')
res = client.get('%s?%s' % (url.path, url.query))
if res.status_code == 302:
res = client.get(res['Location'])
response.encoding = res.get('content-type')
response._content = get_streaming_content(res)
else:
response._content = res.content
response.status_code = 200
return response
示例9: test_retrieve_xform_manifest_linked_form
def test_retrieve_xform_manifest_linked_form(self):
# for linked forms check if manifest media download url for csv
# has a group_delimiter param
data_type = 'media'
data_value = 'xform {} transportation'.format(self.xform.pk)
media = self._add_form_metadata(self.xform, data_type, data_value)
self.view = XFormListViewSet.as_view(
{
"get": "manifest",
"head": "manifest"
}
)
# sign in bob
request = self.factory.head('/')
auth_response = self.view(request, pk=self.xform.pk)
auth = DigestAuth('bob', 'bobbob')
# set up bob's request
request = self.factory.get('/xformsManifest')
request.META.update(auth(request.META, auth_response))
# make request
response = self.view(request, pk=self.xform.pk, format='csv')
# test
manifest_media_url = '{}{}'.format(
media.data['media_url'],
'?group_delimiter=.&repeat_index_tags=_,_')
download_url = response.data[0]['downloadUrl']
self.assertEqual(manifest_media_url, download_url)
url = '/bob/xformsMedia/{}/{}.csv?group_delimiter=.'\
.format(self.xform.pk, self.metadata.pk)
username = 'bob'
password = 'bob'
client = DigestClient()
client.set_authorization(username, password, 'Digest')
req = client.get(url)
self.assertEqual(req.status_code, 200)
# enable meta perms
data_value = "editor-minor|dataentry"
MetaData.xform_meta_permission(self.xform, data_value=data_value)
req = client.get(url)
self.assertEqual(req.status_code, 401)
示例10: FormExportTest
class FormExportTest(TestCase):
def setUp(self):
self.app_id = 'kasdlfkjsldfkjsdlkjf'
self.domain_name = 'form-export-test'
self.domain = create_domain(self.domain_name)
self.username = 'danny'
self.couch_user = CommCareUser.create(self.domain_name, self.username,
password='xxx')
self.couch_user.save()
self.client = Client()
self.client.login(username=self.couch_user.username, password='xxx')
self.url = reverse("receiver_post_with_app_id",
args=[self.domain_name, self.app_id])
def post_it():
f = StringIO(XML_DATA)
f.name = 'form.xml'
response = self.client.post(self.url, {'xml_submission_file': f})
self.form1 = post_it()
self.form2 = post_it()
self.custom_export = FormExportSchema.wrap({
'type': 'form',
'app_id': self.app_id,
'default_format': Format.JSON,
'index': json.dumps([self.domain_name, XMLNS]),
'tables': [{
'index': '#',
'display': 'Export',
'columns': [{'index': 'form.name', 'display': 'Name'}],
}]
})
def test_include_duplicates(self):
self.custom_export.include_errors = True
tmp, _ = self.custom_export.get_export_files()
data = tmp.getvalue()
data = json.loads(data)
self.assertEqual(data['Export']['headers'], ['Name'])
self.assertEqual(len(data['Export']['rows']), 2)
self.custom_export.include_errors = False
tmp, _ = self.custom_export.get_export_files()
data = tmp.getvalue()
data = json.loads(data)
self.assertEqual(data['Export']['headers'], ['Name'])
self.assertEqual(len(data['Export']['rows']), 1)
示例11: setUp
def setUp(self):
self.app_id = 'kasdlfkjsldfkjsdlkjf'
self.domain_name = 'form-export-test'
self.domain = create_domain(self.domain_name)
self.username = 'danny'
self.couch_user = CommCareUser.create(self.domain_name, self.username,
password='xxx')
self.couch_user.save()
self.client = Client()
self.client.login(username=self.couch_user.username, password='xxx')
self.url = reverse("receiver_post_with_app_id",
args=[self.domain_name, self.app_id])
def post_it():
f = StringIO(XML_DATA)
f.name = 'form.xml'
response = self.client.post(self.url, {'xml_submission_file': f})
self.form1 = post_it()
self.form2 = post_it()
self.custom_export = FormExportSchema.wrap({
'type': 'form',
'app_id': self.app_id,
'default_format': Format.JSON,
'index': json.dumps([self.domain_name, XMLNS]),
'tables': [{
'index': '#',
'display': 'Export',
'columns': [{'index': 'form.name', 'display': 'Name'}],
}]
})
示例12: test_data_entry_role_submission_when_requires_auth
def test_data_entry_role_submission_when_requires_auth(self):
self._publish_xls_form_to_project()
self.user.profile.require_auth = True
self.user.profile.save()
alice_data = {'username': 'alice', 'email': '[email protected]',
'password1': 'alice', 'password2': 'alice'}
self._login_user_and_profile(extra_post_data=alice_data)
role.DataEntryRole.add(self.user, self.xform)
paths = [os.path.join(
self.main_directory, 'fixtures', 'transportation',
'instances', s, s + '.xml') for s in self.surveys]
client = DigestClient()
client.set_authorization('alice', 'alice', 'Digest')
self._make_submission(paths[0], username='bob', client=client)
self.assertEqual(self.response.status_code, 201)
示例13: test_submission_when_requires_auth
def test_submission_when_requires_auth(self):
self.user.profile.require_auth = True
self.user.profile.save()
# create a new user
alice = self._create_user('alice', 'alice')
# assign report perms to user
assign_perm('report_xform', alice, self.xform)
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml"
)
client = DigestClient()
client.set_authorization('alice', 'alice', 'Digest')
self._make_submission(
xml_submission_file_path, client=client)
self.assertEqual(self.response.status_code, 201)
示例14: test_unicode_submission
def test_unicode_submission(self):
"""Test xml submissions that contain unicode characters
"""
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..", "fixtures", "tutorial", "instances",
"tutorial_unicode_submission.xml"
)
self.user.profile.require_auth = True
self.user.profile.save()
# create a new user
alice = self._create_user('alice', 'alice')
# assign report perms to user
assign_perm('report_xform', alice, self.xform)
client = DigestClient()
client.set_authorization('alice', 'alice', 'Digest')
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
示例15: TestFormList
class TestFormList(TestBase):
def setUp(self):
super(TestFormList, self).setUp()
self.profile = UserProfile.objects.create(
user=self.user, require_auth=True)
self.profile.save()
self.digest_client = DigestClient()
def test_returns_200_for_owner(self):
self.digest_client.set_authorization('bob', 'bob')
response = self.digest_client.get(reverse(formList, kwargs={
'username': 'bob'
}))
self.assertEqual(response.status_code, 200)
def test_returns_401_for_anon(self):
response = self.anon.get(reverse(formList, kwargs={
'username': 'bob'
}))
self.assertEqual(response.status_code, 401)
def test_returns_200_for_authenticated_non_owner(self):
credentials = ('alice', 'alice',)
self._create_user(*credentials)
self.digest_client.set_authorization(*credentials)
response = self.digest_client.get(reverse(formList, kwargs={
'username': 'bob'
}))
self.assertEqual(response.status_code, 200)