Good question.
From 1917 until May 1941 Germany and the USSR had a very unusal relationship and for every entent and purpose did work together, economically and politically. They did join together to plan and conquer Poland.
Germans believed that the USSR was an example of German espionage. In 1917 the Germans gave Lenin 5 million marks to topple the Russian Government and install himself as it's leader.
In 1927 Germany began trading with the USSR for many reasons, but the German influence in the USSR was substantial. As German took steps to rebuild there military it was the USSR that helped them do this. Germany and the USSR became very strong trading partners, being how both countries were being ignored by the west.
USSR and Japan, (Japan was considered Part of the Alliance in WW1) Japan began it's empire building when the British gained their alliance in removing the German presence from the Pacific. This later brought them into conflict with the Russians. Having over estimated their abilities and underestimated the Russians they quickly came to there senses and sought easiers pickings.
By 1936 the USSR was making the tanks & planes that armed the Nazi war machine so quickly. The assistance the USSR provided Nazi German was critical to Germanys ability to arm itself, german currency and gold were needed in the USSR to fund political expansion oversea's and purchase goods and supplies from the west as well as influnce trade with the west.
To assume that Hitler, and Stalin did not have their own plans is faulty, Hitler had his alliance with the USSR Molotov-Ribbentrop Pact and his alliance with Japan, one was for the conquest of poland the other for the conquest of the USSR.
Spain was a free for all, almost everyone sent people there, the and often on both sides. Spain sent a Spainish Division of troops with the Germans fighting the USSR up until 1943.
The USSR fought the Finns who were Germanys ally.
I do not think that Stalin thought Hitler an ally, I do think they both thought that they were manoveouring one another.