Example #1
0
    def test_view_tasks_twitter_post_status(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})
        current_question = Question.get_current_question()

        # Add user with vote so we can test sway points
        vote_current = Vote(
            question = current_question.key,
            replyid = "123",
            state_abbreviation = "CA",
            party = 1,
            sway_points = 20
        )
        user = User(
            userid = "jrgrafton",
            votes = [vote_current],
            # Used 20 sway points upon creation
            sway_points = 30
        )
        user.put()
        original_sway_points = user.sway_points # Store initial sway points

        # 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()
        ndb.get_context().clear_cache()
        assert current_question.twitterid != None
        assert "Posted new status" in reponse.data
        assert status.is_success(reponse.status_code) == True

        # Ensure user had sway points returned
        assert user.sway_points == original_sway_points + \
                          sway_points_tasks["submit_answer"] + \
                          sway_points_tasks["submit_winning_answer"] + \
                          int(vote_current.sway_points * \
                              sway_points_tasks["refund"])

        # Ensure Twitter timeline has been updated
        twitter_status = twitter_api.get_last_tweet().text
        question_entity = Question.get_current_question()
        assert twitter_status == question_entity.question_text

        # Expect question not to be posted as time is not yet up
        reponse = \
            self.app.get('/tasks/twitter_post_status' \
                '?question_cadence_minutes=52560000')
        assert "New question not ready" in reponse.data
        assert status.is_success(reponse.status_code) == True
Example #2
0
    def test_view_tasks_twitter_post_status(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})
        current_question = Question.get_current_question()

        # Add user with vote so we can test sway points
        vote_current = Vote(
            question=current_question.key, replyid="123", state_abbreviation="CA", party=1, sway_points=20
        )
        user = User(
            userid="jrgrafton",
            votes=[vote_current],
            # Used 20 sway points upon creation
            sway_points=30,
        )
        user.put()
        original_sway_points = user.sway_points  # Store initial sway points

        # 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()
        ndb.get_context().clear_cache()
        assert current_question.twitterid != None
        assert "Posted new status" in reponse.data
        assert status.is_success(reponse.status_code) == True

        # Ensure user had sway points returned
        assert user.sway_points == original_sway_points + sway_points_tasks["submit_answer"] + sway_points_tasks[
            "submit_winning_answer"
        ] + int(vote_current.sway_points * sway_points_tasks["refund"])

        # Ensure Twitter timeline has been updated
        twitter_status = twitter_api.get_last_tweet().text
        question_entity = Question.get_current_question()
        assert twitter_status == question_entity.question_text

        # Expect question not to be posted as time is not yet up
        reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=52560000")
        assert "New question not ready" in reponse.data
        assert status.is_success(reponse.status_code) == True
Example #3
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
Example #4
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