Python httpretty.Response() Examples

The following are 30 code examples of httpretty.Response(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httpretty , or try the search function .
Example #1
Source File: test_task_projects.py    From grimoirelab-sirmordred with GNU General Public License v3.0 6 votes vote down vote up
def setup_http_server():
    url_projects = read_file(URL_PROJECTS_FILE)

    http_requests = []

    def request_callback(method, uri, headers):
        last_request = httpretty.last_request()

        body = url_projects
        http_requests.append(last_request)

        return 200, headers, body

    httpretty.register_uri(httpretty.GET,
                           PROJECTS_URL,
                           responses=[
                               httpretty.Response(body=request_callback)
                           ])

    return http_requests 
Example #2
Source File: mixins.py    From ecommerce with GNU Affero General Public License v3.0 6 votes vote down vote up
def mock_course_runs_contains_endpoint_failure(self, course_run_ids, catalog_id, error, discovery_api_url):
        """
        Helper function to register a discovery API endpoint with failure
        for getting course runs information.
        """
        def callback(request, uri, headers):  # pylint: disable=unused-argument
            raise error

        catalog_contains_course_run_url = '{}catalogs/{}/contains/?course_run_id={}'.format(
            discovery_api_url,
            catalog_id,
            ','.join(course_run_id for course_run_id in course_run_ids),
        )
        httpretty.register_uri(
            method=httpretty.GET,
            uri=catalog_contains_course_run_url,
            responses=[
                httpretty.Response(body=callback, content_type='application/json', status_code=500)
            ]
        ) 
Example #3
Source File: test_edx_api_client.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_non_fatal_error(self):
        self.prepare_for_token_request()
        httpretty.register_uri('GET', FAKE_RESOURCE_URL,
                               responses=[
                                   httpretty.Response(body='Too many requests!', status=429),
                                   httpretty.Response(body='Unavailable', status=503),
                                   httpretty.Response(body='{}', status=200)
                               ])

        responses = list(self.client.paginated_get(FAKE_RESOURCE_URL))
        self.assertEqual(len(responses), 1)
        response = responses[0]
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {})

        num_auth_token_requests = 1
        num_failed_requests = 2
        num_successful_requests = 1
        total_expected_requests = num_auth_token_requests + num_failed_requests + num_successful_requests
        self.assertEqual(len(httpretty.httpretty.latest_requests), total_expected_requests) 
Example #4
Source File: test_paypal.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_report_timeout(self, mock_time):
        responses = list(self.RESPONSES)

        responses.insert(0, self.create_runreport_response(1, 'Report has been created'))
        httpretty.register_uri(
            httpretty.POST,
            TEST_URL,
            responses=[
                httpretty.Response(body=r)
                for r in responses
            ]
        )
        arbitrary_time = 123456789012.1234
        mock_time.time.side_effect = [arbitrary_time, arbitrary_time + 5]

        with self.assertRaises(PaypalTimeoutError):
            self.task.run() 
Example #5
Source File: test_paypal.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_delayed_report(self, mock_time):
        responses = list(self.RESPONSES)

        responses.insert(0, self.create_runreport_response(1, 'Report has been created'))
        responses.insert(1, self.create_results_response())
        httpretty.register_uri(
            httpretty.POST,
            TEST_URL,
            responses=[
                httpretty.Response(body=r)
                for r in responses
            ]
        )
        mock_time.time.return_value = 123456789012.1
        self.task.run()

        self.assertEqual(mock_time.time.call_count, 1)
        mock_time.sleep.assert_has_calls([
            call(5),
            call(5)
        ])

        expected_record = ['2015-08-28', 'paypal', 'testing', 'EDX-123456', 'USD', '50.00', '1.40', 'sale',
                           'instant_transfer', 'paypal', '1FW12345678901234']
        self.assertEquals(self.output_target.value.strip(), '\t'.join(expected_record)) 
Example #6
Source File: test_paypal.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_delayed_report(self, mock_time):
        responses = list(self.RESPONSES)

        responses.insert(0, self.create_runreport_response(1, 'Report has been created'))
        responses.insert(1, self.create_results_response())
        httpretty.register_uri(
            httpretty.POST,
            TEST_URL,
            responses=[
                httpretty.Response(body=r)
                for r in responses
            ]
        )
        mock_time.time.return_value = 123456789012.1
        self.task.run()

        self.assertEqual(mock_time.time.call_count, 1)
        mock_time.sleep.assert_has_calls([
            call(5),
            call(5)
        ])

        expected_record = ['2015-08-28', 'paypal', 'testing', 'EDX-123456', 'USD', '50.00', '1.40', 'sale',
                           'instant_transfer', 'paypal', '1FW12345678901234']
        self.assertEquals(self.output_target.value.strip(), '\t'.join(expected_record)) 
