Example #1
0
class TestRemoteVerifier(unittest.TestCase, VerifierTestCases):
    def setUp(self):
        self.verifier = RemoteVerifier(["*"])

    @patch('browserid.netutils.requests')
    def _verify(self,
                requests,
                response_text='',
                assertion=EXPIRED_ASSERTION,
                status_code=200):
        response = Mock()
        response.text = response_text
        response.status_code = status_code
        requests.request.return_value = response

        return self.verifier.verify(assertion)

    def test_expired_assertion(self):
        try:
            return super(TestRemoteVerifier, self).test_expired_assertion()
        except ConnectionError:
            raise unittest.SkipTest("error connecting to remote server")

    def test_malformed_assertions(self):
        try:
            return super(TestRemoteVerifier, self).test_malformed_assertions()
        except ConnectionError:
            raise unittest.SkipTest("error connecting to remote server")

    def test_handling_of_valid_response_from_server(self):
        response_text = ('{"email": "*****@*****.**", "status": "okay", '
                         '"audience": "http://myfavoritebeer.org"}')
        data = self._verify(response_text=response_text)
        self.assertEquals(data["email"], "*****@*****.**")

    def test_handling_of_invalid_json_from_server(self):
        with self.assertRaises(ConnectionError):
            self._verify(response_text='SERVER RETURNS INVALID JSON')

    @patch('browserid.netutils.requests')
    def test_handling_of_incorrect_audience_returned_by_server(self, requests):
        response_text = ('{"email": "*****@*****.**", "status": "okay", '
                         '"audience": "WRONG"}')
        with self.assertRaises(AudienceMismatchError):
            self._verify(response_text=response_text)

    @patch('browserid.netutils.requests')
    def test_handling_of_500_error_from_server(self, requests):
        with self.assertRaises(ValueError):
            self._verify(status_code=500)

    def test_handling_of_503_error_from_server(self):
        with self.assertRaises(ConnectionError):
            self._verify(status_code=503)
Example #2
0
class TestRemoteVerifier(unittest.TestCase, VerifierTestCases):

    def setUp(self):
        self.verifier = RemoteVerifier(["*"])

    @patch('browserid.netutils.requests')
    def _verify(self, requests, response_text='', assertion=EXPIRED_ASSERTION,
                status_code=200):
        response = Mock()
        response.text = response_text
        response.status_code = status_code
        requests.request.return_value = response

        return self.verifier.verify(assertion)

    def test_expired_assertion(self):
        try:
            return super(TestRemoteVerifier, self).test_expired_assertion()
        except ConnectionError:
            raise unittest.SkipTest("error connecting to remote server")

    def test_malformed_assertions(self):
        try:
            return super(TestRemoteVerifier, self).test_malformed_assertions()
        except ConnectionError:
            raise unittest.SkipTest("error connecting to remote server")

    def test_handling_of_valid_response_from_server(self):
        response_text = ('{"email": "*****@*****.**", "status": "okay", '
                         '"audience": "http://myfavoritebeer.org"}')
        data = self._verify(response_text=response_text)
        self.assertEquals(data["email"], "*****@*****.**")

    def test_handling_of_invalid_json_from_server(self):
        with self.assertRaises(ConnectionError):
            self._verify(response_text='SERVER RETURNS INVALID JSON')

    @patch('browserid.netutils.requests')
    def test_handling_of_incorrect_audience_returned_by_server(self, requests):
        response_text = ('{"email": "*****@*****.**", "status": "okay", '
                         '"audience": "WRONG"}')
        with self.assertRaises(AudienceMismatchError):
            self._verify(response_text=response_text)

    @patch('browserid.netutils.requests')
    def test_handling_of_500_error_from_server(self, requests):
        with self.assertRaises(ValueError):
            self._verify(status_code=500)

    def test_handling_of_503_error_from_server(self):
        with self.assertRaises(ConnectionError):
            self._verify(status_code=503)
Example #3
0
class TestRemoteVerifier(unittest.TestCase, VerifierTestCases):

    def setUp(self):
        self.verifier = RemoteVerifier(["*"])

    @patch('browserid.verifiers.remote.requests')
    def _verify(self, requests, response_text='', assertion=EXPIRED_ASSERTION,
                status_code=200):
        response = Mock()
        response.text = response_text
        response.status_code = status_code
        requests.post.return_value = response

        return self.verifier.verify(assertion)

    def test_handling_of_valid_response_from_server(self):
        response_text = ('{"email": "*****@*****.**", "status": "okay", '
                         '"audience": "http://myfavoritebeer.org"}')
        data = self._verify(response_text=response_text)
        self.assertEquals(data["email"], "*****@*****.**")

    def test_handling_of_invalid_json_from_server(self):
        with self.assertRaises(ConnectionError):
            self._verify(response_text='SERVER RETURNS SOMETHING THAT ISNT JSON')

    @patch('browserid.verifiers.remote.requests')
    def test_handling_of_incorrect_audience_returned_by_server(self, requests):
        response_text = ('{"email": "*****@*****.**", "status": "okay", '
                         '"audience": "WRONG"}')
        with self.assertRaises(AudienceMismatchError):
            self._verify(response_text=response_text)

    @patch('browserid.verifiers.remote.requests')
    def test_handling_of_500_error_from_server(self, requests):
        with self.assertRaises(ValueError):
            self._verify(status_code=500)

    def test_handling_of_503_error_from_server(self):
        with self.assertRaises(ConnectionError):
            self._verify(status_code=503)
