-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for action errors -> HTTP codes
- Loading branch information
Showing
2 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
||
|