Example #7
Source File: test_auth.py    From msrestazure-for-python with MIT License 6 votes vote down vote up
def test_msi_vm_imds_retry(self):
        json_payload = {
            'token_type': "TokenTypeIMDS",
            "access_token": "AccessToken"
        }
        httpretty.register_uri(httpretty.GET,
                               'http://169.254.169.254/metadata/identity/oauth2/token',
                               responses=[
                                   httpretty.Response('', status=404),
                                   httpretty.Response('', status=429),
                                   httpretty.Response('', status=599),
                                   httpretty.Response(body=json.dumps(json_payload)),
                               ],
                               content_type="application/json")

        credentials = MSIAuthentication()
        assert credentials.scheme == "TokenTypeIMDS"
        assert credentials.token == json_payload

        # Assert four requests made only
        assert len(httpretty.httpretty.latest_requests) == 4 
Example #8
Source File: test_task_identities.py    From grimoirelab-sirmordred with GNU General Public License v3.0 6 votes vote down vote up
def setup_http_server():
    remote_identities = read_file(REMOTE_IDENTITIES_FILE)

    http_requests = []

    def request_callback(method, uri, headers):
        last_request = httpretty.last_request()

        if uri.startswith(REMOTE_IDENTITIES_FILE_URL):
            body = remote_identities
        http_requests.append(last_request)

        return 200, headers, body

    httpretty.register_uri(httpretty.GET,
                           REMOTE_IDENTITIES_FILE_URL,
                           responses=[
                               httpretty.Response(body=request_callback)
                           ])

    return http_requests 
Example #9
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_redirect_delete(self):

        url = self.client.format_url("/get_endpoint")
        request = self.client.delete(url, {'check':True})

        httpretty.register_uri(httpretty.DELETE, 'https://my_service.com/http/success/200', status=200)
        httpretty.register_uri(httpretty.DELETE, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="", status=307, method='DELETE', location='/http/success/200'),
                                ])


        response = self.client.send(request)
        assert response.status_code == 200, "Should redirect on 307 with location header"
        assert response.request.method == 'DELETE'

        assert response.history[0].status_code == 307
        assert response.history[0].is_redirect

        httpretty.reset()
        httpretty.register_uri(httpretty.DELETE, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="", status=307, method='DELETE'),
                                ])

        response = self.client.send(request)
        assert response.status_code == 307, "Should not redirect on 307 without location header"
        assert response.history == []
        assert not response.is_redirect 
Example #10
Source File: test_rss.py    From grimoirelab-perceval with GNU General Public License v3.0 5 votes vote down vote up
def configure_http_server():
    bodies_entries_job = read_file('data/rss/rss_entries.xml')

    http_requests = []

    def request_callback(method, uri, headers):
        last_request = httpretty.last_request()

        if uri.startswith(RSS_FEED_URL):
            body = bodies_entries_job
        else:
            body = ''

        requests_http.append(httpretty.last_request())

        http_requests.append(last_request)

        return (200, headers, body)

    httpretty.register_uri(httpretty.GET,
                           RSS_FEED_URL,
                           responses=[
                               httpretty.Response(body=request_callback)
                               for _ in range(2)
                           ])

    return http_requests 
Example #11
Source File: test_stackexchange.py    From grimoirelab-perceval with GNU General Public License v3.0 5 votes vote down vote up
def test_backoff_waiting(self):
        """Test if the clients waits some seconds when backoff field is received"""

        backoff_page = read_file('data/stackexchange/stackexchange_question_backoff_page')
        question_page = read_file('data/stackexchange/stackexchange_question_page_2')

        def request_callback(method, uri, headers):
            params = urllib.parse.parse_qs(urllib.parse.urlparse(uri).query)
            page = params.get('page')[0]
            body = backoff_page if page == '1' else question_page
            return (200, headers, body)

        httpretty.register_uri(httpretty.GET,
                               STACKEXCHANGE_QUESTIONS_URL,
                               responses=[
                                   httpretty.Response(body=request_callback)
                               ])

        client = StackExchangeClient(site="stackoverflow",
                                     tagged="python",
                                     token="aaa", max_questions=1)

        before = time.time()
        raw_pages = [question for question in client.get_questions(from_date=None)]
        after = time.time()

        self.assertEqual(len(raw_pages), 2)

        # backoff value harcoded in the JSON
        diff = after - before
        self.assertGreaterEqual(diff, 0.2) 
