本文整理汇总了Python中django_digest.test.Client.set_authorization方法的典型用法代码示例。如果您正苦于以下问题:Python Client.set_authorization方法的具体用法?Python Client.set_authorization怎么用?Python Client.set_authorization使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django_digest.test.Client
的用法示例。
在下文中一共展示了Client.set_authorization方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _download_xform
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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: TestFormList
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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)
示例4: testOtaRestore
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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: _get_authenticated_client
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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
示例7: test_edited_submission
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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)
示例8: instances_xml
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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: _authenticated_client
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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
示例10: test_retrieve_xform_manifest_linked_form
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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)
示例11: test_data_entry_role_submission_when_requires_auth
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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)
示例12: test_submission_when_requires_auth
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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)
示例13: test_unicode_submission
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
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)
示例14: _check_formList
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
def _check_formList(self):
url = '/%s/formList' % self.user.username
client = DigestClient()
client.set_authorization('bob', 'bob')
response = client.get(url)
self.download_url = \
'http://testserver/%s/forms/%s/form.xml'\
% (self.user.username, self.xform.pk)
self.manifest_url = \
'http://testserver/%s/xformsManifest/%s'\
% (self.user.username, self.xform.pk)
md5_hash = md5(self.xform.xml).hexdigest()
expected_content = """<?xml version="1.0" encoding="utf-8"?>
<xforms xmlns="http://openrosa.org/xforms/xformsList"><xform><formID>transportation_2011_07_25</formID><name>transportation_2011_07_25</name><majorMinorVersion></majorMinorVersion><version></version><hash>md5:%(hash)s</hash><descriptionText>transportation_2011_07_25</descriptionText><downloadUrl>%(download_url)s</downloadUrl><manifestUrl>%(manifest_url)s</manifestUrl></xform></xforms>""" # noqa
expected_content = expected_content % {
'download_url': self.download_url,
'manifest_url': self.manifest_url,
'hash': md5_hash
}
self.assertEqual(response.content, expected_content)
self.assertTrue(response.has_header('X-OpenRosa-Version'))
self.assertTrue(response.has_header('Date'))
示例15: test_submission_to_require_auth_with_perm
# 需要导入模块: from django_digest.test import Client [as 别名]
# 或者: from django_digest.test.Client import set_authorization [as 别名]
def test_submission_to_require_auth_with_perm(self):
"""
test submission to a private form by non-owner is forbidden.
TODO send authentication challenge when xform.require_auth is set.
This is non-trivial because we do not know the xform until we have
parsed the XML.
"""
raise SkipTest
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)
# create a new user
username = 'alice'
alice = self._create_user(username, username)
# assign report perms to user
assign_perm('report_xform', alice, self.xform)
client = DigestClient()
client.set_authorization(username, username, 'Digest')
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"
)
self._make_submission(xml_submission_file_path, client=client)
self.assertEqual(self.response.status_code, 201)