本文整理汇总了Python中test_utils.RequestFactory.user方法的典型用法代码示例。如果您正苦于以下问题:Python RequestFactory.user方法的具体用法?Python RequestFactory.user怎么用?Python RequestFactory.user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类test_utils.RequestFactory
的用法示例。
在下文中一共展示了RequestFactory.user方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_multiple_shared_works
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def test_multiple_shared_works(self):
request = RequestFactory().post(
'/api',
HTTP_AUTHORIZATION='mkt-shared-secret '
'[email protected],56b6f1a3dd735d962c56'
'ce7d8f46e02ec1d4748d2c00c407d75f0969d08bb'
'9c68c31b3371aa8130317815c89e5072e31bb94b4'
'121c5c165f3515838d4d6c60c4,165d631d3c3045'
'458b4516242dad7ae')
request.user = AnonymousUser()
drf_request = Request(request)
# Start with an AnonymousUser on the request, because that's a classic
# situation: we already went through a middleware, it didn't find a
# session cookie, if set request.user = AnonymousUser(), and now we
# are going through the authentication code in the API.
request.user = AnonymousUser()
# Call middleware as they would normally be called.
APIBaseMiddleware().process_request(request)
RestSharedSecretMiddleware().process_request(request)
RestOAuthMiddleware().process_request(request)
drf_request.authenticators = (
authentication.RestSharedSecretAuthentication(),
authentication.RestOAuthAuthentication())
eq_(drf_request.user, self.profile)
eq_(drf_request._request.user, self.profile)
eq_(drf_request.user.is_authenticated(), True)
eq_(drf_request._request.user.is_authenticated(), True)
eq_(drf_request.user.pk, self.profile.pk)
eq_(drf_request._request.user.pk, self.profile.pk)
示例2: get_request
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def get_request(self, profile):
request = RequestFactory().post('/')
if not profile:
request.user = AnonymousUser()
else:
request.user = profile.user
request.amo_user = profile
return request
示例3: test_multiple_fail
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def test_multiple_fail(self):
request = RequestFactory().post('/api')
request.user = AnonymousUser()
drf_request = Request(request)
request.user = AnonymousUser()
drf_request.authenticators = (
authentication.RestSharedSecretAuthentication(),
authentication.RestOAuthAuthentication())
eq_(drf_request.user.is_authenticated(), False)
eq_(drf_request._request.user.is_authenticated(), False)
示例4: _create_mocked_tweet_request
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def _create_mocked_tweet_request(self):
request = RequestFactory().post(
reverse('customercare.twitter_post'),
{'reply_to': 1,
'content': '@foobar try Aurora! #fxhelp'})
request.session = {}
request.twitter = Mock()
request.twitter.authed = True
request.twitter.api = Mock()
return_value = {
'id': 123456790,
'text': '@foobar try Aurora! #fxhelp',
'created_at': datetime.strftime(datetime.utcnow(),
'%a %b %d %H:%M:%S +0000 %Y'),
'user': {
'lang': 'en',
'id': 42,
'screen_name': 'r1cky',
'profile_image_url': 'http://example.com/profile.jpg',
'profile_image_url_https': 'https://example.com/profile.jpg',
}
}
request.twitter.api.update_status.return_value = return_value
credentials = {'screen_name': 'r1cky'}
request.twitter.api.verify_credentials.return_value = credentials
request.user = Mock()
request.user.is_authenticated.return_value = False
return request
示例5: test_session_auth_no_post
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def test_session_auth_no_post(self):
req = RequestFactory().post('/api/')
req.user = AnonymousUser()
for m in self.middlewares:
m().process_request(req)
ok_(not self.auth.authenticate(Request(req)))
ok_(not req.user.is_authenticated())
示例6: dump_app
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def dump_app(id, **kw):
from mkt.webapps.api import AppSerializer
# Because @robhudson told me to.
# Note: not using storage because all these operations should be local.
target_dir = os.path.join(settings.DUMPED_APPS_PATH, 'apps',
str(id / 1000))
target_file = os.path.join(target_dir, str(id) + '.json')
try:
obj = Webapp.objects.get(pk=id)
except Webapp.DoesNotExist:
task_log.info(u'Webapp does not exist: {0}'.format(id))
return
req = RequestFactory().get('/')
req.user = AnonymousUser()
req.REGION = RESTOFWORLD
if not os.path.exists(target_dir):
os.makedirs(target_dir)
task_log.info('Dumping app {0} to {1}'.format(id, target_file))
res = AppSerializer(obj, context={'request': req}).data
json.dump(res, open(target_file, 'w'), cls=JSONEncoder)
return target_file
示例7: req_factory_factory
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def req_factory_factory(url, user=None):
"""Creates a request factory, logged in with the user."""
req = RequestFactory().get(url)
if user:
req.user = user.user
req.groups = req.user.get_profile().groups.all()
return req
示例8: dump_app
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def dump_app(id, **kw):
# Because @robhudson told me to.
from mkt.api.resources import AppResource
# Note: not using storage because all these operations should be local.
target_dir = os.path.join(settings.DUMPED_APPS_PATH, 'apps',
str(id / 1000))
target_file = os.path.join(target_dir, str(id) + '.json')
try:
obj = Webapp.objects.get(pk=id)
except Webapp.DoesNotExist:
task_log.info(u'Webapp does not exist: {0}'.format(id))
return
req = RequestFactory().get('/')
req.user = AnonymousUser()
req.REGION = WORLDWIDE
if not os.path.exists(target_dir):
os.makedirs(target_dir)
task_log.info('Dumping app {0} to {1}'.format(id, target_file))
res = AppResource().dehydrate_objects([obj], request=req)
json.dump(res[0], open(target_file, 'w'), cls=JSONEncoder)
return target_file
示例9: get_request
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def get_request(self, data=None):
if data is None:
data = {}
request = RequestFactory().get("/", data)
request.REGION = mkt.regions.RESTOFWORLD
request.API = True
request.user = AnonymousUser()
return request
示例10: test_require_permission
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def test_require_permission(self):
'''
Test that user without 'conference.delete_attendee' permission can't access the view
'''
perm = Permission.objects.get(pk=1)
req = RequestFactory()
req.user = self.user
req.path = '/'
rsp = export_csv(req, User.objects.all(), self.export_data,
require_permission='auth.add_permission')
self.assertEqual(rsp.status_code, 302)
u = User.objects.get(pk=self.user.pk)
u.user_permissions.add(perm)
req.user = u
rsp = export_csv(req, User.objects.all(), self.export_data,
require_permission='auth.add_permission')
self.assertEqual(rsp.status_code, 200)
示例11: process
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def process(self, authenticated, view=None, lang='en-US', app='firefox'):
if not view:
view = normal_view
request = RequestFactory().get('/', HTTP_X_PJAX=True)
request.user = Mock()
request.APP = amo.APPS[app]
request.LANG = lang
request.user.is_authenticated.return_value = authenticated
return LoginRequiredMiddleware().process_view(request, view, [], {})
示例12: test_multiple_passes
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def test_multiple_passes(self):
req = RequestFactory().get('/')
req.user = AnonymousUser()
self.resource._meta.authentication = (
authentication.SharedSecretAuthentication(),
# Optional auth passes because there are not auth headers.
authentication.OptionalOAuthAuthentication())
eq_(self.resource.is_authenticated(req), None)
示例13: check_permissions
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def check_permissions(self):
req = RequestFactory().get(reverse('comm-thread-detail',
kwargs={'pk': self.thread.pk}))
req.user = self.user
req.amo_user = self.profile
req.groups = req.amo_user.groups.all()
return ThreadPermission().has_object_permission(
req, 'comm-thread-detail', self.thread)
示例14: test_failed_session_auth
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def test_failed_session_auth(self):
req = RequestFactory().post(
'/api/',
HTTP_AUTHORIZATION='mkt-shared-secret bogus')
req.user = AnonymousUser()
for m in self.middlewares:
m().process_request(req)
ok_(not self.auth.authenticate(Request(req)))
ok_(not req.user.is_authenticated())
示例15: req_factory_factory
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import user [as 别名]
def req_factory_factory(url='', user=None, post=False, data=None, **kwargs):
"""Creates a request factory, logged in with the user."""
req = RequestFactory()
if post:
req = req.post(url, data or {})
else:
req = req.get(url, data or {})
if user:
req.user = UserProfile.objects.get(id=user.id)
req.groups = user.groups.all()
else:
req.user = AnonymousUser()
req.check_ownership = partial(check_ownership, req)
req.REGION = kwargs.pop('region', mkt.regions.REGIONS_CHOICES[0][1])
req.API_VERSION = 2
for key in kwargs:
setattr(req, key, kwargs[key])
return req