It's All Him
The Zohar Hakadosh says that on the day Yosef ran away from Eishes Potifar he achieved the highest level of Tzadik Yesod Olam...
There is a Medrash that says that when Yosef came to do "his work" that day, he was actually going to sin. But then he saw the image of Yaakov Avinu and stopped. There is actually another Medrash (Medrash Abkir - which we don't have today, but is quoted by the other Medrashim) that says that even the image of Yaakov Avinu wasn't enough to stop him. He was also shown the image of his mother Rachel, and only then did he finally run out. But the Medrash Akbir goes on to say that he was overcome with such a strong desire that he actually turned around and headed back towards the house. At that point, Hashem himself appeared to Yosef and told him that if he sinned the world would have to be destroyed. Only then did Yosef finally turn and run away.
The question is asked: What Jew would continue to sin even if he saw the face of Yaakov and Rachel? And what human being - even a non-Jew - would continue to sin even after Hashem Himself came and said that the world will be destroyed if you sin?? So why did this test lift him to the high level of Tzadik Yesod Olam? Anyone would have refrained in such a situation!
And here is the answer that he said over from Tzadikim, which I found truly beautiful:
The entire year prior to that fateful day, the wife of Potifar had tried to seduce Yosef every day. Chazal say that she encompassed the powers of seductions of all women of all times. She was the greatest "Klippah" of this temptation, and Yosef had used his own righteousness and free-choice to avoid her for a full year. He had already done all he could do with his own free will, but his madrega wasn't "complete" because there was no way for him to avoid the feeling deep down of "I was able to overcome this great test"... As great a Tzadik as Yosef was, there was no way for him to not have a slight feeling of "Kochi Ve'Otzem Yadi" in his overcoming these great tests. Therefore, his great Medrega was still missing the true recognition that everything he had achieved was really from Hashem. In order to bring Yosef to the madrega of Tzadik Yesod Olam, he had to come to a complete recognition of this reality. And that was the purpose of this final test. Yosef was shown clearly that he could NOT withstand this last time without clear divine intervention. He saw that he indeed would have fallen had Hashem not stopped him. And with this recognition, Yosef was able to achieve the "Bitul" that completed his Madrega, and through this he reached the high level of Tzadik Yesod Olam.
But there's more...
The shiur went on to explain how this is the bechina of Moshiach ben Yosef. However, the Bechina of Moshiach ben Dovid required an even deeper internalization of this recognition, and that is why David Hamelech was actually brought to sin by Heaven. In order to reach the level needed for Moshiach ben David, he needed an even deeper recognition of this reality, and therefore he wasn't saved from the sin like Yosef was. He was brought to fall, and his Tikkun was accepting that it was brought about by Heaven. His struggle and test were not with the sin itself. That was preordained by Hashem. His test was what he would do AFTER the fall. David retained his faith and didn't fall into Yiush - even though the shechinah left him for 20 years afterwards! That was David's test - and his greatness.
The speaker then quoted from Rav Yechezkel Levinson, the Mashgiach of Ponovitch, who wrote in his sichos Mussar for Elul that everything that happens to us, including our sins, are really from Hashem. So Rav Yechezkel asks, if this is the case, what is asked of us? And he answers something amazing . I couldn't believe it when I heard it. He said that what is asked of us is only to come to the recognition that our falls are from Hashem. As long as we feel WE did the sin, we are far from our Tikkun. Our Teshuvah is nothing more and nothing less than coming to the true recognition that Hashem brought us to fall and not to give up. At that point we can have a true Teffilah to Hashem to take us back. The new "Retzonos" that we develop as a result of our fall, that is the real Teshuvah.
It is for this reason that all new spiritual levels are preceded by a fall. We need a Yeridah Letzorech Aliyah to help us renew our "Retzonos" and yearning. Hashem doesn't need our great deeds and great strength in "overcoming tests". Instead, he wants our "Ratzon". And it often takes a fall for us to have a renewal of our spiritual "retzonos". When we are able to recognize that our climbs - and even our falls come from Hashem, we can rekindle our yearnings and cry out from the heart: "Father, take me back. I want You and nothing else!".