Refactor unit tests and remove Stripe API key dependency for test suite

Removed duplicate test
Refactored test to use better mocking and remove Stripe dependency
Removed setting of Stripe API key from test configs
Removed dodo.py Stripe API check from test task
This commit is contained in:
Jackie Munroe 2019-09-25 14:41:54 -07:00
Родитель 6c9ebfaeea
Коммит 3566896154
5 изменённых файлов: 10 добавлений и 101 удалений

Просмотреть файл

@ -593,7 +593,6 @@ def task_test():
return {
'task_dep': [
'check',
'stripe',
'yarn',
'venv',
'dynalite:stop',

Просмотреть файл

@ -25,7 +25,7 @@ ddb_process = None
def pytest_configure():
"""Called before testing begins"""
global ddb_process
for name in ("boto3", "botocore", "stripe"):
for name in ("boto3", "botocore"):
logging.getLogger(name).setLevel(logging.CRITICAL)
if os.getenv("AWS_LOCAL_DYNAMODB") is None:
os.environ["AWS_LOCAL_DYNAMODB"] = f"http://127.0.0.1:{CFG.DYNALITE_PORT}"
@ -37,9 +37,6 @@ def pytest_configure():
os.environ["ALLOWED_ORIGIN_SYSTEMS"] = "Test_system,Test_System,Test_System1"
sys._called_from_test = True
# Set stripe api key
stripe.api_key = CFG.STRIPE_API_KEY
# Locate absolute path of dynalite
dynalite = f"{CFG.REPO_ROOT}/node_modules/.bin/dynalite"

Просмотреть файл

@ -57,7 +57,7 @@ def get_file(filename, path=THIS_PATH, **overrides):
def pytest_configure():
"""Called before testing begins"""
global ddb_process
for name in ("boto3", "botocore", "stripe"):
for name in ("boto3", "botocore"):
logging.getLogger(name).setLevel(logging.CRITICAL)
if os.getenv("AWS_LOCAL_DYNAMODB") is None:
os.environ["AWS_LOCAL_DYNAMODB"] = f"http://127.0.0.1:{CFG.DYNALITE_PORT}"
@ -70,9 +70,6 @@ def pytest_configure():
os.environ["ALLOWED_ORIGIN_SYSTEMS"] = "Test_system,Test_System,Test_System1"
sys._called_from_test = True
# Set stripe api key
stripe.api_key = CFG.STRIPE_API_KEY
# Locate absolute path of dynalite
dynalite = f"{CFG.REPO_ROOT}/node_modules/.bin/dynalite"

Просмотреть файл

@ -214,94 +214,6 @@ def test_subscribe_customer_invalid_data(monkeypatch):
subscribe_customer(mock_customer, "invalid_plan_id")
def test_subscribe_customer_existing(app, monkeypatch):
"""
GIVEN create a subscription
WHEN provided a customer and plan
THEN validate subscription is created
"""
client = app.app.test_client()
plans_data = [
{
"id": "plan_123",
"product": "prod_1",
"interval": "month",
"amount": 25,
"currency": "usd",
"nickname": "Plan 1",
},
{
"id": "plan_2",
"product": "prod_1",
"interval": "year",
"amount": 250,
"currency": "usd",
"nickname": "Plan 2",
},
]
product_data = {"name": "Product 1"}
plans = Mock(return_value=plans_data)
product = Mock(return_value=product_data)
subhub_account = MagicMock()
get_user = MagicMock()
user_id = PropertyMock(return_value="user123")
cust_id = PropertyMock(return_value="cust123")
type(get_user).user_id = user_id
type(get_user).cust_id = cust_id
subhub_account.get_user = get_user
stripe_customer = Mock(
return_value={
"metadata": {"userid": "user123"},
"subscriptions": {
"data": [{"plan": {"id": "plan_123"}, "status": "active"}]
},
"sources": {
"data": [
{
"funding": "blah",
"last4": "1234",
"exp_month": "02",
"exp_year": "2020",
}
]
},
}
)
mock_true = Mock(return_value=True)
monkeypatch.setattr("stripe.Plan.list", plans)
monkeypatch.setattr("stripe.Product.retrieve", product)
monkeypatch.setattr("sub.payments.has_existing_plan", mock_true)
monkeypatch.setattr("flask.g.subhub_account", subhub_account)
monkeypatch.setattr("stripe.Customer.retrieve", stripe_customer)
path = "v1/sub/customer/user123/subscriptions"
data = {
"pmt_token": "tok_visa",
"plan_id": "plan_123",
"origin_system": "Test_system",
"email": "user123@example.com",
"display_name": "John Tester",
}
response = client.post(
path,
headers={"Authorization": "fake_payment_api_key"},
data=json.dumps(data),
content_type="application/json",
)
logger.info("response data", data=response.data)
assert response.status_code == 409
def test_cancel_subscription_no_subscription_found(monkeypatch):
"""

Просмотреть файл

@ -219,7 +219,9 @@ def test_subscribe_success(
assert response.status_code == 201
def test_subscribe_customer_existing(app, monkeypatch):
@mock.patch("sub.payments.has_existing_plan")
@mock.patch("sub.payments.existing_or_new_customer")
def test_subscribe_customer_existing(mock_new_customer, mock_has_plan, app):
"""
GIVEN a route that attempts to make a subscribe a customer
WHEN the customer already exists
@ -227,10 +229,12 @@ def test_subscribe_customer_existing(app, monkeypatch):
"""
client = app.app.test_client()
fh = open("tests/unit/fixtures/stripe_cust_test1.json")
cust_test1 = json.loads(fh.read())
fh.close()
mock_true = Mock(return_value=True)
monkeypatch.setattr("sub.payments.has_existing_plan", mock_true)
mock_new_customer.return_value = convert_to_stripe_object(cust_test1)
mock_has_plan.return_value = True
path = "v1/sub/customer/subtest/subscriptions"
data = {