Skip to content

Commit

Permalink
Remove 2 hour download limit (#242)
Browse files Browse the repository at this point in the history
Adds code to refresh DDS token using OAuth token.
Upgrades DukeDSClient requirement for Duke-GCB/DukeDSClient#314 changes.
  • Loading branch information
johnbradley authored Nov 19, 2020
1 parent 0d60102 commit 8e835d5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
46 changes: 37 additions & 9 deletions download_service/tests_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.test.testcases import TestCase
from download_service.utils import make_client
from download_service.utils import make_client, CustomOAuthDataServiceAuth
from gcb_web_auth.models import DDSUserCredential, DDSEndpoint
from django.contrib.auth.models import User
from unittest.mock import patch, call, Mock
Expand Down Expand Up @@ -30,17 +30,45 @@ def test_makes_client_when_from_existing_dds_user_credential(self, mock_client,
self.assertEqual(mock_client.call_args, call(config=mock_get_dds_config_for_credentials.return_value))
self.assertEqual(client, mock_client.return_value)

@patch('download_service.utils.get_dds_token')
@patch('download_service.utils.make_auth_config')
@patch('download_service.utils.CustomOAuthDataServiceAuth')
@patch('download_service.utils.get_dds_config_for_credentials')
@patch('download_service.utils.get_oauth_token')
@patch('download_service.utils.get_default_dds_endpoint')
@patch('download_service.utils.Client')
def test_makes_client_from_dds_token_when_no_dds_user_credential(self, mock_client, mock_make_auth_config, mock_get_dds_token):
def test_makes_client_from_dds_token_when_no_dds_user_credential(self, mock_client, mock_get_default_dds_endpoint,
mock_get_oauth_token,
mock_get_dds_config_for_credentials,
mock_custom_oauth_data_service_auth):
mock_get_default_dds_endpoint.return_value = Mock(
api_root='somehost',
openid_provider_service_id='12345'
)
self.assertEqual(DDSUserCredential.objects.count(), 0)
mock_key = Mock()
mock_get_dds_token.return_value.key = mock_key
client = make_client(self.user)
self.assertEqual(mock_get_dds_token.call_args, call(self.user))
self.assertEqual(mock_make_auth_config.call_args, call(mock_key))
self.assertEqual(mock_client.call_args, call(config=mock_make_auth_config.return_value))
self.assertEqual(client, mock_client.return_value)
client_config = mock_client.call_args[1]['config']
self.assertEqual(client_config.url, 'somehost')
create_data_service_auth = mock_client.call_args[1]['create_data_service_auth']
data_service_auth = create_data_service_auth(None)
self.assertEqual(data_service_auth, mock_custom_oauth_data_service_auth.return_value)
mock_custom_oauth_data_service_auth.assert_called_with(self.user, '12345', None, set_status_msg=print)


class TestCustomOAuthDataServiceAuth(TestCase):
def setUp(self):
self.user = Mock()
self.authentication_service_id = '1234'
self.config = Mock()

@patch('download_service.utils.get_oauth_token')
def test_create_oauth_access_token(self, mock_get_oauth_token):
mock_get_oauth_token.return_value.token_dict = {
'access_token': '5678'
}
auth = CustomOAuthDataServiceAuth(self.user, self.authentication_service_id, self.config)
token = auth.create_oauth_access_token()
self.assertEqual(token, '5678')

def test_get_authentication_service_id(self):
auth = CustomOAuthDataServiceAuth(self.user, self.authentication_service_id, self.config)
self.assertEqual(auth.get_authentication_service_id(), '1234')
35 changes: 30 additions & 5 deletions download_service/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
from ddsc.sdk.client import Client
from ddsc.core.ddsapi import OAuthDataServiceAuth
from ddsc.config import Config
from gcb_web_auth.models import DDSUserCredential
from gcb_web_auth.utils import get_dds_config_for_credentials, get_dds_token, make_auth_config
from gcb_web_auth.utils import get_dds_config_for_credentials, get_oauth_token, get_default_dds_endpoint


def make_client(user):
try:
dds_credential = DDSUserCredential.objects.get(user=user)
config = get_dds_config_for_credentials(dds_credential)
return Client(config=config)
except DDSUserCredential.DoesNotExist:
# No DDSUserCredential configured for this user, fall back to OAuth
# May raise an OAuthConfigurationException
dds_token = get_dds_token(user)
config = make_auth_config(dds_token.key)
client = Client(config=config)
return client
endpoint = get_default_dds_endpoint()
config = Config()
config.update_properties({
Config.URL: endpoint.api_root,
})
authentication_service_id = endpoint.openid_provider_service_id

def create_data_service_auth(config, set_status_msg=print):
return CustomOAuthDataServiceAuth(user, authentication_service_id, config, set_status_msg=set_status_msg)

return Client(config=config, create_data_service_auth=create_data_service_auth)


class CustomOAuthDataServiceAuth(OAuthDataServiceAuth):
def __init__(self, user, authentication_service_id, config, set_status_msg=print):
super().__init__(config, set_status_msg)
self._auth = None
self.user = user
self.authentication_service_id = authentication_service_id

def create_oauth_access_token(self):
oauth_token = get_oauth_token(self.user)
return oauth_token.token_dict.get('access_token')

def get_authentication_service_id(self):
return self.authentication_service_id
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ django-simple-history==1.9.1
django-sslserver==0.19
djangorestframework==3.9.1
drf-ember-backend==1.1
DukeDSClient==3.0.0
DukeDSClient==3.1.0
enum34==1.1.6
funcsigs==0.4
future==0.16.0
Expand Down

0 comments on commit 8e835d5

Please sign in to comment.