Exemplo n.º 1
0
async def main():
    port = 56153
    base = URL("http://localhost").with_port(port)
    config = Config.withOverrides({
        Setting.DRIVE_AUTHORIZE_URL:
        str(base.with_path("o/oauth2/v2/auth")),
        Setting.AUTHENTICATE_URL:
        str(base.with_path("drive/authorize")),
        Setting.DRIVE_TOKEN_URL:
        str(base.with_path("token")),
        Setting.DRIVE_REFRESH_URL:
        str(base.with_path("oauth2/v4/token")),
        Setting.INGRESS_PORT:
        56152
    })
    injector = Injector(SimServerModule(config))
    server = injector.get(SimulationServer)

    # start the server
    runner = aiohttp.web.AppRunner(server.createApp())
    await runner.setup()
    site = aiohttp.web.TCPSite(runner, "0.0.0.0", port=port)
    await site.start()
    print("Server started on port " + str(port))
    print("Open a browser at http://localhost:" + str(port))
async def generate_config(server_url, ports, cleandir):
    return Config.withOverrides({
        Setting.DRIVE_URL: server_url,
        Setting.HASSIO_URL: server_url + "/",
        Setting.HOME_ASSISTANT_URL: server_url + "/core/api/",
        Setting.AUTHENTICATE_URL: server_url + "/drive/authorize",
        Setting.DRIVE_REFRESH_URL: server_url + "/oauth2/v4/token",
        Setting.DRIVE_AUTHORIZE_URL: server_url + "/o/oauth2/v2/auth",
        Setting.DRIVE_TOKEN_URL: server_url + "/token",
        Setting.REFRESH_URL: server_url + "/drive/refresh",
        Setting.ERROR_REPORT_URL: server_url + "/logerror",
        Setting.HASSIO_TOKEN: "test_header",
        Setting.SECRETS_FILE_PATH: "secrets.yaml",
        Setting.CREDENTIALS_FILE_PATH: "credentials.dat",
        Setting.FOLDER_FILE_PATH: "folder.dat",
        Setting.RETAINED_FILE_PATH: "retained.json",
        Setting.ID_FILE_PATH: "id.json",
        Setting.DATA_CACHE_FILE_PATH: "data_cache.json",
        Setting.STOP_ADDON_STATE_PATH: "stop_addon.json",
        Setting.INGRESS_TOKEN_FILE_PATH: "ingress.dat",
        Setting.DEFAULT_DRIVE_CLIENT_ID: "test_client_id",
        Setting.DEFAULT_DRIVE_CLIENT_SECRET: "test_client_secret",
        Setting.BACKUP_DIRECTORY_PATH: cleandir,
        Setting.PORT: ports.ui,
        Setting.INGRESS_PORT: ports.ingress,
        Setting.SNAPSHOT_STARTUP_DELAY_MINUTES: 0,
    })
async def generate_config(server_url: URL, ports, cleandir):
    return Config.withOverrides({
        Setting.DRIVE_URL: str(server_url),
        Setting.SUPERVISOR_URL: str(server_url) + "/",
        Setting.AUTHORIZATION_HOST: str(server_url),
        Setting.TOKEN_SERVER_HOSTS: str(server_url),
        Setting.DRIVE_REFRESH_URL: str(server_url.with_path("/oauth2/v4/token")),
        Setting.DRIVE_AUTHORIZE_URL: str(server_url.with_path("/o/oauth2/v2/auth")),
        Setting.DRIVE_TOKEN_URL: str(server_url.with_path("/token")),
        Setting.SUPERVISOR_TOKEN: "test_header",
        Setting.SECRETS_FILE_PATH: "secrets.yaml",
        Setting.CREDENTIALS_FILE_PATH: "credentials.dat",
        Setting.FOLDER_FILE_PATH: "folder.dat",
        Setting.RETAINED_FILE_PATH: "retained.json",
        Setting.ID_FILE_PATH: "id.json",
        Setting.DATA_CACHE_FILE_PATH: "data_cache.json",
        Setting.STOP_ADDON_STATE_PATH: "stop_addon.json",
        Setting.INGRESS_TOKEN_FILE_PATH: "ingress.dat",
        Setting.DEFAULT_DRIVE_CLIENT_ID: "test_client_id",
        Setting.DEFAULT_DRIVE_CLIENT_SECRET: "test_client_secret",
        Setting.BACKUP_DIRECTORY_PATH: cleandir,
        Setting.PORT: ports.ui,
        Setting.INGRESS_PORT: ports.ingress,
        Setting.BACKUP_STARTUP_DELAY_MINUTES: 0,
    })
