Exemplo n.º 1
0
 def test_get_collection_two_pages(self):
     mock_requests = MagicMock()
     mock_requests.get.side_effect = [
         fake_response_with_pages(status_code=200,
                       json_return_value={"results": [1, 2, 3]},
                       num_pages=2),
         fake_response_with_pages(status_code=200,
                       json_return_value={"results": [4, 5]},
                       num_pages=2)
     ]
     api = DataServiceApi(auth=None, url="something.com/v1/", http=mock_requests)
     response = api._get_collection(url_suffix="projects", data={}, content_type=ContentType.json)
     self.assertEqual([1, 2, 3, 4, 5], response.json()["results"])
     call_args_list = mock_requests.get.call_args_list
     self.assertEqual(2, len(call_args_list))
     # Check first request
     call_args = call_args_list[0]
     first_param = call_args[0][0]
     self.assertEqual('something.com/v1/projects', first_param)
     dict_param = call_args[1]
     self.assertEqual({'Content-Type': 'application/json'}, dict_param['headers'])
     self.assertIn('"per_page": 100', dict_param['params'])
     # Check second request
     call_args = call_args_list[1]
     first_param = call_args[0][0]
     self.assertEqual('something.com/v1/projects', first_param)
     dict_param = call_args[1]
     self.assertEqual({'Content-Type': 'application/json'}, dict_param['headers'])
     self.assertIn('"per_page": 100', dict_param['params'])
     self.assertIn('"page": 2', dict_param['params'])
Exemplo n.º 2
0
 def test_get_collection_one_page(self):
     mock_requests = MagicMock()
     mock_requests.get.side_effect = [
         fake_response_with_pages(status_code=200,
                                  json_return_value={"results": [1, 2, 3]},
                                  num_pages=1)
     ]
     api = DataServiceApi(auth=None,
                          url="something.com/v1/",
                          http=mock_requests)
     response = api._get_collection(url_suffix="users",
                                    data={},
                                    content_type=ContentType.json)
     self.assertEqual([1, 2, 3], response.json()["results"])
     call_args_list = mock_requests.get.call_args_list
     self.assertEqual(1, len(call_args_list))
     # Check first request
     call_args = call_args_list[0]
     first_param = call_args[0][0]
     self.assertEqual('something.com/v1/users', first_param)
     dict_param = call_args[1]
     self.assertEqual('application/json',
                      dict_param['headers']['Content-Type'])
     self.assertIn("DukeDSClient/", dict_param['headers']['User-Agent'])
     self.assertIn('"per_page": 100', dict_param['params'])
Exemplo n.º 3
0
 def __init__(self, config):
     """
     Setup to allow fetching project tree.
     :param config: ddsc.config.Config settings to use for connecting to the dataservice.
     """
     self.config = config
     auth = DataServiceAuth(self.config)
     self.data_service = DataServiceApi(auth, self.config.url)
Exemplo n.º 4
0
 def test_get_single_page_works_on_paging_response(self):
     mock_requests = MagicMock()
     mock_requests.get.side_effect = [
         fake_response_with_pages(status_code=200, json_return_value={"ok": True}, num_pages=3)
     ]
     api = DataServiceApi(auth=None, url="something.com/v1/", http=mock_requests)
     resp = api._get_single_page(url_suffix='stuff', data={}, content_type=ContentType.json, page_num=1)
     self.assertEqual(True, resp.json()['ok'])
Exemplo n.º 5
0
 def test_get_single_item_raises_error_on_paging_response(self):
     mock_requests = MagicMock()
     mock_requests.get.side_effect = [
         fake_response_with_pages(status_code=200, json_return_value={}, num_pages=3)
     ]
     api = DataServiceApi(auth=None, url="something.com/v1/", http=mock_requests)
     with self.assertRaises(ValueError) as er:
         resp = api._get_single_item(url_suffix='stuff', data={})
     self.assertEqual(UNEXPECTED_PAGING_DATA_RECEIVED, str(er.exception))
