Signs That Allah Is Testing You
Question

If a calamity befalls a Muslim, how can we know whether it is a punishment for his sins or a test to raise him in status?
Praise be to Allah.According to the Quran and Sunnah, there are two direct causes for calamities and trials , in addition to the wisdom of Allah, may He be exalted, in what He wills and decrees. 

Sins and acts of disobedience that the person has committed, whether they constitute kufr (disbelief) or are no more than regular sins or are major sins. Allah, may He be glorified and exalted, will test the one who committed them with calamity by way of requital and immediate punishment (i.e., in this world). 

Allah, may He be glorified and exalted, says (interpretation of the meaning): “…but whatever of evil befalls you, is from yourself” [An-Nisa’ 4:79]. The commentators said: i.e., because of your sin. 
And He, may He be glorified and exalted, says (interpretation of the meaning): “And whatever of misfortune befalls you, it is because of what your hands have earned. And He pardons much” [Ash-Shura 42:30]. (Tafsir al-Quran al-‘Azim, 2/363)
Anas (may Allah be pleased with him) said: The Messenger of Allah (blessings and peace of Allah be upon him) said:
“If Allah wills good for His slave, He hastens his punishment in this world, and if He wills bad for His slave, He withholds from him (the punishment for) his sin, until He requites him for it on the Day of Resurrection.” (Narrated and classed as hasan by At-Tirmidhi (2396); classed as sahih by al-Albani in Sahih At-Tirmidhi.)

That Allah, may He be exalted, wants to raise in status the patient believer, so He tests him with calamity so that he will bear it with acceptance and patience , then He will be given the reward of those who are patient in the Hereafter, and will be recorded with Allah as one of the victorious. Calamities befell the prophets and the righteous persistently, and Allah, may He be exalted, made it an honour for them by means of which they attained high status in Paradise. Hence it says in the sahih hadith from the Prophet (blessings and peace of Allah be upon him): “If a certain status has previously been decreed by Allah for a person, and he does not attain it by his deeds, Allah afflicts him in his body or wealth or children.” (Narrated by Abu Dawud, 3090; classed as sahih by Al-Albani in As-Silsilah As-Sahihah, no. 2599.) 

