Python responses.add_callback() Examples

The following are 30 code examples of responses.add_callback(). 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 responses , or try the search function .
Example #1
Source File: test_junebug.py    From casepro with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_create_identity(self):
        """
        The create_identity method should make the correct request to create an identity with the correct details in the
        identity store.
        """
        identity_store = IdentityStore("http://identitystore.org/", "auth-token", "msisdn")

        url = "http://identitystore.org/api/v1/identities/"
        responses.add_callback(
            responses.POST,
            url,
            callback=self.create_identity_callback,
            match_querystring=True,
            content_type="application/json",
        )

        identity = identity_store.create_identity(["tel:+1234"], name="Test identity", language="eng")
        self.assertEqual(identity["details"], {"addresses": {"msisdn": {"+1234": {}}}, "default_addr_type": "msisdn"}) 
Example #2
Source File: test_client.py    From google-maps-services-python with Apache License 2.0 6 votes vote down vote up
def test_retry_intermittent(self):
        class request_callback:
            def __init__(self):
                self.first_req = True

            def __call__(self, req):
                if self.first_req:
                    self.first_req = False
                    return (500, {}, "Internal Server Error.")
                return (200, {}, '{"status":"OK","results":[]}')

        responses.add_callback(
            responses.GET,
            "https://maps.googleapis.com/maps/api/geocode/json",
            content_type="application/json",
            callback=request_callback(),
        )

        client = googlemaps.Client(key="AIzaasdf")
        client.geocode("Sesame St.")

        self.assertEqual(2, len(responses.calls)) 
Example #3
Source File: test_auth.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_not_bearer_auth(self):
        url = 'https://registry.example.com/v2/fedora/tags/list'

        def unsupported_callback(request):
            headers = {'www-authenticate': 'Spam realm={}'.format(BEARER_REALM_URL)}
            return (401, headers, json.dumps('unauthorized'))

        responses.add_callback(responses.GET, url, callback=unsupported_callback)
        responses.add(responses.GET, url, status=200, json='success')  # Not actually called

        auth = HTTPBearerAuth()

        response = requests.get(url, auth=auth)
        assert response.json() == 'unauthorized'
        assert response.status_code == 401
        assert len(responses.calls) == 1 
Example #4
Source File: test_auth.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_non_401_error_propagated(self):

        def bearer_teapot_callback(request):
            headers = {'www-authenticate': 'Bearer realm={}'.format(BEARER_REALM_URL)}
            return (418, headers, json.dumps("I'm a teapot!"))

        url = 'https://registry.example.com/v2/fedora/tags/list'
        responses.add_callback(responses.GET, url, callback=bearer_teapot_callback)
        responses.add(responses.GET, url, status=200, json='success')  # Not actually called

        auth = HTTPBearerAuth()

        response = requests.get(url, auth=auth)
        assert response.json() == "I'm a teapot!"
        assert response.status_code == 418
        assert len(responses.calls) == 1 
Example #5
Source File: test_auth.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_token_cached_per_repo(self):
        responses.add(responses.GET, BEARER_REALM_URL + '?scope=repository:fedora:pull',
                      json={'token': BEARER_TOKEN}, match_querystring=True)
        responses.add(responses.GET, BEARER_REALM_URL + '?scope=repository:centos:pull',
                      json={'token': BEARER_TOKEN}, match_querystring=True)

        fedora_url = 'https://registry.example.com/v2/fedora/tags/list'
        responses.add_callback(responses.GET, fedora_url, callback=bearer_unauthorized_callback)
        responses.add(responses.GET, fedora_url, status=200, json='fedora-success')
        responses.add(responses.GET, fedora_url, status=200, json='fedora-success-also')

        centos_url = 'https://registry.example.com/v2/centos/tags/list'
        responses.add_callback(responses.GET, centos_url, callback=bearer_unauthorized_callback)
        responses.add(responses.GET, centos_url, status=200, json='centos-success')
        responses.add(responses.GET, centos_url, status=200, json='centos-success-also')

        auth = HTTPBearerAuth()

        assert requests.get(fedora_url, auth=auth).json() == 'fedora-success'
        assert requests.get(fedora_url, auth=auth).json() == 'fedora-success-also'

        assert requests.get(centos_url, auth=auth).json() == 'centos-success'
        assert requests.get(centos_url, auth=auth).json() == 'centos-success-also'

        assert len(responses.calls) == 8 
