All the risks can never be eliminated, unfortunately.
Heavy metals can be determined by ICP-OES or ICP-MS, which are incredibly expensive pieces of instrumentation.
Atomic spectroscopy would work as well.
In US pharmacopoeia reagent tests are used as well, so those might be applicable, but are far less sensitive. These are rather cheap, but would point out only massive contamination - I do not recall the exact values now, but they were pretty high.
Bacteriological contamination can be determined more easily by filtering the unopened vial through a membrane with pores small enough to catch the bacteria and then staining the membrane with stains, that color the bacterial cell walls. There might be better, more suitable ways, but this is the one that had popped into my mind immediately.
Random harmful substances cannot be easily determined. A mass spectrometer with a NIST database could compare the findings in the samples with database of pesticides etc, but we'd be looking at 50 000$+ starting investment and massive maintenance costs.