async def test_change_specify_folder_setting(reader: ReaderHelper, server,
                                             session, coord: Coordinator,
                                             folder_finder: FolderFinder):
    await coord.sync()
    assert folder_finder.getCachedFolder() is not None

    old_folder = folder_finder.getCachedFolder()
    # Change some config
    update = {
        "config": {
            "specify_snapshot_folder": True
        },
        "snapshot_folder": ""
    }
    assert await reader.postjson("saveconfig", json=update) == {
        'message': 'Settings saved',
        "reload_page": False
    }

    # verify the snapshot folder was reset, which triggers the error dialog to find a new folder
    assert folder_finder.getCachedFolder() == old_folder

    await coord.waitForSyncToFinish()
    result = await reader.postjson("getstatus")
    assert result["last_error"] is None
async def test_existing_folder_already_exists(time, drive: DriveSource, config: Config, folder_finder: FolderFinder):
    await drive.get()
    drive.checkBeforeChanges()

    # Reset folder, try again
    folder_finder.reset()
    await drive.get()
    with pytest.raises(ExistingBackupFolderError):
        drive.checkBeforeChanges()
async def test_existing_resolved_create_new(time, drive: DriveSource, config: Config, folder_finder: FolderFinder):
    await drive.get()
    drive.checkBeforeChanges()

    folder_id = await drive.getFolderId()

    # Reset folder, try again
    folder_finder.reset()
    await drive.get()
    with pytest.raises(ExistingBackupFolderError):
        drive.checkBeforeChanges()

    folder_finder.resolveExisting(False)
    await drive.get()
    drive.checkBeforeChanges()
    assert await drive.getFolderId() != folder_id
async def test_change_specify_folder_setting_with_manual_creds(
        reader: ReaderHelper, google, session, coord: Coordinator,
        folder_finder: FolderFinder, drive: DriveSource, config):
    google.resetDriveAuth()
    # Generate manual credentials
    req_path = "manualauth?client_id={}&client_secret={}".format(
        google._custom_drive_client_id, google._custom_drive_client_secret)
    data = await reader.getjson(req_path)
    assert "auth_url" in data
    async with session.get(data["auth_url"], allow_redirects=False) as resp:
        code = (await resp.json())["code"]
    drive.saveCreds(None)
    assert not drive.enabled()
    req_path = "manualauth?code={}".format(code)
    await reader.getjson(req_path)
    assert drive.isCustomCreds()

    await coord.sync()
    assert folder_finder.getCachedFolder() is not None

    # Specify the snapshot folder, which should cache the new one
    update = {
        "config": {
            Setting.SPECIFY_SNAPSHOT_FOLDER.value: True
        },
        "snapshot_folder": "12345"
    }
    assert await reader.postjson("saveconfig", json=update) == {
        'message': 'Settings saved',
        "reload_page": False
    }
    assert folder_finder.getCachedFolder() == "12345"

    # Un change the folder, which should keep the existing folder
    update = {
        "config": {
            Setting.SPECIFY_SNAPSHOT_FOLDER.value: False
        },
        "snapshot_folder": ""
    }
    assert await reader.postjson("saveconfig", json=update) == {
        'message': 'Settings saved',
        "reload_page": False
    }
    assert folder_finder.getCachedFolder() == "12345"