Example #12
Source File: test_discourse.py    From grimoirelab-perceval with GNU General Public License v3.0 5 votes vote down vote up
def test_fetch_topic_last_posted_at_null_from_archive(self):
        """Test whether list of topics is returned from the archive when a topic has last_posted_at null"""

        bodies_topics = [read_file('data/discourse/discourse_topics_last_posted_at_null.json'),
                         read_file('data/discourse/discourse_topics_empty.json')]
        body_topic_1149 = read_file('data/discourse/discourse_topic_1149.json')

        def request_callback(method, uri, headers):
            if uri.startswith(DISCOURSE_TOPICS_URL):
                body = bodies_topics.pop(0)
            elif uri.startswith(DISCOURSE_TOPIC_URL_1149):
                body = body_topic_1149
            else:
                raise Exception
            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               responses=[
                                   httpretty.Response(body=request_callback)
                                   for _ in range(2)
                               ])
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPIC_URL_1149,
                               responses=[
                                   httpretty.Response(body=request_callback)
                               ])

        # On this tests two topics will be retrieved.
        # One of them has last_posted_at with null
        self._test_fetch_from_archive() 
Example #13
Source File: test_githubql.py    From grimoirelab-perceval with GNU General Public License v3.0 5 votes vote down vote up
def test_events_pagination(self):
        """Test whether the GraphQL API call works properly on paginated results"""

        requests = []

        events_page_1 = read_file('data/github/github_events_page_1')
        events_page_2 = read_file('data/github/github_events_page_2')
        bodies_json = [events_page_1, events_page_2]

        def request_callback(method, uri, headers):
            body = bodies_json.pop(0)
            requests.append(httpretty.last_request())
            return 200, headers, body

        rate_limit = read_file('data/github/rate_limit')

        httpretty.register_uri(httpretty.GET,
                               GITHUB_RATE_LIMIT,
                               body=rate_limit,
                               status=200,
                               forcing_headers={
                                   'X-RateLimit-Remaining': '20',
                                   'X-RateLimit-Reset': '15'
                               })
        httpretty.register_uri(httpretty.POST,
                               GITHUB_API_GRAPHQL_URL,
                               responses=[httpretty.Response(body=request_callback)
                                          for _ in range(2)],
                               status=200,
                               forcing_headers={
                                   'X-RateLimit-Remaining': '20',
                                   'X-RateLimit-Reset': '15'
                               })

        client = GitHubQLClient("zhquan_example", "repo", ["aaa"], None)
        events = [event for event in client.events(issue_number=1, is_pull=False, from_date=DEFAULT_DATETIME)]
        self.assertEqual(len(events[0]), 2)
        self.assertEqual(len(events[1]), 2)
        self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa") 
Example #14
Source File: test_telegram.py    From grimoirelab-perceval with GNU General Public License v3.0 5 votes vote down vote up
def setup_http_server():
    """Setup a mock HTTP server"""

    http_requests = []

    body_msgs = read_file('data/telegram/telegram_messages.json')
    body_msgs_next = read_file('data/telegram/telegram_messages_next.json')
    body_msgs_empty = read_file('data/telegram/telegram_messages_empty.json')

    def request_callback(method, uri, headers):
        params = urllib.parse.parse_qs(urllib.parse.urlparse(uri).query)

        if 'offset' in params and params['offset'] == ['319280321']:
            body = body_msgs_next
        elif 'offset' in params and params['offset'] == ['319280324']:
            body = body_msgs_empty
        else:
            body = body_msgs

        http_requests.append(httpretty.last_request())

        return 200, headers, body

    httpretty.register_uri(httpretty.GET,
                           TELEGRAM_UPDATES_URL,
                           responses=[
                               httpretty.Response(body=request_callback)
                           ])

    return http_requests 
