def parse_data(self, data): # Process replies logging.info("parse_data: %s" % str(data)) current_question = Question.get_current_question() # Avoid race condition triggered by first question post if current_question == None: return # Retweeted poll if "event" in data and data["event"] == "quoted_tweet": target_id = str(data["target_object"]["quoted_status_id"]) screen_name = data["source"]["screen_name"] profile_image_url = data["source"] \ ["profile_image_url_https"] \ .replace("_normal", "_bigger") self.process_retweet_from_screename(current_question, target_id, screen_name, profile_image_url) # Replied to status if "in_reply_to_status_id" in data: reply_status_id = str(data["in_reply_to_status_id"]) # Only acknowledge replies to current question if reply_status_id == current_question.twitterid: screen_name = data["user"]["screen_name"] vote_text = data["text"] profile_image_url = data["user"] \ ["profile_image_url_https"] \ .replace("_normal", "_bigger") state_abbreviation = self.get_state_from_string(data["text"]) party = self.get_party_from_string_using_question( data["text"], current_question) sway_points = self.get_sway_from_string(data["text"]) user = User.query_by_userid(screen_name).get() # Cap sway points if user != None: if user.sway_points < sway_points: sway_points = user.sway_points elif sway_points > User.get_starting_sway_points(): sway_points = User.get_starting_sway_points() # Only add vote for question if user vote was valid if self.add_vote_for_screenname(current_question, str(data["id"]), vote_text, state_abbreviation, party, sway_points, screen_name, profile_image_url): # Add vote for question if vote was valid self.add_vote_for_question(party, sway_points, state_abbreviation, current_question)
def test_view_tasks_twitter_stream(self): if os.getenv('TWITTER_TESTS') == "FALSE": return # Modify in runtests.sh load_fixture('tests/states.json', kind={'State': State}) load_fixture('tests/questions.json', kind={'Question': Question,'State': State}) # Start new thread with Twitter Stream listener stream_thread = Thread(target=self._start_twitter_stream) stream_thread.daemon = True stream_thread.start() # Delete all previous tweets before proceeding twitter_api = TwitterAPI() twitter_api.delete_all_tweets() reponse = \ self.app.get('/tasks/twitter_post_status' \ '?question_cadence_minutes=1&post_to_twitter=True') current_question = Question.get_current_question() # Post reply twitter_status_id = twitter_api.get_last_tweet().id twitter_api.update_status("#yes for #WA #sway10", twitter_status_id) # Wait for stream to be updated time.sleep(2) # Test to see reply has been registered ndb.get_context().clear_cache() users = User.get_all() assert len(users) == 1 users[0].sway_points = User.get_starting_sway_points() - 10 users[0].votes[0].vote_text == "#yes for #WA" current_question = Question.get_current_question() assert len(current_question.state_scores) == 1 assert current_question.state_scores[0].state_abbreviation == "WA" assert current_question.state_scores[0].party_score_votes[0] == 1 assert current_question.state_scores[0].party_score_votes[1] == 0 assert current_question.state_scores[0].party_score_sway[0] == 10 assert current_question.state_scores[0].party_score_sway[1] == 0
def test_view_tasks_twitter_stream(self): if os.getenv("TWITTER_TESTS") == "FALSE": return # Modify in runtests.sh load_fixture("tests/states.json", kind={"State": State}) load_fixture("tests/questions.json", kind={"Question": Question, "State": State}) # Start new thread with Twitter Stream listener stream_thread = Thread(target=self._start_twitter_stream) stream_thread.daemon = True stream_thread.start() # Delete all previous tweets before proceeding twitter_api = TwitterAPI() twitter_api.delete_all_tweets() reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=1&post_to_twitter=True") current_question = Question.get_current_question() # Post reply twitter_status_id = twitter_api.get_last_tweet().id twitter_api.update_status("#yes for #WA #sway10", twitter_status_id) # Wait for stream to be updated time.sleep(2) # Test to see reply has been registered ndb.get_context().clear_cache() users = User.get_all() assert len(users) == 1 users[0].sway_points = User.get_starting_sway_points() - 10 users[0].votes[0].vote_text == "#yes for #WA" current_question = Question.get_current_question() assert len(current_question.state_scores) == 1 assert current_question.state_scores[0].state_abbreviation == "WA" assert current_question.state_scores[0].party_score_votes[0] == 1 assert current_question.state_scores[0].party_score_votes[1] == 0 assert current_question.state_scores[0].party_score_sway[0] == 10 assert current_question.state_scores[0].party_score_sway[1] == 0
def test_model_user(self): # Load fixtures load_fixture('tests/states.json', kind={'State': State}) load_fixture('tests/questions.json', kind={ 'Question': Question, 'State': State }) current_question_entity = Question.get_current_question() User.add_user_vote( None, "jrgrafton_test", "https://pbs.twimg.com//profile_images//440698495// \ 9929_128442162134_504357134_2556732_5649977_n_\ bigger.jpg", Vote(question=current_question_entity.key, replyid="692368266292023296", state_abbreviation="CA", party=0, sway_points=40)) # Ensure user was created user_entity = User.query_by_userid("jrgrafton_test").fetch() assert len(user_entity) == 1 assert user_entity[0].sway_points == User.get_starting_sway_points() - \ 40 assert user_entity[0].userid == "jrgrafton_test" assert "https" in user_entity[0].profile_image_url assert user_entity[0].votes[0].question == current_question_entity.key assert user_entity[0].votes[0].replyid == "692368266292023296" assert user_entity[0].votes[0].state_abbreviation == "CA" assert user_entity[0].votes[0].party == 0 # Ensure a reply to a different question is tallied next_question_entity = Question.get_current_question() User.add_user_vote( user_entity[0], "jrgrafton_test", "https://pbs.twimg.com//profile_images//440698495// \ 9929_128442162134_504357134_2556732_5649977_n_\ bigger.jpg", Vote(question=next_question_entity.key, replyid="692368266292023297", state_abbreviation="WA", party=1, sway_points=10)) # Ensure new vote was collated under existing user user_entity = User.query_by_userid("jrgrafton_test").fetch() assert len(user_entity) == 1 assert len(user_entity[0].votes) == 2 assert user_entity[0].sway_points == User.get_starting_sway_points() - \ (40 + 10) # Verify integrity of new vote assert user_entity[0].votes[1].question == next_question_entity.key assert user_entity[0].votes[1].replyid == "692368266292023297" assert user_entity[0].votes[1].state_abbreviation == "WA" assert user_entity[0].votes[1].party == 1 # Verify integrity of old vote assert user_entity[0].userid == "jrgrafton_test" assert "https" in user_entity[0].profile_image_url assert user_entity[0].votes[0].question == current_question_entity.key assert user_entity[0].votes[0].replyid == "692368266292023296" assert user_entity[0].votes[0].state_abbreviation == "CA" assert user_entity[0].votes[0].party == 0
def test_view_tasks_twitter_stream_listener_on_data(self): load_fixture('tests/states.json', kind={'State': State}) load_fixture('tests/questions_no_votes.json', kind={'Question': Question,'State': State}) twitter_stream = open(os.path.join(os.path.dirname(__file__), 'twitter_stream.json')) twitter_stream = json.load(twitter_stream) twitter_stream_listener = TwitterStreamListener() twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_reply_old"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_reply_old"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_reply_current_invalid"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_post"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_mention"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_favourite"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_favourite_reply"])) users = User.get_all() assert len(users) == 0 current_question = Question.get_current_question() assert len(current_question.state_scores) == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_reply_current_valid_user1"])) users = User.get_all() assert len(users) == 1 assert "https" in users[0].profile_image_url current_question = Question.get_current_question() assert len(current_question.state_scores) == 1 assert current_question.state_scores[0].state_abbreviation == "CA" assert current_question.state_scores[0].party_score_votes[0] == 0 assert current_question.state_scores[0].party_score_votes[1] == 1 assert current_question.state_scores[0].party_score_sway[0] == 0 assert current_question.state_scores[0].party_score_sway[1] == \ User.get_starting_sway_points() assert users[0].sway_points == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_reply_current_valid_user1"])) users = User.get_all() assert len(users) == 1 assert len(users[0].votes) == 1 assert users[0].sway_points == 0 current_question = Question.get_current_question() assert len(current_question.state_scores) == 1 assert current_question.state_scores[0].state_abbreviation == "CA" assert current_question.state_scores[0].party_score_votes[0] == 0 assert current_question.state_scores[0].party_score_votes[1] == 1 assert current_question.state_scores[0].party_score_sway[0] == 0 assert current_question.state_scores[0].party_score_sway[1] == \ User.get_starting_sway_points() assert users[0].sway_points == 0 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_reply_current_valid_user2"])) users = User.get_all() assert len(users) == 2 assert "https" in users[1].profile_image_url current_question = Question.get_current_question() assert len(current_question.state_scores) == 2 assert current_question.state_scores[1].state_abbreviation == "WA" assert current_question.state_scores[1].party_score_votes[0] == 1 assert current_question.state_scores[1].party_score_votes[1] == 0 assert current_question.state_scores[1].party_score_sway[0] == 20 assert current_question.state_scores[1].party_score_sway[1] == 0 assert users[1].sway_points == User.get_starting_sway_points() - 20 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_reply_current_valid_user2"])) users = User.get_all() assert len(users) == 2 assert current_question.state_scores[1].state_abbreviation == "WA" assert current_question.state_scores[1].party_score_votes[0] == 1 assert current_question.state_scores[1].party_score_votes[1] == 0 assert current_question.state_scores[1].party_score_sway[0] == 20 assert current_question.state_scores[1].party_score_sway[1] == 0 assert users[1].sway_points == User.get_starting_sway_points() - 20 twitter_stream_listener.on_data( json.dumps(twitter_stream["twitter_retweet"])) users = User.get_all() assert len(users) == 3 assert users[2].sway_points == User.get_starting_sway_points() + \ sway_points_backend["rewteet_poll"] assert "https" in users[2].profile_image_url
def test_view_tasks_twitter_stream_listener_process_retweet_from_screename(self): load_fixture('tests/states.json', kind={'State': State}) load_fixture('tests/questions.json', kind={'Question': Question,'State': State}) twitter_stream_listener = TwitterStreamListener() current_question = Question.get_current_question() # Invalid retweet id twitter_stream_listener.\ process_retweet_from_screename(current_question, -1, "jrgrafton", "https://pbs.twimg.com// \ profile_images//440698495// \ 9929_128442162134_504357134_ \ 2556732_5649977_n_bigger.jpg") users = User.get_all() assert len(users) == 0 # Valid retweet id - new user twitter_stream_listener.\ process_retweet_from_screename(current_question, current_question.twitterid, "jrgrafton", "https://pbs.twimg.com// \ profile_images//440698495// \ 9929_128442162134_504357134_ \ 2556732_5649977_n_bigger.jpg") users = User.get_all() assert len(users) == 1 assert users[0].sway_points == User.get_starting_sway_points() + \ sway_points_backend["rewteet_poll"] assert "https" in users[0].profile_image_url # Ensure no extra sway given for retweet twitter_stream_listener.\ process_retweet_from_screename(current_question, current_question.twitterid, "jrgrafton", "https://pbs.twimg.com// \ profile_images//440698495// \ 9929_128442162134_504357134_ \ 2556732_5649977_n_bigger.jpg") users = User.get_all() assert len(users) == 1 assert users[0].sway_points == User.get_starting_sway_points() + \ sway_points_backend["rewteet_poll"] # Test existing user reweet user = User( userid = "jrgrafton2", sway_points = 30 ) user.put() # Valid retweet id - new user twitter_stream_listener.\ process_retweet_from_screename(current_question, current_question.twitterid, "jrgrafton2", "https://pbs.twimg.com// \ profile_images//440698495// \ 9929_128442162134_504357134_ \ 2556732_5649977_n_bigger.jpg") users = User.get_all() assert len(users) == 2 assert users[1].sway_points == 30 + sway_points_backend["rewteet_poll"]
def test_model_user(self): # Load fixtures load_fixture('tests/states.json', kind={'State': State}) load_fixture('tests/questions.json', kind={'Question': Question,'State': State}) current_question_entity = Question.get_current_question() User.add_user_vote(None, "jrgrafton_test", "https://pbs.twimg.com//profile_images//440698495// \ 9929_128442162134_504357134_2556732_5649977_n_\ bigger.jpg", Vote( question = current_question_entity.key, replyid = "692368266292023296", state_abbreviation = "CA", party = 0, sway_points = 40 )) # Ensure user was created user_entity = User.query_by_userid("jrgrafton_test").fetch() assert len(user_entity) == 1 assert user_entity[0].sway_points == User.get_starting_sway_points() - \ 40 assert user_entity[0].userid == "jrgrafton_test" assert "https" in user_entity[0].profile_image_url assert user_entity[0].votes[0].question == current_question_entity.key assert user_entity[0].votes[0].replyid == "692368266292023296" assert user_entity[0].votes[0].state_abbreviation == "CA" assert user_entity[0].votes[0].party == 0 # Ensure a reply to a different question is tallied next_question_entity = Question.get_current_question() User.add_user_vote(user_entity[0], "jrgrafton_test", "https://pbs.twimg.com//profile_images//440698495// \ 9929_128442162134_504357134_2556732_5649977_n_\ bigger.jpg", Vote( question = next_question_entity.key, replyid = "692368266292023297", state_abbreviation = "WA", party = 1, sway_points = 10 )) # Ensure new vote was collated under existing user user_entity = User.query_by_userid("jrgrafton_test").fetch() assert len(user_entity) == 1 assert len(user_entity[0].votes) == 2 assert user_entity[0].sway_points == User.get_starting_sway_points() - \ (40 + 10) # Verify integrity of new vote assert user_entity[0].votes[1].question == next_question_entity.key assert user_entity[0].votes[1].replyid == "692368266292023297" assert user_entity[0].votes[1].state_abbreviation == "WA" assert user_entity[0].votes[1].party == 1 # Verify integrity of old vote assert user_entity[0].userid == "jrgrafton_test" assert "https" in user_entity[0].profile_image_url assert user_entity[0].votes[0].question == current_question_entity.key assert user_entity[0].votes[0].replyid == "692368266292023296" assert user_entity[0].votes[0].state_abbreviation == "CA" assert user_entity[0].votes[0].party == 0
def test_view_tasks_twitter_stream_listener_on_data(self): load_fixture("tests/states.json", kind={"State": State}) load_fixture("tests/questions_no_votes.json", kind={"Question": Question, "State": State}) twitter_stream = open(os.path.join(os.path.dirname(__file__), "twitter_stream.json")) twitter_stream = json.load(twitter_stream) twitter_stream_listener = TwitterStreamListener() twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_old"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_old"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_invalid"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_post"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_mention"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_favourite"])) users = User.get_all() assert len(users) == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_favourite_reply"])) users = User.get_all() assert len(users) == 0 current_question = Question.get_current_question() assert len(current_question.state_scores) == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_valid_user1"])) users = User.get_all() assert len(users) == 1 assert "https" in users[0].profile_image_url current_question = Question.get_current_question() assert len(current_question.state_scores) == 1 assert current_question.state_scores[0].state_abbreviation == "CA" assert current_question.state_scores[0].party_score_votes[0] == 0 assert current_question.state_scores[0].party_score_votes[1] == 1 assert current_question.state_scores[0].party_score_sway[0] == 0 assert current_question.state_scores[0].party_score_sway[1] == User.get_starting_sway_points() assert users[0].sway_points == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_valid_user1"])) users = User.get_all() assert len(users) == 1 assert len(users[0].votes) == 1 assert users[0].sway_points == 0 current_question = Question.get_current_question() assert len(current_question.state_scores) == 1 assert current_question.state_scores[0].state_abbreviation == "CA" assert current_question.state_scores[0].party_score_votes[0] == 0 assert current_question.state_scores[0].party_score_votes[1] == 1 assert current_question.state_scores[0].party_score_sway[0] == 0 assert current_question.state_scores[0].party_score_sway[1] == User.get_starting_sway_points() assert users[0].sway_points == 0 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_valid_user2"])) users = User.get_all() assert len(users) == 2 assert "https" in users[1].profile_image_url current_question = Question.get_current_question() assert len(current_question.state_scores) == 2 assert current_question.state_scores[1].state_abbreviation == "WA" assert current_question.state_scores[1].party_score_votes[0] == 1 assert current_question.state_scores[1].party_score_votes[1] == 0 assert current_question.state_scores[1].party_score_sway[0] == 20 assert current_question.state_scores[1].party_score_sway[1] == 0 assert users[1].sway_points == User.get_starting_sway_points() - 20 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_valid_user2"])) users = User.get_all() assert len(users) == 2 assert current_question.state_scores[1].state_abbreviation == "WA" assert current_question.state_scores[1].party_score_votes[0] == 1 assert current_question.state_scores[1].party_score_votes[1] == 0 assert current_question.state_scores[1].party_score_sway[0] == 20 assert current_question.state_scores[1].party_score_sway[1] == 0 assert users[1].sway_points == User.get_starting_sway_points() - 20 twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_retweet"])) users = User.get_all() assert len(users) == 3 assert users[2].sway_points == User.get_starting_sway_points() + sway_points_backend["rewteet_poll"] assert "https" in users[2].profile_image_url
def test_view_tasks_twitter_stream_listener_process_retweet_from_screename(self): load_fixture("tests/states.json", kind={"State": State}) load_fixture("tests/questions.json", kind={"Question": Question, "State": State}) twitter_stream_listener = TwitterStreamListener() current_question = Question.get_current_question() # Invalid retweet id twitter_stream_listener.process_retweet_from_screename( current_question, -1, "jrgrafton", "https://pbs.twimg.com// \ profile_images//440698495// \ 9929_128442162134_504357134_ \ 2556732_5649977_n_bigger.jpg", ) users = User.get_all() assert len(users) == 0 # Valid retweet id - new user twitter_stream_listener.process_retweet_from_screename( current_question, current_question.twitterid, "jrgrafton", "https://pbs.twimg.com// \ profile_images//440698495// \ 9929_128442162134_504357134_ \ 2556732_5649977_n_bigger.jpg", ) users = User.get_all() assert len(users) == 1 assert users[0].sway_points == User.get_starting_sway_points() + sway_points_backend["rewteet_poll"] assert "https" in users[0].profile_image_url # Ensure no extra sway given for retweet twitter_stream_listener.process_retweet_from_screename( current_question, current_question.twitterid, "jrgrafton", "https://pbs.twimg.com// \ profile_images//440698495// \ 9929_128442162134_504357134_ \ 2556732_5649977_n_bigger.jpg", ) users = User.get_all() assert len(users) == 1 assert users[0].sway_points == User.get_starting_sway_points() + sway_points_backend["rewteet_poll"] # Test existing user reweet user = User(userid="jrgrafton2", sway_points=30) user.put() # Valid retweet id - new user twitter_stream_listener.process_retweet_from_screename( current_question, current_question.twitterid, "jrgrafton2", "https://pbs.twimg.com// \ profile_images//440698495// \ 9929_128442162134_504357134_ \ 2556732_5649977_n_bigger.jpg", ) users = User.get_all() assert len(users) == 2 assert users[1].sway_points == 30 + sway_points_backend["rewteet_poll"]