Skip to content

Commit

Permalink
Added tests for action errors -> HTTP codes
Browse files Browse the repository at this point in the history
  • Loading branch information
rwb27 committed Jul 24, 2021
1 parent bbb08b8 commit 1a7a93b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
29 changes: 25 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask import Flask
from flask import Flask, abort
from flask.testing import FlaskClient
from flask.views import MethodView
from marshmallow import validate
Expand Down Expand Up @@ -188,7 +188,7 @@ class TestAction(ActionView):
def post(self):
return "POST"

thing.add_view(TestAction, "TestAction")
thing.add_view(TestAction, "/TestAction")

class TestProperty(PropertyView):
schema = {"count": fields.Integer()}
Expand All @@ -199,7 +199,7 @@ def get(self):
def post(self, args):
pass

thing.add_view(TestProperty, "TestProperty")
thing.add_view(TestProperty, "/TestProperty")

class TestFieldProperty(PropertyView):
schema = fields.String(validate=validate.OneOf(["one", "two"]))
Expand All @@ -210,7 +210,28 @@ def get(self):
def post(self, args):
pass

thing.add_view(TestFieldProperty, "TestFieldProperty")
thing.add_view(TestFieldProperty, "/TestFieldProperty")

class FailAction(ActionView):
wait_for = 1.0
def post(self):
raise Exception("This action is meant to fail with an Exception")

thing.add_view(FailAction, "/FailAction")

class AbortAction(ActionView):
wait_for = 1.0
def post(self):
abort(418, "I'm a teapot! This action should abort with an HTTP code 418")

thing.add_view(AbortAction, "/AbortAction")

class ActionWithValidation(ActionView):
wait_for = 1.0
args = {"test_arg": fields.String(validate=validate.OneOf(["one", "two"]))}
def post(self):
return True
thing.add_view(ActionWithValidation, "/ActionWithValidation")

return thing

Expand Down
50 changes: 50 additions & 0 deletions tests/test_action_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
import time
import json

from labthings import LabThing
from labthings.views import ActionView

@pytest.mark.filterwarnings("ignore:Exception in thread")
def test_action_exception_handling(thing_with_some_views, client):
"""Check errors in an Action are handled correctly
`/FieldProperty` has a validation constraint - it
should return a "bad response" error if invoked with
anything other than
"""
# `/FailAction` raises an `Exception`.
# This ought to return a 201 code representing the
# action that was successfully started - but should
# show that it failed through the "status" field.

# This is correct for the current (24/7/2021) behaviour
# but may want to change for the next version, e.g.
# returning a 500 code. For further discussion...
r = client.post("/FailAction")
assert r.status_code == 201
action = r.get_json()
assert action["status"] == "error"

def test_action_abort_and_validation(thing_with_some_views, client):
"""Check HTTPExceptions result in error codes.
Subclasses of HTTPError should result in a non-200 return code, not
just failures. This covers Marshmallow validation (400) and
use of `abort()`.
"""
# `/AbortAction` should return a 418 error code
r = client.post("/AbortAction")
assert r.status_code == 418

def test_action_validate(thing_with_some_views, client):
# `/ActionWithValidation` should fail with a 400 error
# if `test_arg` is not either `one` or `two`
r = client.post("/ActionWithValidation", data=json.dumps({"test_arg":"one"}))
assert r.status_code in [200, 201]
r = client.post("/ActionWithValidation", data=json.dumps({"test_arg":"three"}))
assert r.status_code in [422]


0 comments on commit 1a7a93b

Please sign in to comment.