Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_result(self, app, path, expected, callback=None):
# see klein's test_resource.py
request = klein.test.test_resource.requestMock(path)
deferred = klein.test.test_resource._render(KleinResource(app), request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.getWrittenData(), six.b(json.dumps(expected)))
if callback is not None:
callback.assert_called_once_with(expected)
def test_valid_macaddr(self):
request = requestMock(b"/api/00:00:DE:CA:FB:AD")
deferred = _render(KleinResource(self.app), request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.code, 200)
self.assertEqual(request.getWrittenData(), six.b(json.dumps("00:00:DE:CA:FB:AD")))
def test_valid_email(self):
request = requestMock(b"/api/user@example.com")
deferred = _render(KleinResource(self.app), request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.code, 200)
self.assertEqual(request.getWrittenData(), six.b(json.dumps("user@example.com")))
def test_invalid_email(self):
request = requestMock(b"/api/not-a-valid-email")
deferred = _render(KleinResource(self.app), request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.code, 400)
self.assertTrue(b"Invalid email address: 'not-a-valid-email'." in request.getWrittenData())
def test_invalid_macaddr(self):
request = requestMock(b"/api/not-a-valid-mac")
deferred = _render(KleinResource(self.app), request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.code, 400)
self.assertTrue(b"Invalid MAC address: 'not-a-valid-mac'." in request.getWrittenData())
def test_invalid_serial(self):
request = requestMock(b"/api/not a valid serial")
deferred = _render(KleinResource(self.app), request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.code, 400)
self.assertTrue(b"Invalid serial number: 'not a valid serial'." in request.getWrittenData())
def test_valid_serial(self):
request = requestMock(b"/api/0xDECAFBAD")
deferred = _render(KleinResource(self.app), request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.code, 200)
self.assertEqual(request.getWrittenData(), six.b(json.dumps("0xDECAFBAD")))
def test_route_match_required_with_cookie(self):
request = requestMock(b"/api/user@example.com")
request.received_cookies = {b'token': mock.sentinel}
deferred = _render(self.kr, request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.getWrittenData(), json.dumps(self.userinfo).encode('ascii'))
self.auth.decode_token.assert_called_once_with(mock.sentinel)
def test_route_match_required_with_header(self):
request = requestMock(b"/api/user@example.com",
headers={b'Authorization': [str(mock.sentinel)]})
deferred = _render(self.kr, request)
self.assertEqual(self.successResultOf(deferred), None)
self.assertEqual(request.getWrittenData(), json.dumps(self.userinfo).encode('ascii'))
self.auth.decode_header_value.assert_called_once_with(str(mock.sentinel))