def get_avatar_id(self, creds): # The ConfigContext(username=...) passed into .get_config() is to # allow sub-classes to change how config.vumi_username and # config.vumi_password are looked up by overriding .get_config(). if credentials.IAnonymous.providedBy(creds): config = yield self.get_config(None, ConfigContext(username=None)) # allow anonymous authentication if no username is configured if config.vumi_username is None: returnValue(None) elif credentials.IUsernamePassword.providedBy(creds): username, password = creds.username, creds.password config = yield self.get_config(None, ConfigContext(username=username)) if (username == config.vumi_username and password == config.vumi_password): returnValue(username) raise error.UnauthorizedLogin()
def test_get_config_for_username(self): app = yield self.app_helper.get_app_worker(self.APP_CONFIG) conv = yield self.app_helper.create_conversation( config=self.CONV_CONFIG, started=True) ctxt = ConfigContext( username="******" % (conv.user_account.key, conv.key)) config = yield app.get_config(None, ctxt=ctxt) self.assertTrue(isinstance(config, RapidSMSConfig)) self.assertEqual(config.vumi_username, self._username_for_conv(conv))
def test_disabling_concurrency_limit(self): conv_resource = StreamingConversationResource(self.app, self.conversation.key) # negative concurrency limit disables it ctxt = ConfigContext(user_account=self.conversation.user_account.key, concurrency_limit=-1) config = yield self.app.get_config(msg=None, ctxt=ctxt) self.assertTrue( (yield conv_resource.is_allowed(config, self.conversation.user_account.key)))
def handle_raw_outbound_message(self, request): config = yield self.get_config( None, ConfigContext(username=request.getUser())) data = json.loads(request.content.read()) content = data['content'] to_addrs = data['to_addr'] if not isinstance(to_addrs, list): raise BadRequestError( "Supplied `to_addr` (%r) was not a list." % (to_addrs,)) in_reply_to = data.get('in_reply_to') endpoint = data.get('endpoint') if in_reply_to is not None: msgs = yield self._handle_reply_to(config, content, to_addrs, in_reply_to) else: msgs = yield self._handle_send_to(config, content, to_addrs, endpoint) returnValue(msgs)
def test_send_rapidsms_nonreply(self): app = yield self.app_helper.get_app_worker(self.APP_CONFIG) conv = yield self.app_helper.create_conversation( config=self.CONV_CONFIG, started=True) ctxt = ConfigContext( username="******" % (conv.user_account.key, conv.key)) config = yield app.get_config(None, ctxt=ctxt) yield app.send_rapidsms_nonreply( "to:123", "Hello!", endpoint="default", config=config) [msg] = self.app_helper.get_dispatched_outbound() self.assertEqual(msg["content"], "Hello!") self.assertEqual(msg["to_addr"], "to:123") self.assertEqual(msg.get_routing_endpoint(), "default") self.assertEqual(msg["helper_metadata"], { u'go': { u'conversation_type': "rapidsms", u'user_account': conv.user_account.key, u'conversation_key': conv.key, } })
def test_get_config_for_badly_formatted_username(self): app = yield self.app_helper.get_app_worker(self.APP_CONFIG) ctxt = ConfigContext(username="******") d = app.get_config(None, ctxt=ctxt) self.assertFailure(d, ValueError)
def get_worker_config(self, user_account_key): ctxt = ConfigContext(user_account=user_account_key) return self.worker.get_config(msg=None, ctxt=ctxt)