Python responses.activate() Examples

The following are 30 code examples of responses.activate(). 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_responses.py    From jarvis with GNU General Public License v2.0 7 votes vote down vote up
def test_callback_no_content_type():
    body = b'test callback'
    status = 400
    reason = 'Bad Request'
    headers = {'foo': 'bar'}
    url = 'http://example.com/'

    def request_callback(request):
        return (status, headers, body)

    @responses.activate
    def run():
        responses.add_callback(
            responses.GET, url, request_callback, content_type=None)
        resp = requests.get(url)
        assert resp.text == "test callback"
        assert resp.status_code == status
        assert resp.reason == reason
        assert 'foo' in resp.headers
        assert 'Content-Type' not in resp.headers

    run()
    assert_reset() 
Example #2
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 7 votes vote down vote up
def test_response():
    @responses.activate
    def run():
        responses.add(responses.GET, 'http://example.com', body=b'test')
        resp = requests.get('http://example.com')
        assert_response(resp, 'test')
        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == 'http://example.com/'
        assert responses.calls[0].response.content == b'test'

        resp = requests.get('http://example.com?foo=bar')
        assert_response(resp, 'test')
        assert len(responses.calls) == 2
        assert responses.calls[1].request.url == 'http://example.com/?foo=bar'
        assert responses.calls[1].response.content == b'test'

    run()
    assert_reset() 
Example #3
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def test_match_querystring_error_regex():
    @responses.activate
    def run():
        """Note that `match_querystring` value shouldn't matter when passing a
        regular expression"""

        responses.add(
            responses.GET,
            re.compile(r'http://example\.com/foo/\?test=1'),
            match_querystring=True)

        with pytest.raises(ConnectionError):
            requests.get('http://example.com/foo/?test=3')

        responses.add(
            responses.GET,
            re.compile(r'http://example\.com/foo/\?test=2'),
            match_querystring=False)

        with pytest.raises(ConnectionError):
            requests.get('http://example.com/foo/?test=4')

    run()
    assert_reset() 
Example #4
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def test_accept_json_body():
    @responses.activate
    def run():
        content_type = 'application/json'

        url = 'http://example.com/'
        responses.add(responses.GET, url, json={"message": "success"})
        resp = requests.get(url)
        assert_response(resp, '{"message": "success"}', content_type)

        url = 'http://example.com/1/'
        responses.add(responses.GET, url, json=[])
        resp = requests.get(url)
        assert_response(resp, '[]', content_type)

    run()
    assert_reset() 
Example #5
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def test_match_empty_querystring():
    @responses.activate
    def run():
        responses.add(
            responses.GET,
            'http://example.com',
            body=b'test',
            match_querystring=True)
        resp = requests.get('http://example.com')
        assert_response(resp, 'test')
        resp = requests.get('http://example.com/')
        assert_response(resp, 'test')
        with pytest.raises(ConnectionError):
            requests.get('http://example.com?query=foo')

    run()
    assert_reset() 
Example #6
Source File: test_openedx_appserver.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_make_active_fails_to_start_services(self, mocks, mock_consul):
        """
        Test make_active() and check if it's behaving correctly when the
        playbooks to start services fail
        """
        mocks.mock_run_appserver_playbooks.return_value = ('', 2)
        instance = OpenEdXInstanceFactory(internal_lms_domain='test.activate.opencraft.co.uk')
        appserver_id = instance.spawn_appserver()
        self.assertEqual(mocks.mock_load_balancer_run_playbook.call_count, 1)
        appserver = instance.appserver_set.get(pk=appserver_id)

        self.assertEqual(instance.appserver_set.get().last_activated, None)

        appserver.make_active()

        instance.refresh_from_db()
        appserver.refresh_from_db()
        self.assertEqual(mocks.mock_run_appserver_playbooks.call_count, 1)
        self.assertFalse(appserver.is_active)
        self.assertEqual(appserver.last_activated, None)
        self.assertEqual(instance.appserver_set.get().last_activated, None)
        self.assertEqual(mocks.mock_load_balancer_run_playbook.call_count, 1)
        self.assertEqual(mocks.mock_enable_monitoring.call_count, 0) 
