Esempio n. 1
0
def remove_single_voting_project(event, _):
    """
    Remove a project from the user's single voting projects list
    """
    project = event["body"]

    # Check if the project name is valid
    if not re.fullmatch(r"[a-z0-9_\.\-]{1,100}", project):
        return create_response(400, "POST", "Invalid project name")

    # Get the logged in user data
    user = User()
    username = get_logged_in_user(event)
    user_data = user.get_user_by_username(username)
    if not user_data:
        return create_response(400, "POST", "User not logged in correctly")

    # Update the list of single voting projects
    if "single_voting_projects" in user_data:
        if project in user_data["single_voting_projects"]:
            user_data["single_voting_projects"].remove(project)

            user.change_single_voting_projects(
                username, user_data["single_voting_projects"])

    return create_response(200, "POST")
Esempio n. 2
0
    def test_change_single_voting_projects(self):
        user = User()

        user.change_single_voting_projects("user_1", ["project_a"])
        self.assertEqual(
            ["project_a"],
            user.get_user_by_username("user_1")["single_voting_projects"])

        user.change_single_voting_projects(
            "user_1", ["project_a", "project_b", "project_c"])
        self.assertEqual(
            ["project_a", "project_b", "project_c"],
            user.get_user_by_username("user_1")["single_voting_projects"],
        )

        user.change_single_voting_projects("user_1", [])
        self.assertEqual(
            [],
            user.get_user_by_username("user_1")["single_voting_projects"],
        )

        self.assertRaises(ValueError, user.change_single_voting_projects,
                          "user_x", [])