def test_get_context_twitter_avatar_redownload_previous(self): """ Given a context with twitter username When i retrieve the context's avatar And the image has not been retrieved for at least 4 hours Then the twitter's user avatar is redownloaded And the new image is returned """ from hashlib import sha1 from .mockers import create_context_full avatar_image = os.path.join(self.conf_dir, "avatar.png") http_mock_twitter_user_image(avatar_image) self.testapp.post('/contexts', json.dumps(create_context_full), oauth2Header(test_manager), status=201) url_hash = sha1(create_context_full['url']).hexdigest() self.testapp.get('/contexts/%s/avatar' % url_hash, '', {}, status=200) self.rewind_context_avatar_mod_time(url_hash, hours=4) rewinded_mod_date = self.get_context_avatar_modification_time(url_hash) self.testapp.get('/contexts/%s/avatar' % url_hash, '', {}, status=200) new_mod_date = self.get_context_avatar_modification_time(url_hash) self.assertNotEqual(rewinded_mod_date, new_mod_date)
def test_get_twitter_api_failing_download(self): """ Given a tweepy api with user image property And a url that fails for some unknown reason When i try to get the user image I get no download """ from max.utils.twitter import download_twitter_user_image avatar_image = os.path.join(self.conf_dir, "avatar.png") http_mock_twitter_user_image(avatar_image, status=500) api = MockTweepyAPI(None, provide_image=True) imagefile = os.path.join(self.tempfolder, 'twitter.png') download_twitter_user_image(api, 'maxupcnet', imagefile) self.assertFileNotExists(imagefile)
def test_get_context_twitter_download_avatar(self): """ Given a context with twitter username When i retrieve the context's avatar Then the twitter's user avatar is downloaded and stored """ from hashlib import sha1 from .mockers import create_context_full avatar_image = os.path.join(self.conf_dir, "avatar.png") http_mock_twitter_user_image(avatar_image) self.testapp.post('/contexts', json.dumps(create_context_full), oauth2Header(test_manager), status=201) url_hash = sha1(create_context_full['url']).hexdigest() response = self.testapp.get('/contexts/%s/avatar' % url_hash, '', {}, status=200) self.assertEqual(self.get_image_dimensions_from(response), (98, 98)) avatar_folder = get_avatar_folder(self.avatar_folder, 'contexts', url_hash) self.assertFileExists(os.path.join(avatar_folder, url_hash))
def test_get_context_twitter_download_error_from_twitter_avatar(self): """ Given a context with twitter username When i retrieve the context's avatar And some error happens when talking to twitter Then the twitter's user avatar is not downloaded nor stored And the default image is retrieved """ from hashlib import sha1 from .mockers import create_context_full avatar_image = os.path.join(self.conf_dir, "avatar.png") http_mock_twitter_user_image(avatar_image) self.testapp.post('/contexts', json.dumps(create_context_full), oauth2Header(test_manager), status=201) url_hash = sha1(create_context_full['url']).hexdigest() response = self.testapp.get('/contexts/%s/avatar' % url_hash, '', {}, status=200) self.assertEqual(self.get_image_dimensions_from(response), (48, 48)) self.assertFileNotExists(os.path.join(self.avatar_folder, '{}.png'.format(url_hash)))
def test_get_context_twitter_avatar_already_downloaded(self): """ Given a context with twitter username When i retrieve the context's avatar And the image has just been retrieved Then the twitter's user avatar is not downloaded again And the existing image is returned """ from hashlib import sha1 from .mockers import create_context_full avatar_image = os.path.join(self.conf_dir, "avatar.png") http_mock_twitter_user_image(avatar_image) self.testapp.post('/contexts', json.dumps(create_context_full), oauth2Header(test_manager), status=201) url_hash = sha1(create_context_full['url']).hexdigest() self.testapp.get('/contexts/%s/avatar' % url_hash, '', {}, status=200) old_mod_date = self.get_context_avatar_modification_time(url_hash) self.testapp.get('/contexts/%s/avatar' % url_hash, '', {}, status=200) new_mod_date = self.get_context_avatar_modification_time(url_hash) self.assertEqual(old_mod_date, new_mod_date)