Example #7
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def test_response_cookies():
    body = b'test callback'
    status = 200
    headers = {'set-cookie': 'session_id=12345; a=b; c=d'}
    url = 'http://example.com/'

    def request_callback(request):
        return (status, headers, body)

    @responses.activate
    def run():
        responses.add_callback(responses.GET, url, request_callback)
        resp = requests.get(url)
        assert resp.text == "test callback"
        assert resp.status_code == status
        assert 'session_id' in resp.cookies
        assert resp.cookies['session_id'] == '12345'
        assert resp.cookies['a'] == 'b'
        assert resp.cookies['c'] == 'd'

    run()
    assert_reset() 
Example #8
Source File: test_openedx_appserver.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_manage_instance_services(self, mocks, mock_consul):
        """
        Test if manage instance services is correctly running the playbook
        """
        instance = OpenEdXInstanceFactory(internal_lms_domain='test.activate.opencraft.co.uk')
        appserver_id = instance.spawn_appserver()
        appserver = instance.appserver_set.get(pk=appserver_id)
        expected_playbook = Playbook(
            version=None,
            source_repo=os.path.join(settings.SITE_ROOT, 'playbooks/manage_services'),
            playbook_path='manage_services.yml',
            requirements_path='requirements.txt',
            variables='services: all\nsupervisord_action: start\n'
        )

        appserver.manage_instance_services(active=True)

        self.assertEqual(mocks.mock_run_appserver_playbooks.call_count, 1)
        mocks.mock_run_appserver_playbooks.assert_called_once_with(
            playbook=expected_playbook,
            working_dir=expected_playbook.source_repo,
        ) 
Example #9
Source File: helpers.py    From python-asana with MIT License 6 votes vote down vote up
def create_decorating_metaclass(decorators, prefix='test_'):
    class DecoratingMethodsMetaclass(type):
        def __new__(cls, name, bases, namespace):
            namespace_items = tuple(namespace.items())
            for key, val in namespace_items:
                if key.startswith(prefix) and callable(val):
                    for dec in decorators:
                        val = dec(val)
                    namespace[key] = val
            return type.__new__(cls, name, bases, dict(namespace))

    return DecoratingMethodsMetaclass


# TestCase subclass that automatically decorates test methods with
# responses.activate and sets up a client instance 
Example #10
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def test_response_with_instance():
    @responses.activate
    def run():
        responses.add(
            responses.Response(
                method=responses.GET,
                url='http://example.com', ))
        resp = requests.get('http://example.com')
        assert_response(resp, '')
        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == 'http://example.com/'

        resp = requests.get('http://example.com?foo=bar')
        assert_response(resp, '')
        assert len(responses.calls) == 2
        assert responses.calls[1].request.url == 'http://example.com/?foo=bar'

    run()
    assert_reset() 
Example #11
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def test_regular_expression_url():
    @responses.activate
    def run():
        url = re.compile(r'https?://(.*\.)?example.com')
        responses.add(responses.GET, url, body=b'test')

        resp = requests.get('http://example.com')
        assert_response(resp, 'test')

        resp = requests.get('https://example.com')
        assert_response(resp, 'test')

        resp = requests.get('https://uk.example.com')
        assert_response(resp, 'test')

        with pytest.raises(ConnectionError):
            requests.get('https://uk.exaaample.com')

    run()
    assert_reset() 
Example #12
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def test_callback():
    body = b'test callback'
    status = 400
    reason = 'Bad Request'
    headers = {'foo': 'bar'}
    url = 'http://example.com/'

    def request_callback(request):
        return (status, headers, body)

    @responses.activate
    def run():
        responses.add_callback(responses.GET, url, request_callback)
        resp = requests.get(url)
        assert resp.text == "test callback"
        assert resp.status_code == status
        assert resp.reason == reason
        assert 'foo' in resp.headers
        assert resp.headers['foo'] == 'bar'

    run()
    assert_reset() 
