The professor is wrong on ethical and pedagogical grounds even if he is within his legal rights. Students pay rather handsomely for the privilege of sitting in class. Professors are well paid for the privilege of teaching. Teachers have certain customary duties toward students, of which writing recommendations (at least for the superior students) is one.
In this day and age, any professor who explicitly refused to write a recommendation for a student because he didn't like the color of his skin would be kicked out the door pronto. I don't imagine the courts would protect him. Ditto for gender discrimination. Other forms of discrimination can probably be gotten away with, but they are contemptible nonetheless.
I've not darkened the door of a college classroom for many a year, but I gather there are plenty of radical professors who give conservative students the shaft because they don't like their politics. That's wrong, as it would be for a conservative professor to sabotage a capable but leftish student. My own experience was quite different. I was conservative. Most of my instructors were quite liberal. We got along very well. I was stimulated by the challenge of opposing views, and I'd like to think the profs appreciated it too. This is how it is supposed to work, and I hope this is still the norm, the horror stories notwithstanding.
You are so right. And while we're exposing fallacies, let's call the professor's actions exactly what they are: blackballing.
At most, the professor should ask the student to demonstrate knowledge of the theory, not agreement with it.
Since the professor appears to be within his legal rights, all students/parents can do is vote with their feet & pocketbooks.