It was narrated from Anas ibn Malik (may Allah be pleased with him) that the Prophet (blessings and peace of Allah be upon him) said: “Great reward comes with great trials. When Allah loves a people, He tests them, and whoever accepts it attains His pleasure, whereas whoever shows discontent with it incurs His wrath.” (Narrated and classed as hasan by At-Tirmidhi (2396); classed as sahih by Al-Albani in As-Silsilah As-Sahihah, no. 146 )
The two reasons are mentioned together in the hadith of `Aishah (may Allah be pleased with her), according to which the Prophet (blessings and peace of Allah be upon him) said: “Nothing befalls a believer , a (prick of a) thorn or more than that, but Allah will raise him one degree in status thereby, or erase a bad deed.” (Narrated by Al-Bukhari  (5641) and Muslim (2573) 
Moreover, in most cases these two causes exist together rather than either of them occurring on its own. 
Do you not see that if Allah tests a person with calamity because of his sin, and he bears it with patience and gratitude, Allah, may He be exalted, will forgive him his sin, raise him in status in Paradise and give him the reward of those who are patient and seek reward with Him? 
When Allah tests a person with a calamity so that he will reach the lofty status that He has decreed for him in Paradise, his previous sins are expiated and it is regarded as requital for those sins in this world, so the requital will not be repeated for him in the Hereafter. This is what happened to some of the messengers and prophets, such as Adam (peace be upon him) and Yunus (peace be upon him): Allah, may He be glorified and exalted, tested Adam with expulsion from Paradise, and He tested Yunus ibn Matta with being swallowed into the belly of the fish. But by means of these tests, Allah raised them in status because of their patience and seeking reward with Him, may He be exalted. It was expiation for what each of them had done of going against His command (blessings and peace of Allah be upon both of them). 
This proves that requital in this world is not separate from the requital in the Hereafter. These two reasons are mentioned together in many sahih Prophetic hadiths, such as that which was narrated by Sa‘d ibn Abi Waqqas (may Allah be pleased with him) who said: I said: O Messenger of Allah, who among the people are most sorely tested? He said: “The Prophets, then the next best and the next best. A man will be tested according to his level of religious commitment. If his religious commitment is solid, his test will be more severe, but if there is any weakness in his religious commitment, he will be tested according to his level of religious commitment. And calamity will continue to befall a person until he walks on the earth with no sin on him.” (Narrated by At-Tirmidhi (2398) who said: (It is) hasan sahih) 
Nevertheless, one of these two reasons may be more apparent in some kinds of tests than the other. That may be understood by looking at the circumstances of the calamity. 
If the one who is affected is a disbeliever, then his calamity cannot be to raise him in status, because the disbeliever will have no weight before Allah on the Day of Resurrection. But there may be a lesson and a reminder for others in that, not to do what he did. Or it may be an immediate punishment for him in this world, in addition to what is stored up for him in the Hereafter. Allah, may He be exalted, says (interpretation of the meaning):
“Is then He (Allah) Who takes charge (guards, maintains, provides, etc.) of every person and knows all that he has earned (like any other deities who know nothing)? Yet they ascribe partners to Allah. Say: "Name them! Is it that you will inform Him of something He knows not in the earth or is it (just) a show of false words." Nay! To those who disbelieve, their plotting is made fair seeming, and they have been hindered from the Right Path, and whom Allah sends astray, for him, there is no guide.
For them is a torment in the life of this world, and certainly, harder is the torment of the Hereafter. And they have no protector against Allah” [Ar-Ra‘d 13:33-34].
But if the one who is affected is a Muslim who commits sin openly or is a blatant evildoer, then it is most likely that he is being requited and punished with this calamity, because expiation of sins comes before raising in status, and the sinner is in greater need of expiation for his sins than of being raised in status. 
On the other hand, if the Muslim is a devoted worshipper, obedient and righteous, and there is nothing between him and Allah but true ‘ubudiyyah (servitude), gratitude, praise, repentance and submission to Him, may He be glorified, then it is most likely that this calamity is a kind of honour and raising in status, and people are the witnesses of Allah on earth. If they know him to be righteous, then they may give him the glad tidings of raised status before Allah if he is patient in bearing the calamity. 
But if the one who is affected shows displeasure and panic, then it cannot be thought that his calamity is an honour from Allah to raise him in status, because Allah, may He be glorified, knew that he would not be patient and accept it with contentment. So in this case it is most likely that it is a requital and punishment. 
One of the righteous said: 
“The sign of calamity by way of punishment and requital is lack of patience when a calamity befalls, and panic and complaining to people. And the sign of calamity by way of expiation and erasing of sins is beautiful patience without complaining, panic or impatience, and lack of reluctance in fulfilling commands and doing acts of worship.
 The sign of calamity by way of raising a person in status is contentment and acceptance, peace of mind, and patience in accepting the will of Allah until relief comes.”
All of this is merely circumstantial evidence that one may ponder in order to learn something of the wisdom of Allah , may He be exalted, that is behind calamities and trials. But he cannot be certain as to which is the case with regard to himself or other people who are afflicted with calamities. 
Perhaps it is more important than all of these details to note that the practical benefit that one should think about is that every calamity and test is good for him and will bring reward if he is patient and seeks reward with Allah, and that every test and calamity is bad for him if he panics and gets angry. If he trains himself to bear calamities with patience and to be content with the decree of Allah, after that it will not matter if he knows the reason for the calamity or not. Rather it is always better for him to concern himself with his sins and shortcomings and to look for some mistake that he has committed, for all of us make mistakes. Who among us has not been negligent in his duties towards Allah, may He be exalted? If Allah, may He be glorified and exalted, caused the Muslims to suffer the calamity of many of them being killed on the day of Uhud, even though they were the Companions of the Prophet (blessings and peace of Allah be upon him) and the best of mankind after the messengers and prophets, because they had gone against the command of the Prophet (blessings and peace of Allah be upon him), then how can anyone think after that that he is entitled to be raised in status for anything that befalls him? When Ibrahim ibn Adham (may Allah have mercy on him) saw strong winds and clouds building up in the sky, he would say: This is because of my sins; if I were to depart from among you, it would not befall you. 
So how about us with all our shortcomings and sins? 
And even more important than all of that is that one should always think positively of one’s Lord, in all situations, for whatever comes from Allah, may He be glorified and exalted, is good and He is the Protector of the pious and Oft-Forgiving. 
We ask Allah, may He be exalted, to have mercy on us and forgive us, to teach us that which will benefit us and reward us for our calamities, for He is All-Hearing and answers prayers. 
And Allah knows best.