Example #13
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_custom_adapter():
    @responses.activate
    def run():
        url = "http://example.com"
        responses.add(responses.GET, url, body=b'test')

        calls = [0]

        class DummyAdapter(requests.adapters.HTTPAdapter):
            def send(self, *a, **k):
                calls[0] += 1
                return super(DummyAdapter, self).send(*a, **k)

        # Test that the adapter is actually used
        session = requests.Session()
        session.mount("http://", DummyAdapter())

        resp = session.get(url, allow_redirects=False)
        assert calls[0] == 1

        # Test that the response is still correctly emulated
        session = requests.Session()
        session.mount("http://", DummyAdapter())

        resp = session.get(url)
        assert_response(resp, 'test')

    run() 
Example #14
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_no_content_type():
    @responses.activate
    def run():
        url = 'http://example.com/'
        responses.add(responses.GET, url, body='test', content_type=None)
        resp = requests.get(url)
        assert_response(resp, 'test', content_type=None)

    run()
    assert_reset() 
Example #15
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_throw_connection_error_explicit():
    @responses.activate
    def run():
        url = 'http://example.com'
        exception = HTTPError('HTTP Error')
        responses.add(responses.GET, url, exception)

        with pytest.raises(HTTPError) as HE:
            requests.get(url)

        assert str(HE.value) == 'HTTP Error'

    run()
    assert_reset() 
Example #16
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_match_querystring():
    @responses.activate
    def run():
        url = 'http://example.com?test=1&foo=bar'
        responses.add(responses.GET, url, match_querystring=True, body=b'test')
        resp = requests.get('http://example.com?test=1&foo=bar')
        assert_response(resp, 'test')
        resp = requests.get('http://example.com?foo=bar&test=1')
        assert_response(resp, 'test')
        resp = requests.get('http://example.com/?foo=bar&test=1')
        assert_response(resp, 'test')

    run()
    assert_reset() 
Example #17
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_activate_doesnt_change_signature_for_method():
    class TestCase(object):
        def test_function(self, a, b=None):
            return (self, a, b)

    test_case = TestCase()
    argspec = getargspec(test_case.test_function)
    decorated_test_function = responses.activate(test_case.test_function)
    assert argspec == getargspec(decorated_test_function)
    assert decorated_test_function(1, 2) == test_case.test_function(1, 2)
    assert decorated_test_function(3) == test_case.test_function(3) 
Example #18
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_handles_chinese_url():
    url = u'http://example.com/test?type=2&ie=utf8&query=汉字'

    @responses.activate
    def run():
        responses.add(responses.GET, url, body='test', match_querystring=True)

        resp = requests.get(url)

        assert_response(resp, 'test')

    run()
    assert_reset() 
Example #19
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_headers():
    @responses.activate
    def run():
        responses.add(
            responses.GET,
            'http://example.com',
            body='',
            headers={
                'X-Test': 'foo',
            })
        resp = requests.get('http://example.com')
        assert resp.headers['X-Test'] == 'foo'

    run()
    assert_reset() 
Example #20
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_legacy_adding_headers():
    @responses.activate
    def run():
        responses.add(
            responses.GET,
            'http://example.com',
            body='',
            adding_headers={
                'X-Test': 'foo',
            })
        resp = requests.get('http://example.com')
        assert resp.headers['X-Test'] == 'foo'

    run()
    assert_reset() 
Example #21
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_multiple_urls():
    @responses.activate
    def run():
        responses.add(responses.GET, 'http://example.com/one', body='one')
        responses.add(responses.GET, 'http://example.com/two', body='two')

        resp = requests.get('http://example.com/two')
        assert_response(resp, 'two')
        resp = requests.get('http://example.com/one')
        assert_response(resp, 'one')

    run()
    assert_reset() 
Example #22
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_accept_string_body():
    @responses.activate
    def run():
        url = 'http://example.com/'
        responses.add(responses.GET, url, body='test')
        resp = requests.get(url)
        assert_response(resp, 'test')

    run()
    assert_reset() 
Example #23
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_match_querystring_regex():
    @responses.activate
    def run():
        """Note that `match_querystring` value shouldn't matter when passing a
        regular expression"""

        responses.add(
            responses.GET,
            re.compile(r'http://example\.com/foo/\?test=1'),
            body='test1',
            match_querystring=True)

        resp = requests.get('http://example.com/foo/?test=1')
        assert_response(resp, 'test1')

        responses.add(
            responses.GET,
            re.compile(r'http://example\.com/foo/\?test=2'),
            body='test2',
            match_querystring=False)

        resp = requests.get('http://example.com/foo/?test=2')
        assert_response(resp, 'test2')

    run()
    assert_reset() 
