Skip to content

Commit 3a68ff8

Browse files
committed
Finalise aws and other tests, revert changes made to setup_aws during erroneous 'fix linting' PR.
1 parent ab84a40 commit 3a68ff8

File tree

8 files changed

+136
-43
lines changed

8 files changed

+136
-43
lines changed

.github/workflows/code_test_and_deploy.yml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ jobs:
8585
FPR="$(gpg --list-secret-keys --with-colons | awk -F: '/^fpr:/ {print $10; exit}')"
8686
pass init "$FPR"
8787
88-
8988
# run SSH tests only on Linux because Windows and macOS
9089
# are already run within a virtual container and so cannot
9190
# run Linux containers because nested containerisation is disabled.
@@ -104,22 +103,18 @@ jobs:
104103
run: |
105104
pytest tests/tests_transfers/gdrive
106105
107-
# - name: Test AWS
108-
# env:
109-
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
110-
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
111-
# AWS_REGION: ${{ secrets.AWS_REGION }}
112-
# AWS_BUCKET_NAME: ${{ secrets.AWS_BUCKET_NAME }}
113-
# run: |
114-
# pytest tests/tests_transfers/aws
115-
116-
# - name: All Other Tests
117-
# run: |
118-
# pytest --ignore=tests/tests_transfers/ssh --ignore=tests/tests_transfers/gdrive --ignore=tests/tests_transfers/aws
106+
- name: Test AWS
107+
env:
108+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
109+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
110+
AWS_REGION: ${{ secrets.AWS_REGION }}
111+
AWS_BUCKET_NAME: ${{ secrets.AWS_BUCKET_NAME }}
112+
run: |
113+
pytest tests/tests_transfers/aws
119114
120-
- name: RClone Encryption
115+
- name: All Other Tests
121116
run: |
122-
pytest -k test_rclone_encryption
117+
pytest --ignore=tests/tests_transfers/ssh --ignore=tests/tests_transfers/gdrive --ignore=tests/tests_transfers/aws
123118
124119
build_sdist_wheels:
125120
name: Build source distribution

datashuttle/tui/screens/setup_aws.py

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from textual.screen import ModalScreen
1212
from textual.widgets import Button, Input, Static
1313

14+
from datashuttle.utils import rclone_encryption
15+
1416

