Add file input to DOM to create File objects
This commit is contained in:
parent
05211354ea
commit
83db84091f
33
jardin.py
33
jardin.py
|
@ -62,18 +62,37 @@ def get_missing_filenames(localFiles, gardenFiles):
|
|||
|
||||
return missingFiles
|
||||
|
||||
def create_file_input(browser):
|
||||
javascript = """const fileInput = document.createElement("input");
|
||||
fileInput.id = "upload";
|
||||
fileInput.type = "file";
|
||||
fileInput.multiple = true;
|
||||
|
||||
fileInput.addEventListener("change", () => {
|
||||
for (const file of fileInput.files) {
|
||||
console.log(file); // Debug
|
||||
// Miro.request(file); // What I'll probably actually use (or wrapper method)
|
||||
//addFile(file); // Original (token not accessible from minified code)
|
||||
}
|
||||
fileInput.value = null;
|
||||
});
|
||||
|
||||
document.body.appendChild(fileInput);
|
||||
"""
|
||||
|
||||
browser.execute_script(javascript)
|
||||
|
||||
def get_user_id(browser):
|
||||
url = browser.get_current_url()
|
||||
url = browser.current_url
|
||||
userId = re.search("[0-9a-f]{24}", url)[0]
|
||||
return userId
|
||||
|
||||
def upload_file(browser, userId, file):
|
||||
pass
|
||||
|
||||
def upload_files(browser, missingFiles):
|
||||
def upload_files(browser, directory, missingFiles):
|
||||
create_file_input(browser)
|
||||
uploadInput = browser.find_element(By.ID, "upload")
|
||||
userId = get_user_id(browser)
|
||||
for file in missingFiles:
|
||||
upload_file(browser, userId, file)
|
||||
uploadInput.send_keys(directory + file)
|
||||
|
||||
def main(email, password, directory):
|
||||
browser = initialize_webdriver()
|
||||
|
@ -83,7 +102,7 @@ def main(email, password, directory):
|
|||
garden_filenames = get_garden_filenames(browser)
|
||||
local_filenames = get_local_filenames(directory)
|
||||
missing_files = get_missing_filenames(local_filenames, garden_filenames)
|
||||
upload_files(browser, missing_files)
|
||||
upload_files(browser, directory, missing_files)
|
||||
#browser.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -134,45 +134,60 @@ class TestJardin(unittest.TestCase):
|
|||
self.assertEqual(missingFilenames, ["file1.jpg", "file3.jpg", "file5.jpg"])
|
||||
|
||||
@patch("jardin.webdriver.Firefox")
|
||||
def test_get_user_id_calls_get_current_url(self, mockFirefox):
|
||||
mockUserId = "73927475b259be67f8cea98f"
|
||||
mockUrl = f"https://filegarden.com/users/{mockUserId}/garden/#"
|
||||
mockFirefox.get_current_url.return_value = mockUrl
|
||||
def test_create_file_input_runs_correct_script(self, mockFirefox):
|
||||
expectedScript = """const fileInput = document.createElement("input");
|
||||
fileInput.id = "upload";
|
||||
fileInput.type = "file";
|
||||
fileInput.multiple = true;
|
||||
|
||||
jardin.get_user_id(mockFirefox)
|
||||
fileInput.addEventListener("change", () => {
|
||||
for (const file of fileInput.files) {
|
||||
console.log(file); // Debug
|
||||
// Miro.request(file); // What I'll probably actually use (or wrapper method)
|
||||
//addFile(file); // Original (token not accessible from minified code)
|
||||
}
|
||||
fileInput.value = null;
|
||||
});
|
||||
|
||||
mockFirefox.get_current_url.assert_called_once()
|
||||
document.body.appendChild(fileInput);
|
||||
"""
|
||||
|
||||
jardin.create_file_input(mockFirefox)
|
||||
|
||||
mockFirefox.execute_script.assert_called_once_with(expectedScript)
|
||||
|
||||
@patch("jardin.webdriver.Firefox")
|
||||
def test_get_user_id_returns_user_id_from_url(self, mockFirefox):
|
||||
mockUserId = "73927475b259be67f8cea98f"
|
||||
mockUrl = f"https://filegarden.com/users/{mockUserId}/garden/#"
|
||||
mockFirefox.get_current_url.return_value = mockUrl
|
||||
mockFirefox.current_url = mockUrl
|
||||
|
||||
userId = jardin.get_user_id(mockFirefox)
|
||||
|
||||
self.assertEqual(userId, mockUserId)
|
||||
|
||||
@patch("jardin.get_user_id")
|
||||
@patch("jardin.create_file_input")
|
||||
@patch("jardin.webdriver.Firefox")
|
||||
def test_upload_files_calls_get_user_id(self, mockFirefox, mockGetUserId):
|
||||
jardin.upload_files(mockFirefox, [])
|
||||
def test_upload_files_calls_correct_setup_methods(self, mockFirefox, mockCreateFileInput, mockGetUserId):
|
||||
jardin.upload_files(mockFirefox, "", [])
|
||||
|
||||
mockCreateFileInput.assert_called_once_with(mockFirefox)
|
||||
mockGetUserId.assert_called_once_with(mockFirefox)
|
||||
|
||||
@patch("jardin.get_user_id")
|
||||
@patch("jardin.upload_file")
|
||||
@patch("jardin.webdriver.Firefox")
|
||||
def test_upload_files_calls_upload_file_once_per_file(self, mockFirefox, mockUploadFile, mockGetUserId):
|
||||
def test_upload_files_calls_upload_file_once_per_file(self, mockFirefox, mockGetUserId):
|
||||
mockGetUserId.return_value = "userid"
|
||||
mockFiles = ["file1.jpg", "file2.jpg", "file3.jpg"]
|
||||
mockDirectory = "/home/luser/gardenfiles/"
|
||||
|
||||
jardin.upload_files(mockFirefox, mockFiles)
|
||||
jardin.upload_files(mockFirefox, mockDirectory, mockFiles)
|
||||
|
||||
mockUploadFile.assert_has_calls([
|
||||
call(mockFirefox, "userid", "file1.jpg"),
|
||||
call(mockFirefox, "userid", "file2.jpg"),
|
||||
call(mockFirefox, "userid", "file3.jpg")
|
||||
mockFirefox.find_element.return_value.send_keys.assert_has_calls([
|
||||
call(mockDirectory + "file1.jpg"),
|
||||
call(mockDirectory + "file2.jpg"),
|
||||
call(mockDirectory + "file3.jpg")
|
||||
])
|
||||
|
||||
@patch("jardin.initialize_webdriver")
|
||||
|
@ -205,7 +220,7 @@ class TestJardin(unittest.TestCase):
|
|||
mockGetGardenFilenames.assert_called_once_with(mockInitializeWebdriver.return_value)
|
||||
mockGetLocalFilenames.assert_called_once_with(mockDirectory)
|
||||
mockGetMissingFilenames.assert_called_once_with(mockGetLocalFilenames.return_value, mockGetGardenFilenames.return_value)
|
||||
mockUploadFiles.assert_called_once_with(mockInitializeWebdriver.return_value, mockGetMissingFilenames.return_value)
|
||||
mockUploadFiles.assert_called_once_with(mockInitializeWebdriver.return_value, mockDirectory, mockGetMissingFilenames.return_value)
|
||||
#mockInitializeWebdriver.return_value.close.assert_called_once()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in New Issue