def test_malformed_pub_key_document(self):
        # We need the first request to raise a 404, so we replace
        # post with a custom function here.
        def post(url, data, verify=True):
            response = Mock()
            if not post.called:
                response.status_code = 404
                post.called = True
            response.text = 'I AINT NO JSON, FOOL!'
            return response
        post.called = False

        with patch('browserid.netutils.requests') as requests:
            requests.request = post
            with self.assertRaises(InvalidIssuerError):
                fetch_support_document('test.com')
    def _fetch(self,
               hostname,
               requests,
               well_known_url=None,
               side_effect=None,
               response_text='',
               status_code=200):
        response = Mock()
        response.text = response_text
        response.status_code = status_code
        requests.request.side_effect = side_effect
        requests.request.return_value = response

        kwargs = {}
        if well_known_url is not None:
            kwargs['well_known_url'] = well_known_url

        supportdoc = fetch_support_document(hostname, **kwargs)

        try:
            key = supportdoc['public-key']
        except KeyError:
            raise InvalidIssuerError('Host %r has malformed public key '
                                     'document' % hostname)

        return key
    def test_malformed_pub_key_document(self):
        # We need the first request to raise a 404, so we replace
        # post with a custom function here.
        def post(url, data, verify=True):
            response = Mock()
            if not post.called:
                response.status_code = 404
                post.called = True
            response.text = 'I AINT NO JSON, FOOL!'
            return response

        post.called = False

        with patch('browserid.netutils.requests') as requests:
            requests.request = post
            with self.assertRaises(InvalidIssuerError):
                fetch_support_document('test.com')
    def _fetch(self, hostname, requests, well_known_url=None,
               side_effect=None, response_text='', status_code=200):
        response = Mock()
        response.text = response_text
        response.status_code = status_code
        requests.request.side_effect = side_effect
        requests.request.return_value = response

        kwargs = {}
        if well_known_url is not None:
            kwargs['well_known_url'] = well_known_url

        supportdoc = fetch_support_document(hostname, **kwargs)

        try:
            key = supportdoc['public-key']
        except KeyError:
            raise InvalidIssuerError('Host %r has malformed public key '
                                     'document' % hostname)

        return key