I wouldn't say Nazi Germany and the USSR were on friendly terms, but like most other countries they had pragmatic relations and some trade. You have to remember that German business were present abroad and foreign in Germany, like Ford.
Fascism itself is the reaction to communism. It is a last line of defense against workers defending their interests, when all other soft liberal "democratic" methods have been exhausted. Nazis were a last resort of German big business to survive in a post-Versailles redistributed markets hyper inflation internal turmoil environment. So called "jewish" Bolshevism was the main enemy, and communists and fascists frequently engaged in street battles in Weimar times. When the nazis got to power communists were put in prison. Thälmann as early as 1933. In 1936 you had the anticomintern pact. And while Germany supported Franco, the USSR supported the communist part of Republican Spain. Dimitrov of the Comintern outlined a strategy to combat fascism in 35. Nazi Germany and the USSR were not on friendly terms, but they tolerated each other as diplomatic counterparts as both were part of the world as it was. When Germany was "granted" parts of CZ by Chamberlain, the Soviets were not even asked for an opinion, despite it being a neighbouring country. The USSR attempted to align itself with the western powers against the nazis many times before 1939, but when all options had been exhausted they had no option but to sign a non-aggression pact with the Germans as a last resort. We have to remember that this was a time before the war when people like Churchill praised "great men" like Mussolini for their anti-bolshevism (and whose nations had sent troops to combat the Bolsheviks during the civil war, Churchill included).
When I write Stalin, I actually mean the governing body of the USSR. It's like saying Jobs did this and that, but in essence they are simply symbols of a much bigger apparatus. If they succeed they get most of the praise, but their heads are on the line if things go bad. Stalin personally did not get his way many times, but the embodiment of soviet power in Stalin - always.