Example #15
Source File: mixins.py    From ecommerce with GNU Affero General Public License v3.0 5 votes vote down vote up
def mock_discovery_api_failure(self, error, discovery_api_url, catalog_id=None):
        """
        Helper function to register discovery API endpoint for catalogs
        with failure.
        """
        def callback(request, uri, headers):  # pylint: disable=unused-argument
            raise error

        httpretty.register_uri(
            method=httpretty.GET,
            uri=self.build_discovery_catalogs_url(discovery_api_url, catalog_id),
            responses=[
                httpretty.Response(body=callback, content_type='application/json', status_code=500)
            ]
        ) 
Example #16
Source File: bluegreen_test.py    From tsuru-bluegreen with MIT License 5 votes vote down vote up
def test_remove_units_should_return_false_when_doesnt_remove_all_process_types(self):
    self.bg.total_units = MagicMock(return_value={'web': 2, 'resque': 1})

    httpretty.register_uri(httpretty.DELETE, 'http://tsuruhost.com/apps/xpto/units',
                           data='',
                           responses=[
                               httpretty.Response(body='', status=500),
                               httpretty.Response(body='', status=200)
                           ])

    self.assertFalse(self.bg.remove_units('xpto'))

    requests = httpretty.HTTPretty.latest_requests
    self.assertEqual(len(requests), 2) 
Example #17
Source File: bluegreen_test.py    From tsuru-bluegreen with MIT License 5 votes vote down vote up
def test_remove_units_should_return_false_when_doesnt_add_all_process_types(self):
    self.bg.total_units = MagicMock(return_value={'web': 2, 'resque': 1})

    httpretty.register_uri(httpretty.PUT, 'http://tsuruhost.com/apps/xpto/units',
                           data='',
                           responses=[
                               httpretty.Response(body='', status=500),
                               httpretty.Response(body='', status=200)
                           ])

    self.assertFalse(self.bg.add_units('xpto', {'web': 3, 'resque': 2}))

    requests = httpretty.HTTPretty.latest_requests
    self.assertEqual(len(requests), 2) 
Example #18
Source File: test_paypal.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_normal_run(self):
        httpretty.register_uri(
            httpretty.POST,
            TEST_URL,
            responses=[
                httpretty.Response(body=r)
                for r in self.RESPONSES
            ]
        )

        self.task.run()

        expected_record = ['2015-08-28', 'paypal', 'testing', 'EDX-123456', 'USD', '50.00', '1.40', 'sale',
                           'instant_transfer', 'paypal', '1FW12345678901234']
        self.assertEquals(self.output_target.value.strip(), '\t'.join(expected_record)) 
Example #19
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_redirect_post(self):

        url = self.client.format_url("/get_endpoint")
        request = self.client.post(url, {'check':True})

        httpretty.register_uri(httpretty.GET, 'https://my_service.com/http/success/get/200', status=200)
        httpretty.register_uri(httpretty.POST, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="", status=303, method='POST', location='/http/success/get/200'),
                                ])


        response = self.client.send(request)
        assert response.status_code == 200, "Should redirect with GET on 303 with location header"
        assert response.request.method == 'GET'

        assert response.history[0].status_code == 303
        assert response.history[0].is_redirect

        httpretty.reset()
        httpretty.register_uri(httpretty.POST, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="", status=303, method='POST'),
                                ])

        response = self.client.send(request)
        assert response.status_code == 303, "Should not redirect on 303 without location header"
        assert response.history == []
        assert not response.is_redirect 
Example #20
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_redirect_head(self):

        url = self.client.format_url("/get_endpoint")
        request = self.client.head(url, {'check':True})

        httpretty.register_uri(httpretty.HEAD, 'https://my_service.com/http/success/200', status=200)
        httpretty.register_uri(httpretty.HEAD, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="", status=307, method='HEAD', location='/http/success/200'),
                                ])


        response = self.client.send(request)
        assert response.status_code == 200, "Should redirect on 307 with location header"
        assert response.request.method == 'HEAD'

        assert response.history[0].status_code == 307
        assert response.history[0].is_redirect

        httpretty.reset()
        httpretty.register_uri(httpretty.HEAD, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="", status=307, method='HEAD'),
                                ])

        response = self.client.send(request)
        assert response.status_code == 307, "Should not redirect on 307 without location header"
        assert response.history == []
        assert not response.is_redirect 
