It would apply as it pertains to being "in or affecting commerce"; it'd be quite the stretch to apply that to possession outside the immediate context of a purchase/sale/transfer, though I suppose it wouldn't be the first time the Feds abused the ever-loving hell out of the Commerce Clause. Maybe taking a gun across state lines while high would qualify?
> Also it would make little sense to have a law saying that you can't be a user of illegal drugs at the moment of purchase of a firearm, but you can start using such substances afterwards.
The law in question derives from the federal government's Constitutional authority to regulate interstate and international commerce, which is (I would guess) why the language fixates on that aspect. The federal government doesn't otherwise have the power to infringe on the Second, Fourth, and (possibly) Tenth Amendments - as would likely be necessary to federally require gun owners to submit to random drug tests over something that one's state has made legal.
Meanwhile, my state (last I checked) has laws on the books separately prohibiting intoxication (be it via cannabis, alcohol, or whatever) while in possession of a firearm; to my knowledge, most (all?) states do.