Esempio n. 1
0
    def test_send_xmpp_message(self):
        # mock out logging
        flexmock(logging)
        logging.should_receive('info').and_return()

        # set up a fake xmpp message
        fake_xmpp_message = flexmock(name='xmpp_message')

        # and slip in our mocked message
        flexmock(xmpppy)
        flexmock(xmpppy.protocol)
        xmpppy.protocol.should_receive('Message').with_args(frm=self.my_jid,
          to=re.compile('one|two'), body=self.message, typ=self.message_type) \
          .and_return(fake_xmpp_message)

        # set up a fake xmpp client
        fake_xmpp_client = flexmock(name='xmpp')
        fake_xmpp_client.should_receive('connect').with_args(secure=False) \
          .and_return()
        fake_xmpp_client.should_receive('auth').with_args(
            self.appid, self.uasecret, resource='').and_return()
        fake_xmpp_client.should_receive('send').with_args(
            fake_xmpp_message).and_return()

        # and slip in our mocked xmpp client in lieu of the real one
        xmpppy.should_receive('Client').with_args(self.domain, debug=[]) \
          .and_return(fake_xmpp_client)

        xmpp = xmpp_service_real.XmppService(log=logging.info,
                                             service_name='xmpp',
                                             domain=self.domain,
                                             uaserver='public-ip',
                                             uasecret=self.uasecret)

        # Set up a mocked XMPPRequest, that contains the message we want to send and
        # who we want to send it to.
        fake_request = flexmock(name='xmpp_message_request')
        fake_request.should_receive('jid_list').and_return(['one', 'two'])
        fake_request.should_receive('body').and_return(self.message)
        fake_request.should_receive('type').and_return(self.message_type)

        # _Dynamic_SendMessage will put a NO_ERROR message in the response if the
        # xmpp message was sent successfully, so just make sure the method returned
        # and that the mocked response has that status.
        fake_response = flexmock(name='xmpp_message_response')
        fake_response.should_receive('add_status') \
          .with_args(xmpp_service_pb.XmppMessageResponse.NO_ERROR).and_return()

        self.assertEquals(
            None, xmpp._Dynamic_SendMessage(fake_request, fake_response))
Esempio n. 2
0
    def test_send_channel_message(self):
        # mock out logging
        flexmock(logging)
        logging.should_receive('info').and_return()

        # set up a fake xmpp message
        fake_xmpp_message = flexmock(name='xmpp_message')

        # and slip in our mocked message
        flexmock(xmpppy)
        flexmock(xmpppy.protocol)
        xmpppy.protocol.should_receive('Message').with_args(
            frm=self.my_jid,
            to=re.compile('channel.*key@domain'),
            body=self.message,
            typ=self.message_type).and_return(fake_xmpp_message)

        # set up a fake xmpp client
        fake_xmpp_client = flexmock(name='xmpp')
        fake_xmpp_client.should_receive('connect').with_args(secure=False) \
          .and_return()
        fake_xmpp_client.should_receive('auth').with_args(
            self.appid, self.uasecret, resource='').and_return()
        fake_xmpp_client.should_receive('send').with_args(
            fake_xmpp_message).and_return()

        # and slip in our mocked xmpp client in lieu of the real one
        xmpppy.should_receive('Client').with_args(self.domain, debug=[]) \
          .and_return(fake_xmpp_client)

        xmpp = xmpp_service_real.XmppService(log=logging.info,
                                             service_name='xmpp',
                                             domain=self.domain,
                                             uaserver='public-ip',
                                             uasecret=self.uasecret)

        # Set up a mocked XMPPRequest, that contains the message we want to send and
        # who we want to send it to.
        fake_request = flexmock(name='xmpp_message_request')
        fake_request.should_receive('message').and_return(self.message)
        fake_request.should_receive('application_key').and_return('key')

        # Unlike _Dynamic_SendMessage, the channel version doesn't set the NO_ERROR
        # status, and it doesn't actually return anything, so just make sure that
        # no exceptions were thrown.
        self.assertEquals(None,
                          xmpp._Dynamic_SendChannelMessage(fake_request, None))