Example #24
Source File: test_okta.py    From aws-okta-processor with MIT License 5 votes vote down vote up
def test_okta_mfa_push_challenge(
            self,
            mock_print_tty,
            mock_makedirs,
            mock_open,
            mock_chmod
    ):
        responses.add(
            responses.POST,
            'https://organization.okta.com/api/v1/authn',
            json=json.loads(AUTH_MFA_PUSH_RESPONSE)
        )

        responses.add(
            responses.POST,
            'https://organization.okta.com/api/v1/authn/factors/id/verify',
            json=json.loads(MFA_WAITING_RESPONSE)
        )

        responses.add(
            responses.POST,
            'https://organization.okta.com/api/v1/authn/factors/id/lifecycle/activate/poll',
            json=json.loads(AUTH_TOKEN_RESPONSE)
        )

        responses.add(
            responses.POST,
            'https://organization.okta.com/api/v1/sessions',
            json=json.loads(SESSION_RESPONSE)
        )

        okta = Okta(
            user_name="user_name",
            user_pass="user_pass",
            organization="organization.okta.com"
        )

        self.assertEqual(okta.okta_single_use_token, "single_use_token")
        self.assertEqual(okta.organization, "organization.okta.com")
        self.assertEqual(okta.okta_session_id, "session_token") 
Example #25
Source File: test_responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_connection_error():
    @responses.activate
    def run():
        responses.add(responses.GET, 'http://example.com')

        with pytest.raises(ConnectionError):
            requests.get('http://example.com/foo')

        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == 'http://example.com/foo'
        assert type(responses.calls[0].response) is ConnectionError
        assert responses.calls[0].response.request

    run()
    assert_reset() 
Example #26
Source File: test_paypal.py    From ecommerce with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_resolve_paypal_locale(self, default_locale, cookie_locale, expected_paypal_locale, mock_method):
        """
        Verify that the correct locale for payment processing is fetched from the language cookie
        """
        translation.activate(default_locale)
        mock_method.side_effect = Exception("End of test")  # Force test to end after this call

        toggle_switch('create_and_set_webprofile', True)
        self.request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = cookie_locale
        try:
            self.processor.get_transaction_parameters(self.basket, request=self.request)
        except Exception:  # pylint: disable=broad-except
            pass
        mock_method.assert_called_with(expected_paypal_locale) 
Example #27
Source File: test_email.py    From kpi with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_notifications(self):
        self._create_periodic_task()
        first_log_response = self._send_and_fail()
        failures_reports.delay()
        self.assertEqual(len(mail.outbox), 1)

        expected_record = {
            "username": self.asset.owner.username,
            "email": self.asset.owner.email,
            "language": "en",
            "assets": {
                self.asset.uid: {
                    "name": self.asset.name,
                    "max_length": len(self.hook.name),
                    "logs": [{
                        "hook_name": self.hook.name,
                        "status_code": first_log_response.get("status_code"),
                        "message": first_log_response.get("message"),
                        "uid": first_log_response.get("uid"),
                        "date_modified": dateparse.parse_datetime(first_log_response.get("date_modified"))
                    }]
                }
            }
        }

        plain_text_template = get_template("reports/failures_email_body.txt")

        variables = {
            "username": expected_record.get("username"),
            "assets": expected_record.get("assets"),
            'kpi_base_url': settings.KPI_URL
        }
        # Localize templates
        translation.activate(expected_record.get("language"))
        text_content = plain_text_template.render(variables)

        self.assertEqual(mail.outbox[0].body, text_content) 
