def test_verify_invalid_json(self): # If the response contains invalid JSON, return a failure # result. verifier = base.RemoteVerifier() with patch('django_browserid.base.requests.post') as post: post.return_value = self._response(content='{asg9=3{{{}}{') result = verifier.verify('asdf', 'http://testserver') ok_(not result) ok_(result.reason.startswith('Could not parse verifier response'))
def test_verify_success(self): # If the response contains valid JSON, return a result object # for that response. verifier = base.RemoteVerifier() with patch('django_browserid.base.requests.post') as post: post.return_value = self._response( content='{"status": "okay", "email": "*****@*****.**"}') result = verifier.verify('asdf', 'http://testserver') ok_(result) eq_(result.email, '*****@*****.**')
def test_verify_request_exception(self): # If a RequestException is raised during the POST, raise a # BrowserIDException with the RequestException as the cause. verifier = base.RemoteVerifier() request_exception = requests.exceptions.RequestException() with patch('django_browserid.base.requests.post') as post: post.side_effect = request_exception with self.assertRaises(base.BrowserIDException) as cm: verifier.verify('asdf', 'http://testserver') eq_(cm.exception.exc, request_exception)
def test_verify_kwargs(self): # Any keyword arguments passed to verify should be passed on as # POST arguments. verifier = base.RemoteVerifier() with patch('django_browserid.base.requests.post') as post: post.return_value = self._response(content='{"status":"failure"}') verifier.verify('asdf', 'http://testserver', foo='bar', baz=5) # foo parameter passed with 'bar' value. eq_(post.call_args[1]['data']['foo'], 'bar') eq_(post.call_args[1]['data']['baz'], 5)
def test_verify_invalid_json(self): """ If the response contains invalid JSON, return a failure result. """ verifier = base.RemoteVerifier() with patch('django_browserid.base.requests.post') as post: response = self._response(content='{asg9=3{{{}}{') response.json.side_effect = ValueError("Couldn't parse json") post.return_value = response result = verifier.verify('asdf', 'http://testserver') self.assertTrue(not result) self.assertTrue(result.reason.startswith('Could not parse verifier response'))
def test_verify_success(self): """ If the response contains valid JSON, return a result object for that response. """ verifier = base.RemoteVerifier() with patch('django_browserid.base.requests.post') as post: response = self._response( content='{"status": "okay", "email": "*****@*****.**"}') response.json.return_value = {"status": "okay", "email": "*****@*****.**"} post.return_value = response result = verifier.verify('asdf', 'http://testserver') self.assertTrue(result) self.assertEqual(result.email, '*****@*****.**')