Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENG-4961] make Boa configurable via API v2 #10490

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/base/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@
VARNISH_SERVERS = osf_settings.VARNISH_SERVERS
ESI_MEDIA_TYPES = osf_settings.ESI_MEDIA_TYPES

ADDONS_FOLDER_CONFIGURABLE = ['box', 'dropbox', 's3', 'googledrive', 'figshare', 'owncloud', 'onedrive']
ADDONS_OAUTH = ADDONS_FOLDER_CONFIGURABLE + ['dataverse', 'github', 'bitbucket', 'gitlab', 'mendeley', 'zotero', 'forward', 'boa']
ADDONS_FOLDER_CONFIGURABLE = ['box', 'dropbox', 's3', 'googledrive', 'figshare', 'owncloud', 'onedrive', 'boa']
ADDONS_OAUTH = ADDONS_FOLDER_CONFIGURABLE + ['dataverse', 'github', 'bitbucket', 'gitlab', 'mendeley', 'zotero', 'forward']

BYPASS_THROTTLE_TOKEN = 'test-token'

Expand Down
42 changes: 42 additions & 0 deletions api/nodes/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db import connection
from distutils.version import StrictVersion
from addons.boa.models import BoaProvider

from boaapi.boa_client import BoaClient, BoaException, BOA_API_ENDPOINT
from api.base.exceptions import (
Conflict, EndpointNotImplementedError,
InvalidModelValueError,
Expand Down Expand Up @@ -1093,6 +1095,46 @@ def update(self, instance, validated_data):
return instance


class BoaNodeAddonSettingsSerializer(NodeAddonSettingsSerializer):

username = ser.CharField(required=False, allow_null=True, write_only=True)
password = ser.CharField(required=False, allow_null=True, write_only=True)

def update(self, instance, validated_data):
instance = super().update(instance, validated_data)
username = validated_data.get('username')
password = validated_data.get('password')
user = self.context['request'].user
if username and password:
boa_client = BoaClient(endpoint=BOA_API_ENDPOINT)

try:
boa_client.login(username, password)
except BoaException:
raise exceptions.PermissionDenied('Raised login error')
boa_client.close()

provider = BoaProvider(
account=None,
host=BOA_API_ENDPOINT,
username=username,
password=password,
)
provider.account.save()
provider.account = ExternalAccount.objects.get(
provider=provider.short_name,
provider_id=f'{BOA_API_ENDPOINT}:{username}'.lower(),
)
if provider.account.oauth_key != password:
provider.account.oauth_key = password
provider.account.save()

if not user.external_accounts.filter(id=provider.account.id).exists():
user.external_accounts.add(provider.account)

return instance


class NodeDetailSerializer(NodeSerializer):
"""
Overrides NodeSerializer to make id required.
Expand Down
7 changes: 6 additions & 1 deletion api/nodes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
from api.nodes.serializers import (
NodeSerializer,
ForwardNodeAddonSettingsSerializer,
BoaNodeAddonSettingsSerializer,
NodeAddonSettingsSerializer,
NodeLinksSerializer,
NodeForksSerializer,
Expand Down Expand Up @@ -1419,8 +1420,12 @@ def get_serializer_class(self):
"""
Use NodeDetailSerializer which requires 'id'
"""
if 'provider' in self.kwargs and self.kwargs['provider'] == 'forward':
provider = self.kwargs.get('provider')

if provider == 'forward':
return ForwardNodeAddonSettingsSerializer
elif provider == 'boa':
return BoaNodeAddonSettingsSerializer
else:
return NodeAddonSettingsSerializer

Expand Down
71 changes: 71 additions & 0 deletions api_tests/addons_tests/boa/test_configure_boa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import mock
import pytest
from framework.auth.core import Auth
from api.base.settings.defaults import API_BASE
from osf_tests.factories import ProjectFactory, AuthUserFactory, ExternalAccountFactory

_mock = lambda attributes: type('MockObject', (mock.Mock,), attributes)

def mock_boa_client():
return _mock({
'login': False
})


@pytest.mark.django_db
class TestBoaConfig:

@pytest.fixture()
def user(self):
return AuthUserFactory()

@pytest.fixture()
def node(self, user):
return ProjectFactory(creator=user)

@pytest.fixture()
def enabled_addon(self, node, user):
addon = node.get_or_add_addon('boa', auth=Auth(user))
addon.save()
return addon

@pytest.fixture()
def node_with_authorized_addon(self, user, node, enabled_addon):
external_account = ExternalAccountFactory(provider='boa')
enabled_addon.external_account = external_account
user_settings = user.get_or_add_addon('boa')
user_settings.save()
enabled_addon.user_settings = user_settings
user.external_accounts.add(external_account)
user.save()
enabled_addon.save()
return node

def test_addon_folders_PATCH(self, app, node_with_authorized_addon, user):

payload = {'data': {'attributes': {'folder_path': 'john.e.tordoff/test-project', 'folder_id': '52343250'}}}

resp = app.patch_json_api(
f'/{API_BASE}nodes/{node_with_authorized_addon._id}/addons/boa/',
payload,
auth=user.auth
)
assert resp.status_code == 200
assert resp.json['data']['attributes']['folder_id'] == 'john.e.tordoff/test-project'
assert resp.json['data']['attributes']['folder_path'] == 'john.e.tordoff/test-project'

def test_addon_credentials_PATCH(self, app, node, user, enabled_addon):
resp = app.patch_json_api(
f'/{API_BASE}nodes/{node._id}/addons/boa/',
{
'data': {
'attributes': {
'username': 'johntordoff',
'password': 'Quez_Watkins',
}
}
},
auth=user.auth
)
assert resp.status_code == 200
assert resp.json['data']['attributes']['external_account_id']
Loading