2023-10-19 11:11:51 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2023-10-19 10:42:14 +00:00
|
|
|
import unittest
|
2023-10-20 11:25:19 +00:00
|
|
|
from unittest.mock import patch, call
|
|
|
|
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
2023-10-19 10:42:14 +00:00
|
|
|
|
|
|
|
import jardin
|
|
|
|
|
|
|
|
class TestJardin(unittest.TestCase):
|
2023-10-20 11:25:19 +00:00
|
|
|
@patch("jardin.webdriver.Firefox")
|
2023-10-19 20:00:07 +00:00
|
|
|
def test_initialize_webdriver_calls_Firefox(self, mockFirefox):
|
2023-10-19 10:42:14 +00:00
|
|
|
jardin.initialize_webdriver()
|
|
|
|
|
|
|
|
mockFirefox.assert_called_once()
|
|
|
|
|
2023-10-20 11:25:19 +00:00
|
|
|
@patch("jardin.webdriver.Firefox")
|
2023-10-19 20:34:17 +00:00
|
|
|
def test_navigate_to_filegarden(self, mockFirefox):
|
|
|
|
jardin.navigate_to_filegarden(mockFirefox)
|
|
|
|
|
2023-10-19 20:53:19 +00:00
|
|
|
mockFirefox.get.assert_called_once_with("https://filegarden.com/login")
|
2023-10-19 20:34:17 +00:00
|
|
|
|
2023-10-20 11:25:19 +00:00
|
|
|
@patch("jardin.webdriver.Firefox")
|
2023-10-20 11:43:00 +00:00
|
|
|
def test_input_email_enters_email(self, mockFirefox):
|
2023-10-20 11:25:19 +00:00
|
|
|
mockEmail = "email@mail.com"
|
|
|
|
|
2023-10-20 11:32:48 +00:00
|
|
|
jardin.input_email(mockFirefox, mockEmail)
|
2023-10-20 11:25:19 +00:00
|
|
|
|
|
|
|
mockFirefox.find_element.assert_called_with(By.ID, "email")
|
|
|
|
mockFirefox.find_element.return_value.send_keys.assert_has_calls([
|
|
|
|
call(mockEmail),
|
|
|
|
call(Keys.RETURN)
|
2023-10-20 11:32:48 +00:00
|
|
|
])
|
|
|
|
|
2023-10-20 11:43:00 +00:00
|
|
|
@patch("jardin.webdriver.Firefox")
|
|
|
|
def test_click_password_button_clicks_button(self, mockFirefox):
|
|
|
|
jardin.click_password_button(mockFirefox)
|
|
|
|
|
|
|
|
mockFirefox.find_element.assert_called_with(By.XPATH, "/html/body/div[2]/div[3]/div[1]/form/div[1]/span/button[3]")
|
|
|
|
mockFirefox.find_element.return_value.click.assert_called_once()
|
|
|
|
|
|
|
|
@patch("jardin.webdriver.Firefox")
|
|
|
|
def test_input_password_enters_password(self, mockFirefox):
|
|
|
|
mockPassword = "p4$$w0rd"
|
|
|
|
|
|
|
|
jardin.input_password(mockFirefox, mockPassword)
|
|
|
|
|
|
|
|
mockFirefox.find_element.assert_called_with(By.ID, "password")
|
|
|
|
mockFirefox.find_element.return_value.send_keys.assert_has_calls([
|
|
|
|
call(mockPassword),
|
|
|
|
call(Keys.RETURN)
|
|
|
|
])
|
|
|
|
|
2023-10-20 11:32:48 +00:00
|
|
|
@patch("jardin.webdriver.Firefox")
|
|
|
|
@patch("jardin.input_email")
|
2023-10-20 11:43:00 +00:00
|
|
|
@patch("jardin.click_password_button")
|
|
|
|
@patch("jardin.input_password")
|
|
|
|
def test_login_with_password_performs_login_steps(self, mockInputPassword, mockClickPasswordButton, mockInputEmail, mockFirefox):
|
2023-10-20 11:32:48 +00:00
|
|
|
mockEmail = "email@mail.com"
|
|
|
|
mockPassword = "p4$$w0rd"
|
|
|
|
|
|
|
|
jardin.login_with_password(mockFirefox, mockEmail, mockPassword)
|
|
|
|
|
|
|
|
mockInputEmail.assert_called_once_with(mockFirefox, mockEmail)
|
2023-10-20 11:43:00 +00:00
|
|
|
mockClickPasswordButton.assert_called_once_with(mockFirefox)
|
|
|
|
mockInputPassword.assert_called_once_with(mockFirefox, mockPassword)
|
2023-10-20 11:25:19 +00:00
|
|
|
|
2023-10-20 17:32:23 +00:00
|
|
|
@patch("jardin.webdriver.Firefox")
|
|
|
|
def test_click_go_to_your_garden_clicks_button(self, mockFirefox):
|
|
|
|
jardin.click_go_to_your_garden(mockFirefox)
|
|
|
|
|
|
|
|
mockFirefox.find_element.assert_called_with(By.LINK_TEXT, "GO TO YOUR GARDEN")
|
|
|
|
mockFirefox.find_element.return_value.click.assert_called_once()
|
|
|
|
|
|
|
|
@patch("jardin.webdriver.Firefox")
|
|
|
|
@patch("jardin.bs4.BeautifulSoup")
|
|
|
|
def test_get_garden_filenames_creates_beautifulsoup_and_finds_names(self, mockBeautifulSoup, mockFirefox):
|
|
|
|
jardin.get_garden_filenames(mockFirefox)
|
|
|
|
|
|
|
|
mockBeautifulSoup.assert_called_once_with(mockFirefox.page_source, "lxml")
|
|
|
|
mockBeautifulSoup.return_value.find_all.assert_called_once_with(attrs={"class": "name"})
|
|
|
|
|
|
|
|
@patch("jardin.webdriver.Firefox")
|
|
|
|
def test_get_garden_filenames_returns_filenames_correctly(self, mockFirefox):
|
|
|
|
mockFirefox.page_source = """
|
|
|
|
<html lang="en">
|
|
|
|
<body class="mdc-typography">
|
|
|
|
<div id="items" class="items">
|
|
|
|
<a class="item typeFile selected" draggable="false" ondragstart="return false;" href="https://file.garden/fakeid/file1.jpg">
|
|
|
|
<div class="cell icon">
|
|
|
|
<button class="mdc-icon-button material-icons" type="button">image</button>
|
|
|
|
</div>
|
|
|
|
<div class="cell name" title="file1.jpg">file1.jpg</div>
|
|
|
|
<div class="cell size" title="10 B">10 B</div>
|
|
|
|
<div class="cell type" title="image/jpeg">image/jpeg</div>
|
|
|
|
<div class="cell date" title="Wed Aug 30 2023 13:04:10 GMT+0100 (British Summer Time)">Aug 30 2023 13:04:10</div>
|
|
|
|
</a>
|
|
|
|
<a class="item typeFile" draggable="false" ondragstart="return false;" href="https://file.garden/fakeid/file2.jpg">
|
|
|
|
<div class="cell icon">
|
|
|
|
<button class="mdc-icon-button material-icons" type="button">image</button>
|
|
|
|
</div>
|
|
|
|
<div class="cell name" title="file2.jpg">file2.jpg</div>
|
|
|
|
<div class="cell size" title="20 B">20 B</div>
|
|
|
|
<div class="cell type" title="image/jpeg">image/jpeg</div>
|
|
|
|
<div class="cell date" title="Wed Jun 29 2022 17:37:14 GMT+0100 (British Summer Time)">Jun 29 2022 17:37:14</div>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
filenames = jardin.get_garden_filenames(mockFirefox)
|
|
|
|
|
|
|
|
self.assertEqual(filenames, ["file1.jpg", "file2.jpg"])
|
|
|
|
|
2023-10-20 17:38:21 +00:00
|
|
|
@patch("jardin.os.listdir")
|
|
|
|
def test_get_local_filenames_gets_filenames_in_local_directory(self, mockListdir):
|
|
|
|
mockDirectory = "/home/luser/gardenfiles/"
|
|
|
|
mockFiles = ["file1", "file2", "file3"]
|
|
|
|
mockListdir.return_value = mockFiles
|
|
|
|
|
|
|
|
localFiles = jardin.get_local_filenames(mockDirectory)
|
|
|
|
|
|
|
|
mockListdir.assert_called_once_with(mockDirectory)
|
|
|
|
self.assertEqual(localFiles, mockFiles)
|
|
|
|
|
2023-10-20 19:03:39 +00:00
|
|
|
def test_get_missing_filenames_returns_local_filenames_without_garden_filenames(self):
|
|
|
|
mockLocalFilenames = ["file1.jpg", "file2.jpg", "file3.jpg", "file4.jpg", "file5.jpg"]
|
|
|
|
mockGardenFilenames = ["file2.jpg", "file4.jpg", "file6.jpg", "file8.jpg", "file10.jpg"]
|
|
|
|
|
|
|
|
missingFilenames = jardin.get_missing_filenames(mockLocalFilenames, mockGardenFilenames)
|
|
|
|
|
|
|
|
self.assertEqual(missingFilenames, ["file1.jpg", "file3.jpg", "file5.jpg"])
|
|
|
|
|
2023-10-19 20:00:07 +00:00
|
|
|
@patch("jardin.initialize_webdriver")
|
2023-10-19 20:34:17 +00:00
|
|
|
@patch("jardin.navigate_to_filegarden")
|
2023-10-20 11:52:58 +00:00
|
|
|
@patch("jardin.login_with_password")
|
2023-10-20 17:32:23 +00:00
|
|
|
@patch("jardin.click_go_to_your_garden")
|
|
|
|
@patch("jardin.get_garden_filenames")
|
2023-10-20 17:40:40 +00:00
|
|
|
@patch("jardin.get_local_filenames")
|
2023-10-19 20:34:17 +00:00
|
|
|
def test_main_calls_methods_in_correct_order(self,
|
2023-10-20 17:40:40 +00:00
|
|
|
mockGetLocalFilenames,
|
2023-10-20 17:32:23 +00:00
|
|
|
mockGetGardenFilenames,
|
|
|
|
mockClickGoToYourGarden,
|
2023-10-20 11:52:58 +00:00
|
|
|
mockLoginWithPassword,
|
|
|
|
mockNavigateToFilegarden,
|
|
|
|
mockInitializeWebdriver):
|
|
|
|
mockEmail = "email@mail.com"
|
|
|
|
mockPassword = "p4$$w0rd"
|
2023-10-20 17:40:40 +00:00
|
|
|
mockDirectory = "/home/luser/gardenfiles/"
|
2023-10-20 11:52:58 +00:00
|
|
|
|
2023-10-20 17:40:40 +00:00
|
|
|
jardin.main(mockEmail, mockPassword, mockDirectory)
|
2023-10-19 20:00:07 +00:00
|
|
|
|
|
|
|
mockInitializeWebdriver.assert_called_once()
|
2023-10-20 11:52:58 +00:00
|
|
|
mockNavigateToFilegarden.assert_called_once_with(mockInitializeWebdriver.return_value)
|
|
|
|
mockLoginWithPassword.assert_called_once_with(mockInitializeWebdriver.return_value, mockEmail, mockPassword)
|
2023-10-20 17:32:23 +00:00
|
|
|
mockClickGoToYourGarden.assert_called_once_with(mockInitializeWebdriver.return_value)
|
|
|
|
mockGetGardenFilenames.assert_called_once_with(mockInitializeWebdriver.return_value)
|
2023-10-20 17:40:40 +00:00
|
|
|
mockGetLocalFilenames.assert_called_once_with(mockDirectory)
|
2023-10-20 11:52:58 +00:00
|
|
|
|
2023-10-20 17:32:23 +00:00
|
|
|
#mockInitializeWebdriver.return_value.close.assert_called_once()
|
2023-10-19 20:00:07 +00:00
|
|
|
|
2023-10-19 11:11:51 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|