Example #21
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_retry_501(self):
        httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint",
                           responses=[
                                httpretty.Response(body="retry response", status=501),
                                httpretty.Response(body='success response', status=202),
                                ])

        response = self.client.send(self.request)
        self.assertEqual(response.status_code, 501, msg="Shouldn't retry on 501") 
Example #22
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_retry_404(self):
        httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint",
                           responses=[
                                httpretty.Response(body="retry response", status=404),
                                httpretty.Response(body='success response', status=202),
                                ])

        response = self.client.send(self.request)
        self.assertEqual(response.status_code, 404, msg="Shouldn't retry on 404") 
Example #23
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_retry_max(self):
        httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="retry response", status=502),
                                httpretty.Response(body="retry response", status=502),
                                httpretty.Response(body="retry response", status=502),
                                httpretty.Response(body="retry response", status=502),
                                ])

        with self.assertRaises(ClientRequestError, msg="Max retries reached"):
            self.client.send(self.request) 
Example #24
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_retry_3_times(self):
        httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="retry response", status=502),
                                httpretty.Response(body="retry response", status=502),
                                httpretty.Response(body="retry response", status=502),
                                httpretty.Response(body='success response', status=202),
                                ])

        response = self.client.send(self.request)
        self.assertEqual(response.status_code, 202, msg="Should retry 3 times") 
Example #25
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_retry_408(self):
        httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="retry response", status=408),
                                httpretty.Response(body='success response', status=202),
                                ])
        response = self.client.send(self.request)
        self.assertEqual(response.status_code, 202, msg="Should retry on 408") 
Example #26
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_redirect_get(self):

        url = self.client.format_url("/get_endpoint")
        request = self.client.get(url, {'check':True})

        httpretty.register_uri(httpretty.GET, "https://my_service.com/http/finished",
                        responses=[
                        httpretty.Response(body="", status=200, method='GET'),
                        ])

        httpretty.register_uri(httpretty.GET, "https://my_service.com/http/redirect3",
                        responses=[
                        httpretty.Response(body="", status=307, method='GET', location='/http/finished'),
                        ])

        httpretty.register_uri(httpretty.GET, "https://my_service.com/http/redirect2",
                        responses=[
                        httpretty.Response(body="", status=307, method='GET', location='/http/redirect3'),
                        ])

        httpretty.register_uri(httpretty.GET, "https://my_service.com/http/redirect1",
                        responses=[
                        httpretty.Response(body="", status=307, method='GET', location='/http/redirect2'),
                        ])

        httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint",
                        responses=[
                        httpretty.Response(body="", status=307, method='GET', location='/http/redirect1'),
                        ])

        with self.assertRaises(ClientRequestError, msg="Should exceed maximum redirects"):
            self.client.send(request) 
Example #27
Source File: test_runtime.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_request_redirect_put(self):

        url = self.client.format_url("/get_endpoint")
        request = self.client.put(url, {'check':True})

        httpretty.register_uri(httpretty.PUT, "https://my_service.com/get_endpoint",
                                responses=[
                                httpretty.Response(body="", status=305, method='PUT', location='/http/success/200'),
                                ])

        response = self.client.send(request)
        assert response.status_code == 305, "Should not redirect on 305"
        assert response.history == []
        assert not response.is_redirect 
Example #28
Source File: test_jira.py    From grimoirelab-perceval with GNU General Public License v3.0 4 votes vote down vote up
def test_get_comments(self):
        """Test get comments API call"""

        bodies_json = [read_file('data/jira/jira_comments_issue_page_1.json'),
                       read_file('data/jira/jira_comments_issue_page_2.json')]

        bodies = bodies_json[:]
        bodies = list(bodies_json)

        def request_callback(method, uri, headers):
            body = bodies.pop(0)
            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               JIRA_ISSUE_1_COMMENTS_URL,
                               responses=[httpretty.Response(body=request_callback)
                                          for _ in range(2)])

        client = JiraClient(url='http://example.com', project='perceval',
                            user='user', password='password',
                            ssl_verify=False, cert=None, max_results=2)

        pages = [page for page in client.get_comments("1")]

        expected_req = [
            {
                'jql': ['project = perceval AND updated > 0 order by updated asc'],
                'maxResults': ['2'],
                'startAt': ['0']
            },
            {
                'jql': ['project = perceval AND updated > 0 order by updated asc'],
                'maxResults': ['2'],
                'startAt': ['2']
            }
        ]

        self.assertEqual(len(pages), 2)

        requests = httpretty.HTTPretty.latest_requests
        request = requests[0]
        self.assertEqual(request.method, 'GET')
        self.assertRegex(request.path, '/rest/api/2/issue/1/comment')
        self.assertDictEqual(request.querystring, expected_req[0])

        request = requests[1]
        self.assertEqual(request.method, 'GET')
        self.assertRegex(request.path, '/rest/api/2/issue/1/comment')
        self.assertDictEqual(request.querystring, expected_req[1])

        self.assertEqual(pages[0], bodies_json[0])
        self.assertEqual(pages[1], bodies_json[1]) 
