It is entirely up to you to agree to this.
If you don’t, the University is free to assign you a failing mark. Many Australian universities have announced that a failing mark will not be recorded on academic records - if your university is one of these then there will be no record of your failure.
From a contract law perspective, you would not be ...
It seems the answer is yes, i.e. it is legal.
Section 30 of the Australian 1984 Sex Discrimination Act discussing when it is lawful to discriminate based on sex:
(g) the occupant of the position is required to enter areas ordinarily used only by persons of the relevant sex while those persons are in a state of undress; or