Skip to content

Commit

Permalink
clean-up node_setting user_setting mocking for GraveyValet
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed May 23, 2024
1 parent cdaafec commit 647f35c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 65 deletions.
78 changes: 32 additions & 46 deletions addons/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,65 +58,51 @@ def format_last_known_metadata(auth, node, file, error_type):


class GravyValetAddonAppConfig:
class MockNodeSetting:
def __init__(self, resource, auth, legacy_config, gv_data):
self.gv_data = gv_data

@property
def configured(self):
return True

@property
def config_id(self):
return self.gv_data.config_id

class MockUserSetting:
def __init__(self, resource, auth, legacy_config, gv_data):
self.gv_data = gv_data

@property
def configured(self):
return True

@property
def config_id(self):
return self.gv_data.config_id

@staticmethod
def get_configured_storage_addons_data(config_id, auth):
resp = requests.get(
return requests.get(
settings.GV_NODE_ADDON_ENDPOINT.format(config_id=config_id),
)
return resp.json()
params={'include': 'external_storage_service'},
# auth=auth TODO: HMAC
).json()

def get_external_service_addon_data(self, auth):
resp = requests.get(
self.configured_storage_addons_data['data']['relationships']['external_storage_service']['links']['related'],
)
return resp.json()
@staticmethod
def get_authorized_storage_account(config_id, auth):
return requests.get(
settings.GV_USER_ADDON_ENDPOINT.format(config_id=config_id),
params={'include': 'external_storage_service'},
# auth=auth TODO: HMAC
).json()

def __init__(self, resource, config_id, auth):
self.config_id = config_id
self.configured_storage_addons_data = self.get_configured_storage_addons_data(config_id, auth)
from osf.models import OSFUser, AbstractNode
if isinstance(resource, AbstractNode):
self.gv_data = self.get_configured_storage_addons_data(config_id, auth)
elif isinstance(resource, OSFUser):
self.gv_data = self.get_authorized_storage_account(config_id, auth)
else:
raise NotImplementedError()

# TODO: Names in GV must be exact matches?
self.external_storage_service_data = self.get_external_service_addon_data(auth)
self.addon_name = self.external_storage_service_data['data']['attributes']['name']
self.legacy_config = settings.ADDONS_AVAILABLE_DICT[self.addon_name]
self.addon_name = self.gv_data['data']['embeds']['external_storage_service']['attributes']['name']
self.legacy_app_config = settings.ADDONS_AVAILABLE_DICT[self.addon_name]

self.resource = resource
self.auth = auth
self.FOLDER_SELECTED = self.legacy_config.FOLDER_SELECTED
self.NODE_AUTHORIZED = self.legacy_config.NODE_DEAUTHORIZED
self.NODE_DEAUTHORIZED = self.legacy_config.NODE_DEAUTHORIZED
self.actions = self.legacy_config.actions

@property
def node_settings(self):
return self.MockNodeSetting(self.resource, self.auth, self.legacy_config, self)
self.FOLDER_SELECTED = self.legacy_app_config.FOLDER_SELECTED
self.NODE_AUTHORIZED = self.legacy_app_config.NODE_DEAUTHORIZED
self.NODE_DEAUTHORIZED = self.legacy_app_config.NODE_DEAUTHORIZED
self.actions = self.legacy_app_config.actions

@property
def user_settings(self):
return self.MockUserSetting(self.resource, self.auth, self.legacy_config, self)
def config(self):
return self.legacy_app_config

@property
def configured(self):
return True

@property
def config_id(self):
return self.gv_data.config_id
7 changes: 4 additions & 3 deletions api/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ def get_user_auth(request):
"""Given a Django request object, return an ``Auth`` object with the
authenticated user attached to it.
"""
user = request.user
private_key = request.query_params.get('view_only', None)
if user.is_anonymous:
user = getattr(request, 'user', None)
query_params = getattr(request, 'query_params', {})
private_key = query_params.get('view_only', None)
if not user or user.is_anonymous:
auth = Auth(None, private_key=private_key)
else:
auth = Auth(user, private_key=private_key)
Expand Down
15 changes: 12 additions & 3 deletions api/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from collections import defaultdict
from distutils.version import StrictVersion
from framework.auth import Auth

