def test_user_gravatar(self):
user_role = Role.query.filter_by(name='User').first()
user = User(email=forgery_py.internet.email_address(),
username=forgery_py.internet.user_name(),
password='old_password',
avatar_hash=None,
role=user_role,
confirmed=True)
db.session.add(user)
db.session.commit()
https_url = 'https://secure.gravatar.com/avatar'
http_url = 'http://www.gravatar.com/avatar'
size = 150
default = 'identicon'
rating = 'g'
hash = hashlib.md5(user.email.encode('utf-8')).hexdigest()
http_gravatar = user.gravatar(size=size, default=default, rating=rating)
self.assertEqual(http_gravatar,
'{url}/{hash}?s={size}&d={default}&r={rating}'.
format(url=http_url, hash=hash, size=size, default=default,
rating=rating))
self.assertNotEqual(http_gravatar,
'{url}/{hash}?s={size}&d={default}&r={rating}'.
format(url=https_url, hash=hash, size=size, default=default,
rating=rating))
# 'PilosusBot.models.request' cannot be patched like this:
# with patch('PilosusBot.models.request.is_secure', new_callable=PropertyMock) as mock_sec:
# mock_sec.return_value = True
# request.is_secure # returns True now
#
# so there's no way to test HTTPS gravatar url other than
# having fun with HTTP headers probably (?)
评论列表
文章目录