Example #4
0
 def setUp(self):
     self.verifier = RemoteVerifier(["*"])
Example #5
0
 def setUp(self):
     self.verifier = RemoteVerifier(["*"])
Example #6
0
class TestRemoteVerifier(unittest.TestCase, VerifierTestCases):

    def setUp(self):
        self.verifier = RemoteVerifier(["*"])

    def test_handling_of_valid_response_from_server(self):
        def urlopen(url, data):
            class response(object):
                @staticmethod
                def read():
                    return '{"email": "*****@*****.**", '\
                           ' "status": "okay", '\
                           ' "audience": "http://myfavoritebeer.org"}'
            return response

        with patched_urlopen(urlopen):
            data = self.verifier.verify(EXPIRED_ASSERTION)
            self.assertEquals(data["email"], "*****@*****.**")

    def test_handling_of_invalid_content_length_header_from_server(self):
        def urlopen(url, data):
            class response(object):
                @staticmethod
                def info():
                    return {"Content-Length": "forty-two"}
                @staticmethod  # NOQA
                def read(size):
                    raise RuntimeError  # pragma: nocover
            return response

        with patched_urlopen(urlopen):
            self.assertRaises(ConnectionError,
                              self.verifier.verify, EXPIRED_ASSERTION)

    def test_handling_of_invalid_json_from_server(self):
        def urlopen(url, data):
            class response(object):
                @staticmethod
                def read():
                    return "SERVER RETURNS SOMETHING THAT ISNT JSON"
            return response

        with patched_urlopen(urlopen):
            self.assertRaises(ConnectionError,
                              self.verifier.verify, EXPIRED_ASSERTION)

    def test_handling_of_incorrect_audience_returned_by_server(self):
        def urlopen(url, data):
            class response(object):
                @staticmethod
                def read():
                    return '{"email": "*****@*****.**", '\
                           ' "status": "okay", '\
                           '"audience": "WRONG"}'
            return response

        with patched_urlopen(urlopen):
            self.assertRaises(AudienceMismatchError,
                              self.verifier.verify, EXPIRED_ASSERTION)

    def test_handling_of_500_error_from_server(self):
        with patched_urlopen(exc=ConnectionError("500 Server Error")):
            self.assertRaises(ValueError,
                            self.verifier.verify, EXPIRED_ASSERTION)

    def test_handling_of_503_error_from_server(self):
        with patched_urlopen(exc=ConnectionError("503 Back Off")):
            self.assertRaises(ConnectionError, self.verifier.verify,
                              EXPIRED_ASSERTION)
class TestRemoteVerifier(unittest.TestCase, VerifierTestCases):
    def setUp(self):
        self.verifier = RemoteVerifier(["*"])

    def test_handling_of_valid_response_from_server(self):
        def urlopen(url, data):
            class response(object):
                @staticmethod
                def read():
                    return '{"email": "*****@*****.**", '\
                           ' "status": "okay", '\
                           ' "audience": "http://myfavoritebeer.org"}'

            return response

        with patched_urlopen(urlopen):
            data = self.verifier.verify(EXPIRED_ASSERTION)
            self.assertEquals(data["email"], "*****@*****.**")

    def test_handling_of_invalid_content_length_header_from_server(self):
        def urlopen(url, data):
            class response(object):
                @staticmethod
                def info():
                    return {"Content-Length": "forty-two"}

                @staticmethod  # NOQA
                def read(size):
                    raise RuntimeError  # pragma: nocover

            return response

        with patched_urlopen(urlopen):
            self.assertRaises(ConnectionError, self.verifier.verify,
                              EXPIRED_ASSERTION)

    def test_handling_of_invalid_json_from_server(self):
        def urlopen(url, data):
            class response(object):
                @staticmethod
                def read():
                    return "SERVER RETURNS SOMETHING THAT ISNT JSON"

            return response

        with patched_urlopen(urlopen):
            self.assertRaises(ConnectionError, self.verifier.verify,
                              EXPIRED_ASSERTION)

    def test_handling_of_incorrect_audience_returned_by_server(self):
        def urlopen(url, data):
            class response(object):
                @staticmethod
                def read():
                    return '{"email": "*****@*****.**", '\
                           ' "status": "okay", '\
                           '"audience": "WRONG"}'

            return response

        with patched_urlopen(urlopen):
            self.assertRaises(AudienceMismatchError, self.verifier.verify,
                              EXPIRED_ASSERTION)

    def test_handling_of_500_error_from_server(self):
        with patched_urlopen(exc=ConnectionError("500 Server Error")):
            self.assertRaises(ValueError, self.verifier.verify,
                              EXPIRED_ASSERTION)

    def test_handling_of_503_error_from_server(self):
        with patched_urlopen(exc=ConnectionError("503 Back Off")):
            self.assertRaises(ConnectionError, self.verifier.verify,
                              EXPIRED_ASSERTION)