from django_bulk_update.helper import bulk_update
from django.conf import settings as django_settings
Expand Down Expand Up @@ -636,8 +637,12 @@ def bulk_get_file_nodes_from_wb_resp(self, files_list):
for item in files_list:
attrs = item['attributes']
if waffle.flag_is_active(self.request, features.ENABLE_GV):
gv_config = GravyValetAddonAppConfig(self, attrs['provider'], self.request)
short_name = gv_config.legacy_config.short_name
gv_config = GravyValetAddonAppConfig(
self,
attrs['provider'],
auth=get_user_auth(self.request),
)
short_name = gv_config.legacy_app_config.short_name
else:
short_name = attrs['provider']

Expand Down Expand Up @@ -688,7 +693,11 @@ def get_file_node_from_wb_resp(self, item):
"""Takes file data from wb response, touches/updates metadata for it, and returns file object"""
attrs = item['attributes']
if waffle.flag_is_active(self.request, features.ENABLE_GV):
provider_determinent = GravyValetAddonAppConfig(self, attrs['provider'], self.request).legacy_config.name
provider_determinent = GravyValetAddonAppConfig(
self,
attrs['provider'],
get_user_auth(self.request),
).legacy_app_config.short_name
else:
provider_determinent = attrs['provider']

Expand Down
7 changes: 2 additions & 5 deletions osf/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
from website.util.metrics import OsfSourceTags, CampaignSourceTags
from website.util import api_url_for, api_v2_url, web_url_for
from .base import BaseModel, GuidMixin, GuidMixinQuerySet
from api.base.utils import get_user_auth
from api.caching.tasks import update_storage_usage
from api.caching import settings as cache_settings
from api.caching.utils import storage_usage_cache
Expand Down Expand Up @@ -2436,11 +2437,7 @@ def get_addon(self, name, is_deleted=False):
request, user_id = get_request_and_user_id()

if waffle.flag_is_active(request, features.ENABLE_GV):
return GravyValetAddonAppConfig(
self,
name,
auth=Auth(request.user) if getattr(request, 'user', None) else None
).node_settings
return GravyValetAddonAppConfig(self, name, auth=get_user_auth(request))
else:
return super().get_addon(name, is_deleted)

Expand Down
10 changes: 4 additions & 6 deletions osf/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from django.utils import timezone
from guardian.shortcuts import get_objects_for_user

from framework.auth import Auth, signals, utils
from framework.auth import signals, utils
from framework.auth.core import generate_verification_key
from framework.auth.exceptions import (ChangePasswordError, ExpiredTokenError,
InvalidTokenError,
Expand All @@ -37,6 +37,8 @@
from framework.exceptions import PermissionsError
from framework.sessions.utils import remove_sessions_for_user
from api.share.utils import update_share
from api.base.utils import get_user_auth, Auth

from osf.utils.requests import get_current_request
from osf.exceptions import reraise_django_validation_errors, UserStateError
from .base import BaseModel, GuidMixin, GuidMixinQuerySet
Expand Down Expand Up @@ -1228,11 +1230,7 @@ def get_addon(self, name, is_deleted=False):
request, user_id = get_request_and_user_id()

if waffle.flag_is_active(request, features.ENABLE_GV):
return GravyValetAddonAppConfig(
self,
name,
auth=Auth(request.user) if getattr(request, 'user', None) else None
).user_settings
return GravyValetAddonAppConfig(self, name, auth=get_user_auth(request))
else:
return super().get_addon(name, is_deleted)

Expand Down
4 changes: 2 additions & 2 deletions website/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,6 @@ def from_node_usage(cls, usage_bytes, private_limit=None, public_limit=None):
GV_RESOURCE_ENDPOINT = GV_API_ROOT + 'resource-references/?filter[resource_uri]={resource_uri}'
GV_USER_ENDPOINT = GV_API_ROOT + 'user-references/?filter[user_uri]={owner_uri}'
# These two are for `get_addon` vs `get_addons`
GV_USER_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/authorized-storage-accounts/{account_id}'
GV_NODE_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/configured-storage-addons/{config_id}/base_account/'
GV_USER_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/authorized-storage-accounts/{config_id}'
GV_NODE_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/configured-storage-addons/{config_id}/'
GV_EXTERNAL_STORAGE_ENDPOINT = 'http://192.168.168.167:8004/v1/external-storage-services/{service_id}'

0 comments on commit 647f35c

Please sign in to comment.