@@ -57,19 +57,25 @@ def session_ids(client):
5757
5858
5959class TestSession (IntegrationTest ):
60+
61+ @classmethod
6062 @client_context .require_sessions
61- def setUp (self ):
62- super (TestSession , self ).setUp ()
63+ def setUpClass (cls ):
64+ super (TestSession , cls ).setUpClass ()
65+ # Create a second client so we can make sure clients cannot share
66+ # sessions.
67+ cls .client2 = rs_or_single_client ()
6368
6469 # Redact no commands, so we can test user-admin commands have "lsid".
65- self .sensitive_commands = monitoring ._SENSITIVE_COMMANDS .copy ()
70+ cls .sensitive_commands = monitoring ._SENSITIVE_COMMANDS .copy ()
6671 monitoring ._SENSITIVE_COMMANDS .clear ()
6772
68- def tearDown (self ):
69- monitoring ._SENSITIVE_COMMANDS .update (self .sensitive_commands )
70- super (TestSession , self ).tearDown ()
73+ @classmethod
74+ def tearDownClass (cls ):
75+ monitoring ._SENSITIVE_COMMANDS .update (cls .sensitive_commands )
76+ super (TestSession , cls ).tearDownClass ()
7177
72- def _test_ops (self , client , * ops , ** kwargs ):
78+ def _test_ops (self , client , * ops ):
7379 listener = client .event_listeners ()[0 ][0 ]
7480
7581 for f , args , kw in ops :
@@ -103,6 +109,18 @@ def _test_ops(self, client, *ops, **kwargs):
103109 with self .assertRaisesRegex (InvalidOperation , "ended session" ):
104110 f (* args , ** kw )
105111
112+ # Test a session cannot be used on another client.
113+ with self .client2 .start_session () as s :
114+ # In case "f" modifies its inputs.
115+ args = copy .copy (args )
116+ kw = copy .copy (kw )
117+ kw ['session' ] = s
118+ with self .assertRaisesRegex (
119+ InvalidOperation ,
120+ 'Can only use session with the MongoClient'
121+ ' that started it' ):
122+ f (* args , ** kw )
123+
106124 # No explicit session.
107125 for f , args , kw in ops :
108126 listener .results .clear ()
@@ -980,8 +998,8 @@ def test_explicit_session_logout(self):
980998 client .admin .logout ()
981999 db .authenticate ('second-user' , 'pass' )
9821000
983- err = 'session was used after authenticating with different' \
984- ' credentials'
1001+ err = ( 'Cannot use session after authenticating with different'
1002+ ' credentials' )
9851003
9861004 with self .assertRaisesRegex (InvalidOperation , err ):
9871005 # Auth has changed between find and getMore.
0 commit comments