Esempio n. 3
0
def setup_stubs(
    request_data,
    app_id,
    application_root,
    trusted,
    blobstore_path,
    datastore_consistency,
    datastore_path,
    datastore_require_indexes,
    datastore_auto_id_policy,
    images_host_prefix,
    logs_path,
    mail_smtp_host,
    mail_smtp_port,
    mail_smtp_user,
    mail_smtp_password,
    mail_enable_sendmail,
    mail_show_mail_body,
    matcher_prospective_search_path,
    search_index_path,
    taskqueue_auto_run_tasks,
    taskqueue_default_http_server,
    uaserver_path,
    user_login_url,
    user_logout_url,
    xmpp_path):
  """Configures the APIs hosted by this server.

  Args:
    request_data: An apiproxy_stub.RequestInformation instance used by the
        stubs to lookup information about the request associated with an API
        call.
    app_id: The str application id e.g. "guestbook".
    application_root: The path to the directory containing the user's
        application e.g. "/home/joe/myapp".
    trusted: A bool indicating if privileged APIs should be made available.
    blobstore_path: The path to the file that should be used for blobstore
        storage.
    datastore_consistency: The datastore_stub_util.BaseConsistencyPolicy to
        use as the datastore consistency policy.
    datastore_path: The path to the file that should be used for datastore
        storage.
    datastore_require_indexes: A bool indicating if the same production
        datastore indexes requirements should be enforced i.e. if True then
        a google.appengine.ext.db.NeedIndexError will be be raised if a query
        is executed without the required indexes.
    datastore_auto_id_policy: The type of sequence from which the datastore
        stub assigns auto IDs, either datastore_stub_util.SEQUENTIAL or
        datastore_stub_util.SCATTERED.
    images_host_prefix: The URL prefix (protocol://host:port) to prepend to
        image urls on calls to images.GetUrlBase.
    logs_path: Path to the file to store the logs data in.
    mail_smtp_host: The SMTP hostname that should be used when sending e-mails.
        If None then the mail_enable_sendmail argument is considered.
    mail_smtp_port: The SMTP port number that should be used when sending
        e-mails. If this value is None then mail_smtp_host must also be None.
    mail_smtp_user: The username to use when authenticating with the
        SMTP server. This value may be None if mail_smtp_host is also None or if
        the SMTP server does not require authentication.
    mail_smtp_password: The password to use when authenticating with the
        SMTP server. This value may be None if mail_smtp_host or mail_smtp_user
        is also None.
    mail_enable_sendmail: A bool indicating if sendmail should be used when
        sending e-mails. This argument is ignored if mail_smtp_host is not None.
    mail_show_mail_body: A bool indicating whether the body of sent e-mails
        should be written to the logs.
    matcher_prospective_search_path: The path to the file that should be used to
        save prospective search subscriptions.
    search_index_path: The path to the file that should be used for search index
        storage.
    taskqueue_auto_run_tasks: A bool indicating whether taskqueue tasks should
        be run automatically or it the must be manually triggered.
    taskqueue_default_http_server: A str containing the address of the http
        server that should be used to execute tasks.
    uaserver_path: (AppScale-specific) A str containing the FQDN or IP address
        of the machine that runs a UserAppServer.
    user_login_url: A str containing the url that should be used for user login.
    user_logout_url: A str containing the url that should be used for user
        logout.
    xmpp_path: (AppScale-specific) A str containing the FQDN or IP address of
        the machine that runs ejabberd, where XMPP clients should connect to.
  """

  apiproxy_stub_map.apiproxy.RegisterStub(
      'app_identity_service',
      app_identity_stub.AppIdentityServiceStub())

  blob_storage = datastore_blob_storage.DatastoreBlobStorage(app_id)
  apiproxy_stub_map.apiproxy.RegisterStub(
      'blobstore',
      blobstore_stub.BlobstoreServiceStub(blob_storage,
                                          request_data=request_data))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'capability_service',
      capability_stub.CapabilityServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'channel',
      channel_service_stub.ChannelServiceStub(request_data=request_data))

  datastore = datastore_distributed.DatastoreDistributed(
      app_id, datastore_path, require_indexes=datastore_require_indexes,
      trusted=trusted, root_path=application_root)

  apiproxy_stub_map.apiproxy.ReplaceStub(
      'datastore_v3', datastore)

  apiproxy_stub_map.apiproxy.RegisterStub(
      'datastore_v4',
      datastore_v4_stub.DatastoreV4Stub(app_id))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'file',
      file_service_stub.FileServiceStub(blob_storage))

  serve_address = os.environ['NGINX_HOST']
  try:
    from google.appengine.api.images import images_stub
  except ImportError:

    logging.warning('Could not initialize images API; you are likely missing '
                    'the Python "PIL" module.')
    # We register a stub which throws a NotImplementedError for most RPCs.
    from google.appengine.api.images import images_not_implemented_stub
    apiproxy_stub_map.apiproxy.RegisterStub(
        'images',
        images_not_implemented_stub.ImagesNotImplementedServiceStub(
            host_prefix=images_host_prefix))
  else:
    host_prefix = 'http://{}'.format(serve_address)
    apiproxy_stub_map.apiproxy.RegisterStub(
        'images',
        images_stub.ImagesServiceStub(host_prefix=host_prefix))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'logservice',
      logservice_stub.LogServiceStub(persist=True))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'mail',
      mail_stub.MailServiceStub(mail_smtp_host,
                                mail_smtp_port,
                                mail_smtp_user,
                                mail_smtp_password,
                                enable_sendmail=mail_enable_sendmail,
                                show_mail_body=mail_show_mail_body))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'memcache',
      memcache_distributed.MemcacheService())

  apiproxy_stub_map.apiproxy.RegisterStub(
      'search',
      appscale_search_stub.SearchServiceStub(app_id=app_id))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'modules',
      modules_stub.ModulesServiceStub(request_data))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'system',
      system_stub.SystemServiceStub(request_data=request_data))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'taskqueue',
      taskqueue_distributed.TaskQueueServiceStub(app_id, serve_address))

  urlmatchers_to_fetch_functions = []
  urlmatchers_to_fetch_functions.extend(
      gcs_dispatcher.URLMATCHERS_TO_FETCH_FUNCTIONS)
  apiproxy_stub_map.apiproxy.RegisterStub(
      'urlfetch',
      urlfetch_stub.URLFetchServiceStub(
          urlmatchers_to_fetch_functions=urlmatchers_to_fetch_functions))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'user',
      user_service_stub.UserServiceStub(login_url=user_login_url,
                                        logout_url=user_logout_url,
                                        request_data=request_data))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'xmpp',
      xmpp_service_real.XmppService(domain=xmpp_path, uaserver=uaserver_path))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'matcher',
      prospective_search_stub.ProspectiveSearchStub(
          matcher_prospective_search_path,
          apiproxy_stub_map.apiproxy.GetStub('taskqueue')))

  apiproxy_stub_map.apiproxy.RegisterStub(
      'remote_socket',
      _remote_socket_stub.RemoteSocketServiceStub())