Skip to content

Commit

Permalink
add mocking for box upgrade for gravyvalet
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed May 9, 2024
1 parent fb266c6 commit 0b9d7f6
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
91 changes: 90 additions & 1 deletion addons/box/apps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,95 @@
import requests
from addons.base.apps import BaseAddonAppConfig, generic_root_folder
from addons.box.settings import MAX_UPLOAD_SIZE

from website.settings import GV_USER_DOMAIN, GV_RESOURCE_DOMAIN

box_root_folder = generic_root_folder('box')


class GravyValetBoxAddonAppConfig():
class MockNodeSetting:
def __init__(self, resource, auth):
resp = requests.get(
GV_RESOURCE_DOMAIN.format(owner_uri=resource.absolute_url),
auth=(auth.user.username, auth.user.password)
)
from addons.box.models import NodeSettings
settings_obj, created = NodeSettings.objects.get_or_create(owner=resource)

# No addon found in GV
if resp.status_code == 404 and created:
resp.raise_for_status()

# addon disabled on GV, but not here
if resp.status_code == 404 or resp.status_code == 410:
settings_obj.delete()
resp.raise_for_status()

data = resp.json()

configured_storage_addon = data[0]['relationships']['configured_storage_addons']['links']['self']
resp = requests.get(
configured_storage_addon,
auth=(auth.user.username, auth.user.password)

)
data = resp.json()
self.folder_id = data['data']['attributes']['root_folder']

# settings_obj.set_folder(folder_id)

class MockUserSetting:
def __init__(self, resource, auth):
resp = requests.get(
GV_USER_DOMAIN.format(owner_uri=resource.absolute_url),
auth=auth
)
from addons.box.models import UserSettings
settings_obj, created = UserSettings.objects.get_or_create(owner=resource)
# No addon found in GV
if resp.status_code == 404 and created:
resp.raise_for_status()

# addon disabled on GV, but not here
if resp.status_code == 404 or resp.status_code == 410:
# Hit super to avoid complex request context bug
from addons.base.models import BaseOAuthUserSettings
super(BaseOAuthUserSettings, settings_obj).delete()

self.owner = resource
# self.oauth_grants = data[]...

def __init__(self, settings, resource, auth):
self.name = settings.name
self.label = settings.label
self.short_name = settings.short_name
self.owners = settings.owners
self.configs = settings.configs
self.categories = settings.categories
self.has_hgrid_files = settings.has_hgrid_files
self.max_file_size = settings.max_file_size
self.resource = settings.resource
self.auth = auth

@property
def get_hgrid_data(self):
return box_root_folder

FOLDER_SELECTED = 'box_folder_selected'
NODE_AUTHORIZED = 'box_node_authorized'
NODE_DEAUTHORIZED = 'box_node_deauthorized'

actions = (FOLDER_SELECTED, NODE_AUTHORIZED, NODE_DEAUTHORIZED, )

@property
def node_settings(self):
return self.MockNodeSetting(self.resource, self.auth)

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


class BoxAddonAppConfig(BaseAddonAppConfig):

name = 'addons.box'
Expand Down Expand Up @@ -38,3 +124,6 @@ def user_settings(self):
@property
def node_settings(self):
return self.get_model('NodeSettings')

def create_mock_config_synced_with_gravy_valet(self, node) -> GravyValetBoxAddonAppConfig:
return GravyValetBoxAddonAppConfig(self, node)
1 change: 1 addition & 0 deletions api/nodes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,7 @@ def get_provider_item(self, storage_addon):
return NodeStorageProvider(self.get_node(), storage_addon.config.short_name, storage_addon)

def get_queryset(self):
raise NotImplementedError()
return [
self.get_provider_item(addon)
for addon
Expand Down
21 changes: 15 additions & 6 deletions osf/models/mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytz
import markupsafe
import logging
import waffle

from django.apps import apps
from django.contrib.auth.models import Group, AnonymousUser
Expand Down Expand Up @@ -31,6 +32,7 @@
from .validators import validate_title
from .tag import Tag
from osf.utils import sanitize
from osf import features
from .validators import validate_subject_hierarchy, validate_email, expand_subject_hierarchy
from osf.utils.fields import NonNaiveDateTimeField
from osf.utils.datetime_aware_jsonfield import DateTimeAwareJSONField
Expand Down Expand Up @@ -511,18 +513,25 @@ def get_or_add_addon(self, name, *args, **kwargs):
return self.add_addon(name, *args, **kwargs)

def get_addon(self, name, is_deleted=False):

try:
settings_model = self._settings_model(name)
except LookupError:
return None

if not settings_model:
return None
try:
settings_obj = settings_model.objects.get(owner=self)
if not settings_obj.is_deleted or is_deleted:
return settings_obj
except ObjectDoesNotExist:
pass

if waffle.switch_is_active(features.ENABLE_GV) and hasattr(settings_model, 'create_mock_config_synced_with_gravy_valet'):
settings_obj = settings_model.create_mock_config_synced_with_gravy_valet(self)
else:
try:
settings_obj = settings_model.objects.get(owner=self)
except ObjectDoesNotExist:
return None
if not settings_obj.is_deleted or is_deleted:
return settings_obj

return None

def add_addon(self, addon_name, auth=None, override=False, _force=False):
Expand Down
2 changes: 2 additions & 0 deletions website/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2142,3 +2142,5 @@ def from_node_usage(cls, usage_bytes, private_limit=None, public_limit=None):

WAFFLE_VALUES_YAML = 'osf/features.yaml'
DEFAULT_DRAFT_NODE_TITLE = 'Untitled'
GV_RESOURCE_DOMAIN = 'http://192.168.168.167:8004/v1/resource-references/?filter[resource_uri]={owner_uri}'
GV_USER_DOMAIN = 'http://192.168.168.167:8004/v1/user-references/?filter[user_uri]={owner_uri}'

0 comments on commit 0b9d7f6

Please sign in to comment.