Ejemplo n.º 1
0
    def request(self, method, url, **kwargs):
        try:
            url = urljoin(self._host_address, url)
            json = kwargs.get(u"json")

            if json is not None:
                kwargs[u"data"] = json_lib.dumps(_filter_out_none(json))
            if u"json" in kwargs:
                del kwargs[u"json"]

            self._renew_authentication(use_cache=True)

            tries = 0
            max_tries = 2
            while tries < max_tries:
                response, unauthorized = self._try_make_request(method, url, **kwargs)
                tries += 1

                if unauthorized and tries < max_tries:
                    self._renew_authentication()
                    continue

                if response.status_code >= 400:
                    response.raise_for_status()

                if not kwargs.get(u"stream"):
                    response.encoding = u"utf-8"  # setting this manually speeds up read times

                return Py42Response(response)
        except requests.HTTPError as err:
            raise_py42_error(err)
Ejemplo n.º 2
0
 def mock_get(uri, **kwargs):
     if uri == "/api/ServerEnv":
         response = mocker.MagicMock(spec=Response)
         response.text = json.dumps({"stsBaseUrl": STS_BASE_URL})
         response.status_code = 200
     elif uri == "/c42api/v3/auth/jwt":
         response = mocker.MagicMock(spec=Response)
         response.text = json.dumps({"data": {"v3_user_token": V3_TOKEN}})
         response.status_code = 200
     return Py42Response(response)
Ejemplo n.º 3
0
 def mock_post(uri, **kwargs):
     response = mocker.MagicMock(spec=Response)
     response.text = json.dumps({
         "data": {
             "loginToken": TMP_LOGIN_TOKEN,
             "serverUrl": STORAGE_HOST_ADDRESS
         }
     })
     response.status_code = 200
     return Py42Response(response)
Ejemplo n.º 4
0
def mock_get_restore_status_responses(mocker, storage_archive_client,
                                      json_responses):
    responses = []
    for json_response in json_responses:
        get_restore_status_response = mocker.MagicMock(spec=Response)
        get_restore_status_response.text = json_response
        get_restore_status_response.status_code = 200
        responses.append(Py42Response(get_restore_status_response))

    storage_archive_client.get_restore_status.side_effect = responses
Ejemplo n.º 5
0
    def mock_get_file_path_metadata(*args, **kwargs):

        if not args[0] == session_id:
            raise Exception("Unexpected archive session ID")

        if not args[1] == device_guid:
            raise Exception("Unexpected device GUID")

        file_id = kwargs["file_id"]

        if file_id not in file_id_responses:
            raise Exception(
                "Unexpected request with file_id: {0}".format(file_id))

        mock_response = mocker.MagicMock(spec=Response)
        mock_response.status_code = 200
        mock_response.text = file_id_responses[file_id]
        get_file_path_metadata_response = Py42Response(mock_response)

        return get_file_path_metadata_response
Ejemplo n.º 6
0
 def test_status_code_returns_expected_url(self, mock_response_not_json):
     response = Py42Response(mock_response_not_json)
     assert response.url == "http://www.example.com"
Ejemplo n.º 7
0
 def mock_get_orgs_empty_response(self, mocker):
     response = mocker.MagicMock(spec=Response)
     response.status_code = 200
     response.encoding = "utf-8"
     response.text = MOCK_EMPTY_GET_ORGS_RESPONSE
     return Py42Response(response)
Ejemplo n.º 8
0
 def test_iter_content_calls_request_iter_content(self,
                                                  mock_response_not_json):
     response = Py42Response(mock_response_not_json)
     response.iter_content(128, True)
     mock_response_not_json.iter_content.assert_called_once_with(
         chunk_size=128, decode_unicode=True)
Ejemplo n.º 9
0
 def mock_get_all_matter_custodians_empty_response(self, mocker):
     response = mocker.MagicMock(spec=Response)
     response.status_code = 200
     response.encoding = "utf-8"
     response.text = MOCK_EMPTY_GET_ALL_MATTER_CUSTODIANS_RESPONSE
     return Py42Response(response)
Ejemplo n.º 10
0
 def mock_start_restore(device_guid, session_id, path_set, num_files,
                        num_dires, size, **kwargs):
     start_restore_response = mocker.MagicMock(spec=Response)
     start_restore_response.text = response
     start_restore_response.status_code = 200
     return Py42Response(start_restore_response)
Ejemplo n.º 11
0
 def mock_get(uri, **kwargs):
     response = mocker.MagicMock(spec=Response)
     response.text = json.dumps({"data": {"v3_user_token": V3_TOKEN}})
     response.status_code = 200
     return Py42Response(response)
Ejemplo n.º 12
0
 def mock_get_all_empty_response(self, mocker, py42_response):
     response = mocker.MagicMock(spec=Response)
     response.status_code = 200
     response.encoding = "utf-8"
     response.text = MOCK_EMPTY_GET_DEVICE_RESPONSE
     return Py42Response(response)
Ejemplo n.º 13
0
 def test_status_code_returns_expected_value(self, mock_response_not_json):
     response = Py42Response(mock_response_not_json)
     assert response.status_code == 200
Ejemplo n.º 14
0
 def mock_get(uri, **kwargs):
     response = mocker.MagicMock(spec=Response)
     response.text = json.dumps({})
     response.status_code = 200
     return Py42Response(response)
Ejemplo n.º 15
0
 def test_raw_text_no_data_node_returns_raw_json_no_data_node(
         self, mock_response_not_json):
     response = Py42Response(mock_response_not_json)
     assert response.raw_text == PLAIN_TEXT
Ejemplo n.º 16
0
 def test_raw_text_with_data_node_returns_raw_json_with_data_node(
         self, mock_response_list_data_node):
     response = Py42Response(mock_response_list_data_node)
     assert response.raw_text == JSON_LIST_WITH_DATA_NODE
Ejemplo n.º 17
0
 def test_text_json_no_data_node_returns_raw_json(
         self, mock_response_list_no_data_node):
     response = Py42Response(mock_response_list_no_data_node)
     assert response.text == JSON_LIST_NO_DATA_NODE
Ejemplo n.º 18
0
 def test_getitem_returns_dict_keys_no_data_node(
         self, mock_response_dict_no_data_node):
     response = Py42Response(mock_response_dict_no_data_node)
     assert type(response["item_list_key"]) == dict
Ejemplo n.º 19
0
 def test_getitem_returns_list_items_with_data_node(
         self, mock_response_list_data_node):
     response = Py42Response(mock_response_list_data_node)
     assert type(response["item_list_key"]) == list
Ejemplo n.º 20
0
 def mock_post(uri, **kwargs):
     response = mocker.MagicMock(spec=Response)
     response.text = json.dumps({"data": [V1_TOKEN_PART1, V1_TOKEN_PART2]})
     response.status_code = 200
     return Py42Response(response)
Ejemplo n.º 21
0
 def test_headers_returns_request_headers(self, mock_response_not_json):
     response = Py42Response(mock_response_not_json)
     assert response.headers == mock_response_not_json.headers
Ejemplo n.º 22
0
 def mock_get_all_matters_response(self, mocker):
     response = mocker.MagicMock(spec=Response)
     response.status_code = 200
     response.encoding = "utf-8"
     response.text = MOCK_GET_ALL_MATTERS_RESPONSE
     return Py42Response(response)
Ejemplo n.º 23
0
 def post_api_mock_response(self, mocker):
     response = mocker.MagicMock(spec=Response)
     response.status_code = 200
     response.encoding = "utf-8"
     response.text = MOCK_text
     return Py42Response(response)