def create_session(config): session = Session(config) session.add_authenticator( create_authenticator( "cryptosign", authid="foo", authrole="role0", privkey="a" * 64, )) def joined(session, details): print("joined: {} {}".format(session, details)) session.config.extra['running'].callback(session) session.on('join', joined) def left(session, details): if "no_such_procedure" in str(details.reason): session.config.extra['running'].errback(Exception(details.reason)) session.on('leave', left) def disconnected(*args, **kw): print("disconnect: {} {}".format(args, kw)) session.on('disconnect', disconnected) return session
def test_two_authenticators(self): session = Session(mock.Mock()) class TestAuthenticator(IAuthenticator): name = "test" def on_challenge(self, session, challenge): raise NotImplementedError def on_welcome(self, authextra): raise NotImplementedError auth0 = TestAuthenticator() auth0.authextra = { "foo": "value0", "bar": "value1", } auth0._args = {} auth1 = TestAuthenticator() auth1.authextra = { "bar": "value1", "qux": "what", } auth1._args = {} session.add_authenticator(auth0) session.add_authenticator(auth1)
def test_inconsistent_authextra(self): session = Session(mock.Mock()) class TestAuthenticator(IAuthenticator): name = "test" def on_challenge(self, session, challenge): raise NotImplementedError def on_welcome(self, authextra): raise NotImplementedError auth0 = TestAuthenticator() auth0.authextra = { "foo": "value0", "bar": "value1", } auth0._args = {} auth1 = TestAuthenticator() auth1.authextra = { "foo": "value1", } auth1._args = {} session.add_authenticator(auth0) with self.assertRaises(ValueError) as ctx: session.add_authenticator(auth1) self.assertIn("Inconsistent authextra", str(ctx.exception))
def test_inconsistent_authids(self): session = Session(mock.Mock()) auth0 = create_authenticator( "wampcra", authid="alice", secret="p4ssw0rd", ) auth1 = create_authenticator( "wampcra", authid="bob", secret="password42", ) session.add_authenticator(auth0) with self.assertRaises(ValueError) as ctx: session.add_authenticator(auth1) assert "authids" in str(ctx.exception)
def make_session(config): @coroutine def on_join(session, details): print("on_join: {}".format(details)) def add2(a, b): return a + b yield session.register(add2, 'com.example.add2') try: res = yield session.call('com.example.add2', 2, 3) print("result: {}".format(res)) except Exception as e: print("error: {}".format(e)) finally: session.leave() session = Session(config=config) session.on('join', on_join) return session