Example #6
Source File: test_client.py    From google-maps-services-python with Apache License 2.0 6 votes vote down vote up
def test_retry(self):
        class request_callback:
            def __init__(self):
                self.first_req = True

            def __call__(self, req):
                if self.first_req:
                    self.first_req = False
                    return (200, {}, '{"status":"OVER_QUERY_LIMIT"}')
                return (200, {}, '{"status":"OK","results":[]}')

        responses.add_callback(
            responses.GET,
            "https://maps.googleapis.com/maps/api/geocode/json",
            content_type="application/json",
            callback=request_callback(),
        )

        client = googlemaps.Client(key="AIzaasdf")
        client.geocode("Sesame St.")

        self.assertEqual(2, len(responses.calls))
        self.assertEqual(responses.calls[0].request.url, responses.calls[1].request.url) 
Example #7
Source File: test_junebug.py    From casepro with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_get_identities(self):
        """
        The get_identities function should call the correct URL, and return the relevant identities.
        """
        identity_store = IdentityStore("http://identitystore.org/", "auth-token", "msisdn")

        responses.add_callback(
            responses.GET,
            "http://identitystore.org/api/v1/identities/?details__name=test",
            match_querystring=True,
            callback=self.get_identities_callback,
            content_type="application/json",
        )

        [identity] = identity_store.get_identities(details__name="test")
        self.assertEqual(identity.name, "test") 
Example #8
Source File: test_junebug.py    From casepro with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_get_identities_for_address(self):
        """
        The get_identities_for_address to create the correct request to list all of the identities for a specified
        address and address type.
        """
        identity_store = IdentityStore("http://identitystore.org/", "auth-token", "msisdn")

        query = "?details__addresses__msisdn=%2B1234"
        url = "http://identitystore.org/api/v1/identities/search/"
        responses.add_callback(
            responses.GET,
            url + query,
            callback=self.single_identity_callback,
            match_querystring=True,
            content_type="application/json",
        )

        [identity] = identity_store.get_identities_for_address("+1234")
        self.assertEqual(identity["details"]["addresses"]["msisdn"], {"+1234": {}}) 
Example #9
Source File: test_hvcs.py    From python-semantic-release with MIT License 6 votes vote down vote up
def test_should_post_changelog(self, mock_token):
        def request_callback(request):
            payload = json.loads(request.body)
            self.assertEqual(payload["tag_name"], "v1.0.0")
            self.assertEqual(payload["body"], "text")
            self.assertEqual(payload["draft"], False)
            self.assertEqual(payload["prerelease"], False)
            self.assertEqual("token super-token", request.headers["Authorization"])

            return 201, {}, json.dumps({})

        responses.add_callback(
            responses.POST,
            self.url,
            callback=request_callback,
            content_type="application/json",
        )
        status = Github.post_release_changelog("relekang", "rmoq", "1.0.0", "text")
        self.assertTrue(status) 