Exemplo n.º 6
0
 def rebuild_data_service(config, data_service_auth_data):
     """
     Deserialize value into DataServiceApi object.
     :param config:
     :param data_service_auth_data:
     :return:
     """
     auth = DataServiceAuth(config)
     auth.set_auth_data(data_service_auth_data)
     return DataServiceApi(auth, config.url)
Exemplo n.º 7
0
 def test_get_project_transfers(self):
     return_value = {"results": [{"id": "1234"}]}
     mock_requests = MagicMock()
     mock_requests.get.side_effect = [
         fake_response_with_pages(status_code=200,
                                  json_return_value=return_value,
                                  num_pages=1)
     ]
     api = DataServiceApi(auth=None,
                          url="something.com/v1/",
                          http=mock_requests)
     resp = api.get_project_transfers(project_id='4521')
     self.assertEqual(1, len(resp.json()['results']))
     self.assertEqual("1234", resp.json()['results'][0]['id'])
Exemplo n.º 8
0
 def test_auth_provider_add_user(self):
     user = {
         "id": "abc4e9-9987-47eb-bb4e-19f0203efbf6",
         "username": "******",
     }
     mock_requests = MagicMock()
     mock_requests.post.side_effect = [
         fake_response(status_code=200, json_return_value=user)
     ]
     api = DataServiceApi(auth=None,
                          url="something.com/v1",
                          http=mock_requests)
     result = api.auth_provider_add_user('123', "joe")
     self.assertEqual(200, result.status_code)
     self.assertEqual(user, result.json())
     expected_url = 'something.com/v1/auth_providers/123/affiliates/joe/dds_user/'
     self.assertEqual(expected_url,
                      mock_requests.post.call_args_list[0][0][0])
Exemplo n.º 9
0
def upload_async(data_service_auth_data, config, upload_id, filename, index,
                 num_chunks_to_send, progress_queue):
    """
    Method run in another process called from ParallelChunkProcessor.make_and_start_process.
    :param data_service_auth_data: tuple of auth data for rebuilding DataServiceAuth
    :param config: dds.Config configuration settings to use during upload
    :param upload_id: uuid unique id of the 'upload' we are uploading chunks into
    :param filename: str path to file who's contents we will be uploading
    :param index: int offset into filename where we will start sending bytes from (must multiply by upload_bytes_per_chunk)
    :param num_chunks_to_send: int number of chunks of config.upload_bytes_per_chunk size to send.
    :param progress_queue: ProgressQueue queue to send notifications of progress or errors
    """
    auth = DataServiceAuth(config)
    auth.set_auth_data(data_service_auth_data)
    data_service = DataServiceApi(auth, config.url)
    sender = ChunkSender(data_service, upload_id, filename,
                         config.upload_bytes_per_chunk, index,
                         num_chunks_to_send, progress_queue)
    return sender.send()
Exemplo n.º 10
0
    def authenticate(self, token):
        """
        Authenticate a user with a DukeDS API token. Returns None if no user could be authenticated,
        and sets the errors list with the reasons
        :param token: A JWT token
        :return: an authenticated, populated user if found, or None if not.
        """
        self.failure_reason = None
        # 1. check if token is valid for this purpose
        try:
            check_jwt_token(token)
        except InvalidTokenError as e:
            self.failure_reason = e
            # Token may be expired or may not be valid for this service, so return None
            return None

        # Token is a JWT and not expired
        # 2. Check if token exists in database
        user = get_local_user(token)
        if user:
            # token matched a user, return it
            return user

        # 3. Token appears valid but we have not seen it before.
        # Fetch user details from DukeDS

        config = make_auth_config(token)
        auth = DataServiceAuth(config)
        api = DataServiceApi(auth, config.url)
        try:
            response = api.get_current_user()
            response.raise_for_status()
            user_dict = response.json()
        except HTTPError as e:
            self.failure_reason = e
            return None
        # DukeDS shouldn't stomp over existing user details
        user = self.save_user(user_dict, False)

        # 4. Have a user, save their token
        save_dukeds_token(user, token)
        self.handle_new_user(user, user_dict)
        return user