Example #29
Source File: test_jira.py    From grimoirelab-perceval with GNU General Public License v3.0 4 votes vote down vote up
def test_get_issues(self):
        """Test get issues API call"""

        from_date = str_to_datetime('2015-01-01')

        requests = []

        bodies_json = [read_file('data/jira/jira_issues_page_1.json'),
                       read_file('data/jira/jira_issues_page_2.json')]

        bodies = bodies_json[:]
        bodies = list(bodies_json)

        def request_callback(method, uri, headers):
            body = bodies.pop(0)
            requests.append(httpretty.last_request())
            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               JIRA_SEARCH_URL,
                               responses=[httpretty.Response(body=request_callback)
                                          for _ in range(2)])

        client = JiraClient(url='http://example.com', project='perceval',
                            user='user', password='password',
                            ssl_verify=False, cert=None, max_results=2)

        pages = [page for page in client.get_issues(from_date)]

        expected_req = [
            {
                'expand': ['renderedFields,transitions,operations,changelog'],
                'jql': ['project = perceval AND updated > 1420070400000 order by updated asc'],
                'maxResults': ['2'],
                'startAt': ['0']
            },
            {
                'expand': ['renderedFields,transitions,operations,changelog'],
                'jql': ['project = perceval AND updated > 1420070400000 order by updated asc'],
                'maxResults': ['2'],
                'startAt': ['2']
            }
        ]

        self.assertEqual(len(pages), 2)

        self.assertEqual(requests[0].method, 'GET')
        self.assertRegex(requests[0].path, '/rest/api/2/search')
        self.assertDictEqual(requests[0].querystring, expected_req[0])

        self.assertEqual(requests[1].method, 'GET')
        self.assertRegex(requests[1].path, '/rest/api/2/search')
        self.assertDictEqual(requests[1].querystring, expected_req[1])

        self.assertEqual(pages[0], bodies_json[0])
        self.assertEqual(pages[1], bodies_json[1]) 
Example #30
Source File: test_jira.py    From grimoirelab-perceval with GNU General Public License v3.0 4 votes vote down vote up
def test_fetch_from_archive(self):
        """Test whether a list of issues is returned from an archive"""

        requests = []

        bodies_json = [read_file('data/jira/jira_issues_page_1.json'),
                       read_file('data/jira/jira_issues_page_2.json')]
        comment_json = read_file('data/jira/jira_comments_issue_page_2.json')
        empty_comment = read_file('data/jira/jira_comments_issue_empty.json')
        body = read_file('data/jira/jira_fields.json')

        def request_callback(method, uri, headers):
            body = bodies_json.pop(0)
            requests.append(httpretty.last_request())
            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               JIRA_SEARCH_URL,
                               responses=[httpretty.Response(body=request_callback)
                                          for _ in range(2)])

        httpretty.register_uri(httpretty.GET,
                               JIRA_ISSUE_1_COMMENTS_URL,
                               body=empty_comment,
                               status=200)

        httpretty.register_uri(httpretty.GET,
                               JIRA_ISSUE_2_COMMENTS_URL,
                               body=comment_json,
                               status=200)

        httpretty.register_uri(httpretty.GET,
                               JIRA_ISSUE_3_COMMENTS_URL,
                               body=empty_comment,
                               status=200)

        httpretty.register_uri(httpretty.GET,
                               JIRA_FIELDS_URL,
                               body=body, status=200)

        self._test_fetch_from_archive(from_date=None)

        self.assertEqual(("test", "test"), self.backend_write_archive.client.session.auth)
        self.assertIsNone(self.backend_read_archive.client.session.auth)