def test_create_topic(self, _): expected_votes_user_2 = get_expected_votes_data("user_2") + [ dict( topic="topic_eee", project_name="project_d", vote_count=1, last_vote="9999", ), ] expected_votes_user_3 = [ dict( topic="topic_fff", project_name="project_e", vote_count=1, last_vote="9999", ), ] vote = Vote() vote.create_topic("user_2", "topic_eee", "project_d") vote.create_topic("user_3", "topic_fff", "project_e") self.assertEqual(expected_votes_user_2, vote.get_votes_for_user("user_2")) self.assertEqual(expected_votes_user_3, vote.get_votes_for_user("user_3"))
def get_votes_for_project(event, _): """ Handle get votes request for a project :param event: event :return: votes data as JSON """ # Get all parameters username = event["pathParameters"]["user"] project = event["pathParameters"]["project"] # Check if the user stats are public or the user is logged in user = User() user_data = user.get_user_by_username(username) if user_data and (user_data["is_public"] or username == get_logged_in_user(event)): vote = Vote() votes_data = vote.get_votes_for_project(username, project) result = dict( single_voting_projects=user_data.get("single_voting_projects"), votes=votes_data, ) return create_response(200, body=json.dumps(result)) else: return create_response(400, "GET", "Invalid user")
def test_get_votes_for_user(self): vote = Vote() self.assertEqual( get_expected_votes_data("user_1"), vote.get_votes_for_user("user_1") ) self.assertEqual( get_expected_votes_data("user_2"), vote.get_votes_for_user("user_2") ) self.assertEqual([], vote.get_votes_for_user("user_3"))
def do_vote(event, _): """ Post a vote to the database :param event: event """ # Get all parameters username = event["pathParameters"]["user"].lower() project = event["pathParameters"]["project"].lower() topic = event["pathParameters"]["topic"].lower() log.debug("Voting for: %s, %s, %s", username, project, topic) # Check all parameters for validity and return if some of them is not valid if not re.fullmatch(r"[a-z0-9_\.\-]{3,30}", username): return if not re.fullmatch(r"[a-z0-9_\.\-]{1,100}", project): return if not re.fullmatch(r"[a-z0-9_\.\-]{1,100}", topic): return # Check if this IP address already voted for this topic log.debug(f"Event: {event}") ip_address = get_ip_address(event) vote_history = VoteHistory() if vote_history.check_ip_voted(username, get_topic_key(project, topic), ip_address): return # Check if the user has multiple voting for a project disabled user = User() user_data = user.get_user_by_username(username) # Stop if the user is not registered if not user_data: return # Stop ff the project is a single voting and the user already voted if ("single_voting_projects" in user_data and project in user_data["single_voting_projects"] and vote_history.check_ip_voted_project(username, project, ip_address)): return # Do the voting vote = Vote() vote.add_vote(username, project, topic) vote_history.add_vote_history(username, project, topic, ip_address)
def test_set_vote_hidden(self): vote = Vote() vote.set_vote_hidden("user_1", "project_a", "topic_bbb", True) self.assertTrue(vote.get_votes_for_project("user_1", "project_a")[0]["hidden"]) vote.set_vote_hidden("user_1", "project_b", "topic_ccc", False) self.assertFalse(vote.get_votes_for_project("user_1", "project_b")[0]["hidden"])
def test_set_vote_count(self, _): vote = Vote() # Check votes count set correctly self.assertEqual(10, vote.get_vote_count("user_1", "project_a/topic_aaa")) vote.set_vote_count("user_1", "project_a/topic_aaa", 100) self.assertEqual(100, vote.get_vote_count("user_1", "project_a/topic_aaa")) # Check timestamp set correctly votes_user_1 = vote.get_votes_for_user("user_1") self.assertEqual("9999", votes_user_1[0]["last_vote"])
def delete_vote(event, _): """ Delete a vote :param event: event :return: votes data as JSON """ user = event["pathParameters"]["user"].lower() project = event["pathParameters"]["project"].lower() topic = event["pathParameters"]["topic"].lower() # Check if we are changing the currently logged in user loggedin_user = get_logged_in_user(event) if user != loggedin_user: return create_response(400, "POST", "User not logged in correctly") vote = Vote() # Check if the topic exists if vote.get_vote_count(user, get_topic_key(project, topic)) > 0: vote.delete_vote(user, project, topic) return create_response(200, "POST") else: return create_response(400, "POST", "Topic doesn't exist")
def test_get_vote_count(self): vote = Vote() # Test existing topics self.assertEqual(10, vote.get_vote_count("user_1", "project_a/topic_aaa")) self.assertEqual(20, vote.get_vote_count("user_1", "project_a/topic_bbb")) self.assertEqual(30, vote.get_vote_count("user_1", "project_b/topic_ccc")) self.assertEqual(40, vote.get_vote_count("user_2", "project_c/topic_ddd")) # Test non-existing topics self.assertEqual(0, vote.get_vote_count("user_1", "project_a/topic_xxx")) self.assertEqual(0, vote.get_vote_count("user_1", "project_x/topic_xxx")) self.assertEqual(0, vote.get_vote_count("user_x", "project_x/topic_xxx")) self.assertEqual(0, vote.get_vote_count("user_2", "project_a/topic_aaa")) self.assertEqual(0, vote.get_vote_count("user_2", "project_c/topic_aaa"))
def test_get_votes_for_project(self): vote = Vote() self.assertEqual( get_expected_votes_data("user_1", "project_a"), vote.get_votes_for_project("user_1", "project_a"), ) self.assertEqual( get_expected_votes_data("user_1", "project_b"), vote.get_votes_for_project("user_1", "project_b"), ) self.assertEqual( get_expected_votes_data("user_2", "project_c"), vote.get_votes_for_project("user_2", "project_c"), ) self.assertEqual([], vote.get_votes_for_project("user_2", "project_y")) self.assertEqual([], vote.get_votes_for_project("user_3", "project_z"))
def test_delete_vote(self): vote = Vote() vote.delete_vote("user_1", "project_b", "topic_ccc") self.assertEqual([], vote.get_votes_for_project("user_1", "project_b"))
def test_add_vote(self): vote = Vote() # Test existing topic vote.add_vote("user_1", "project_a", "topic_aaa") self.assertEqual(11, vote.get_vote_count("user_1", "project_a/topic_aaa")) vote.add_vote("user_1", "project_a", "topic_aaa") self.assertEqual(12, vote.get_vote_count("user_1", "project_a/topic_aaa")) # Test new topic vote.add_vote("user_1", "project_a", "topic_xxx") self.assertEqual(1, vote.get_vote_count("user_1", "project_a/topic_xxx")) vote.add_vote("user_1", "project_a", "topic_xxx") self.assertEqual(2, vote.get_vote_count("user_1", "project_a/topic_xxx"))