async def main():
    port = 56153
    base = URL("http://localhost").with_port(port)
    config = Config.withOverrides({
        Setting.DRIVE_AUTHORIZE_URL:
        str(base.with_path("o/oauth2/v2/auth")),
        Setting.AUTHENTICATE_URL:
        str(base.with_path("drive/authorize")),
        Setting.DRIVE_TOKEN_URL:
        str(base.with_path("token")),
        Setting.DRIVE_REFRESH_URL:
        str(base.with_path("oauth2/v4/token"))
    })
    injector = Injector(BaseModule(config, override_dns=False))
    server = injector.get(
        ClassAssistedBuilder[SimulationServer]).build(port=port)
    await server.reset({
        'snapshot_min_size': 1024 * 1024 * 3,
        'snapshot_max_size': 1024 * 1024 * 5,
        "drive_refresh_token": "test_refresh_token",
        "drive_upload_sleep": 0,
        "snapshot_wait_time": 0,
        "hassio_header": "test_header"
    })

    # start the server
    runner = aiohttp.web.AppRunner(server.createApp())
    await runner.setup()
    site = aiohttp.web.TCPSite(runner, "0.0.0.0", port=port)
    await site.start()
    print("Server started on port " + str(port))
 def getConfig(self) -> Config:
     return Config.withOverrides({
         Setting.DRIVE_AUTHORIZE_URL:
         str(self._base_url.with_path("o/oauth2/v2/auth")),
         Setting.AUTHORIZATION_HOST:
         str(self._base_url),
         Setting.TOKEN_SERVER_HOSTS:
         str(self._base_url),
         Setting.DRIVE_TOKEN_URL:
         str(self._base_url.with_path("token")),
         Setting.DRIVE_REFRESH_URL:
         str(self._base_url.with_path("oauth2/v4/token")),
         Setting.INGRESS_PORT:
         56152
     })
async def injector(cleandir, server_url, ui_port, ingress_port):
    drive_creds = Creds(FakeTime(), "test_client_id", None,
                        "test_access_token", "test_refresh_token")
    with open(os.path.join(cleandir, "secrets.yaml"), "w") as f:
        f.write("for_unit_tests: \"password value\"\n")

    with open(os.path.join(cleandir, "credentials.dat"), "w") as f:
        f.write(json.dumps(drive_creds.serialize()))

    config = Config.withOverrides({
        Setting.DRIVE_URL: server_url,
        Setting.HASSIO_URL: server_url + "/",
        Setting.HOME_ASSISTANT_URL: server_url + "/homeassistant/api/",
        Setting.AUTHENTICATE_URL: server_url + "/drive/authorize",
        Setting.DRIVE_REFRESH_URL: server_url + "/oauth2/v4/token",
        Setting.DRIVE_AUTHORIZE_URL: server_url + "/o/oauth2/v2/auth",
        Setting.DRIVE_TOKEN_URL: server_url + "/token",
        Setting.REFRESH_URL: server_url + "/drive/refresh",
        Setting.ERROR_REPORT_URL: server_url + "/logerror",
        Setting.HASSIO_TOKEN: "test_header",
        Setting.SECRETS_FILE_PATH: "secrets.yaml",
        Setting.CREDENTIALS_FILE_PATH: "credentials.dat",
        Setting.FOLDER_FILE_PATH: "folder.dat",
        Setting.RETAINED_FILE_PATH: "retained.json",
        Setting.ID_FILE_PATH: "id.json",
        Setting.INGRESS_TOKEN_FILE_PATH: "ingress.dat",
        Setting.DEFAULT_DRIVE_CLIENT_ID: "test_client_id",
        Setting.DEFAULT_DRIVE_CLIENT_SECRET: "test_client_secret",
        Setting.BACKUP_DIRECTORY_PATH: cleandir,
        Setting.PORT: ui_port,
        Setting.INGRESS_PORT: ingress_port
    })

    # PROBLEM: Something in uploading snapshot chunks hangs between the client and server, so his keeps tests from
    # taking waaaaaaaay too long.  Remove this line and the @pytest.mark.flaky annotations once the problem is identified.
    config.override(Setting.GOOGLE_DRIVE_TIMEOUT_SECONDS, 5)

    # logging.getLogger('injector').setLevel(logging.DEBUG)
    return Injector([
        BaseModule(config),
        TestModule(cleandir, server_url, ui_port, ingress_port)
    ])