Skip to content

Commit

Permalink
Add tests for ServiceProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
numberoverzero committed Apr 7, 2015
1 parent 9886d76 commit 48e638a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyservice/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def raise_exception(self, exception):
whitelisted = name in self.obj.api["exceptions"]
debugging = self.obj.api["debug"]
if not whitelisted and not debugging:
raise wsgi.INTERNAL_ERROR
name = wsgi.INTERNAL_ERROR.__class__.__name__
args = wsgi.INTERNAL_ERROR.args

# Don't leak incomplete operation state
self.response.clear()
Expand Down
84 changes: 84 additions & 0 deletions tests/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,87 @@ def capture_extra(request, response, context):
process()

assert captured_response["extra"] == {"this should": "be available"}


def test_service_processor_invokes_function(service):
''' The mapped function for the operation should be called '''

called = False
request_body = ujson.dumps({"key": "value"})

@service.operation("foo")
def foo(request, response, context):
nonlocal called
called = True

assert request.key == "value"

process = processors.ServiceProcessor(service, "foo", request_body)

assert not called
process()
assert called


def test_service_processor_raises_whitelisted(service):
''' Serialize and return a whitelisted exception '''

service.api["exceptions"].append("FooException")

@service.operation("foo")
def foo(request, response, context):
raise service.exceptions.FooException(2, "args")

process = processors.ServiceProcessor(service, "foo", "{}")
result = process()

expected = {
"__exception__": {
"cls": "FooException",
"args": [2, "args"]
}
}
assert ujson.loads(result) == expected


def test_service_processor_raises_debugging(service):
''' Serialize and return non-whitelist when debugging '''

@service.operation("foo")
def foo(request, response, context):
raise service.exceptions.FooException(2, "args")

process = processors.ServiceProcessor(service, "foo", "{}")

# Don't redact exception when debugging
service.api["debug"] = True
result = process()

expected = {
"__exception__": {
"cls": "FooException",
"args": [2, "args"]
}
}
assert ujson.loads(result) == expected


def test_service_processor_redacts_non_whitelist(service):
''' Scrub non-whitelist exception data when not debugging '''

@service.operation("foo")
def foo(request, response, context):
raise service.exceptions.FooException(2, "args")

process = processors.ServiceProcessor(service, "foo", "{}")

# Don't redact exception when debugging
result = process()

expected = {
"__exception__": {
"cls": "RequestException",
"args": [500]
}
}
assert ujson.loads(result) == expected

0 comments on commit 48e638a

Please sign in to comment.