def test_build_text_source_ignore_retweets(self): service = TwitterBotNamesService( [TweetFactory(retweeted_tweet=TweetFactory())]) text_sources = service.build_text_sources() text_sources.should.be.empty
def test_extract(self): extractor = HashTagTextExtractor() expect(extractor.extract(TweetFactory(hashtags=['johnDoe']))).to.eq([ ('#johnDoe', 'John Doe') ]) expect(extractor.extract(TweetFactory(hashtags=['JohnDoe']))).to.eq([ ('#JohnDoe', 'John Doe') ])
def test_call_save_log(self): response = MagicMock(save_log=MagicMock(), build_user_responses=MagicMock(return_value=['message_1', 'message_2'])) outgoing_tweet_1 = TweetFactory() outgoing_tweet_2 = TweetFactory() self.handler.twitter_service.build_responses = MagicMock(return_value=[response]) self.handler.client.tweet = MagicMock(side_effect=[outgoing_tweet_1, outgoing_tweet_2]) self.handler.on_tweet(TweetFactory()) response.save_log.assert_has_calls([call(outgoing_tweet_1), call(outgoing_tweet_2)])
def test_is_mentioning_twitterbot(self): client = MockTweepyWrapperFactory(screen_name='abc') context = {'client': client, 'for_user_id': 123} tweet = TweetFactory(text='something', user_mentions=[{ 'id': 123 }], context=context) expect(tweet.is_mentioning_twitterbot).to.be.true() tweet = TweetFactory(text='something', context=context) expect(tweet.is_mentioning_twitterbot).to.be.false()
def test_save_log_with_originating_tweet(self): TwitterBotResponseLog.objects.all().delete() response = TwitterResponse(incoming_tweet=TweetFactory()) response.entity_url = 'some url' response.originating_tweet = TweetFactory(text='some text') outgoing_tweet = TweetFactory() response.save_log(outgoing_tweet) TwitterBotResponseLog.objects.count().should.equal(1) log = TwitterBotResponseLog.objects.first() log.originating_tweet_content.should.equal('some text')
def test_get_originating_tweet(self): oldest_tweet = TweetFactory(created_at=datetime.strptime( '01-01-1970 00:00:00', '%d-%m-%Y %H:%M:%S')) self.service.tweets = [ oldest_tweet, TweetFactory(created_at=oldest_tweet.created_at + timedelta(days=1)), TweetFactory(created_at=oldest_tweet.created_at + timedelta(days=2)) ] originating_tweet = self.service.get_originating_tweet() originating_tweet.should.be(oldest_tweet)
def test_save_log(self): TwitterBotResponseLog.objects.all().delete() response = TwitterResponse(incoming_tweet=TweetFactory(text='some text')) response.entity_url = 'some url' response.matched_strings = 'some strings' outgoing_tweet = TweetFactory() response.save_log(outgoing_tweet) TwitterBotResponseLog.objects.count().should.equal(1) log = TwitterBotResponseLog.objects.first() log.incoming_tweet_content.should.equal('some text') log.entity_url.should.equal('some url') log.matched_strings.should.equal('some strings') log.originating_tweet_content.should.be.none
def test_build_responses(self): officer = OfficerFactory() rebuild_index() ResponseTemplateFactory(response_type='officer') originating_tweet = TweetFactory(text=officer.display_name) incoming_tweet = TweetFactory(retweeted_tweet=originating_tweet) self.service.get_recipients = MagicMock(return_value=['r1', 'r2']) responses = self.service.build_responses(incoming_tweet) len(responses).should.equal(1) for response in responses: response.recipients.should.equal(['r1', 'r2']) response.incoming_tweet.should.be(incoming_tweet) response.originating_tweet.should.be(originating_tweet)
def test_in_reply_to_tweet(self): in_reply_to_tweet = TweetFactory(id=123) client = MockTweepyWrapperFactory() client.register(in_reply_to_tweet) tweet = TweetContext(original_tweet={'in_reply_to_status_id': 123}, context={'client': client}) expect(tweet.in_reply_to_tweet.id).to.eq(123) expect(tweet.in_reply_to_tweet).to.eq(tweet.in_reply_to_tweet)
def setUp(self): super(URLContentTextExtractorTestCase, self).setUp() self.extractor = URLContentTextExtractor() client = MockTweepyWrapperFactory() self.tweet = TweetFactory(urls=['http://fakeurl.com/articles/1/'], context={ 'client': client, 'for_user_id': 123 })
def test_call_responsebot_client_tweet(self): message = 'some message' response = MagicMock(build_user_responses=MagicMock(return_value=[message])) with patch('twitterbot.services.twitter_bot_service.TwitterBotService.build_responses', return_value=[response]): self.handler.on_tweet(TweetFactory()) self.handler.client.tweet.assert_called_once_with(message)
def test_get_all_names(self, mock_requests_get): two_words_named_officer = OfficerFactory(officer_first='John', officer_last='Doe') three_words_named_officer = OfficerFactory(officer_first='John', officer_last='von Doe') hashtagged_officer = OfficerFactory(officer_first='Hash', officer_last='Tagged') relevant_officer_1 = OfficerFactory(officer_first='Relevant', officer_last='1') relevant_officer_2 = OfficerFactory(officer_first='Relevant', officer_last='2') relevant_linked_text_1 = 'CPD {name}'.format( name=relevant_officer_1.display_name) relevant_linked_text_2 = 'Chicago Police {name}'.format( name=relevant_officer_2.display_name) url = 'http://url.com' tweets = [ TweetFactory(text=two_words_named_officer.display_name), TweetFactory(text=three_words_named_officer.display_name), TweetFactory(urls=[{ 'expanded_url': url }]), TweetFactory(hashtags=[{ 'text': '#{first}{last}'.format(first=hashtagged_officer.officer_first, last=hashtagged_officer.officer_last) }]), ] service = TwitterBotNamesService(tweets) with patch('bs4.BeautifulSoup.getText', return_value=relevant_linked_text_1): names = service.get_all_names() names.should.contain(two_words_named_officer.display_name) names.should.contain(three_words_named_officer.display_name) names.should.contain(relevant_officer_1.display_name) names.should.contain(hashtagged_officer.display_name) with patch('bs4.BeautifulSoup.getText', return_value=relevant_linked_text_2): names = service.get_all_names() names.should.contain(relevant_officer_2.display_name)
def test_retweeted_status(self): client = MockTweepyWrapperFactory() retweeted_status = TweetFactory(id=123) client.register(retweeted_status) tweet = TweetContext(original_tweet={'retweeted_status': { 'id': 123 }}, context={'client': client}) expect(tweet.retweeted_status.id).to.eq(123) expect(tweet.retweeted_status).to.eq(tweet.retweeted_status)
def test_quoted_tweet_id(self): client = MockTweepyWrapperFactory() quoted_status = TweetFactory(id=123) client.register(quoted_status) tweet = TweetContext(original_tweet={ 'quoted_status_id': 123, 'quoted_status': None }, context={'client': client}) expect(tweet.quoted_status.id).to.eq(123)
def test_build_user_responses(self): response = TwitterResponse(entity=OfficerFactory(), incoming_tweet=TweetFactory()) response.message = '{user} something else' response.recipients = ['user1', 'user2'] messages = response.build_user_responses() len(messages).should.equal(len(response.recipients)) for recipient in response.recipients: messages.should.contain('{user} something else'.format(user=recipient))
def test_debug_log_when_unable_to_tweet(self): self.handler.client.tweet = MagicMock(return_value=None) self.handler.twitter_service.build_responses = MagicMock( return_value=[MagicMock(build_user_responses=MagicMock(return_value=['msg']))] ) with patch('twitterbot.handlers.bot_log') as mock_bot_log: self.handler.on_tweet(TweetFactory()) mock_bot_log.assert_has_calls([call('Incoming tweet: Random text here'), call('Unable to send: msg')])
def test_ignore_self_tweets(self): retweeted = TweetFactory(user_id=124) quoted = TweetFactory(user_id=124) requested_quoted = TweetFactory(user_id=124) replied_tweet = TweetFactory(user_id=124) tweet = TweetFactory(in_reply_to_tweet_id=replied_tweet.id, retweeted_tweet=retweeted, quoted_tweet=quoted) client = MagicMock( get_tweet=MagicMock(side_effect=[replied_tweet, requested_quoted]), get_current_user=MagicMock(return_value=MagicMock(id=124))) service = TwitterBotTweetsService(client) tweets = service.get_all_related_tweets(tweet) tweets.should.contain(tweet) tweets.shouldnt.contain(replied_tweet) tweets.shouldnt.contain(quoted) tweets.shouldnt.contain(requested_quoted) tweets.shouldnt.contain(retweeted)
def test_update_message_url(self): officer = MagicMock() officer.absolute_view_url = '/absolute_url' incoming_tweet = TweetFactory(id=10) response = TwitterResponse(entity=officer, message='{url} something', incoming_tweet=incoming_tweet) expected_url = 'https://example.com/absolute_url?reply_to=10' response.update_message_url() response.message.should.contain(expected_url) response.entity_url.should.equal(expected_url)
def test_get_all_related_tweets(self): replied_tweet = TweetFactory(user_id=124) level_2_quoted_tweet = TweetFactory(user_id=124) quoted_tweet = TweetFactory( quoted_tweet_id_str=level_2_quoted_tweet.id, user_id=124) retweeted_tweet = TweetFactory(user_id=124) incoming_tweet = TweetFactory(user_id=124, in_reply_to_tweet_id=replied_tweet.id, quoted_tweet=quoted_tweet, retweeted_tweet=retweeted_tweet) client = MagicMock( get_tweet=MagicMock( side_effect=[replied_tweet, level_2_quoted_tweet]), get_current_user=MagicMock(return_value=MagicMock(id=123))) service = TwitterBotTweetsService(client) tweets = service.get_all_related_tweets(incoming_tweet) tweets.should.contain(level_2_quoted_tweet) tweets.should.contain(retweeted_tweet) tweets.should.contain(quoted_tweet) tweets.should.contain(replied_tweet) tweets.should.contain(incoming_tweet)
def test_get_recipients(self): poster = 'poster' mentioned = 'mentioned' self.service.tweets = [ TweetFactory(screen_name=poster, user_mentions=[{ 'screen_name': mentioned }]) ] recipients = self.service.get_recipients() recipients.should.contain(poster) recipients.should.contain(mentioned)
def test_do_not_get_names_from_unrelevant_articles(self, mock_requests_get): non_relevant_officer = OfficerFactory(officer_first='Non', officer_last='Relevant') non_relevant_linked_text = non_relevant_officer.display_name url = 'http://url.com' tweets = [TweetFactory(urls=[{'expanded_url': url}])] service = TwitterBotNamesService(tweets) with patch('bs4.BeautifulSoup.getText', return_value=non_relevant_linked_text): names = service.get_all_names() names.shouldnt.contain(non_relevant_officer.display_name)
def test_get_non_existent_originating_tweet(self): self.service.tweets = [TweetFactory()] originating_tweet = self.service.get_originating_tweet() originating_tweet.should.be.none
def test_user_mention_screen_names(self): tweet = TweetFactory(original_tweet=self.original_tweet) expect(tweet.user_mention_screen_names).to.eq(['Abc'])
def test_exclude_self(self): tweet1 = TweetFactory(user_mentions=[{'screen_name': 'abc'}]) tweet2 = TweetFactory(user_mentions=[{'screen_name': 'def'}], id=123) extractor = TweetMentionRecipientExtractor() client = MockTweepyWrapperFactory(subscription_screen_name='def') expect(extractor.extract([tweet1, tweet2], {'client': client, 'for_user_id': 123})).to.eq(['abc'])
def test_remove_duplicate(self): tweet1 = TweetFactory(user_mentions=[{'screen_name': 'abc'}]) tweet2 = TweetFactory(user_mentions=[{'screen_name': 'abc'}, {'screen_name': 'def'}]) extractor = TweetMentionRecipientExtractor() expect(set(extractor.extract([tweet1, tweet2], self.context))).to.eq(set(['abc', 'def']))
def test_extract(self): tweet = TweetFactory(user_mentions=[{'screen_name': 'abc'}]) extractor = TweetMentionRecipientExtractor() expect(extractor.extract([tweet], self.context)).to.eq(['abc'])
def test_exclude_self(self): tweet1 = TweetFactory(author_screen_name='abc') tweet2 = TweetFactory(author_screen_name='def', id=123) extractor = TweetAuthorRecipientExtractor() client = MockTweepyWrapperFactory(subscription_screen_name='def') expect(extractor.extract([tweet1, tweet2], {'client': client, 'for_user_id': 123})).to.eq(['abc'])
def test_remove_duplicate(self): tweet1 = TweetFactory(author_screen_name='abc') tweet2 = TweetFactory(author_screen_name='abc') extractor = TweetAuthorRecipientExtractor() expect(extractor.extract([tweet1, tweet2], self.context)).to.eq(['abc'])
def test_extract(self): tweet1 = TweetFactory(author_screen_name='abc') tweet2 = TweetFactory(author_screen_name='def') extractor = TweetAuthorRecipientExtractor() expect(set(extractor.extract([tweet1, tweet2], self.context))).to.eq(set(['abc', 'def']))
def setUp(self): tweet = MagicMock(return_value=TweetFactory()) client = MagicMock(tweet=tweet) self.handler = CPDBTweetHandler(client)