Skip to content

Commit 1a7a93b

Browse files
committed
Added tests for action errors -> HTTP codes
1 parent bbb08b8 commit 1a7a93b

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

tests/conftest.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
from apispec import APISpec
77
from apispec.ext.marshmallow import MarshmallowPlugin
8-
from flask import Flask
8+
from flask import Flask, abort
99
from flask.testing import FlaskClient
1010
from flask.views import MethodView
1111
from marshmallow import validate
@@ -188,7 +188,7 @@ class TestAction(ActionView):
188188
def post(self):
189189
return "POST"
190190

191-
thing.add_view(TestAction, "TestAction")
191+
thing.add_view(TestAction, "/TestAction")
192192

193193
class TestProperty(PropertyView):
194194
schema = {"count": fields.Integer()}
@@ -199,7 +199,7 @@ def get(self):
199199
def post(self, args):
200200
pass
201201

202-
thing.add_view(TestProperty, "TestProperty")
202+
thing.add_view(TestProperty, "/TestProperty")
203203

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

213-
thing.add_view(TestFieldProperty, "TestFieldProperty")
213+
thing.add_view(TestFieldProperty, "/TestFieldProperty")
214+
215+
class FailAction(ActionView):
216+
wait_for = 1.0
217+
def post(self):
218+
raise Exception("This action is meant to fail with an Exception")
219+
220+
thing.add_view(FailAction, "/FailAction")
221+
222+
class AbortAction(ActionView):
223+
wait_for = 1.0
224+
def post(self):
225+
abort(418, "I'm a teapot! This action should abort with an HTTP code 418")
226+
227+
thing.add_view(AbortAction, "/AbortAction")
228+
229+
class ActionWithValidation(ActionView):
230+
wait_for = 1.0
231+
args = {"test_arg": fields.String(validate=validate.OneOf(["one", "two"]))}
232+
def post(self):
233+
return True
234+
thing.add_view(ActionWithValidation, "/ActionWithValidation")
214235

215236
return thing
216237

tests/test_action_api.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
import time
3+
import json
4+
5+
from labthings import LabThing
6+
from labthings.views import ActionView
7+
8+
@pytest.mark.filterwarnings("ignore:Exception in thread")
9+
def test_action_exception_handling(thing_with_some_views, client):
10+
"""Check errors in an Action are handled correctly
11+
12+
13+
14+
`/FieldProperty` has a validation constraint - it
15+
should return a "bad response" error if invoked with
16+
anything other than
17+
"""
18+
# `/FailAction` raises an `Exception`.
19+
# This ought to return a 201 code representing the
20+
# action that was successfully started - but should
21+
# show that it failed through the "status" field.
22+
23+
# This is correct for the current (24/7/2021) behaviour
24+
# but may want to change for the next version, e.g.
25+
# returning a 500 code. For further discussion...
26+
r = client.post("/FailAction")
27+
assert r.status_code == 201
28+
action = r.get_json()
29+
assert action["status"] == "error"
30+
31+
def test_action_abort_and_validation(thing_with_some_views, client):
32+
"""Check HTTPExceptions result in error codes.
33+
34+
Subclasses of HTTPError should result in a non-200 return code, not
35+
just failures. This covers Marshmallow validation (400) and
36+
use of `abort()`.
37+
"""
38+
# `/AbortAction` should return a 418 error code
39+
r = client.post("/AbortAction")
40+
assert r.status_code == 418
41+
42+
def test_action_validate(thing_with_some_views, client):
43+
# `/ActionWithValidation` should fail with a 400 error
44+
# if `test_arg` is not either `one` or `two`
45+
r = client.post("/ActionWithValidation", data=json.dumps({"test_arg":"one"}))
46+
assert r.status_code in [200, 201]
47+
r = client.post("/ActionWithValidation", data=json.dumps({"test_arg":"three"}))
48+
assert r.status_code in [422]
49+
50+

0 commit comments

Comments
 (0)