예제 #1
0
    def get(self, user_id):
        """
        Fetch a single user's data if a user_id is specified.
        Otherwise fetch the list of all users.
        Returned info contains user_id, name, group name,email,
        admin status, and date_created.
        """
        if user_id:
            return get_from_user_id(user_id)
        else:
            # No user_id given; this is a GET all users request.
            if not current_user.is_admin:
                error(403, "Logged in user not admin ")

            user_db_data = user_db_util.fetchall(g.database)

            response_data: Dict[str, List[Dict[str, str]]] = {"users": []}
            for user_entry in user_db_data:
                response_data["users"].append({
                    "id": user_entry["user_id"],
                    "email": user_entry["email"],
                    "name": user_entry["name"],
                    "group": user_entry["group_name"],
                    "admin": user_entry["admin"],
                    "timestamp": user_entry["date_created"]
                    })

            return jsonify(response_data), 201
예제 #2
0
    def test_get_all_users(self):
        """
        test inserts two new users, then fetches from the table with the db
        get function and raw sql to compare the results. Checks the number of
        returned rows is the same, and that all of the columns match from
        each returned row.
        """

        email1 = "*****@*****.**"
        self.create_example_user(email1)

        email2 = "*****@*****.**"

        self.create_example_user(email2)

        users_get_endpoint_result = user.fetchall(self.database)

        verify_query = """
            SELECT * FROM USERS;"""
        self.database.cursor.execute(verify_query)

        verify_rows = [r._asdict() for r in self.database.cursor.fetchall()]

        assert len(verify_rows) == len(users_get_endpoint_result)

        for (email, name, group_name, hashed_password, admin) in [
            (r["email"], r["name"], r["group_name"], r["hashed_password"],
             r["admin"]) for r in users_get_endpoint_result
        ]:

            self.verify_user_data(email, name, group_name, hashed_password,
                                  admin)