Your question really has two parts to it, and I will deal with each separately.
First, why did Job have to go through the trials that he suffered?
It is important to understand that chapters one and two of Job do NOT answer this question. Satan’s motive for putting Job through the trials is clear.
“But put forth thine hand now, and touch all that he hath, and he will curse thee to thy face.” (Job 1:11)
“But put forth thine hand now, and touch his bone and his flesh, and he will curse thee to thy face.” (Job 2:5)
The devil does not succeed in his goal. While the Lord actually brings up Job to begin with, He does not tell Satan His motives for doing so. Therefore, God’s purpose (and thus the only one we are concerned with) for putting Job through the trials is not explained here. Although the Lord never explains why He did allow this to take place to Job, Job learned the intended lesson.
Job’s problem, although he was perfect (Job 1:1, 8; 2:3), is that his perfection is nothing before a holy and righteous God (Is 64:6). He had not been able to realize this before and the Lord needed to teach him this lesson. Job’s attitude (the good and the bad) is summed up in one of the key verses in the book.
“Though he slay me, yet will I trust in him: but I will maintain mine own ways before him.” (Job 13:15)
And in spite of the vain and misguided efforts of his “friends” to counsel Job and point out his nonexistent sin, the Lord’s rebuke of Job in chapters 38-41 accomplishes His goal as Job learns his lesson well; a lesson his last words in the book testify.
“Wherefore I abhor myself, and repent in dust and ashes. “ (Job 42:6)
Second, who wrote the book of Job?
The internal evidence of the book would indicate Elihu as the author. There are two reasons for this conclusion. Number one, he is the only person who repeatedly refers to what he says in the first person.
“I said, Days should speak, and multitude of years should teach wisdom.” (Job 32:7)
“Therefore I said, Hearken to me; I also will shew mine opinion.” (Job 32:10)
“I said, I will answer also my part, I also will shew mine opinion.” (Job 32:17)
Second, he is the only counselor of Job that is NOT rebuked by the Lord for his words.
“And it was so, that after the LORD had spoken these words unto Job, the LORD said to Eliphaz the Temanite, My wrath is kindled against thee, and against thy two friends: for ye have not spoken of me the thing that is right, as my servant Job hath. Therefore take unto you now seven bullocks and seven rams, and go to my servant Job, and offer up for yourselves a burnt offering; and my servant Job shall pray for you: for him will I accept: lest I deal with you after your folly, in that ye have not spoken of me the thing which is right, like my servant Job. So Eliphaz the Temanite and Bildad the Shuhite and Zophar the Naamathite went, and did according as the LORD commanded them: the LORD also accepted Job.” (Job 42:7-9)
Not that a rebuked person can’t be used to write scripture (look at Jonah), but the absence of Elihu’s condemnation is not accidental by the Lord.
So, Job learned that his own righteousness could not save him (even though the Lord never expressly said that to him) and the author of the book is probably Elihu.
That I May Know Him,