@@ -57,19 +57,25 @@ def session_ids(client):
57
57
58
58
59
59
class TestSession (IntegrationTest ):
60
+
61
+ @classmethod
60
62
@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 ()
63
68
64
69
# 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 ()
66
71
monitoring ._SENSITIVE_COMMANDS .clear ()
67
72
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 ()
71
77
72
- def _test_ops (self , client , * ops , ** kwargs ):
78
+ def _test_ops (self , client , * ops ):
73
79
listener = client .event_listeners ()[0 ][0 ]
74
80
75
81
for f , args , kw in ops :
@@ -103,6 +109,18 @@ def _test_ops(self, client, *ops, **kwargs):
103
109
with self .assertRaisesRegex (InvalidOperation , "ended session" ):
104
110
f (* args , ** kw )
105
111
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
+
106
124
# No explicit session.
107
125
for f , args , kw in ops :
108
126
listener .results .clear ()
@@ -980,8 +998,8 @@ def test_explicit_session_logout(self):
980
998
client .admin .logout ()
981
999
db .authenticate ('second-user' , 'pass' )
982
1000
983
- err = 'session was used after authenticating with different' \
984
- ' credentials'
1001
+ err = ( 'Cannot use session after authenticating with different'
1002
+ ' credentials' )
985
1003
986
1004
with self .assertRaisesRegex (InvalidOperation , err ):
987
1005
# Auth has changed between find and getMore.
0 commit comments