Esempio n. 1
0
def test_get_tenants():
    config = Mock(auth={
        "url": "http://sample-url",
        "timeout_sleep": 1,
        "connection_retries": 3
    },
                  dojot={
                      "management": {
                          "user": "******",
                          "tenant": "sample-tenant"
                      }
                  })
    mock_self = Mock(get_management_token=Mock(return_value="123"),
                     config=config)

    patch_http_perform = patch("dojot.module.HttpRequester.do_it",
                               return_value={"tenants": "admin"})
    with patch_http_perform as mock_http_perform:
        tenants = Auth.get_tenants(mock_self)
        mock_http_perform.assert_called_with("http://sample-url/admin/tenants",
                                             "123", 3, 1)
        assert tenants == "admin"
    patch_http_perform = patch("dojot.module.HttpRequester.do_it",
                               return_value=None)
    with patch_http_perform as mock_http_perform:
        tenants = Auth.get_tenants(mock_self)
        mock_http_perform.assert_called_with("http://sample-url/admin/tenants",
                                             "123", 3, 1)
        assert tenants is None
Esempio n. 2
0
def test_get_tenants_fail():
    config = Mock(keycloak={
        "timeout_sleep": 1,
        "connection_retries": 3,
        "base_path": "http://kc_base_path/",
        "tenants_endpoint": "keycloak_tenants_endpoint/",
        "ignore_realm": "master",
        "credentials": {
            "username": "******",
            "password": "******",
            "client_id": "admin-cli",
            "grant_type": "password",
        }
    },
                  dojot={
                      "management": {
                          "user": "******",
                          "tenant": "sample-tenant"
                      }
                  })
    mock_self = Mock(get_management_token=Mock(
        side_effect=requests.exceptions.ConnectionError()),
                     config=config)

    tenants = Auth.get_tenants(mock_self)
    assert tenants is None
Esempio n. 3
0
def main():
    """
    Main, inits mongo, messenger, create channels read channels for device
    and device-data topics and add callbacks to events related to that subjects
    """
    config = Config()
    auth = Auth(config)
    LOGGER.debug("Initializing persister...")
    persister = Persister()
    persister.init_mongodb()
    persister.create_indexes_for_notifications(auth.get_tenants())
    LOGGER.debug("... persister was successfully initialized.")
    LOGGER.debug("Initializing dojot messenger...")
    messenger = Messenger("Persister", config)
    messenger.init()
    messenger.create_channel(config.dojot['subjects']['devices'], "r")
    messenger.create_channel(config.dojot['subjects']['device_data'], "r")
    # TODO: add notifications to config on dojot-module-python
    messenger.create_channel("dojot.notifications", "r")
    messenger.on(config.dojot['subjects']['devices'], "message",
                 persister.handle_event_devices)
    messenger.on(config.dojot['subjects']['device_data'], "message",
                 persister.handle_event_data)
    messenger.on(config.dojot['subjects']['tenancy'], "message",
                 persister.handle_new_tenant)
    messenger.on("dojot.notifications", "message",
                 persister.handle_notification)
    LOGGER.debug("... dojot messenger was successfully initialized.")
Esempio n. 4
0
def main():
    """
    Main, inits mongo, messenger, create channels read channels for device
    and device-data topics and add callbacks to events related to that subjects
    """
    config = Config()
    auth = Auth(config)
    LOGGER.debug("Initializing persister...")
    persister = Persister()
    persister.init_mongodb()
    persister.create_indexes_for_notifications(auth.get_tenants())
    LOGGER.debug("... persister was successfully initialized.")
    LOGGER.debug("Initializing dojot messenger...")

    start_dojot_messenger(config, persister,
                          conf.dojot_persist_notifications_only)
    LOGGER.debug("... dojot messenger was successfully initialized.")

    # Create falcon app
    app = falcon.API()
    app.add_route('/persister/log', LoggingInterface())
    httpd = simple_server.make_server('0.0.0.0',
                                      os.environ.get("PERSISTER_PORT", 8057),
                                      app)
    httpd.serve_forever()
Esempio n. 5
0
def test_get_tenants():
    config = Mock(keycloak={
        "timeout_sleep": 1,
        "connection_retries": 3,
        "base_path": "http://kc_base_path/",
        "ignore_realm": "master",
        "credentials": {
            "username": "******",
            "password": "******",
            "client_id": "admin-cli",
            "grant_type": "password",
        }
    },
                  dojot={
                      "management": {
                          "user": "******",
                          "tenant": "sample-tenant"
                      }
                  })
    mock_self = Mock(get_management_token=Mock(return_value="123"),
                     config=config)

    master_tenant = '[{"realm": "master"}, {"realm": "admin"}]'
    patch_http_perform = patch("dojot.module.HttpRequester.do_it",
                               return_value=json.loads(master_tenant))
    with patch_http_perform as mock_http_perform:
        tenants = Auth.get_tenants(mock_self)
        mock_http_perform.assert_called_with(
            "http://kc_base_path/admin/realms", "123", 3, 1)
        assert tenants[0] == "admin"
    patch_http_perform = patch("dojot.module.HttpRequester.do_it",
                               return_value=None)
    with patch_http_perform as mock_http_perform:
        tenants = Auth.get_tenants(mock_self)
        mock_http_perform.assert_called_with(
            "http://kc_base_path/admin/realms", "123", 3, 1)
        assert tenants is None
Esempio n. 6
0
def main():
    """
    Main, inits mongo, messenger, create channels read channels for device
    and device-data topics and add callbacks to events related to that subjects
    """
    config = Config()
    auth = Auth(config)
    LOGGER.debug("Initializing persister...")
    persister = Persister()
    persister.init_mongodb()
    persister.create_indexes_for_notifications(auth.get_tenants())
    LOGGER.debug("... persister was successfully initialized.")
    LOGGER.debug("Initializing dojot messenger...")
    messenger = Messenger("Persister", config)
    messenger.init()
    messenger.create_channel(config.dojot['subjects']['devices'], "r")
    messenger.create_channel(config.dojot['subjects']['device_data'], "r")
    # TODO: add notifications to config on dojot-module-python
    messenger.create_channel("dojot.notifications", "r")
    messenger.on(config.dojot['subjects']['devices'], "message",
                 persister.handle_event_devices)
    messenger.on(config.dojot['subjects']['device_data'], "message",
                 persister.handle_event_data)
    messenger.on(config.dojot['subjects']['tenancy'], "message",
                 persister.handle_new_tenant)
    messenger.on("dojot.notifications", "message",
                 persister.handle_notification)
    LOGGER.debug("... dojot messenger was successfully initialized.")

    # Create falcon app
    app = falcon.API()
    app.add_route('/persister/log', LoggingInterface())
    httpd = simple_server.make_server('0.0.0.0',
                                      os.environ.get("PERSISTER_PORT", 8057),
                                      app)
    httpd.serve_forever()