Beispiel #1
0
def valid_cmr_response(file):
    """Return a valid login response"""
    json_response = common.read_file(file)
    return tutil.MockResponse(json_response)
Beispiel #2
0
    def test_get(self, urlopen_mock):
        """ Test the get method, get a network resource """
        # Setup
        recorded_data_file = tutil.resolve_full_path(
            '../data/cmr/search/one_cmr_result.json')
        urlopen_mock.return_value = valid_cmr_response(recorded_data_file)

        data = net.get("http://cmr.earthdata.nasa.gov/search")
        self.assertEqual(276, (data['hits']), "only required parameters")

        data = net.get("http://cmr.earthdata.nasa.gov/search",
                       accept='application/xml')
        self.assertEqual(276, (data['hits']), "with an accept")

        data = net.get("http://cmr.earthdata.nasa.gov/search",
                       headers={'platforms': 'SMAP'})
        self.assertEqual(276, (data['hits']), "with a header")

        # test that a 200 can be processed with headers by writing to log
        urlopen_mock.return_value = valid_cmr_response(recorded_data_file, 200,
                                                       [('key', 'value')])
        try:
            net.logger.setLevel('DEBUG')
            with self.assertLogs(net.logger, level='DEBUG') as test_log:
                data = net.get("http://cmr.earthdata.nasa.gov/search")
            self.assertEqual(test_log.output, [
                'DEBUG:cmr.util.network: Headers->CMR= None',
                "DEBUG:cmr.util.network: CMR->Headers = {'key': 'value'}"
            ])
        except AssertionError:
            self.fail("no log entry")
        net.logger.setLevel('ERROR')

        # test that a 204 can be processed
        urlopen_mock.return_value = valid_cmr_response(recorded_data_file, 204,
                                                       [('key', 'value')])
        data = net.get("http://cmr.earthdata.nasa.gov/search")
        self.assertEqual({'http-headers': {
            'key': 'value'
        }}, data, "a 204 response")

        # standard tea pot test : error handling
        urlopen_mock.return_value = tutil.MockResponse("I'm a tea pot", 416,
                                                       [('key', 'value')])
        data = net.get("http://cmr.earthdata.nasa.gov/search")
        self.assertEqual("I'm a tea pot", data, "a 416 response")

        # exception handling
        urlopen_mock.side_effect = urlerr.HTTPError(Mock(status=500), "500",
                                                    "Server Error", None, None)
        data = net.get("http://cmr.earthdata.nasa.gov/search")
        expected = {
            'code': '500',
            'reason': 'Server Error',
            'errors': ['Server Error']
        }
        self.assertEqual(expected, data, "exception was not caught")

        # test an error
        urlopen_mock.side_effect = urlerr.HTTPError(Mock(status=500), "500",
                                                    "Unprocessable Entity",
                                                    None, None)
        data = net.get("http://cmr.earthdata.nasa.gov/search/fake")
        expected = {
            'code': '500',
            'reason': 'Unprocessable Entity',
            'errors': ['Unprocessable Entity']
        }
        self.assertEqual(expected, data, "an exeption")
        urlopen_mock.side_effect = None

        # test list response such as the provider list from ingest
        recorded_data_file = tutil.resolve_full_path(
            '../data/cmr/ingest/providers.json')
        urlopen_mock.return_value = valid_cmr_response(recorded_data_file)
        data = net.get(
            "http://cmr.earthdata.nasa.gov/ingest/providers?pretty=true")
        self.assertEqual(110, len(data['items']))
Beispiel #3
0
def valid_cmr_response(file, status=200):
    """return a valid login response"""
    json_response = common.read_file(file)
    return tutil.MockResponse(json_response, status=status)
Beispiel #4
0
    def test_search_by_page(self, clr_scroll_mock, urlopen_mock):
        """
        Test the inner function which performs the first half of a search
        """
        recorded_file = tutil.resolve_full_path(
            '../data/cmr/search/ten_results_from_ghrc.json')
        urlopen_mock.return_value = valid_cmr_response(recorded_file, 200)
        query = {'keyword': 'water'}
        response = scom.search_by_page('collections', query)
        self.assertEqual(10, len(response), 'assumed page_state')

        # page state uses scroll
        page_state = scom.create_page_state(limit=4000)
        urlopen_mock.return_value = valid_cmr_response(
            recorded_file, 200, [('CMR-Scroll-Id', 'si-01')])
        page_state['CMR-Scroll-Id'] = 'abcd'
        response = scom.search_by_page('collections',
                                       query,
                                       page_state=page_state)
        self.assertEqual(20, len(response), 'assumed page_state')

        # error processing 1
        urlopen_mock.return_value = tutil.MockResponse("I'm a tea pot", 418)
        response = scom.search_by_page('collections',
                                       query,
                                       config={'debug': True})
        expected = {
            'errors': ['unknown response: I\'m a tea pot'],
            'code': 0,
            'reason': 'unknown response: I\'m a tea pot'
        }
        self.assertEqual(expected, response, "exeption")

        # error processing 2
        urlopen_mock.return_value = valid_cmr_response('{"errors":["Error"]}',
                                                       500)
        urlopen_mock.side_effect = urlerr.HTTPError(Mock(status=500), "500",
                                                    "Server Error", None, None)
        response = scom.search_by_page('collections', query)
        expected = {
            'code': '500',
            'reason': 'Server Error',
            'errors': ['Server Error']
        }
        self.assertEqual(expected, response, "exeption")

        # bad clear response is logged
        recorded_file = tutil.resolve_full_path(
            '../data/cmr/search/ten_results_from_ghrc.json')
        clr_scroll_mock.return_value = {'errors': ['bad scroll id']}
        urlopen_mock.return_value = valid_cmr_response(recorded_file, 200)
        urlopen_mock.side_effect = None
        response = scom.search_by_page('collections',
                                       query,
                                       page_state=page_state)
        self.assertEqual(10, len(response), "bad scroll id")

        # takes to long
        recorded_file = tutil.resolve_full_path(
            '../data/cmr/search/ten_results_from_ghrc.json')
        page_state['took'] = 300001
        page_state['page_size'] = 1
        urlopen_mock.return_value = valid_cmr_response(recorded_file, 200)
        response = scom.search_by_page('collections',
                                       query,
                                       page_state=page_state)
        self.assertEqual(10, len(response), "bad scroll id")