def test_customer_update_subscription_active(monkeypatch): updated_customer = Mock( return_value={ "metadata": {"userid": str(UID)}, "sources": { "data": [ { "funding": "100", "last4": "6655", "exp_month": "03", "exp_year": "2019", } ] }, "subscriptions": {"data": [get_file("subscription_active.json")]}, } ) charge_retrieve = {"failure_code": "somecode", "failure_message": "somemessage"} invoice_retrieve = {"charge": "true"} subhub_account = Mock(return_value=MockSubhubAccount()) product = Mock(return_value={"name": "Mozilla Product"}) monkeypatch.setattr("flask.g.subhub_account", subhub_account) monkeypatch.setattr("stripe.Customer.retrieve", updated_customer) monkeypatch.setattr("stripe.Invoice.retrieve", invoice_retrieve) monkeypatch.setattr("stripe.Charge.retrieve", charge_retrieve) monkeypatch.setattr("stripe.Product.retrieve", product) customer_update(str(UID)) updated_customer.assert_called()
def test_customer_update_subscription_cancel_at_period_end(monkeypatch): updated_customer = Mock( return_value={ "metadata": {"userid": str(UID)}, "sources": { "data": [ { "funding": "100", "last4": "6655", "exp_month": "03", "exp_year": "2019", } ] }, "subscriptions": { "data": [ get_file("subscription_active.json", cancel_at_period_end=True) ] }, } ) subhub_account = Mock(return_value=MockSubhubAccount()) product = Mock(return_value={"name": "Mozilla Product"}) monkeypatch.setattr("flask.g.subhub_account", subhub_account) monkeypatch.setattr("stripe.Customer.retrieve", updated_customer) monkeypatch.setattr("stripe.Product.retrieve", product) result = customer_update(str(UID)) updated_customer.assert_called() assert result[0]["subscriptions"][0]["cancel_at_period_end"] == True
def test_customer_update_success(monkeypatch): 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": [] }, "sources": { "data": [{ "funding": "blah", "last4": "1234", "exp_month": "02", "exp_year": "2020", }] }, }) monkeypatch.setattr("flask.g.subhub_account", subhub_account) monkeypatch.setattr("stripe.Customer.retrieve", stripe_customer) data, code = payments.customer_update("user123") assert 200 == code
def customer_update_subscription_incomplete(monkeypatch, charge): updated_customer = Mock( return_value={ "metadata": {"userid": str(UID)}, "sources": { "data": [ { "funding": "100", "last4": "6655", "exp_month": "03", "exp_year": "2019", } ] }, "subscriptions": { "data": [ get_file("subscription_incomplete.json", cancel_at_period_end=True) ] }, }, wraps=Customer, ) charge_retrieve = Mock( return_value={"failure_code": "somecode", "failure_message": "somemessage"} ) invoice_retrieve = Mock(return_value={"charge": charge}) subhub_account = Mock(return_value=MockSubhubAccount()) product = Mock(return_value={"name": "Mozilla Product"}) monkeypatch.setattr("stripe.Invoice.retrieve", invoice_retrieve) monkeypatch.setattr("flask.g.subhub_account", subhub_account) monkeypatch.setattr("stripe.Customer.retrieve", updated_customer) monkeypatch.setattr("stripe.Charge.retrieve", charge_retrieve) monkeypatch.setattr("stripe.Product.retrieve", product) result = customer_update(str(UID)) updated_customer.assert_called() invoice_retrieve.assert_called() assert result[0]["subscriptions"][0]["cancel_at_period_end"] is True
def test_customer_update_mismatch(self): self.mock_fetch_customer.return_value = self.valid_customer customer_updated = customer_update("user321") logger.info("customer mismatch", customer_updated=customer_updated) assert customer_updated[1] == 400
def test_customer_update_none(self): self.mock_fetch_customer.return_value = None customer_updated = customer_update("user123") assert customer_updated[1] == 404
def test_customer_update(self): self.mock_fetch_customer.return_value = self.valid_customer customer_updated = customer_update("user123") assert customer_updated[1] == 200