Example #28
Source File: test_okta_client.py    From gimme-aws-creds with Apache License 2.0 5 votes vote down vote up
def test_missing_saml_response(self):
        """Test that the SAML reponse was successful (failed)"""
        responses.add(responses.GET, 'https://example.okta.com/app/gimmecreds/exkatg7u9g6LJfFrZ0h7/sso/saml', status=200, body="")
        with self.assertRaises(RuntimeError):
            result = self.client.get_saml_response('https://example.okta.com/app/gimmecreds/exkatg7u9g6LJfFrZ0h7/sso/saml')

    # @responses.activate
    # def test_get_aws_account_info(self):
    #     """Test the gimme_creds_server response"""
    #     responses.add(responses.POST, 'https://localhost:8443/saml/SSO', status=200)
    #     responses.add(responses.GET, self.gimme_creds_server + '/api/v1/accounts', status=200, body=json.dumps(self.api_results))
    #     # The SAMLResponse value doesn't matter because the API response is mocked
    #     saml_data = {'SAMLResponse': 'BASE64_String', 'RelayState': '', 'TargetUrl': 'https://localhost:8443/saml/SSO'}
    #     result = self.client._get_aws_account_info(self.gimme_creds_server, saml_data)
    #     assert_equals(self.client.aws_access, self.api_results) 
Example #29
Source File: test_openedx_appserver.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_make_active_no_load_balancer_reconfiguration(self, mocks, mock_consul):
        """
        Test make_active() and make_active(active=False) when the load balancer
        reconfiguration is disabled
        """
        instance = OpenEdXInstanceFactory(internal_lms_domain='test.activate.opencraft.co.uk')
        appserver_id = instance.spawn_appserver()
        self.assertEqual(mocks.mock_load_balancer_run_playbook.call_count, 0)
        appserver = instance.appserver_set.get(pk=appserver_id)

        self.assertEqual(instance.appserver_set.get().last_activated, None)

        with freeze_time('2017-01-17 11:25:00') as freezed_time:
            appserver.make_active()
        activation_time = utc.localize(freezed_time())

        instance.refresh_from_db()
        appserver.refresh_from_db()
        self.assertTrue(appserver.is_active)
        self.assertEqual(appserver.last_activated, activation_time)
        self.assertEqual(instance.appserver_set.get().last_activated, activation_time)
        self.assertEqual(mocks.mock_load_balancer_run_playbook.call_count, 0)
        self.assertEqual(mocks.mock_enable_monitoring.call_count, 1)

        # Test deactivate
        appserver.make_active(active=False)
        instance.refresh_from_db()
        appserver.refresh_from_db()
        self.assertFalse(appserver.is_active)
        self.assertEqual(appserver.last_activated, activation_time)
        self.assertFalse(instance.get_active_appservers().exists())
        self.assertEqual(mocks.mock_load_balancer_run_playbook.call_count, 0)
        self.assertEqual(mocks.mock_disable_monitoring.call_count, 0) 
Example #30
Source File: test_openedx_appserver.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_make_active(self, mocks, mock_consul):
        """
        Test make_active() and make_active(active=False)
        """
        instance = OpenEdXInstanceFactory(internal_lms_domain='test.activate.opencraft.co.uk')
        appserver_id = instance.spawn_appserver()
        self.assertEqual(mocks.mock_load_balancer_run_playbook.call_count, 1)
        appserver = instance.appserver_set.get(pk=appserver_id)

        self.assertEqual(instance.appserver_set.get().last_activated, None)

        with freeze_time('2017-01-17 11:25:00') as freezed_time:
            appserver.make_active()
        activation_time = utc.localize(freezed_time())

        instance.refresh_from_db()
        appserver.refresh_from_db()
        self.assertTrue(appserver.is_active)
        self.assertEqual(appserver.last_activated, activation_time)
        self.assertEqual(instance.appserver_set.get().last_activated, activation_time)
        self.assertEqual(mocks.mock_load_balancer_run_playbook.call_count, 2)
        self.assertEqual(mocks.mock_enable_monitoring.call_count, 1)
        self.assertEqual(mocks.mock_run_appserver_playbooks.call_count, 1)

        # Test deactivate
        appserver.make_active(active=False)
        instance.refresh_from_db()
        appserver.refresh_from_db()
        self.assertFalse(appserver.is_active)
        self.assertEqual(appserver.last_activated, activation_time)
        self.assertFalse(instance.get_active_appservers().exists())
        self.assertEqual(mocks.mock_load_balancer_run_playbook.call_count, 3)
        self.assertEqual(mocks.mock_disable_monitoring.call_count, 0)
        self.assertEqual(mocks.mock_run_appserver_playbooks.call_count, 2)