def get_video_info(video_id): # this gets called inside add_credit_to_video_url(). Try queing # up a second call to add_credit_to_video_url() if self.first_call: self.first_call = False add_credit_to_video_url(self.video_url, self.account) return self.mock_get_video_info.return_value
def add_amara_credit(video_url_id): video_url = VideoUrl.objects.get(id=video_url_id) account = get_sync_account(video_url.video, video_url) if credit.should_add_credit_to_video_url(video_url, account): try: credit.add_credit_to_video_url(video_url, account) except google.OAuthError: logger.exception("Error adding youtube credit")
def test_add_credit(self): # test add_credit_to_video_url add_credit_to_video_url(self.video_url, self.account) new_description = "\n\n".join([ 'test description', "Help us caption & translate this video!", shortlink_for_video(self.video) ]) self.mock_get_new_access_token.assert_called_with( self.account.oauth_refresh_token) self.mock_update_video_description.assert_called_with( self.video_url.videoid, 'test-access-token', new_description)
def test_dont_add_credit_twice(self): # test that add_credit_to_video_url only alters the description once add_credit_to_video_url(self.video_url, self.account) self.mock_get_new_access_token.reset_mock() self.mock_update_video_description.reset_mock() self.mock_get_video_info.reset_mock() add_credit_to_video_url(self.video_url, 'test-access-token') self.assertEqual(self.mock_get_new_access_token.call_count, 0) self.assertEqual(self.mock_update_video_description.call_count, 0) self.assertEqual(self.mock_get_video_info.call_count, 0)
def test_concurency(self): # simulate add_credit_to_video_url() being called by a second thread # while it's still running in the first self.first_call = True def get_video_info(video_id): # this gets called inside add_credit_to_video_url(). Try queing # up a second call to add_credit_to_video_url() if self.first_call: self.first_call = False add_credit_to_video_url(self.video_url, self.account) return self.mock_get_video_info.return_value self.mock_get_video_info.side_effect = get_video_info add_credit_to_video_url(self.video_url, self.account) self.assertEqual(self.mock_update_video_description.call_count, 1)
def test_dont_add_credit_if_description_has_credit_text(self): # If the description already has our credit text we shouldn't try to # re-add it (this covers the case for descriptions that were altered # by the old accountlinker code). self.mock_get_video_info.return_value = YouTubeVideoInfoFactory( description="\n\n".join([ 'Test description', "Help us caption & translate this video!", shortlink_for_video(self.video) ])) add_credit_to_video_url(self.video_url, self.account) # in this case we should call get_new_access_token() and # get_video_info() to get the description, but avoid the # update_video_description() API call. self.assertEqual(self.mock_get_new_access_token.call_count, 1) self.assertEqual(self.mock_get_video_info.call_count, 1) self.assertEqual(self.mock_update_video_description.call_count, 0)
def add_amara_credit(video_url_id): video_url = VideoUrl.objects.get(id=video_url_id) account = get_sync_account(video_url.video, video_url) if credit.should_add_credit_to_video_url(video_url, account): credit.add_credit_to_video_url(video_url, account)