本文整理汇总了Python中mock.MagicMock.user方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.user方法的具体用法?Python MagicMock.user怎么用?Python MagicMock.user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.user方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_profile_access_is_authorized
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_profile_access_is_authorized(self):
request = MagicMock()
self.assertTrue(self.presta.profile_access_is_authorized(request))
# no global
self.famille.visibility_global = False
self.assertFalse(self.famille.profile_access_is_authorized(request))
self.famille.visibility_global = True
request.user = AnonymousUser()
self.assertFalse(self.famille.profile_access_is_authorized(request))
self.famille.visibility_prestataire = False
request.user = self.user2
self.assertFalse(self.famille.profile_access_is_authorized(request))
self.famille.visibility_prestataire = True
self.assertTrue(self.famille.profile_access_is_authorized(request))
self.famille.visibility_family = False
request.user = self.famille.user
self.assertTrue(self.famille.profile_access_is_authorized(request))
request.user = User.objects.create_user("d", "[email protected]", "d")
models.Famille(user=request.user, email="[email protected]").save()
self.assertFalse(self.famille.profile_access_is_authorized(request))
self.famille.visibility_family = True
self.assertTrue(self.famille.profile_access_is_authorized(request))
示例2: test_get_cid
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_get_cid(self):
request = MagicMock()
request.user = get_user()
request.context = get_context()
self.assertEqual(get_cid(request), 1)
request.user.companies.append(get_company(2))
self.assertEqual(get_cid(request), 200)
# ref bug :#522
request.user = get_manager()
self.assertEqual(get_cid(request), 200)
request.user = get_admin()
self.assertEqual(get_cid(request), 200)
示例3: _build_mock_request
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def _build_mock_request(self, user=None, get=None, post=None):
request = MagicMock()
if user:
request.user = user
request.user.is_authenticated = MagicMock(return_value=True)
else:
request.user = AnonymousUser()
request.GET = {}
request.POST = {}
request.resolver_match.kwargs = {}
if get is not None:
request.GET.update(get)
if post is not None:
request.POST.update(post)
return request
示例4: test_copy_to_category
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_copy_to_category(self):
""" copy all subrecords that don't have _clonable=True and
are not singletons
"""
request = MagicMock()
request.user = self.user;
view = self.setup_view(views.EpisodeCopyToCategoryView, request)
testmodels.Colour.objects.create(
episode=self.episode, name="purple"
)
testmodels.HatWearer.objects.create(
episode=self.episode, name="hat wearer"
)
testmodels.EpisodeName.objects.create(
episode=self.episode, name="episode name"
)
view.post(request, pk=self.episode.pk, category="Outpatient")
new_episode = models.Episode.objects.exclude(id=self.episode.id).get()
self.assertEqual(new_episode.hatwearer_set.get().name, "hat wearer")
self.assertEqual(new_episode.colour_set.count(), 0)
self.assertEqual(new_episode.category_name, "Outpatient")
# a singleton will be created but not populate it
self.assertEqual(
new_episode.episodename_set.filter(name="episode name").count(), 0
)
示例5: test_get_station
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_get_station(self, ChuteStorage, Popen):
ChuteStorage.chuteList = {
self.chute.name: self.chute
}
proc = MagicMock()
Popen.return_value = proc
proc.stdout = [
"Station 12:34:56:78:9a:bc (on wlan0)",
" inactive time: 304 ms",
" rx bytes: 18816",
" rx packets: 75",
" tx bytes: 5386",
" tx packets: 21",
" signal: -29 dBm",
" tx bitrate: 54.0 MBit/s"
]
request = MagicMock()
request.user = User.get_internal_user()
result = self.api.get_station(request, self.chute.name,
self.interface['name'], '12:34:56:78:9a:bc')
station = json.loads(result)
assert station['mac_addr'] == '12:34:56:78:9a:bc'
assert station['rx_bytes'] == '18816'
assert station['signal'] == '-29 dBm'
assert station['tx_bitrate'] == '54.0 MBit/s'
示例6: test_ChuteApi_get_network
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_ChuteApi_get_network(ChuteStorage):
update_manager = MagicMock()
update_manager.assign_change_id.return_value = 1
api = chute_api.ChuteApi(update_manager)
iface = {
'name': 'vwlan0',
'type': 'wifi',
'internalIntf': 'wlan0'
}
chute = Chute(name="test")
chute.setCache("networkInterfaces", [iface])
ChuteStorage.chuteList = {
"test": chute
}
request = MagicMock()
request.user = User.get_internal_user()
result = api.get_network(request, "test", "nomatch")
assert result == "{}"
result = api.get_network(request, "test", "vwlan0")
data = json.loads(result)
assert data['name'] == iface['name']
assert data['type'] == iface['type']
assert data['interface'] == iface['internalIntf']
示例7: test_ChuteApi_operations
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_ChuteApi_operations():
update_manager = MagicMock()
update_manager.assign_change_id.return_value = 1
api = chute_api.ChuteApi(update_manager)
body = {
'config': {}
}
request = MagicMock()
request.content.read.return_value = json.dumps(body)
request.user = User.get_internal_user()
functions = [
api.update_chute,
api.stop_chute,
api.start_chute,
api.restart_chute,
api.delete_chute
]
for func in functions:
print("Calling ChuteApi {}".format(func.__name__))
data = func(request, "test")
assert isinstance(data, basestring)
result = json.loads(data)
assert result['change_id'] == 1
示例8: test_ChuteApi_get_chute
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_ChuteApi_get_chute(ChuteStorage, ChuteContainer):
update_manager = MagicMock()
api = chute_api.ChuteApi(update_manager)
request = MagicMock()
request.user = User.get_internal_user()
container = MagicMock()
container.getStatus.return_value = "running"
ChuteContainer.return_value = container
chute = MagicMock()
chute.name = "test"
chute.state = "running"
chute.version = 5
chute.environment = {}
chute.resources = {}
ChuteStorage.chuteList = {
"test": chute
}
data = api.get_chute(request, chute.name)
assert isinstance(data, basestring)
result = json.loads(data)
assert result['name'] == chute.name
assert result['version'] == chute.version
示例9: test_list_with_some_contents
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_list_with_some_contents(self):
c1 = Colour(name="blue", episode=self.episode).save()
c2 = Colour(name="red", episode=self.episode).save()
mock_request = MagicMock(name='mock request')
mock_request.user = self.user
response = self.viewset().list(mock_request)
data = [
{
u'consistency_token': u'',
u'created': None,
u'created_by_id': None,
u'episode_id': 1,
u'id': 1,
u'name': u'blue',
u'updated': None,
u'updated_by_id': None
},
{
u'consistency_token': u'',
u'created': None,
u'created_by_id': None,
u'episode_id': 1,
u'id': 2,
u'name': u'red',
u'updated': None,
u'updated_by_id': None
}
]
self.assertEqual(data, json.loads(response.content.decode('UTF-8')))
示例10: test_is_user_info
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_is_user_info(self):
'Test we get the user-object back from a user-info'
userInfo = MagicMock()
userInfo.user = 'This is not a user'
r = userInfo_to_user(userInfo)
self.assertEqual(userInfo.user, r)
示例11: assert_expire_in_extended
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def assert_expire_in_extended(self, req_path, should_expire_in, should_extend, headers=None, query_string=None):
request = MagicMock(path=req_path)
request.method = "GET"
request.user = MagicMock()
request.user.is_authenticated.return_value = True
thirty_mins_ago = (datetime.datetime.now() - datetime.timedelta(minutes=30)).strftime("%Y-%m-%dT%H:%M:%S.%f")
request.session = {"_session_security": thirty_mins_ago}
if headers:
request.META = headers
if query_string:
request.REQUEST = query_string
before_response = {}
response = self.middleware.process_response(request, before_response)
self.assertIsNotNone(response.get("Session-Expires-In"))
self.assertIsAlmostEqualMinutes(response.get("Session-Expires-In"), should_expire_in - 30, delta=1.0)
self.middleware.process_request(request)
response2 = self.middleware.process_response(request, before_response)
self.assertIsNotNone(response2.get("Session-Expires-In"))
if should_extend:
self.assertIsAlmostEqualMinutes(response.get("Session-Expires-In"), should_expire_in, delta=1.0)
else:
self.assertIsAlmostEqualMinutes(response.get("Session-Expires-In"), should_expire_in - 30, delta=1.0)
示例12: test_remove_review
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_remove_review(self, name, user_check, side_effect=None):
# Create Mocks
#if name == 'manager':
# import ipdb; ipdb.set_trace()
rev_object = MagicMock()
rev_object.pk = '333333'
rev_object.user = self.user
rev_object.organization = self.org
self.user.userprofile.rated_offerings = [self.offering.pk]
self.offering.rating = 3.75
self.offering.comments = ['333333', '444444', '555555', '666666']
rev_object.offering = self.offering
rev_object.rating = 3
review_manager.Review = MagicMock()
review_manager.Review.objects.get.return_value = rev_object
if side_effect:
side_effect(self, rev_object)
error = False
try:
rm = review_manager.ReviewManager()
rm.remove_review(self.user, '333333')
except:
error = True
# Check result
self.assertFalse(error)
self.assertFalse('333333' in self.offering)
self.offering.save.assert_called_once_with()
rev_object.delete.assert_called_once_with()
# Check user or organization models
user_check(self)
示例13: test_module_render_with_jump_to_id
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_module_render_with_jump_to_id(self):
"""
This test validates that the /jump_to_id/<id> shorthand for intracourse linking works assertIn
expected. Note there's a HTML element in the 'toy' course with the url_name 'toyjumpto' which
defines this linkage
"""
mock_request = MagicMock()
mock_request.user = self.mock_user
course = get_course_with_access(self.mock_user, "load", self.course_key)
field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
self.course_key, self.mock_user, course, depth=2
)
module = render.get_module(
self.mock_user, mock_request, self.course_key.make_usage_key("html", "toyjumpto"), field_data_cache
)
# get the rendered HTML output which should have the rewritten link
html = module.render(STUDENT_VIEW).content
# See if the url got rewritten to the target link
# note if the URL mapping changes then this assertion will break
self.assertIn("/courses/" + self.course_key.to_deprecated_string() + "/jump_to_id/vertical_test", html)
示例14: test_render_proc_ts
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_render_proc_ts(self):
"""
**test_render_proc_ts** * Test that items are grabbed correctly
"""
with patch('cr_hunt.views.add_hunt_its') as addf:
with patch('cr_hunt.views.redirect') as red:
request = MagicMock()
request.method = "POST"
def side_effect(*args):
def second_call(*args):
return 'test321'
request.POST.get.side_effect = second_call
return 'test123'
request.POST.get = MagicMock(side_effect=side_effect)
#request.POST.get('title', '').return_value = 'test123'
#request.POST.get('start', '').return_value = 'test321'
request.user = MagicMock()
attrs = {'is_authenticated.return_value':True}
request.user.configure_mock(**attrs)
request.user.username = 'user1'
views.render_proc_ts(request)
assert addf.called
addf.assert_called_with('111222', 'test123', 'test321', 'user1')
assert red.called #test that redirect is called
red.assert_called_with('cr_aitem')
red.reset_mock()
attrs = {'is_authenticated.return_value':False}
request.user.configure_mock(**attrs)
views.render_proc_ts(request)
assert red.called
red.assert_called_with('cr_error')
示例15: test_render_main
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import user [as 别名]
def test_render_main(self):
"""
**test_render_main** * Will test that correct url is called
"""
with patch('cr_hunt.views.render_to_response') as rend:
with patch('cr_hunt.views.redirect') as red:
with patch('cr_hunt.views.gen_hunt_id') as genhid:
with patch('cr_hunt.views.get_cur_count') as getcount:
request = MagicMock()
request.user = MagicMock()
attrs = {'is_authenticated.return_value':True}
request.user.configure_mock(**attrs)
genhid.return_value = 'test1'
request.user.username = MagicMock()
request.user.username.return_value = 'user1'
getcount.return_value = 1
views.render_main(request)
assert rend.called # Check that rend got called
rend.assert_called_with("cr_hunt/cr_hunt_main.html") # Check that correct url is called
self.assertEqual(views.hunt_id, 'test1') #check that hunt_id got reassigned correctly
self.assertEqual(views.i_counter, 1) #check that i_counter is initialized to 0
attrs = {'is_authenticated.return_value':False}
request.user.configure_mock(**attrs)
views.render_main(request)
assert red.called
red.assert_called_with('cr_error')