Example #10
Source File: postmen_test.py    From postmen-sdk-python with MIT License 6 votes vote down vote up
def testRetryDelay():
    global call
    call = 0
    def request_callback(request):
        global call
        if call == 0 :
            call = 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        elif call == 1 :
            call = 2
            "second attempt"
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true,"details":[]},"data":{}}')
        elif call == 2 :
            call = 3
            return (200, headers,  '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    api.get('labels')
    responses.reset()

# TEST 9 
Example #11
Source File: postmen_test.py    From postmen-sdk-python with MIT License 6 votes vote down vote up
def testRetryMaxAttempt(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call < 4 :
            call += 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        else :
            return (200, headers,  '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    before = time.time()
    api.get('labels')
    after = time.time()
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)

# TEST 10 
Example #12
Source File: postmen_test.py    From postmen-sdk-python with MIT License 6 votes vote down vote up
def testRetryMaxAttemptExceeded(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call < 5 :
            call += 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        else :
            pytest.fail("Maximum 5 attempts of retry, test #10 failed")
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    assert "PROBLEM" in str(e.value.message())
    assert 999 == e.value.code()

# TEST 11 
Example #13
Source File: postmen_test.py    From postmen-sdk-python with MIT License 6 votes vote down vote up
def testArguments11(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call < 1 :
            call += 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        else :
            pytest.fail("Shall not retry if retry = False, test #11 failed")
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION', retry=False)
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    assert "PROBLEM" in str(e.value.message())
    assert 999 == e.value.code()
    assert e.value.retryable()

# TEST 12 
Example #14
Source File: postmen_test.py    From postmen-sdk-python with MIT License 6 votes vote down vote up
def testArgument12(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call == 0 :
            call = 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":false, "details":[]},"data":{}}')
        elif call == 1 :
            pytest.fail("Shall not retry if non retryable, test #12 failed")
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    # print(e)
    assert "PROBLEM" in str(e.value.message())
    assert not e.value.retryable()
    monkeypatch.setattr(time, 'sleep', lambda s: None)

# TEST 13 
Example #15
Source File: test_api.py    From course-discovery with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_ingest_with_existing_banner_image(self):
        TieredCache.dangerous_clear_all_tiers()
        programs = self.mock_api()

        for program_data in programs:
            banner_image_url = program_data.get('banner_image_urls', {}).get('w1440h480')
            if banner_image_url:
                responses.add_callback(
                    responses.GET,
                    banner_image_url,
                    callback=mock_jpeg_callback(),
                    content_type=JPEG
                )

        self.loader.ingest()
        # Verify the API was called with the correct authorization header
        self.assert_api_called(6)

        for program in programs:
            self.assert_program_loaded(program)
            self.assert_program_banner_image_loaded(program) 
Example #16
Source File: test_tapioca.py    From tapioca-wrapper with MIT License 6 votes vote down vote up
def test_token_expired_automatically_refresh_authentication(self):
        self.first_call = True

        def request_callback(request):
            if self.first_call:
                self.first_call = False
                return (401, {'content_type': 'application/json'}, json.dumps({"error": "Token expired"}))
            else:
                self.first_call = None
                return (201, {'content_type': 'application/json'}, '')

        responses.add_callback(
            responses.POST, self.wrapper.test().data,
            callback=request_callback,
            content_type='application/json',
        )

        response = self.wrapper.test().post()

        # refresh_authentication method should be able to update api_params
        self.assertEqual(response._api_params['token'], 'new_token') 
Example #17
Source File: test_tapioca.py    From tapioca-wrapper with MIT License 6 votes vote down vote up
def test_raises_error_if_refresh_authentication_method_returns_falsy_value(self):
        client = FailTokenRefreshClient(token='token', refresh_token_by_default=True)

        self.first_call = True

        def request_callback(request):
            if self.first_call:
                self.first_call = False
                return (401, {}, '')
            else:
                self.first_call = None
                return (201, {}, '')

        responses.add_callback(
            responses.POST, client.test().data,
            callback=request_callback,
            content_type='application/json',
        )

        with self.assertRaises(ClientError):
            client.test().post() 
Example #18
Source File: test_tapioca.py    From tapioca-wrapper with MIT License 6 votes vote down vote up
def test_stores_refresh_authentication_method_response_for_further_access(self):
        self.first_call = True

        def request_callback(request):
            if self.first_call:
                self.first_call = False
                return (401, {}, '')
            else:
                self.first_call = None
                return (201, {}, '')

        responses.add_callback(
            responses.POST, self.wrapper.test().data,
            callback=request_callback,
            content_type='application/json',
        )

        response = self.wrapper.test().post()

        self.assertEqual(response().refresh_data, 'new_token') 
Example #19
Source File: test_workflow.py    From explo with GNU General Public License v3.0 6 votes vote down vote up
def test_workflow_csrf_token():
    """ Test a simple http get and the extraction of a css selector """

    body_contact = b'<form><input type="hidden" name="csrf" id="csrf" value="token_1337"><input type="text" name="name"></form>'
    body_success = b'<div id=error>You have an error in your SQL syntax</div>'

    def request_callback_csrf(request):
        """ Checks for a valid CSRF token (token_1337) and returns a fake 'sql injection success' message """
        assert 'csrf=token_1337' in request.body

        return (200, {}, body_success)

    responses.add(responses.GET, 'http://test.com/contact',
                  body=body_contact, status=200,
                  content_type='text/html')

    responses.add_callback(
        responses.POST, 'http://test.com/contact',
        callback=request_callback_csrf,
        content_type='text/html',
    )

    blocks = explo.core.load_blocks(BLOCKS_CSRF)

    assert explo.core.process_blocks(blocks) 
Example #20
Source File: test_refresh_course_metadata.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def mock_organizations_api(self):
        bodies = mock_data.ORGANIZATIONS_API_BODIES
        url = self.partner.organizations_api_url + 'organizations/'
        responses.add_callback(
            responses.GET,
            url,
            callback=mock_api_callback(url, bodies),
            content_type=JSON
        )
        return bodies 
Example #21
Source File: test_pocket.py    From pocket-api with MIT License 5 votes vote down vote up
def test_tags():
    responses.add_callback(
        responses.POST, _pocket._get_url('send'),
        callback=success_request_callback,
        content_type='application/json',
    )

    _pocket.tags_add(123, [1, 2, 3])
    _pocket.tags_remove(123, [2, 3])
    _pocket.tags_replace(123, [4, 5, 6])
    _pocket.tag_rename('old_tag', 'new_tag')
    response = _pocket.commit()

    assert len(responses.calls) == 1
    assert response['actions'][0]['action'] == 'tags_add'
    assert response['actions'][0]['item_id'] == 123
    assert response['actions'][0]['tags'] == [1, 2, 3]
    assert response['actions'][1]['action'] == 'tags_remove'
    assert response['actions'][1]['item_id'] == 123
    assert response['actions'][1]['tags'] == [2, 3]
    assert response['actions'][2]['action'] == 'tags_replace'
    assert response['actions'][2]['item_id'] == 123
    assert response['actions'][2]['tags'] == [4, 5, 6]
    assert response['actions'][3]['action'] == 'tag_rename'
    assert response['actions'][3]['old_tag'] == 'old_tag'
    assert response['actions'][3]['new_tag'] == 'new_tag' 
Example #22
Source File: test_pocket.py    From pocket-api with MIT License 5 votes vote down vote up
def test_favorite():
    responses.add_callback(
        responses.POST, _pocket._get_url('send'),
        callback=success_request_callback,
        content_type='application/json',
    )

    response = _pocket.favorite(123).unfavorite(123).commit()
    assert len(responses.calls) == 1
    assert response['actions'][0]['action'] == 'favorite'
    assert response['actions'][0]['item_id'] == 123
    assert response['actions'][1]['action'] == 'unfavorite'
    assert response['actions'][1]['item_id'] == 123 
Example #23
Source File: test_pocket.py    From pocket-api with MIT License 5 votes vote down vote up
def test_reset():
    responses.add_callback(
        responses.POST, _pocket._get_url('send'),
        callback=success_request_callback,
        content_type='application/json',
    )

    _pocket.bulk_add(123, url='test_url').reset()
    assert len(responses.calls) == 0 
Example #24
Source File: test_pocket.py    From pocket-api with MIT License 5 votes vote down vote up
def test_add_bulk():
    responses.add_callback(
        responses.POST, _pocket._get_url('send'),
        callback=success_request_callback,
        content_type='application/json',
    )

    response = _pocket.bulk_add(123, url='test_url').commit()
    assert len(responses.calls) == 1
    assert response['actions'][0]['action'] == 'add'
    assert response['actions'][0]['item_id'] == 123
    assert response['actions'][0]['url'] == 'test_url' 
Example #25
Source File: test_pocket.py    From pocket-api with MIT License 5 votes vote down vote up
def test_failed_retrieve():
    responses.add_callback(
        responses.POST, _pocket._get_url('get'),
        callback=failed_request_callback,
        content_type='application/json',
    )

    with pytest.raises(PocketException):
        _pocket.retrieve() 
Example #26
Source File: test_refresh_course_metadata.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def mock_programs_api(self):
        bodies = mock_data.PROGRAMS_API_BODIES
        url = self.partner.programs_api_url + 'programs/'
        responses.add_callback(
            responses.GET,
            url,
            callback=mock_api_callback(url, bodies),
            content_type=JSON
        )
        return bodies 
Example #27
Source File: mixins.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def mock_login_response(self, status):
        """ Mock the response of the marketing site login """
        response_url = '{root}/admin'.format(
            root=self.api_root
        )

        def request_callback(request):
            headers = {
                'location': response_url
            }
            return (302, headers, None)

        responses.add_callback(
            responses.POST,
            '{root}/user'.format(root=self.api_root),
            callback=request_callback,
            content_type='text/html',
        )

        responses.add(
            responses.GET,
            response_url,
            body='',
            content_type='text/html',
            status=status
        ) 
Example #28
Source File: test_api.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def mock_courses_api(self):
        # Create existing seats to be removed by ingest
        audit_run_type = CourseRunType.objects.get(slug=CourseRunType.AUDIT)
        credit_run_type = CourseRunType.objects.get(slug=CourseRunType.CREDIT_VERIFIED_AUDIT)
        verified_run_type = CourseRunType.objects.get(slug=CourseRunType.VERIFIED_AUDIT)
        audit_run = CourseRunFactory(title_override='audit', key='audit/course/run', type=audit_run_type,
                                     course__partner=self.partner)
        verified_run = CourseRunFactory(title_override='verified', key='verified/course/run', type=verified_run_type,
                                        course__partner=self.partner)
        credit_run = CourseRunFactory(title_override='credit', key='credit/course/run', type=credit_run_type,
                                      course__partner=self.partner)
        no_currency_run = CourseRunFactory(title_override='no currency', key='nocurrency/course/run',
                                           type=verified_run_type, course__partner=self.partner)

        professional_type = SeatTypeFactory.professional()
        SeatFactory(course_run=audit_run, type=professional_type)
        SeatFactory(course_run=verified_run, type=professional_type)
        SeatFactory(course_run=credit_run, type=professional_type)
        SeatFactory(course_run=no_currency_run, type=professional_type)

        bodies = mock_data.ECOMMERCE_API_BODIES
        url = self.api_url + 'courses/'
        responses.add_callback(
            responses.GET,
            url,
            callback=mock_api_callback(url, bodies),
            content_type=JSON
        )
        return bodies 
Example #29
Source File: test_api.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def mock_api(self):
        bodies = mock_data.PROGRAMS_API_BODIES
        self.create_mock_organizations(bodies)
        self.create_mock_courses_and_runs(bodies)

        url = self.api_url + 'programs/'
        responses.add_callback(
            responses.GET,
            url,
            callback=mock_api_callback(url, bodies),
            content_type=JSON
        )

        # We exclude the one invalid item
        return bodies[:-1] 
Example #30
Source File: test_refresh_course_metadata.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def mock_ecommerce_courses_api(self):
        bodies = mock_data.ECOMMERCE_API_BODIES
        url = self.partner.ecommerce_api_url + 'courses/'
        responses.add_callback(
            responses.GET,
            url,
            callback=mock_api_callback(url, bodies),
            content_type=JSON
        )
        return bodies