class InvoiceTestCase(BaseTestCase): def setUp(self): super(InvoiceTestCase, self).setUp() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" self.sub.save() @patch("hc.payments.views.braintree") def test_it_works(self, mock_braintree): tx = Mock() tx.id = "abc123" tx.customer_details.id = "test-customer-id" tx.created_at = None mock_braintree.Transaction.find.return_value = tx self.client.login(username="******", password="******") r = self.client.get("/invoice/abc123/") self.assertContains(r, "ABC123") # tx.id in uppercase @patch("hc.payments.views.braintree") def test_it_checks_customer_id(self, mock_braintree): tx = Mock() tx.id = "abc123" tx.customer_details.id = "test-another-customer-id" tx.created_at = None mock_braintree.Transaction.find.return_value = tx self.client.login(username="******", password="******") r = self.client.get("/invoice/abc123/") self.assertEqual(r.status_code, 403)
class BillingTestCase(BaseTestCase): def setUp(self): super(BillingTestCase, self).setUp() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" self.sub.save() @patch("hc.payments.views.braintree") def test_it_works(self, mock_braintree): m1 = Mock(id="abc123", amount=123) m2 = Mock(id="def456", amount=456) mock_braintree.Transaction.search.return_value = [m1, m2] self.client.login(username="******", password="******") r = self.client.get("/billing/") self.assertContains(r, "123") self.assertContains(r, "def456") def test_it_saves_company_details(self): self.client.login(username="******", password="******") r = self.client.post("/billing/", {"bill_to": "foo\nbar"}) self.assertEqual(r.status_code, 302) self.profile.refresh_from_db() self.assertEqual(self.profile.bill_to, "foo\nbar")
class CancelPlanTestCase(BaseTestCase): def setUp(self): super(CancelPlanTestCase, self).setUp() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.plan_id = "P5" self.sub.save() self.profile.ping_log_limit = 1000 self.profile.check_limit = 500 self.profile.sms_limit = 50 self.profile.save() @patch("hc.payments.models.braintree") def test_it_works(self, mock_braintree): self.client.login(username="******", password="******") r = self.client.post("/pricing/cancel_plan/") self.assertRedirects(r, "/pricing/") self.sub.refresh_from_db() self.assertEqual(self.sub.subscription_id, "") self.assertEqual(self.sub.plan_id, "") # User's profile should have standard limits profile = Profile.objects.get(user=self.alice) self.assertEqual(profile.ping_log_limit, 100) self.assertEqual(profile.check_limit, 20) self.assertEqual(profile.sms_limit, 0) self.assertFalse(profile.team_access_allowed)
def test_it_cancels_previous_subscription(self, mock): self._setup_mock(mock) sub = Subscription(user=self.alice) sub.subscription_id = "prev-sub" sub.save() r = self.run_create_plan() self.assertRedirects(r, "/pricing/") assert mock.Subscription.cancel.called
def test_it_cancels_previous_subscription(self, mock): self._setup_mock(mock) sub = Subscription(user=self.alice) sub.subscription_id = "prev-sub" sub.save() r = self.run_update() self.assertRedirects(r, "/accounts/profile/billing/") self.assertTrue(mock.Subscription.cancel.called)
class PricingTestCase(BaseTestCase): def test_anonymous(self): r = self.client.get("/pricing/") self.assertContains(r, "Unlimited Team Members", status_code=200) self.assertNotContains(r, "jumbotron") # A subscription object should have NOT been created self.assertFalse(Subscription.objects.exists()) def test_authenticated(self): self.client.login(username="******", password="******") r = self.client.get("/pricing/") self.assertContains(r, "Unlimited Team Members", status_code=200) self.assertContains(r, "jumbotron") # A subscription object still should have NOT been created self.assertFalse(Subscription.objects.exists()) def test_authenticated_for_project(self): self.client.login(username="******", password="******") r = self.client.get("/projects/%s/pricing/" % self.project.code) self.assertContains(r, "Unlimited Team Members", status_code=200) self.assertContains(r, "jumbotron") @override_settings(USE_PAYMENTS=True) def test_pricing_is_visible_for_all(self): for email in ("*****@*****.**", "*****@*****.**"): self.client.login(username=email, password="******") r = self.client.get("/docs/") self.assertContains(r, "Pricing") def test_it_offers_to_switch(self): self.client.login(username="******", password="******") r = self.client.get("/projects/%s/pricing/" % self.project.code) self.assertContains(r, "To manage billing for this project") def test_it_shows_active_plan(self): self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.plan_id = "P20" self.sub.plan_name = "Business ($20 / month)" self.sub.save() self.client.login(username="******", password="******") r = self.client.get("/pricing/") self.assertContains(r, "Business ($20 / month)", status_code=200) r = self.client.get("/projects/%s/pricing/" % self.project.code) self.assertContains(r, "Business ($20 / month)", status_code=200)
class PdfInvoiceTestCase(BaseTestCase): def setUp(self): super(PdfInvoiceTestCase, self).setUp() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" self.sub.save() self.tx = Mock() self.tx.id = "abc123" self.tx.customer_details.id = "test-customer-id" self.tx.created_at = now() self.tx.currency_iso_code = "USD" self.tx.amount = 5 self.tx.subscription_details.billing_period_start_date = now() self.tx.subscription_details.billing_period_end_date = now() @skipIf(reportlab is None, "reportlab not installed") @patch("hc.payments.models.braintree") def test_it_works(self, mock_braintree): mock_braintree.Transaction.find.return_value = self.tx self.client.login(username="******", password="******") r = self.client.get("/invoice/pdf/abc123/") self.assertTrue(b"ABC123" in r.content) self.assertTrue(b"*****@*****.**" in r.content) @patch("hc.payments.models.braintree") def test_it_checks_customer_id(self, mock_braintree): tx = Mock() tx.id = "abc123" tx.customer_details.id = "test-another-customer-id" tx.created_at = None mock_braintree.Transaction.find.return_value = tx self.client.login(username="******", password="******") r = self.client.get("/invoice/pdf/abc123/") self.assertEqual(r.status_code, 403) @skipIf(reportlab is None, "reportlab not installed") @patch("hc.payments.models.braintree") def test_it_shows_company_data(self, mock): self.sub.address_id = "aa" self.sub.save() mock.Transaction.find.return_value = self.tx mock.Address.find.return_value = {"company": "Alice and Partners"} self.client.login(username="******", password="******") r = self.client.get("/invoice/pdf/abc123/") self.assertTrue(b"Alice and Partners" in r.content)
def test_it_works(self, mock_braintree): sub = Subscription(user=self.alice) sub.customer_id = "fake-customer-id" sub.save() mock_braintree.ClientToken.generate.return_value = "test-token" self.client.login(username="******", password="******") r = self.client.get("/pricing/token/") self.assertContains(r, "test-token", status_code=200) # A subscription object should have been created assert Subscription.objects.count() == 1
class CancelPlanTestCase(BaseTestCase): def setUp(self): super(CancelPlanTestCase, self).setUp() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.plan_id = "P5" self.sub.save() @patch("hc.payments.views.braintree") def test_it_works(self, mock_braintree): self.client.login(username="******", password="******") r = self.client.post("/pricing/cancel_plan/") self.assertRedirects(r, "/pricing/") self.sub.refresh_from_db() self.assertEqual(self.sub.subscription_id, "") self.assertEqual(self.sub.plan_id, "")
class PricingTestCase(BaseTestCase): def test_anonymous(self): r = self.client.get("/pricing/") self.assertContains(r, "Unlimited Team Members", status_code=200) # A subscription object should have NOT been created assert Subscription.objects.count() == 0 def test_authenticated(self): self.client.login(username="******", password="******") r = self.client.get("/pricing/") self.assertContains(r, "Unlimited Team Members", status_code=200) # A subscription object still should have NOT been created assert Subscription.objects.count() == 0 @override_settings(USE_PAYMENTS=True) def test_pricing_is_visible_for_all(self): for email in ("*****@*****.**", "*****@*****.**"): self.client.login(username=email, password="******") r = self.client.get("/docs/") self.assertContains(r, "Pricing") def test_it_offers_to_switch(self): self.client.login(username="******", password="******") r = self.client.get("/pricing/") self.assertContains(r, "To manage this team") def test_it_shows_active_plan(self): self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.plan_id = "P20" self.sub.plan_name = "Standard ($20 / month)" self.sub.save() self.client.login(username="******", password="******") r = self.client.get("/pricing/") self.assertContains(r, "Standard ($20 / month)", status_code=200)
class BillingHistoryTestCase(BaseTestCase): def setUp(self): super(BillingHistoryTestCase, self).setUp() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" self.sub.save() @patch("hc.payments.models.braintree") def test_it_works(self, mock_braintree): m1 = Mock(id="abc123", amount=123, created_at=now()) m2 = Mock(id="def456", amount=456, created_at=now()) mock_braintree.Transaction.search.return_value = [m1, m2] self.client.login(username="******", password="******") r = self.client.get("/accounts/profile/billing/history/") self.assertContains(r, "123") self.assertContains(r, "456")
class BillingHistoryTestCase(BaseTestCase): def setUp(self): super(BillingHistoryTestCase, self).setUp() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" self.sub.save() @patch("hc.payments.models.braintree") def test_it_works(self, mock_braintree): m1 = Mock(id="abc123", amount=123) m2 = Mock(id="def456", amount=456) mock_braintree.Transaction.search.return_value = [m1, m2] self.client.login(username="******", password="******") r = self.client.get("/accounts/profile/billing/history/") self.assertContains(r, "123") self.assertContains(r, "def456")
class ChargeWebhookTestCase(BaseTestCase): def setUp(self): super(ChargeWebhookTestCase, self).setUp() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" self.sub.send_invoices = True self.sub.save() self.tx = Mock() self.tx.id = "abc123" self.tx.customer_details.id = "test-customer-id" self.tx.created_at = now() self.tx.currency_iso_code = "USD" self.tx.amount = 5 self.tx.subscription_details.billing_period_start_date = now() self.tx.subscription_details.billing_period_end_date = now() @skipIf(reportlab is None, "reportlab not installed") @patch("hc.payments.views.Subscription.objects.by_braintree_webhook") def test_it_works(self, mock_getter): mock_getter.return_value = self.sub, self.tx r = self.client.post("/pricing/charge/") self.assertEqual(r.status_code, 200) # See if email was sent self.assertEqual(len(mail.outbox), 1) msg = mail.outbox[0] self.assertEqual(msg.subject, "Invoice from Mychecks") self.assertEqual(msg.to, ["*****@*****.**"]) self.assertEqual(msg.attachments[0][0], "MS-HC-ABC123.pdf") @patch("hc.payments.views.Subscription.objects.by_braintree_webhook") def test_it_obeys_send_invoices_flag(self, mock_getter): mock_getter.return_value = self.sub, self.tx self.sub.send_invoices = False self.sub.save() r = self.client.post("/pricing/charge/") self.assertEqual(r.status_code, 200) # It should not send the email self.assertEqual(len(mail.outbox), 0) @skipIf(reportlab is None, "reportlab not installed") @patch("hc.payments.views.Subscription.objects.by_braintree_webhook") def test_it_uses_invoice_email(self, mock_getter): mock_getter.return_value = self.sub, self.tx self.sub.invoice_email = "*****@*****.**" self.sub.save() r = self.client.post("/pricing/charge/") self.assertEqual(r.status_code, 200) # See if the email was sent to Alice's accountant: self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, ["*****@*****.**"])
class BillingTestCase(TestCase): def setUp(self): super(BillingTestCase, self).setUp() self.alice = User(username="******", email="*****@*****.**") self.alice.set_password("password") self.alice.save() self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" self.sub.save() @patch("hc.payments.views.braintree") def test_it_works(self, mock_braintree): m1 = Mock(id="abc123", amount=123) m2 = Mock(id="def456", amount=456) mock_braintree.Transaction.search.return_value = [m1, m2] self.client.login(username="******", password="******") r = self.client.get("/billing/") self.assertContains(r, "123") self.assertContains(r, "def456")
class UpdateSubscriptionTestCase(BaseTestCase): def _setup_mock(self, mock): """ Set up Braintree calls that the controller will use. """ mock.Subscription.create.return_value.is_success = True mock.Subscription.create.return_value.subscription.id = "t-sub-id" def run_update(self, plan_id="P20", nonce="fake-nonce"): form = {"plan_id": plan_id, "nonce": nonce} self.client.login(username="******", password="******") return self.client.post("/pricing/update/", form, follow=True) @patch("hc.payments.models.braintree") def test_it_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_update() self.assertRedirects(r, "/accounts/profile/billing/") self.assertContains(r, "Your billing plan has been updated!") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "P20") self.assertEqual(sub.plan_name, "Business ($20 / month)") # User's profile should have a higher limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 100) self.assertEqual(self.profile.team_limit, 9) self.assertEqual(self.profile.sms_limit, 50) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called # because there was no previous subscription self.assertFalse(mock.Subscription.cancel.called) self.assertTrue(mock.Subscription.create.called) @patch("hc.payments.models.braintree") def test_supporter_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_update("S5") self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "S5") self.assertEqual(sub.plan_name, "Supporter ($5 / month)") # User's profile should have adjusted limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 20) self.assertEqual(self.profile.team_limit, 2) self.assertEqual(self.profile.sms_limit, 5) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called assert not mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_yearly_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_update("Y192") self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "Y192") self.assertEqual(sub.plan_name, "Business ($192 / year)") # User's profile should have a higher limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 100) self.assertEqual(self.profile.team_limit, 9) self.assertEqual(self.profile.sms_limit, 50) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called assert not mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_plus_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_update("P80") self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "P80") self.assertEqual(sub.plan_name, "Business Plus ($80 / month)") # User's profile should have a higher limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 1000) self.assertEqual(self.profile.team_limit, 500) self.assertEqual(self.profile.sms_limit, 500) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called assert not mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_it_cancels(self, mock): self._setup_mock(mock) self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.plan_id = "P20" self.sub.plan_name = "Business ($20/mo)" self.sub.save() self.profile.sms_limit = 1 self.profile.sms_sent = 1 self.profile.save() r = self.run_update("") self.assertRedirects(r, "/accounts/profile/billing/") self.assertContains(r, "Your billing plan has been updated!") # Subscription should be cleared sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "") self.assertEqual(sub.plan_id, "") self.assertEqual(sub.plan_name, "") # User's profile should have standard limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 100) self.assertEqual(self.profile.check_limit, 20) self.assertEqual(self.profile.team_limit, 2) self.assertEqual(self.profile.sms_limit, 5) self.assertTrue(mock.Subscription.cancel.called) def test_bad_plan_id(self): r = self.run_update(plan_id="this-is-wrong") self.assertEqual(r.status_code, 400) @patch("hc.payments.models.braintree") def test_it_cancels_previous_subscription(self, mock): self._setup_mock(mock) sub = Subscription(user=self.alice) sub.subscription_id = "prev-sub" sub.save() r = self.run_update() self.assertRedirects(r, "/accounts/profile/billing/") self.assertTrue(mock.Subscription.cancel.called) @patch("hc.payments.models.braintree") def test_subscription_creation_failure(self, mock): mock.Subscription.create.return_value.is_success = False mock.Subscription.create.return_value.message = "sub failure" r = self.run_update() self.assertRedirects(r, "/accounts/profile/billing/") self.assertContains(r, "sub failure") @patch("hc.payments.models.braintree") def test_failed_plan_change_resets_limits(self, mock): # Initial state: the user has a subscription and a high check limit: sub = Subscription.objects.for_user(self.alice) sub.subscription_id = "old-sub-id" sub.save() self.profile.check_limit = 1000 self.profile.save() # Simulate a subscription creation failure: mock.Subscription.create.return_value.is_success = False mock.Subscription.create.return_value.message = "sub failure" r = self.run_update() # It should cancel the current plan self.assertTrue(mock.Subscription.cancel.called) # It should clear out the limits: self.profile.refresh_from_db() self.assertEqual(self.profile.check_limit, 20) # And it should show the error message from API: self.assertRedirects(r, "/accounts/profile/billing/") self.assertContains(r, "sub failure") @patch("hc.payments.models.braintree") def test_it_updates_payment_method(self, mock): # Initial state: the user has a subscription and a high check limit: sub = Subscription.objects.for_user(self.alice) sub.plan_id = "P20" sub.subscription_id = "old-sub-id" sub.save() r = self.run_update() # It should update the existing subscription self.assertTrue(mock.Subscription.update.called) self.assertRedirects(r, "/accounts/profile/billing/") self.assertContains(r, "Your payment method has been updated!")
class SetPlanTestCase(BaseTestCase): def _setup_mock(self, mock): """ Set up Braintree calls that the controller will use. """ mock.Subscription.create.return_value.is_success = True mock.Subscription.create.return_value.subscription.id = "t-sub-id" def run_set_plan(self, plan_id="P5"): form = {"plan_id": plan_id} self.client.login(username="******", password="******") return self.client.post("/pricing/set_plan/", form, follow=True) @patch("hc.payments.models.braintree") def test_it_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_set_plan() self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "P5") # User's profile should have a higher limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 500) self.assertEqual(self.profile.team_limit, 9) self.assertEqual(self.profile.sms_limit, 50) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called assert not mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_yearly_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_set_plan("Y48") self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "Y48") # User's profile should have a higher limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 500) self.assertEqual(self.profile.team_limit, 9) self.assertEqual(self.profile.sms_limit, 50) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called assert not mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_it_cancels(self, mock): self._setup_mock(mock) self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.plan_id = "P5" self.sub.save() self.profile.sms_limit = 1 self.profile.sms_sent = 1 self.profile.save() r = self.run_set_plan("") self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be cleared sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "") self.assertEqual(sub.plan_id, "") # User's profile should have standard limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 100) self.assertEqual(self.profile.check_limit, 20) self.assertEqual(self.profile.team_limit, 2) self.assertEqual(self.profile.sms_limit, 0) assert mock.Subscription.cancel.called def test_bad_plan_id(self): r = self.run_set_plan(plan_id="this-is-wrong") self.assertEqual(r.status_code, 400) @patch("hc.payments.models.braintree") def test_it_cancels_previous_subscription(self, mock): self._setup_mock(mock) sub = Subscription(user=self.alice) sub.subscription_id = "prev-sub" sub.save() r = self.run_set_plan() self.assertRedirects(r, "/accounts/profile/billing/") assert mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_subscription_creation_failure(self, mock): self._setup_mock(mock) mock.Subscription.create.return_value.is_success = False mock.Subscription.create.return_value.message = "sub failure" r = self.run_set_plan() self.assertRedirects(r, "/accounts/profile/billing/") self.assertContains(r, "sub failure")
class UpdatePaymentMethodTestCase(BaseTestCase): def _setup_mock(self, mock): """ Set up Braintree calls that the controller will use. """ mock.PaymentMethod.create.return_value.is_success = True mock.PaymentMethod.create.return_value.payment_method.token = "fake" @patch("hc.payments.models.braintree") def test_it_retrieves_paypal(self, mock): self._setup_mock(mock) mock.paypal_account.PayPalAccount = dict mock.credit_card.CreditCard = list mock.PaymentMethod.find.return_value = {"email": "*****@*****.**"} self.sub = Subscription(user=self.alice) self.sub.payment_method_token = "fake-token" self.sub.save() self.client.login(username="******", password="******") r = self.client.get("/accounts/profile/billing/payment_method/") self.assertContains(r, "*****@*****.**") @patch("hc.payments.models.braintree") def test_it_retrieves_cc(self, mock): self._setup_mock(mock) mock.paypal_account.PayPalAccount = list mock.credit_card.CreditCard = dict mock.PaymentMethod.find.return_value = {"masked_number": "1***2"} self.sub = Subscription(user=self.alice) self.sub.payment_method_token = "fake-token" self.sub.save() self.client.login(username="******", password="******") r = self.client.get("/accounts/profile/billing/payment_method/") self.assertContains(r, "1***2") @patch("hc.payments.models.braintree") def test_it_creates_payment_method(self, mock): self._setup_mock(mock) self.sub = Subscription(user=self.alice) self.sub.customer_id = "test-customer" self.sub.save() self.client.login(username="******", password="******") form = {"payment_method_nonce": "test-nonce"} r = self.client.post("/accounts/profile/billing/payment_method/", form) self.assertRedirects(r, "/accounts/profile/billing/") @patch("hc.payments.models.braintree") def test_it_creates_customer(self, mock): self._setup_mock(mock) mock.Customer.create.return_value.is_success = True mock.Customer.create.return_value.customer.id = "test-customer-id" self.sub = Subscription(user=self.alice) self.sub.save() self.client.login(username="******", password="******") form = {"payment_method_nonce": "test-nonce"} self.client.post("/accounts/profile/billing/payment_method/", form) self.sub.refresh_from_db() self.assertEqual(self.sub.customer_id, "test-customer-id") @patch("hc.payments.models.braintree") def test_it_updates_subscription(self, mock): self._setup_mock(mock) self.sub = Subscription(user=self.alice) self.sub.customer_id = "test-customer" self.sub.subscription_id = "fake-id" self.sub.save() mock.Customer.create.return_value.is_success = True mock.Customer.create.return_value.customer.id = "test-customer-id" self.client.login(username="******", password="******") form = {"payment_method_nonce": "test-nonce"} self.client.post("/accounts/profile/billing/payment_method/", form) self.assertTrue(mock.Subscription.update.called)
class SetPlanTestCase(BaseTestCase): def _setup_mock(self, mock): """ Set up Braintree calls that the controller will use. """ mock.Subscription.create.return_value.is_success = True mock.Subscription.create.return_value.subscription.id = "t-sub-id" def run_set_plan(self, plan_id="P20"): form = {"plan_id": plan_id} self.client.login(username="******", password="******") return self.client.post("/pricing/set_plan/", form, follow=True) @patch("hc.payments.models.braintree") def test_it_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_set_plan() self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "P20") self.assertEqual(sub.plan_name, "Standard ($20 / month)") # User's profile should have a higher limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 50) self.assertEqual(self.profile.team_limit, 9) self.assertEqual(self.profile.sms_limit, 50) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called assert not mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_yearly_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_set_plan("Y192") self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "Y192") self.assertEqual(sub.plan_name, "Standard ($192 / year)") # User's profile should have a higher limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 50) self.assertEqual(self.profile.team_limit, 9) self.assertEqual(self.profile.sms_limit, 50) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called assert not mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_plus_works(self, mock): self._setup_mock(mock) self.profile.sms_limit = 0 self.profile.sms_sent = 1 self.profile.save() r = self.run_set_plan("P80") self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be filled out: sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "t-sub-id") self.assertEqual(sub.plan_id, "P80") self.assertEqual(sub.plan_name, "Plus ($80 / month)") # User's profile should have a higher limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 1000) self.assertEqual(self.profile.check_limit, 500) self.assertEqual(self.profile.team_limit, 500) self.assertEqual(self.profile.sms_limit, 500) self.assertEqual(self.profile.sms_sent, 0) # braintree.Subscription.cancel should have not been called assert not mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_it_cancels(self, mock): self._setup_mock(mock) self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.plan_id = "P20" self.sub.save() self.profile.sms_limit = 1 self.profile.sms_sent = 1 self.profile.save() r = self.run_set_plan("") self.assertRedirects(r, "/accounts/profile/billing/") # Subscription should be cleared sub = Subscription.objects.get(user=self.alice) self.assertEqual(sub.subscription_id, "") self.assertEqual(sub.plan_id, "") # User's profile should have standard limits self.profile.refresh_from_db() self.assertEqual(self.profile.ping_log_limit, 100) self.assertEqual(self.profile.check_limit, 20) self.assertEqual(self.profile.team_limit, 2) self.assertEqual(self.profile.sms_limit, 0) assert mock.Subscription.cancel.called def test_bad_plan_id(self): r = self.run_set_plan(plan_id="this-is-wrong") self.assertEqual(r.status_code, 400) @patch("hc.payments.models.braintree") def test_it_cancels_previous_subscription(self, mock): self._setup_mock(mock) sub = Subscription(user=self.alice) sub.subscription_id = "prev-sub" sub.save() r = self.run_set_plan() self.assertRedirects(r, "/accounts/profile/billing/") assert mock.Subscription.cancel.called @patch("hc.payments.models.braintree") def test_subscription_creation_failure(self, mock): self._setup_mock(mock) mock.Subscription.create.return_value.is_success = False mock.Subscription.create.return_value.message = "sub failure" r = self.run_set_plan() self.assertRedirects(r, "/accounts/profile/billing/") self.assertContains(r, "sub failure")
class AddressTestCase(BaseTestCase): @patch("hc.payments.models.braintree") def test_it_retrieves_address(self, mock): mock.Address.find.return_value = {"company": "FooCo"} self.sub = Subscription(user=self.alice) self.sub.address_id = "aa" self.sub.save() self.client.login(username="******", password="******") r = self.client.get("/accounts/profile/billing/address/") self.assertContains(r, "FooCo") @patch("hc.payments.models.braintree") def test_it_creates_address(self, mock): mock.Address.create.return_value.is_success = True mock.Address.create.return_value.address.id = "bb" self.sub = Subscription(user=self.alice) self.sub.customer_id = "test-customer" self.sub.save() self.client.login(username="******", password="******") form = {"company": "BarCo"} r = self.client.post("/accounts/profile/billing/address/", form) self.assertRedirects(r, "/accounts/profile/billing/") self.sub.refresh_from_db() self.assertEqual(self.sub.address_id, "bb") @patch("hc.payments.models.braintree") def test_it_updates_address(self, mock): mock.Address.update.return_value.is_success = True self.sub = Subscription(user=self.alice) self.sub.customer_id = "test-customer" self.sub.address_id = "aa" self.sub.save() self.client.login(username="******", password="******") form = {"company": "BarCo"} r = self.client.post("/accounts/profile/billing/address/", form) self.assertRedirects(r, "/accounts/profile/billing/") @patch("hc.payments.models.braintree") def test_it_creates_customer(self, mock): mock.Address.create.return_value.is_success = True mock.Address.create.return_value.address.id = "bb" mock.Customer.create.return_value.is_success = True mock.Customer.create.return_value.customer.id = "test-customer-id" self.sub = Subscription(user=self.alice) self.sub.save() self.client.login(username="******", password="******") form = {"company": "BarCo"} self.client.post("/accounts/profile/billing/address/", form) self.sub.refresh_from_db() self.assertEqual(self.sub.customer_id, "test-customer-id")