2016-11-09 14:56:54 +00:00
|
|
|
import pytest
|
|
|
|
|
from flask import url_for
|
2016-11-10 14:53:39 +00:00
|
|
|
from sqlalchemy.exc import DataError
|
2016-11-09 14:56:54 +00:00
|
|
|
|
2023-08-11 11:47:57 -07:00
|
|
|
from app.v2.errors import ValidationError
|
|
|
|
|
|
2016-11-09 14:56:54 +00:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@pytest.fixture(scope="function")
|
2020-04-27 15:27:08 +01:00
|
|
|
def app_for_test():
|
2016-11-09 14:56:54 +00:00
|
|
|
import flask
|
|
|
|
|
from flask import Blueprint
|
2021-03-10 13:55:06 +00:00
|
|
|
|
|
|
|
|
from app import init_app
|
2016-11-09 14:56:54 +00:00
|
|
|
from app.authentication.auth import AuthError
|
2020-07-22 17:00:20 +01:00
|
|
|
from app.v2.errors import BadRequestError, TooManyRequestsError
|
2016-11-09 14:56:54 +00:00
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
2023-08-29 14:54:30 -07:00
|
|
|
app.config["TESTING"] = True
|
2019-07-26 13:26:20 +01:00
|
|
|
init_app(app)
|
2016-11-09 14:56:54 +00:00
|
|
|
|
|
|
|
|
from app.v2.errors import register_errors
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
|
|
|
blue = Blueprint("v2_under_test", __name__, url_prefix="/v2/under_test")
|
2016-11-09 14:56:54 +00:00
|
|
|
|
|
|
|
|
@blue.route("/raise_auth_error", methods=["GET"])
|
|
|
|
|
def raising_auth_error():
|
|
|
|
|
raise AuthError("some message", 403)
|
|
|
|
|
|
|
|
|
|
@blue.route("/raise_bad_request", methods=["GET"])
|
|
|
|
|
def raising_bad_request():
|
|
|
|
|
raise BadRequestError(message="you forgot the thing")
|
|
|
|
|
|
|
|
|
|
@blue.route("/raise_too_many_requests", methods=["GET"])
|
|
|
|
|
def raising_too_many_requests():
|
|
|
|
|
raise TooManyRequestsError(sending_limit="452")
|
|
|
|
|
|
|
|
|
|
@blue.route("/raise_validation_error", methods=["GET"])
|
|
|
|
|
def raising_validation_error():
|
|
|
|
|
from app.schema_validation import validate
|
|
|
|
|
from app.v2.notifications.notification_schemas import post_sms_request
|
2023-08-29 14:54:30 -07:00
|
|
|
|
2016-11-09 14:56:54 +00:00
|
|
|
validate({"template_id": "bad_uuid"}, post_sms_request)
|
|
|
|
|
|
2016-11-10 14:53:39 +00:00
|
|
|
@blue.route("raise_data_error", methods=["GET"])
|
|
|
|
|
def raising_data_error():
|
|
|
|
|
raise DataError("There was a db problem", "params", "orig")
|
|
|
|
|
|
|
|
|
|
@blue.route("raise_exception", methods=["GET"])
|
|
|
|
|
def raising_exception():
|
|
|
|
|
raise AssertionError("Raising any old exception")
|
|
|
|
|
|
2016-11-09 14:56:54 +00:00
|
|
|
register_errors(blue)
|
|
|
|
|
app.register_blueprint(blue)
|
|
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_error(app_for_test):
|
|
|
|
|
with app_for_test.test_request_context():
|
|
|
|
|
with app_for_test.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
response = client.get(url_for("v2_under_test.raising_auth_error"))
|
2016-11-09 14:56:54 +00:00
|
|
|
assert response.status_code == 403
|
2019-07-26 13:26:20 +01:00
|
|
|
error = response.json
|
2023-08-29 14:54:30 -07:00
|
|
|
assert error == {
|
|
|
|
|
"status_code": 403,
|
|
|
|
|
"errors": [{"error": "AuthError", "message": "some message"}],
|
|
|
|
|
}
|
2016-11-09 14:56:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_bad_request_error(app_for_test):
|
|
|
|
|
with app_for_test.test_request_context():
|
|
|
|
|
with app_for_test.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
response = client.get(url_for("v2_under_test.raising_bad_request"))
|
2016-11-09 14:56:54 +00:00
|
|
|
assert response.status_code == 400
|
2019-07-26 13:26:20 +01:00
|
|
|
error = response.json
|
2023-08-29 14:54:30 -07:00
|
|
|
assert error == {
|
|
|
|
|
"status_code": 400,
|
|
|
|
|
"errors": [
|
|
|
|
|
{"error": "BadRequestError", "message": "you forgot the thing"}
|
|
|
|
|
],
|
|
|
|
|
}
|
2016-11-09 14:56:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_too_many_requests_error(app_for_test):
|
|
|
|
|
with app_for_test.test_request_context():
|
|
|
|
|
with app_for_test.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
response = client.get(url_for("v2_under_test.raising_too_many_requests"))
|
2016-11-09 14:56:54 +00:00
|
|
|
assert response.status_code == 429
|
2019-07-26 13:26:20 +01:00
|
|
|
error = response.json
|
2023-08-29 14:54:30 -07:00
|
|
|
assert error == {
|
|
|
|
|
"status_code": 429,
|
|
|
|
|
"errors": [
|
|
|
|
|
{
|
|
|
|
|
"error": "TooManyRequestsError",
|
|
|
|
|
"message": "Exceeded send limits (452) for today",
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
}
|
2016-11-09 14:56:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_validation_error(app_for_test):
|
|
|
|
|
with app_for_test.test_request_context():
|
|
|
|
|
with app_for_test.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
response = client.get(url_for("v2_under_test.raising_validation_error"))
|
2016-11-09 14:56:54 +00:00
|
|
|
assert response.status_code == 400
|
2019-07-26 13:26:20 +01:00
|
|
|
error = response.json
|
2016-11-09 14:56:54 +00:00
|
|
|
assert len(error.keys()) == 2
|
2023-08-29 14:54:30 -07:00
|
|
|
assert error["status_code"] == 400
|
|
|
|
|
assert len(error["errors"]) == 2
|
|
|
|
|
assert {
|
|
|
|
|
"error": "ValidationError",
|
|
|
|
|
"message": "phone_number is a required property",
|
|
|
|
|
} in error["errors"]
|
|
|
|
|
assert {
|
|
|
|
|
"error": "ValidationError",
|
|
|
|
|
"message": "template_id is not a valid UUID",
|
|
|
|
|
} in error["errors"]
|
2023-08-11 11:47:57 -07:00
|
|
|
ve = ValidationError("phone_number is a required property")
|
|
|
|
|
assert ve.message == "Your notification has failed validation"
|
|
|
|
|
assert ve.status_code == 400
|
2016-11-10 14:53:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_data_errors(app_for_test):
|
|
|
|
|
with app_for_test.test_request_context():
|
|
|
|
|
with app_for_test.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
response = client.get(url_for("v2_under_test.raising_data_error"))
|
2016-11-10 14:53:39 +00:00
|
|
|
assert response.status_code == 404
|
2019-07-26 13:26:20 +01:00
|
|
|
error = response.json
|
2023-08-29 14:54:30 -07:00
|
|
|
assert error == {
|
|
|
|
|
"status_code": 404,
|
|
|
|
|
"errors": [{"error": "DataError", "message": "No result found"}],
|
|
|
|
|
}
|
2016-11-10 14:53:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_internal_server_error_handler(app_for_test):
|
|
|
|
|
with app_for_test.test_request_context():
|
|
|
|
|
with app_for_test.test_client() as client:
|
|
|
|
|
response = client.get(url_for("v2_under_test.raising_exception"))
|
|
|
|
|
assert response.status_code == 500
|
2019-07-26 13:26:20 +01:00
|
|
|
error = response.json
|
2023-08-29 14:54:30 -07:00
|
|
|
assert error == {
|
|
|
|
|
"status_code": 500,
|
|
|
|
|
"errors": [
|
|
|
|
|
{"error": "AssertionError", "message": "Internal server error"}
|
|
|
|
|
],
|
|
|
|
|
}
|
2019-07-26 13:26:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_bad_method(app_for_test):
|
|
|
|
|
with app_for_test.test_request_context():
|
|
|
|
|
with app_for_test.test_client() as client:
|
|
|
|
|
response = client.post(url_for("v2_under_test.raising_exception"))
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 405
|
|
|
|
|
|
2022-04-05 17:06:08 +01:00
|
|
|
assert response.get_json(force=True) == {
|
2021-06-01 10:53:28 +01:00
|
|
|
"result": "error",
|
2023-08-29 14:54:30 -07:00
|
|
|
"message": "The method is not allowed for the requested URL.",
|
2021-06-01 10:53:28 +01:00
|
|
|
}
|