本文整理汇总了Python中test_utils.RequestFactory.get方法的典型用法代码示例。如果您正苦于以下问题:Python RequestFactory.get方法的具体用法?Python RequestFactory.get怎么用?Python RequestFactory.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类test_utils.RequestFactory
的用法示例。
在下文中一共展示了RequestFactory.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestDRFSwitch
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
class TestDRFSwitch(TestCase):
def setUp(self):
self.factory = RequestFactory()
def test_responses(self):
view = SwitchToDRF('LanguageView')
request = self.factory.get(reverse('api.language', args=['1.5']))
request.APP = Mock(id=1)
request.user = AnonymousUser()
eq_(view(request, api_version=1.5).__module__, 'django.http')
piston_response = view(request, api_version=1.5).content
self.create_switch('drf', db=True)
eq_(view(request, api_version=1.5).__module__,
'rest_framework.response')
drf_response = view(request, api_version=1.5).render().content
eq_(piston_response, drf_response)
def test_wrong_format_exceptions(self):
view = SwitchToDRF('LanguageView')
request = self.factory.get(reverse('api.language', args=['1.5']))
request.APP = Mock(id=1)
request.GET = {'format': 'foo'}
request.user = AnonymousUser()
response = view(request, api_version=1.5)
eq_(response.content, '{"msg": "Not implemented yet."}')
eq_(response.status_code, 200)
self.create_switch('drf', db=True)
response = view(request, api_version=1.5)
self.assertTrue('<error>Not found</error>'
in response.render().content)
eq_(response.status_code, 404)
示例2: test_session_version_sidebar
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
def test_session_version_sidebar(self):
request = RequestFactory()
request.GET = {}
request.session = {}
request.APP = amo.FIREFOX
request.get(reverse('search.search'))
facets = {
u'platforms': [{u'count': 58, u'term': 1}],
u'appversions': [{u'count': 58, u'term': 5000000200100}],
u'categories': [{u'count': 55, u'term': 1}],
u'tags': [],
}
versions = version_sidebar(request, {}, facets)
assert versions[0].selected
versions = version_sidebar(request, {'appver': '5.0'}, facets)
assert versions[1].selected
versions = version_sidebar(request, {}, facets)
assert versions[1].selected
request.GET['appver'] = ''
versions = version_sidebar(request, {}, facets)
assert versions[0].selected
示例3: test_version_sidebar
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
def test_version_sidebar(self):
request = RequestFactory()
request.GET = {}
request.APP = amo.FIREFOX
request.get(reverse('search.search'))
facets = {
u'platforms': [{u'count': 58, u'term': 1}],
u'appversions': [{u'count': 58, u'term': 5000000200100}],
u'categories': [{u'count': 55, u'term': 1}],
u'tags': [],
}
versions = version_sidebar(request, {}, facets)
assert versions[0].selected
versions = version_sidebar(request, {'appver': '5.0'}, facets)
assert versions[1].selected
# We're not storing the version in the session anymore: no memories.
versions = version_sidebar(request, {}, facets)
assert versions[0].selected
# We read the appver from the cleaned form data.
request.GET['appver'] = '123.4'
# No form data, fallback to default (first entry).
versions = version_sidebar(request, {}, facets)
assert versions[0].selected
# Form data has the proper version, use it.
versions = version_sidebar(request, {'appver': '5.0'}, facets)
assert versions[1].selected
# Form data has the proper version, which is new: add it.
versions = version_sidebar(request, {'appver': '123.4'}, facets)
assert versions[1].selected
eq_(len(versions), 3)
示例4: TestProfileMiddleware
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
class TestProfileMiddleware(TestCase):
def setUp(self):
self.factory = RequestFactory()
def test_safe_view_request(self):
request = self.factory.get(reverse("users_edit"))
middleware = ProfileMiddleware()
response = middleware.process_request(request)
self.assertFalse(response)
def test_safe_path_request(self):
request = self.factory.get("/admin/something/")
middleware = ProfileMiddleware()
response = middleware.process_request(request)
self.assertFalse(response)
def test_authed_user_request(self):
request = self.factory.get(reverse("users_dashboard"))
request.user = complete_user
middleware = ProfileMiddleware()
response = middleware.process_request(request)
self.assertFalse(response)
def test_anon_user_request(self):
request = self.factory.get(reverse("users_dashboard"))
request.user = incomplete_user
middleware = ProfileMiddleware()
response = middleware.process_request(request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response["Location"], reverse("users_edit"))
示例5: TestCollectionMembershipField
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
class TestCollectionMembershipField(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
self.collection = Collection.objects.create(**self.collection_data)
self.app = amo.tests.app_factory()
self.collection.add_app(self.app)
self.field = CollectionMembershipField()
self.membership = CollectionMembership.objects.all()[0]
self.request = RequestFactory()
self.profile = FeatureProfile(apps=True).to_signature()
def test_to_native(self):
resource = AppResource().full_dehydrate(Bundle(obj=self.app))
native = self.field.to_native(self.membership)
for key, value in native.iteritems():
if key == 'resource_uri':
eq_(value, self.app.get_api_url(pk=self.app.pk))
else:
eq_(value, resource.data[key])
def test_field_to_native(self):
self.field.parent = self.collection
self.field.source = 'collectionmembership_set'
self.field.context = {
'request': self.request.get('/', {'pro': self.profile})
}
# Ensure that the one app is included in the default response.
native = self.field.field_to_native(self.collection,
'collectionmembership_set')
eq_(len(native), 1)
eq_(int(native[0]['id']), self.app.id)
# ...but is not included when there is a feature profile mismatch.
self.app.current_version.features.update(has_geolocation=True)
native_without = self.field.field_to_native(self.collection,
'collectionmembership_set')
eq_(len(native_without), 0)
def test_field_to_native_invalid_profile(self):
self.field.parent = self.collection
self.field.source = 'collectionmembership_set'
self.field.context = {
'request': self.request.get('/', {'pro': 'muahaha'})
}
# Ensure that no exceptions were raised.
native = self.field.field_to_native(self.collection,
'collectionmembership_set')
# Ensure that no filtering took place.
eq_(len(native), 1)
eq_(int(native[0]['id']), self.app.id)
示例6: TestDRFSwitch
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
class TestDRFSwitch(TestCase):
fixtures = ['base/addon_3615',]
def setUp(self):
self.factory = RequestFactory()
self.user = UserProfile.objects.get(email='[email protected]')
def test_responses(self):
view = SwitchToDRF('Language')
request = self.factory.get(reverse('api.language', args=['1.5']))
request.APP = Mock(id=1)
request.user = AnonymousUser()
eq_(view(request, api_version=1.5).__module__, 'django.http.response')
piston_response = view(request, api_version=1.5).content
self.create_switch('drf', db=True)
eq_(view(request, api_version=1.5).__module__,
'rest_framework.response')
drf_response = view(request, api_version=1.5).render().content
eq_(piston_response, drf_response)
def test_responses_with_handler(self):
view = SwitchToDRF('User', with_handler=True)
request = self.factory.get(reverse('api.language', args=['2']))
class App():
id = 1
def __str__(self):
return str(self.id)
request.APP = App()
request.user = AnonymousUser()
request.amo_user = self.user
eq_(view(request, api_version=2).__module__, 'django.http.response')
self.create_switch('drf', db=True)
eq_(view(request, api_version=2).__module__, 'rest_framework.response')
def test_wrong_format_exceptions(self):
view = SwitchToDRF('Language')
request = self.factory.get(reverse('api.language', args=['1.5']))
request.APP = Mock(id=1)
request.GET = {'format': 'foo'}
request.user = AnonymousUser()
response = view(request, api_version=1.5)
eq_(response.content, '{"msg": "Not implemented yet."}')
eq_(response.status_code, 200)
self.create_switch('drf', db=True)
response = view(request, api_version=1.5)
self.assertTrue('<error>Not found</error>'
in response.render().content)
eq_(response.status_code, 404)
示例7: TestAddSubmissionView
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
class TestAddSubmissionView(TestCase):
def __init__(self, *args, **kwargs):
super(TestAddSubmissionView, self).__init__(*args, **kwargs)
# Add context and template to the response
on_template_render = curry(store_rendered_templates, {})
signals.template_rendered.connect(on_template_render,
dispatch_uid="template-render")
def setUp(self):
self.factory = RequestFactory()
self.ideation = MagicMock()
def test_add_submission_get(self):
request = self.factory.get('/')
request.user = AnonymousUser()
request.development = development_mock
response = views.add_submission(request, self.ideation)
eq_(response.status_code, 200)
def test_invalid_form(self):
request = self.factory.post('/', BLANK_EXTERNALS)
request.user = AnonymousUser()
request.development = development_mock
response = views.add_submission(request, self.ideation)
eq_(response.status_code, 200)
示例8: TestContext
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
class TestContext(TestCase):
def setUp(self):
self.factory = RequestFactory()
def render(self, content, request=None):
if not request:
request = self.factory.get('/')
tpl = jinja2.Template(content)
return jingo.render_to_string(request, tpl)
def test_request(self):
eq_(self.render('{{ request.path }}'), '/')
def test_settings(self):
eq_(self.render('{{ settings.SITE_ID }}'), '1')
def test_languages(self):
eq_(self.render("{{ LANGUAGES['en-us'] }}"), 'English (US)')
def test_languages(self):
eq_(self.render("{{ LANG }}"), 'en-US')
def test_lang_dir(self):
eq_(self.render("{{ DIR }}"), 'ltr')
示例9: GetNextUrlTests
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
class GetNextUrlTests(TestCase):
def setUp(self):
super(GetNextUrlTests, self).setUp()
self.r = RequestFactory()
self.patcher = patch('django.contrib.sites.models.Site.objects')
mock = self.patcher.start()
mock.get_current.return_value.domain = 'su.mo.com'
def tearDown(self):
self.patcher.stop()
super(GetNextUrlTests, self).tearDown()
def test_query_string(self):
"""Query-strings remain intact."""
r = self.r.get('/', {'next': '/new?f=b'})
eq_('/new?f=b', get_next_url(r))
def test_good_host_https(self):
"""Full URLs work with valid hosts."""
r = self.r.post('/users/login',
{'next': 'https://su.mo.com/kb/new'})
eq_('https://su.mo.com/kb/new', get_next_url(r))
def test_post(self):
"""'next' in POST overrides GET."""
r = self.r.post('/?next=/foo', {'next': '/bar'})
eq_('/bar', get_next_url(r))
def test_get(self):
"""'next' can be a query-string parameter."""
r = self.r.get('/users/login', {'next': '/kb/new'})
eq_('/kb/new', get_next_url(r))
def test_referer(self):
"""Use HTTP referer if nothing else."""
r = self.r.get('/')
r.META['HTTP_REFERER'] = 'http://su.mo.com/new'
eq_('http://su.mo.com/new', get_next_url(r))
def test_bad_host_https(self):
r = self.r.get('/', {'next': 'https://example.com'})
eq_(None, get_next_url(r))
def test_bad_host_protocol_relative(self):
"""Protocol-relative URLs do not let bad hosts through."""
r = self.r.get('/', {'next': '//example.com'})
eq_(None, get_next_url(r))
示例10: test_authenticated
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
def test_authenticated(self, unread_count_for):
"""Test authenticated user with flag active."""
unread_count_for.return_value = 3
rf = RequestFactory()
request = rf.get("/")
request.user = user(save=True)
eq_(3, unread_message_count(request)["unread_message_count"])
assert unread_count_for.called
示例11: test_anonymous
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
def test_anonymous(self, unread_count_for):
"""Test anonymous user with flag active."""
unread_count_for.return_value = 3
rf = RequestFactory()
request = rf.get('/')
request.user = AnonymousUser()
eq_(0, unread_message_count(request)['unread_message_count'])
assert not unread_count_for.called
示例12: test_authenticated
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
def test_authenticated(self, unread_count_for):
"""Test authenticated user with flag active."""
unread_count_for.return_value = 3
rf = RequestFactory()
request = rf.get('/')
request.user = get_user('rrosario')
eq_(3, unread_message_count(request)['unread_message_count'])
assert unread_count_for.called
示例13: test_stub_aurora_installer_override_locale
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
def test_stub_aurora_installer_override_locale(self):
rf = RequestFactory()
get_request = rf.get("/fake")
get_request.locale = "fr"
doc = pq(render("{{ download_firefox('aurora', " "force_full_installer=True) }}", {"request": get_request}))
links = doc(".download-list a")[:3]
for link in links:
ok_("stub" not in pq(link).attr("href"))
示例14: test_stub_aurora_installer_disabled_en_us
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
def test_stub_aurora_installer_disabled_en_us(self):
rf = RequestFactory()
get_request = rf.get("/fake")
get_request.locale = "en-US"
doc = pq(render("{{ download_firefox('aurora') }}", {"request": get_request}))
links = doc("li a")[:3]
for link in links:
ok_("stub" not in pq(link).attr("href"))
示例15: test_stub_aurora_installer_disabled_locale
# 需要导入模块: from test_utils import RequestFactory [as 别名]
# 或者: from test_utils.RequestFactory import get [as 别名]
def test_stub_aurora_installer_disabled_locale(self):
rf = RequestFactory()
get_request = rf.get("/fake")
get_request.locale = "fr"
doc = pq(render("{{ download_button('button', build='aurora') }}", {"request": get_request}))
links = doc(".download-list a")[:3]
for link in links:
ok_("stub" not in pq(link).attr("href"))