1517
class SetupAwsScreen(ModalScreen):
1618
"""Dialog window that sets up connection to an Amazon Web Service S3 bucket.
@@ -26,7 +28,7 @@ def __init__(self, interface: Interface) -> None:
2628
super(SetupAwsScreen, self).__init__()
2729

2830
self.interface = interface
29-
self.stage = 0
31+
self.stage = "init"
3032

3133
def compose(self) -> ComposeResult:
3234
"""Set widgets on the SetupAwsScreen."""
@@ -52,18 +54,35 @@ def on_mount(self) -> None:
5254
self.query_one("#setup_aws_secret_access_key_input").visible = False
5355

5456
def on_button_pressed(self, event: Button.Pressed) -> None:
55-
"""Handle button press on the screen."""
57+
"""Handle button press on the screen.
58+
59+
The `setup_aws_ok_button` is used for all 'positive' events ('Yes, Ok')
60+
and 'setup_aws_cancel_button' is used for 'negative' events ('No', 'Cancel').
61+
The appropriate action to take on the button press is determined by the
62+
current stage.
63+
64+
"""
5665
if event.button.id == "setup_aws_cancel_button":
57-
self.dismiss()
66+
if self.stage == "ask_rclone_encryption":
67+
message = "AWS Connection Successful!" #
68+
self.query_one("#setup_aws_messagebox_message").update(message)
69+
self.query_one("#setup_aws_ok_button").label = "Finish"
70+
self.query_one("#setup_aws_cancel_button").remove()
71+
self.stage = "finished"
72+
else:
73+
self.dismiss()
5874

59-
if event.button.id == "setup_aws_ok_button":
60-
if self.stage == 0:
75+
elif event.button.id == "setup_aws_ok_button":
76+
if self.stage == "init":
6177
self.prompt_user_for_aws_secret_access_key()
6278

63-
elif self.stage == 1:
79+
elif self.stage == "use_secret_access_key":
6480
self.use_secret_access_key_to_setup_aws_connection()
6581

66-
elif self.stage == 2:
82+
elif self.stage == "ask_rclone_encryption":
83+
self.set_rclone_encryption()
84+
85+
elif self.stage == "finished":
6786
self.dismiss()
6887

6988
def prompt_user_for_aws_secret_access_key(self) -> None:
@@ -73,10 +92,15 @@ def prompt_user_for_aws_secret_access_key(self) -> None:
7392
self.query_one("#setup_aws_messagebox_message").update(message)
7493
self.query_one("#setup_aws_secret_access_key_input").visible = True
7594

76-
self.stage += 1
95+
self.query_one("#setup_aws_ok_button")
96+
97+
self.stage = "use_secret_access_key"
7798

7899
def use_secret_access_key_to_setup_aws_connection(self) -> None:
79-
"""Set up the AWS connection and inform user of success or failure."""
100+
"""Set up the AWS connection and failure.
101+
102+
If success, move onto the rclone_encryption screen.
103+
"""
80104
secret_access_key = self.query_one(
81105
"#setup_aws_secret_access_key_input"
82106
).value
@@ -86,11 +110,13 @@ def use_secret_access_key_to_setup_aws_connection(self) -> None:
86110
)
87111

88112
if success:
89-
message = "AWS Connection Successful!"
90-
self.query_one(
91-
"#setup_aws_secret_access_key_input"
92-
).visible = False
113+
message = f"{rclone_encryption.get_explanation_message(self.interface.project.cfg)}"
114+
self.query_one("#setup_aws_messagebox_message").update(message)
93115

116+
self.query_one("#setup_aws_secret_access_key_input").remove()
117+
self.query_one("#setup_aws_ok_button").label = "Yes"
118+
self.query_one("#setup_aws_cancel_button").label = "No"
119+
self.stage = "ask_rclone_encryption"
94120
else:
95121
message = (
96122
f"AWS setup failed. Please check your configs and secret access key"
@@ -100,7 +126,23 @@ def use_secret_access_key_to_setup_aws_connection(self) -> None:
100126
"#setup_aws_secret_access_key_input"
101127
).disabled = True
102128

103-
self.query_one("#setup_aws_ok_button").label = "Finish"
104-
self.query_one("#setup_aws_messagebox_message").update(message)
105-
self.query_one("#setup_aws_cancel_button").disabled = True
106-
self.stage += 1
129+
self.query_one("#setup_aws_ok_button").label = "Retry"
130+
self.query_one("#setup_aws_messagebox_message").update(message)
131+
132+
def set_rclone_encryption(self):
133+
"""Try and encrypt the Rclone config file and inform the user of success / failure."""
134+
success, output = self.interface.try_setup_rclone_encryption()
135+
136+
if success:
137+
message = (
138+
"The rclone_encryption was successfully set. Setup complete!"
139+
)
140+
self.query_one("#setup_aws_messagebox_message").update(message)
141+
self.query_one("#setup_aws_ok_button").label = "Finish"
142+
self.query_one("#setup_aws_cancel_button").remove()
143+
self.stage = "finished"
144+
else:
145+
message = (
146+
f"The rclone_encryption set up failed. Exception: {output}"
147+
)
148+
self.query_one("#setup_aws_messagebox_message").update(message)

datashuttle/tui/screens/setup_gdrive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def set_rclone_encryption(self):
357357

358358
if success:
359359
self.set_finish_page(
360-
"The password was successfully set. Setup complete!"
360+
"The encryption was successful. Setup complete!"
361361
)
362362
else:
363363
message = f"The password set up failed. Exception: {output}"

tests/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,13 @@ def check_config_file(config_path, *kwargs):
432432
assert value == config_yaml[name], f"{name}"
433433

434434

435+
def check_rclone_file_is_encrypted(rclone_config_path):
436+
with open(rclone_config_path, "r", encoding="utf-8") as file:
437+
first_line = file.readline().strip()
438+
439+
assert first_line == "# Encrypted rclone configuration File"
440+
441+
435442
# -----------------------------------------------------------------------------
436443
# Test Helpers
437444
# -----------------------------------------------------------------------------

tests/tests_integration/test_rclone_encryption.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from datashuttle.utils import rclone_encryption
44

5+
from .. import test_utils
56
from ..base import BaseTest
67
from ..tests_transfers.ssh import ssh_test_utils
78

@@ -42,10 +43,7 @@ def test_set_and_remove_password(self, project):
4243

4344
assert "RCLONE_PASSWORD_COMMAND" not in os.environ
4445

45-
with open(rclone_config_path, "r", encoding="utf-8") as f:
46-
first_line = f.readline().strip()
47-
48-
assert first_line == "# Encrypted rclone configuration File"
46+
test_utils.check_rclone_file_is_encrypted(rclone_config_path)
4947

5048
rclone_encryption.remove_rclone_encryption(project.cfg)
5149

tests/tests_transfers/aws/test_tui_setup_aws.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import pytest
44

5+
from datashuttle import DataShuttle
56
from datashuttle.tui.app import TuiApp
67
from datashuttle.tui.screens.project_manager import ProjectManagerScreen
78
from datashuttle.utils import rclone, utils
89

10+
from ... import test_utils
911
from ...tests_tui.tui_base import TuiBase
1012
from . import aws_test_utils
1113

@@ -31,12 +33,18 @@ def central_path_and_project(self, setup_project_paths):
3133

3234
yield central_path, project_name
3335

34-
rclone.call_rclone(
35-
f"purge central_{project_name}_aws:{central_path}"
36-
) # TODO: I think this will fail, needs config
36+
project = DataShuttle(project_name)
37+
38+
rclone.call_rclone_for_central_connection(
39+
project.cfg,
40+
f"purge central_{project_name}_aws:{central_path} {rclone.get_config_arg(project.cfg)}",
41+
)
3742

3843
@pytest.mark.asyncio
39-
async def test_aws_connection_setup(self, central_path_and_project):
44+
@pytest.mark.parametrize("set_encryption", [True, False])
45+
async def test_aws_connection_setup(
46+
self, central_path_and_project, set_encryption
47+
):
4048
"""Test AWS connection setup via the TUI.
4149
4250
AWS connection details are filled in the configs tab. The setup
@@ -60,12 +68,40 @@ async def test_aws_connection_setup(self, central_path_and_project):
6068
)
6169