Exemplo n.º 11
0
 def test_list_auth_roles(self):
     return_value = {
         "results": [{
             "id": "project_admin",
             "name": "Project Admin",
         }]
     }
     mock_requests = MagicMock()
     mock_requests.get.side_effect = [
         fake_response_with_pages(status_code=200,
                                  json_return_value=return_value,
                                  num_pages=1)
     ]
     api = DataServiceApi(auth=None,
                          url="something.com/v1/",
                          http=mock_requests)
     resp = api.get_auth_roles(context='')
     self.assertEqual(1, len(resp.json()['results']))
     self.assertEqual("Project Admin", resp.json()['results'][0]['name'])
Exemplo n.º 12
0
 def test_get_auth_providers(self):
     provider = {
         "id": "aca35ba3-a44a-47c2-8b3b-afe43a88360d",
         "service_id": "cfde039d-f550-47e7-833c-9ebc4e257847",
         "name": "Duke Authentication Service",
         "is_deprecated": True,
         "is_default": True,
         "login_initiation_url": "https://someurl"
     }
     json_results = {"results": [provider]}
     mock_requests = MagicMock()
     mock_requests.get.side_effect = [
         fake_response_with_pages(status_code=200,
                                  json_return_value=json_results,
                                  num_pages=1)
     ]
     api = DataServiceApi(auth=None,
                          url="something.com/v1",
                          http=mock_requests)
     result = api.get_auth_providers()
     self.assertEqual(200, result.status_code)
     self.assertEqual(json_results, result.json())
     self.assertEqual('something.com/v1/auth_providers',
                      mock_requests.get.call_args_list[0][0][0])
Exemplo n.º 13
0
 def test_constructor_creates_session_when_passed_none(self):
     api = DataServiceApi(auth=None, url="something.com/v1/", http=None)
     self.assertIsNotNone(api.http)
     self.assertEqual(type(api.http), requests.sessions.Session)
Exemplo n.º 14
0
    def test_relations_methods(self):
        api = DataServiceApi(auth=None, url="base/v1/", http=MagicMock())
        api._get_collection = MagicMock()
        api._get_single_item = MagicMock()
        api._post = MagicMock()
        api._delete = MagicMock()

        # This endpoint is a little strange with using object_kind and relation types as magic values
        api.get_relations('dds-file', '123')
        api._get_collection.assert_called_with('/relations/dds-file/123', {})
        api.get_relation('124')
        api._get_single_item.assert_called_with('/relations/124/', {})

        api.create_used_relation('125', 'dds-file', '456')
        payload = {
            'entity': {
                'kind': 'dds-file',
                'id': '456'
            },
            'activity': {
                'id': '125'
            }
        }
        api._post.assert_called_with('/relations/used', payload)

        api.create_was_generated_by_relation('126', 'dds-file', '457')
        payload = {
            'entity': {
                'kind': 'dds-file',
                'id': '457'
            },
            'activity': {
                'id': '126'
            }
        }
        api._post.assert_called_with('/relations/was_generated_by', payload)

        api.create_was_generated_by_relation('126', 'dds-file', '457')
        payload = {
            'entity': {
                'kind': 'dds-file',
                'id': '457'
            },
            'activity': {
                'id': '126'
            }
        }
        api._post.assert_called_with('/relations/was_generated_by', payload)

        api.create_was_invalidated_by_relation('127', 'dds-file', '458')
        payload = {
            'entity': {
                'kind': 'dds-file',
                'id': '458'
            },
            'activity': {
                'id': '127'
            }
        }
        api._post.assert_called_with('/relations/was_invalidated_by', payload)

        api.create_was_derived_from_relation('128', 'dds-file', '129',
                                             'dds-file')
        payload = {
            'generated_entity': {
                'kind': 'dds-file',
                'id': '129'
            },
            'used_entity': {
                'kind': 'dds-file',
                'id': '128'
            }
        }
        api._post.assert_called_with('/relations/was_derived_from', payload)

        api.delete_relation('130')
        api._delete.assert_called_with('/relations/130/', {})