本文整理汇总了Python中django.http.HttpRequest.GET['api_key']方法的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest.GET['api_key']方法的具体用法?Python HttpRequest.GET['api_key']怎么用?Python HttpRequest.GET['api_key']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.http.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.GET['api_key']方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_is_authenticated_get_params
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_is_authenticated_get_params(self):
auth = ApiKeyAuthentication()
request = HttpRequest()
# Simulate sending the signal.
john_doe = User.objects.get(username='johndoe')
create_api_key(User, instance=john_doe, created=True)
# No username/api_key details should fail.
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Wrong username details.
request.GET['username'] = 'foo'
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# No api_key.
request.GET['username'] = 'daniel'
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Wrong user/api_key.
request.GET['username'] = 'daniel'
request.GET['api_key'] = 'foo'
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Correct user/api_key.
john_doe = User.objects.get(username='johndoe')
request.GET['username'] = 'johndoe'
request.GET['api_key'] = john_doe.api_key.key
self.assertEqual(auth.is_authenticated(request), True)
self.assertEqual(auth.get_identifier(request), 'johndoe')
示例2: test_is_authenticated_get_params
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_is_authenticated_get_params(self):
auth = ApiKeyAuthentication()
request = HttpRequest()
# Simulate sending the signal.
john_doe = CustomUser.objects.get(pk=1)
create_api_key(CustomUser, instance=john_doe, created=True)
# No username/api_key details should fail.
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Wrong username (email) details.
request.GET['username'] = '[email protected]'
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# No api_key.
request.GET['username'] = john_doe.email
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Wrong user/api_key.
request.GET['username'] = john_doe.email
request.GET['api_key'] = 'foo'
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Correct user/api_key.
ApiKey.objects.all().delete()
create_api_key(CustomUser, instance=john_doe, created=True)
request.GET['username'] = john_doe.email
request.GET['api_key'] = john_doe.api_key.key
self.assertEqual(auth.is_authenticated(request), True)
self.assertEqual(auth.get_identifier(request), john_doe.email)
示例3: test_is_authenticated
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_is_authenticated(self):
auth = ApiKeyAuthentication()
request = HttpRequest()
# No username/api_key details should fail.
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Wrong username details.
request.GET['username'] = 'foo'
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# No api_key.
request.GET['username'] = 'daniel'
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Wrong user/api_key.
request.GET['username'] = 'daniel'
request.GET['api_key'] = 'foo'
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Correct user/api_key.
john_doe = User.objects.get(username='johndoe')
john_doe.save()
request.GET['username'] = 'johndoe'
request.GET['api_key'] = john_doe.api_key.key
self.assertEqual(auth.is_authenticated(request), True)
示例4: test_apikey_and_basic_auth
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_apikey_and_basic_auth(self):
auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
request = HttpRequest()
john_doe = User.objects.get(username='johndoe')
# No API Key or HTTP Basic auth details should fail.
self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
# Basic Auth still returns appropriately.
self.assertEqual(auth.is_authenticated(request)['WWW-Authenticate'], 'Basic Realm="django-tastypie"')
# API Key Auth works.
request = HttpRequest()
request.GET['username'] = 'johndoe'
request.GET['api_key'] = john_doe.api_key.key
self.assertEqual(auth.is_authenticated(request), True)
self.assertEqual(auth.get_identifier(request), 'johndoe')
# Basic Auth works.
request = HttpRequest()
john_doe = User.objects.get(username='johndoe')
john_doe.set_password('pass')
john_doe.save()
request.META['HTTP_AUTHORIZATION'] = 'Basic %s' % base64.b64encode('johndoe:pass'.encode('utf-8')).decode('utf-8')
self.assertEqual(auth.is_authenticated(request), True)
示例5: test_authentication
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_authentication(self):
request = HttpRequest()
auth = ApiKeyOrGuestAuthentication()
self.assertTrue(auth.is_authenticated(request))
request.GET['username'] = self.staff_user.email
request.GET['api_key'] = self.staff_user.api_key.key
self.assertTrue(auth.is_authenticated(request))
示例6: test_multiauth_apikey_and_basic_auth__api_key_works_in_query
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_multiauth_apikey_and_basic_auth__api_key_works_in_query(self):
auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
request = HttpRequest()
john_doe = User.objects.get(username='johndoe')
request.GET['username'] = john_doe.username
request.GET['api_key'] = john_doe.api_key.key
self.assertEqual(auth.is_authenticated(request), True)
self.assertEqual(auth.get_identifier(request), john_doe.username)
示例7: test_auth
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_auth(self):
"""
Tests user authentication using APIKEY and username
"""
auth = MonlogAuthentication()
request = HttpRequest()
testapp = User.objects.get(username=self.username) #API key created in setUp()
request.GET['api_key'] = testapp.api_key.key
self.assertEqual(auth.is_authenticated(request), True)
示例8: test_apikey_and_authentication
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_apikey_and_authentication(self):
auth = MultiAuthentication(ApiKeyAuthentication(), Authentication())
request = HttpRequest()
john_doe = User.objects.get(username='johndoe')
# No username/api_key details should pass.
self.assertEqual(auth.is_authenticated(request), True)
# The identifier should be the basic auth stock.
self.assertEqual(auth.get_identifier(request), 'noaddr_nohost')
# Wrong username details.
request = HttpRequest()
request.GET['username'] = 'foo'
self.assertEqual(auth.is_authenticated(request), True)
self.assertEqual(auth.get_identifier(request), 'noaddr_nohost')
# No api_key.
request = HttpRequest()
request.GET['username'] = 'daniel'
self.assertEqual(auth.is_authenticated(request), True)
self.assertEqual(auth.get_identifier(request), 'noaddr_nohost')
# Wrong user/api_key.
request = HttpRequest()
request.GET['username'] = 'daniel'
request.GET['api_key'] = 'foo'
self.assertEqual(auth.is_authenticated(request), True)
self.assertEqual(auth.get_identifier(request), 'noaddr_nohost')
request = HttpRequest()
request.GET['username'] = 'johndoe'
request.GET['api_key'] = john_doe.api_key.key
self.assertEqual(auth.is_authenticated(request), True)
self.assertEqual(auth.get_identifier(request), john_doe.username)
示例9: test_custom_user_identifier
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_custom_user_identifier(self):
# username authorized
request = HttpRequest()
user = self.u2
api_key = self.api_key(user)
self.assertTrue(api_key is not None and len(api_key) > 0)
user_identifier = 'my_custom_user_identifier'
auth = MicroblogApiKeyAuthentication(user_identifier=user_identifier)
request.GET[user_identifier] = user.username
request.GET['api_key'] = api_key
self.assertTrue(auth.is_authenticated(request))
self.assertTrue(request.user == user)
# email authorized
request.GET[user_identifier] = user.username
request.GET['api_key'] = api_key
self.assertTrue(auth.is_authenticated(request))
self.assertTrue(request.user == user)
# username unauthorized
request.GET[user_identifier] = 'bad_username'
request.GET['api_key'] = api_key
self.assertFalse(auth.is_authenticated(request))
# email unauthorized
request.GET[user_identifier] = '[email protected]'
request.GET['api_key'] = api_key
self.assertFalse(auth.is_authenticated(request))
# username and bad api key
request.GET[user_identifier] = user.username
request.GET['api_key'] = 'bad_key'
self.assertFalse(auth.is_authenticated(request))
# email and bad api key
request.GET[user_identifier] = user.email
request.GET['api_key'] = 'bad_key'
self.assertFalse(auth.is_authenticated(request))
示例10: test_is_authenticated_get_params
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_is_authenticated_get_params(self):
auth = ApiKeyAuthentication()
request = HttpRequest()
# No username/api_key details should fail.
self.assertFalse(auth.is_authenticated(request))
# Wrong username details.
request.GET['username'] = 'foo'
self.assertFalse(auth.is_authenticated(request))
# No api_key.
request.GET['username'] = self.user.username
self.assertFalse(auth.is_authenticated(request))
# Wrong user/api_key.
request.GET['username'] = self.user.username
request.GET['api_key'] = 'foo'
self.assertFalse(auth.is_authenticated(request))
# Correct user/api_key.
request.GET['username'] = self.user.username
request.GET['api_key'] = self.user.api_key.key
self.assertTrue(auth.is_authenticated(request))
示例11: test_whitelisting
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_whitelisting(self):
auth = ApiKeyAuthentication(whitelisted_methods=['a_method'])
request = HttpRequest()
# Simulate sending the signal.
john_doe = User.objects.get(username='johndoe')
create_api_key(User, instance=john_doe, created=True)
# Calling with a whitelisted method_name without credentials should work
self.assertEqual(auth.is_authenticated(request, method_name='a_method'), True)
# Calling any other method should require the Api Key
self.assertEqual(isinstance(auth.is_authenticated(request, method_name='another_method'), HttpUnauthorized), True)
# Correct user/api_key
john_doe = User.objects.get(username='johndoe')
request.GET['username'] = 'johndoe'
request.GET['api_key'] = john_doe.api_key.key
self.assertEqual(auth.is_authenticated(request, method_name="another_method"), True)
self.assertEqual(auth.get_identifier(request), 'johndoe')
self.assertEqual(auth.is_authenticated(request, method_name="a_method"), True)
self.assertEqual(auth.get_identifier(request), 'johndoe')
示例12: test_require_api_key_ok
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_require_api_key_ok(self):
req = HttpRequest()
req.GET['api_key'] = 'Key'
resp = view_api_key(req)
self.assertEqual(resp.status_code, 200)
示例13: test_require_api_key_wrong
# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import GET['api_key'] [as 别名]
def test_require_api_key_wrong(self):
req = HttpRequest()
req.GET['api_key'] = 'oula'
resp = view_api_key(req)
self.assertEqual(resp.status_code, 401)