Some drugs, medications and substances are banned in sport and competition.
Sporting associations have a general right to make rules for the conduct of organised sporting competitions. Those rules are binding, on the basis of a contract, on players who want to participate in those organised competitions.
At elite levels, the rules always include the right of sporting bodies to require players to have a drug test for performance enhancing drugs like anabolic steroids, in and out of competition. Some sports also test for recreational drugs.
The general principle is that if an athlete refuses or fails a drug test, the sporting bodies are end, subject to their own constitutions and rules of procedural fairness, to suspend or ban the player.