6270
assert (
63-
"AWS Connection Successful!"
71+
"Would you like to encrypt the RClone config file"
6472
in pilot.app.screen.query_one(
6573
"#setup_aws_messagebox_message"
6674
).renderable
6775
)
6876

77+
if set_encryption:
78+
await self.scroll_to_click_pause(pilot, "#setup_aws_ok_button")
79+
80+
assert (
81+
"The rclone_encryption was successfully set. Setup complete!"
82+
in pilot.app.screen.query_one(
83+
"#setup_aws_messagebox_message"
84+
).renderable
85+
)
86+
87+
project = pilot.app.screen.interface.project
88+
89+
test_utils.check_rclone_file_is_encrypted(
90+
project.cfg.rclone.get_rclone_central_connection_config_filepath()
91+
)
92+
93+
else:
94+
await self.scroll_to_click_pause(
95+
pilot, "#setup_aws_cancel_button"
96+
)
97+
98+
assert (
99+
"AWS Connection Successful!"
100+
in pilot.app.screen.query_one(
101+
"#setup_aws_messagebox_message"
102+
).renderable
103+
)
104+
69105
@pytest.mark.asyncio
70106
async def test_aws_connection_setup_failed(self, central_path_and_project):
71107
"""Test AWS connection setup using an incorrect client secret and check

tests/tests_transfers/gdrive/test_tui_setup_gdrive.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,18 @@ async def test_gdrive_connection_setup_without_browser(
117117
)
118118

119119
assert (
120-
"The password was successfully set. Setup complete!"
120+
"The encryption was successful. Setup complete!"
121121
in pilot.app.screen.query_one(
122122
"#gdrive_setup_messagebox_message"
123123
).renderable
124124
)
125125

126+
project = pilot.app.screen.interface.project
127+
128+
test_utils.check_rclone_file_is_encrypted(
129+
project.cfg.rclone.get_rclone_central_connection_config_filepath()
130+
)
131+
126132
else:
127133
await self.scroll_to_click_pause(
128134
pilot, "#setup_gdrive_set_encryption_no_button"

tests/tests_transfers/ssh/test_ssh_transfer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
from ... import test_utils
67
from . import ssh_test_utils
78
from .base_ssh import BaseSSHTransfer
89

@@ -157,3 +158,11 @@ def test_ssh_wildcards_3(self, ssh_setup):
157158
self.run_and_check_transfers(
158159
project, sub_names, ses_names, datatype, expected_transferred_paths
159160
)
161+
162+
def test_rclone_config_file_encrypted(self, ssh_setup):
163+
"""Quick confidence check the set up rclone config is indeed ecrypted."""
164+
pathtable, project = ssh_setup
165+
166+
test_utils.check_rclone_file_is_encrypted(
167+
project.cfg.rclone.get_rclone_central_connection_config_filepath()
168+
)

0 commit comments

Comments
 (0)