зеркало из https://github.com/mozilla/subhub.git
COPS-852 Update sub/hub app.py tests
- Updated tests for sub/app.py - Updated tests for hub/app.py - Added new method to handle GetError when no db connection exists
This commit is contained in:
Родитель
4051b09564
Коммит
5fd718e327
|
@ -19,6 +19,12 @@ exclude_lines =
|
|||
if .debug:
|
||||
raise NotImplementedError
|
||||
if __name__ == .__main__.:
|
||||
logger.
|
||||
stripe.log
|
||||
stripe.verify_ssl_certs
|
||||
stripe.api_base
|
||||
from
|
||||
import
|
||||
omit =
|
||||
*/tests/*
|
||||
*/.venv/*
|
||||
|
|
|
@ -6,6 +6,7 @@ import os
|
|||
import sys
|
||||
import connexion
|
||||
import stripe
|
||||
import pynamodb
|
||||
|
||||
from flask import current_app, g, jsonify
|
||||
from flask_cors import CORS
|
||||
|
@ -57,6 +58,11 @@ def server_stripe_card_error(e):
|
|||
return jsonify({"message": f"{e.user_message}", "code": f"{e.code}"}), 402
|
||||
|
||||
|
||||
def database_connection_error(e):
|
||||
logger.error("unable to connect to db", error=e)
|
||||
return jsonify({"message": "Server Error", "status_code": "bad_connection"}), 500
|
||||
|
||||
|
||||
def is_docker() -> bool:
|
||||
path = "/proc/self/cgroup"
|
||||
return (
|
||||
|
@ -66,7 +72,8 @@ def is_docker() -> bool:
|
|||
)
|
||||
|
||||
|
||||
if is_docker():
|
||||
# excluding from coverage as this is for local testing only
|
||||
if is_docker(): # pragma: no cover
|
||||
stripe.log = "DEBUG"
|
||||
if CFG.STRIPE_LOCAL is not True:
|
||||
stripe.verify_ssl_certs = False
|
||||
|
@ -82,6 +89,7 @@ def create_app(config=None) -> Any:
|
|||
region = "localhost"
|
||||
host = f"http://dynamodb:{CFG.DYNALITE_PORT}" if is_docker() else CFG.DYNALITE_URL
|
||||
stripe.api_key = CFG.STRIPE_API_KEY
|
||||
logger.debug("aws", aws=CFG.AWS_EXECUTION_ENV)
|
||||
if CFG.AWS_EXECUTION_ENV:
|
||||
region = "us-west-2"
|
||||
host = None
|
||||
|
@ -95,6 +103,15 @@ def create_app(config=None) -> Any:
|
|||
table_name=CFG.DELETED_USER_TABLE, region=region, host=host
|
||||
)
|
||||
|
||||
# Setup error handlers
|
||||
@app.app.errorhandler(SubHubError)
|
||||
def display_subhub_errors(e: SubHubError):
|
||||
if e.status_code == 500:
|
||||
logger.error("display hub errors", error=e)
|
||||
response = jsonify(e.to_dict())
|
||||
response.status_code = e.status_code
|
||||
return response
|
||||
|
||||
if not app.app.hub_table.model.exists():
|
||||
app.app.hub_table.model.create_table(
|
||||
read_capacity_units=1, write_capacity_units=1, wait=True
|
||||
|
@ -105,14 +122,6 @@ def create_app(config=None) -> Any:
|
|||
read_capacity_units=1, write_capacity_units=1, wait=True
|
||||
)
|
||||
|
||||
@app.app.errorhandler(SubHubError)
|
||||
def display_subhub_errors(e: SubHubError):
|
||||
if e.status_code == 500:
|
||||
logger.error("display subhub errors", error=e)
|
||||
response = jsonify(e.to_dict())
|
||||
response.status_code = e.status_code
|
||||
return response
|
||||
|
||||
for error in (
|
||||
stripe.error.APIConnectionError,
|
||||
stripe.error.APIError,
|
||||
|
@ -133,6 +142,9 @@ def create_app(config=None) -> Any:
|
|||
for error in (stripe.error.CardError,):
|
||||
app.app.errorhandler(error)(server_stripe_card_error)
|
||||
|
||||
for error in (pynamodb.exceptions.GetError,):
|
||||
app.app.errorhandler(error)(database_connection_error)
|
||||
|
||||
@app.app.before_request
|
||||
def before_request():
|
||||
headers = dump_safe_headers(request.headers)
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
from flask import jsonify
|
||||
from stripe.error import AuthenticationError, CardError, StripeError
|
||||
|
||||
from hub.app import create_app
|
||||
from hub.app import server_stripe_error
|
||||
from hub.app import intermittent_stripe_error
|
||||
from hub.app import server_stripe_error_with_params
|
||||
from hub.app import server_stripe_card_error
|
||||
from shared.cfg import CFG
|
||||
|
||||
|
||||
def test_create_app():
|
||||
app = create_app()
|
||||
assert app
|
||||
|
||||
|
||||
def test_intermittent_stripe_error():
|
||||
expected = jsonify({"message": "something"}), 503
|
||||
error = StripeError("something")
|
||||
actual = intermittent_stripe_error(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
||||
|
||||
def test_server_stripe_error():
|
||||
expected = (
|
||||
jsonify({"message": "Internal Server Error", "code": "500", "params": None}),
|
||||
500,
|
||||
)
|
||||
error = AuthenticationError("something", code="500")
|
||||
actual = server_stripe_error(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
||||
|
||||
def test_server_stripe_error_with_params():
|
||||
expected = jsonify({"message": "something", "params": "param1", "code": "500"}), 500
|
||||
error = CardError("something", "param1", "500")
|
||||
actual = server_stripe_error_with_params(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
||||
|
||||
def test_server_stripe_card_error():
|
||||
expected = jsonify({"message": "something", "code": "402"}), 402
|
||||
error = CardError("something", "param1", "402")
|
||||
actual = server_stripe_card_error(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
|
@ -0,0 +1,84 @@
|
|||
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
import json
|
||||
from unittest import TestCase
|
||||
from mock import patch
|
||||
|
||||
from flask import jsonify
|
||||
from stripe.error import AuthenticationError, CardError, StripeError
|
||||
from hub.shared.exceptions import SubHubError
|
||||
|
||||
from hub.app import create_app
|
||||
from hub.app import server_stripe_error
|
||||
from hub.app import intermittent_stripe_error
|
||||
from hub.app import server_stripe_error_with_params
|
||||
from hub.app import server_stripe_card_error
|
||||
from shared.cfg import CFG
|
||||
from shared.log import get_logger
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
def test_create_app():
|
||||
app = create_app()
|
||||
assert app
|
||||
# subhub_error = app.display_subhub_errors("bad things")
|
||||
print(f"subhub error {dir(app)} app= {app}")
|
||||
|
||||
|
||||
def test_intermittent_stripe_error():
|
||||
expected = jsonify({"message": "something"}), 503
|
||||
error = StripeError("something")
|
||||
actual = intermittent_stripe_error(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
||||
|
||||
def test_server_stripe_error():
|
||||
expected = (
|
||||
jsonify({"message": "Internal Server Error", "code": "500", "params": None}),
|
||||
500,
|
||||
)
|
||||
error = AuthenticationError("something", code="500")
|
||||
actual = server_stripe_error(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
||||
|
||||
def test_server_stripe_error_with_params():
|
||||
expected = jsonify({"message": "something", "params": "param1", "code": "500"}), 500
|
||||
error = CardError("something", "param1", "500")
|
||||
actual = server_stripe_error_with_params(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
||||
|
||||
def test_server_stripe_card_error():
|
||||
expected = jsonify({"message": "something", "code": "402"}), 402
|
||||
error = CardError("something", "param1", "402")
|
||||
actual = server_stripe_card_error(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
||||
|
||||
class TestApp(TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.app = create_app()
|
||||
self.client = self.app.app.test_client()
|
||||
|
||||
def test_custom_404(self):
|
||||
path = "/v1/versions"
|
||||
response = self.client.get(path)
|
||||
self.assertEqual(response.status_code, 404)
|
||||
# self.assertIn(path, response.data)
|
||||
print(f"path {path} data {response}")
|
||||
|
||||
# def test_subhub_error(self):
|
||||
# with pytest.raises(SubHubError) as subhub_error:
|
||||
# expected = jsonify({"message": "something"}), 503
|
||||
# error = StripeError("something")
|
||||
# actual = intermittent_stripe_error(error)
|
||||
# assert actual[0].json == expected[0].json
|
||||
# assert actual[1] == expected[1]
|
||||
# assert actual[0]["status_code"] == expected[0]["status_code"]
|
|
@ -7,7 +7,7 @@ from pynamodb.attributes import UnicodeAttribute, ListAttribute
|
|||
from pynamodb.connection import Connection
|
||||
from pynamodb.indexes import GlobalSecondaryIndex, AllProjection
|
||||
from pynamodb.models import Model, DoesNotExist
|
||||
from pynamodb.exceptions import PutError, DeleteError
|
||||
from pynamodb.exceptions import PutError, DeleteError, GetError
|
||||
|
||||
from shared.log import get_logger
|
||||
|
||||
|
@ -61,6 +61,9 @@ class SubHubAccount:
|
|||
)
|
||||
logger.debug("get user", subscription_user=subscription_user)
|
||||
return subscription_user
|
||||
except GetError as e:
|
||||
logger.error("get error, unable to reach database", uid=uid)
|
||||
raise e
|
||||
except DoesNotExist:
|
||||
logger.debug("get user does not exist", uid=uid)
|
||||
return None
|
||||
|
|
|
@ -12,6 +12,8 @@ from sub.shared.exceptions import (
|
|||
SecretStringMissingError,
|
||||
)
|
||||
|
||||
from hub.shared.exceptions import SubHubError as HubError
|
||||
|
||||
|
||||
def test_subhub_error():
|
||||
message = "message"
|
||||
|
@ -24,6 +26,17 @@ def test_subhub_error():
|
|||
)
|
||||
|
||||
|
||||
def test_hub_error():
|
||||
message = "message"
|
||||
status_code = 513
|
||||
payload = dict(some="payload")
|
||||
ex = HubError(message, status_code=status_code, payload=payload)
|
||||
assert (
|
||||
str(ex)
|
||||
== "SubHubError(message=message, status_code=513, payload={'some': 'payload'})"
|
||||
)
|
||||
|
||||
|
||||
def test_intermittent_error():
|
||||
message = "message"
|
||||
ex1 = IntermittentError(message)
|
||||
|
|
|
@ -8,6 +8,7 @@ import logging
|
|||
import connexion
|
||||
import stripe
|
||||
import stripe.error
|
||||
import pynamodb
|
||||
|
||||
from typing import Optional, Union
|
||||
from flask import current_app, g, jsonify, request
|
||||
|
@ -38,7 +39,8 @@ def is_docker() -> bool:
|
|||
)
|
||||
|
||||
|
||||
if is_docker():
|
||||
# excluding from coverage as this is for local testing only
|
||||
if is_docker(): # pragma: no cover
|
||||
stripe.log = "DEBUG"
|
||||
if CFG.STRIPE_LOCAL is not True:
|
||||
stripe.verify_ssl_certs = False
|
||||
|
@ -82,13 +84,18 @@ def server_stripe_card_error(e):
|
|||
return jsonify({"message": f"{e.user_message}", "code": f"{e.code}"}), 402
|
||||
|
||||
|
||||
def database_connection_error(e):
|
||||
logger.error("unable to connect to db", error=e)
|
||||
return jsonify({"message": "Server Error", "status_code": "bad_connection"}), 500
|
||||
|
||||
|
||||
def create_app(config=None) -> connexion.FlaskApp:
|
||||
# configure_logger()
|
||||
logger.info("creating flask app", config=config)
|
||||
region = "localhost"
|
||||
host = f"http://dynamodb:{CFG.DYNALITE_PORT}" if is_docker() else CFG.DYNALITE_URL
|
||||
stripe.api_key = CFG.STRIPE_API_KEY
|
||||
logger.info("aws", aws=CFG.AWS_EXECUTION_ENV)
|
||||
logger.debug("aws", aws=CFG.AWS_EXECUTION_ENV)
|
||||
if CFG.AWS_EXECUTION_ENV:
|
||||
region = "us-west-2"
|
||||
host = None
|
||||
|
@ -122,7 +129,7 @@ def create_app(config=None) -> connexion.FlaskApp:
|
|||
@app.app.errorhandler(SubHubError)
|
||||
def display_subhub_errors(e: SubHubError):
|
||||
if e.status_code == 500:
|
||||
logger.error("display subhub errors", error=e)
|
||||
logger.error("display sub errors", error=e)
|
||||
response = jsonify(e.to_dict())
|
||||
response.status_code = e.status_code
|
||||
return response
|
||||
|
@ -147,6 +154,9 @@ def create_app(config=None) -> connexion.FlaskApp:
|
|||
for error in (stripe.error.CardError,):
|
||||
app.app.errorhandler(error)(server_stripe_card_error)
|
||||
|
||||
for error in (pynamodb.exceptions.GetError,):
|
||||
app.app.errorhandler(error)(database_connection_error)
|
||||
|
||||
@app.app.before_request
|
||||
def before_request():
|
||||
headers = dump_safe_headers(request.headers)
|
||||
|
|
|
@ -4,12 +4,17 @@
|
|||
|
||||
from flask import jsonify
|
||||
from stripe.error import AuthenticationError, CardError, StripeError
|
||||
from pynamodb.exceptions import GetError
|
||||
|
||||
from sub.app import create_app
|
||||
from sub.app import server_stripe_error
|
||||
from sub.app import intermittent_stripe_error
|
||||
from sub.app import server_stripe_error_with_params
|
||||
from sub.app import server_stripe_card_error
|
||||
from sub.app import database_connection_error
|
||||
from shared.log import get_logger
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
def test_create_app():
|
||||
|
@ -50,3 +55,15 @@ def test_server_stripe_card_error():
|
|||
actual = server_stripe_card_error(error)
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
||||
|
||||
def test_database_connection_error():
|
||||
expected = (
|
||||
jsonify({"message": "Server Error", "status_code": "bad_connection"}),
|
||||
500,
|
||||
)
|
||||
error = GetError("Server Error")
|
||||
actual = database_connection_error(error)
|
||||
print(f"dce {actual}")
|
||||
assert actual[0].json == expected[0].json
|
||||
assert actual[1] == expected[1]
|
||||
|
|
Загрузка…
Ссылка в новой задаче