diff --git a/osf/external/gravy_valet/translations.py b/osf/external/gravy_valet/translations.py index 7ad5758cd60..843a23a1ca9 100644 --- a/osf/external/gravy_valet/translations.py +++ b/osf/external/gravy_valet/translations.py @@ -12,7 +12,6 @@ from addons.gitlab.apps import GitLabAddonConfig from addons.googledrive.apps import GoogleDriveAddonConfig from addons.s3.apps import S3AddonAppConfig -from addons.s3.settings import ENCRYPT_UPLOADS_DEFAULT from . import request_helpers as gv_requests @@ -95,6 +94,7 @@ class EphemeralNodeSettings: # retrieved from WB on-demand and cached _credentials: dict = None + _config: dict = None @property def short_name(self): @@ -142,20 +142,9 @@ def serialize_waterbutler_credentials(self): return self._credentials def serialize_waterbutler_settings(self): - # sufficient for Box - # TODO: Define per-service translation (and/or common schemes) - match self.wb_key: - case _LegacyConfigsForWBKey.s3.name: - return { - 'bucket': self.folder_id.split(':/')[0], - 'id': self.folder_id, - 'encrypt_uploads': ENCRYPT_UPLOADS_DEFAULT, - } - - return { - 'folder': self.folder_id.split(':')[1], - 'service': self.short_name, - } + if self._config is None: + self._fetch_wb_config() + return self._config def _fetch_wb_config(self): result = gv_requests.get_waterbutler_config( @@ -164,6 +153,7 @@ def _fetch_wb_config(self): requesting_user=self.active_user ) self._credentials = result.get_attribute('credentials') + self._config = result.get_attribute('config') def create_waterbutler_log(self, *args, **kwargs): pass diff --git a/osf_tests/external/gravy_valet/gv_fakes.py b/osf_tests/external/gravy_valet/gv_fakes.py index 4d8c9e0963a..2d2acd6234b 100644 --- a/osf_tests/external/gravy_valet/gv_fakes.py +++ b/osf_tests/external/gravy_valet/gv_fakes.py @@ -78,6 +78,18 @@ def _serialize_relationships(self): accounts_relationship = self._format_relationship_entry(relationship_path='authorized_storage_accounts') return {'authorized_storage_accounts': accounts_relationship} +@dataclasses.dataclass(frozen=True) +class _FakeWBCredentials(_FakeGVEntity): + + RESOURCE_TYPE = 'waterbutler-credentials' + config: dict + + def _serialize_attributes(self): + return {'config': self.config} + + def _serialize_relationships(self): + return {} + @dataclasses.dataclass(frozen=True) class _FakeResourceReference(_FakeGVEntity): @@ -209,6 +221,7 @@ class FakeGravyValet: r'v1/resource-references(/(?P\d+)|(\?filter\[resource_uri\]=(?P[^&]+)))': '_get_resource', r'v1/authorized-storage-accounts/(?P\d+)': '_get_account', r'v1/configured-storage-addons/(?P\d+)': '_get_addon', + r'v1/configured-storage-addons/(?P\d+)/waterbutler-credentials': '_get_wb_settings', r'v1/user-references/(?P\d+)/authorized_storage_accounts': '_get_user_accounts', r'v1/resource-references/(?P\d+)/configured_storage_addons': '_get_resource_addons', } @@ -374,6 +387,21 @@ def _get_user( include_param=include_param, ) + def _get_wb_settings( + self, + headers: dict, + pk: str, + include_param: str = '', + ) -> str: + creds = _FakeWBCredentials( + pk=10, + config={ + 'folder': pk, + 'service': 'box', + } + ) + return _format_response_body(creds) + def _get_resource( self, headers: dict, diff --git a/osf_tests/test_gv_utils.py b/osf_tests/test_gv_utils.py index c58469a3e0e..6794a4c94f6 100644 --- a/osf_tests/test_gv_utils.py +++ b/osf_tests/test_gv_utils.py @@ -532,13 +532,13 @@ def test_make_ephemeral_node_settings(self, contributor, project, fake_box_addon requesting_user=contributor, requested_resource=project, ) - ephemeral_config = translations.make_ephemeral_node_settings( - addon_data, requesting_user=contributor, requested_resource=project - ) - assert ephemeral_config.short_name == 'box' - assert ephemeral_config.gv_id == fake_box_addon.pk - assert ephemeral_config.config.name == 'addons.box' - assert ephemeral_config.serialize_waterbutler_settings() == { - 'folder': fake_box_addon.root_folder.split(':')[1], - 'service': 'box' - } + ephemeral_config = translations.make_ephemeral_node_settings( + addon_data, requesting_user=contributor, requested_resource=project + ) + assert ephemeral_config.short_name == 'box' + assert ephemeral_config.gv_id == fake_box_addon.pk + assert ephemeral_config.config.name == 'addons.box' + assert ephemeral_config.serialize_waterbutler_settings() == { + 'folder': fake_box_addon.root_folder.split(':')[1], + 'service': 'box' + }