Ejemplo n.º 1
0
 def setUp(self):
     with patch(
         "stripe.Product.retrieve",
         return_value=deepcopy(FAKE_PRODUCT),
         autospec=True,
     ):
         Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
         Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))
Ejemplo n.º 2
0
 def handle(self, *args, **options):
     existed_ids = []
     for plan in Plan.api_list():
         stripe_id = plan.get('id')
         if Plan.objects.filter(stripe_id=stripe_id).exists():
             print('Plan id=%s updated.' % stripe_id)
         else:
             print('Plan id=%s created.' % stripe_id)
         existed_ids.append(stripe_id)
         Plan.sync_from_stripe_data(plan)
Ejemplo n.º 3
0
	def test_has_active_subscription_with_unspecified_plan_with_multiple_subscriptions(
		self, product_retrieve_mock, customer_retrieve_mock, subscription_create_mock
	):
		plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

		self.assert_fks(plan, expected_blank_fks={})

		subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
		subscription_fake["current_period_end"] = datetime_to_unix(
			timezone.now() + timezone.timedelta(days=7)
		)

		subscription_fake_duplicate = deepcopy(FAKE_SUBSCRIPTION)
		subscription_fake_duplicate["current_period_end"] = datetime_to_unix(
			timezone.now() + timezone.timedelta(days=7)
		)
		subscription_fake_duplicate["id"] = "sub_6lsC8pt7IcF8jd"

		subscription_create_mock.side_effect = [
			subscription_fake,
			subscription_fake_duplicate,
		]

		self.customer.subscribe(plan=plan, charge_immediately=False)
		self.customer.subscribe(plan=plan, charge_immediately=False)

		self.assertEqual(2, self.customer.subscriptions.count())

		with self.assertRaises(TypeError):
			self.customer.has_active_subscription()
Ejemplo n.º 4
0
    def test_subscribe_plan_string(self, customer_retrieve_mock,
                                   subscription_create_mock,
                                   send_invoice_mock):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

        self.customer.subscribe(plan=plan.stripe_id, charge_immediately=True)
        self.assertTrue(send_invoice_mock.called)
Ejemplo n.º 5
0
    def test_update_with_plan_model(
        self,
        customer_retrieve_mock,
        subscription_retrieve_mock,
        product_retrieve_mock,
        plan_retrieve_mock,
    ):
        subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
        subscription = Subscription.sync_from_stripe_data(subscription_fake)
        new_plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))

        self.assertEqual(FAKE_PLAN["id"], subscription.plan.id)

        new_subscription = subscription.update(plan=new_plan)

        self.assertEqual(FAKE_PLAN_II["id"], new_subscription.plan.id)

        self.assert_fks(
            subscription,
            expected_blank_fks={
                "djstripe.Customer.coupon",
                "djstripe.Subscription.pending_setup_intent",
            },
        )

        self.assert_fks(new_plan, expected_blank_fks={})
Ejemplo n.º 6
0
 def setUp(self):
     self.user = get_user_model().objects.create_user(
         username="******", email="*****@*****.**")
     self.customer = Customer.objects.create(subscriber=self.user,
                                             stripe_id=FAKE_CUSTOMER["id"],
                                             livemode=False)
     self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
Ejemplo n.º 7
0
    def test_update_with_plan_model(
        self,
        customer_retrieve_mock,
        subscription_retrieve_mock,
        subscription_modify_mock,
        product_retrieve_mock,
        plan_retrieve_mock,
    ):
        subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
        subscription = Subscription.sync_from_stripe_data(subscription_fake)
        new_plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))

        self.assertEqual(FAKE_PLAN["id"], subscription.plan.id)

        # Update the Subscription's plan
        subscription_updated = deepcopy(FAKE_SUBSCRIPTION)
        subscription_updated["plan"] = deepcopy(FAKE_PLAN_II)
        subscription_modify_mock.return_value = subscription_updated

        new_subscription = subscription.update(plan=new_plan)

        self.assertEqual(FAKE_PLAN_II["id"], new_subscription.plan.id)

        self.assert_fks(subscription,
                        expected_blank_fks=self.default_expected_blank_fks)

        self.assert_fks(new_plan, expected_blank_fks={})
Ejemplo n.º 8
0
    def setUp(self):
        with patch("stripe.Product.retrieve",
                   return_value=deepcopy(FAKE_PRODUCT)):
            self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

        self.site = AdminSite()
        self.plan_admin = PlanAdmin(Plan, self.site)
Ejemplo n.º 9
0
    def test_has_active_subscription_with_unspecified_plan_with_multiple_subscriptions(
            self, customer_retrieve_mock, subscription_create_mock):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

        subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
        subscription_fake["current_period_end"] = datetime_to_unix(
            timezone.now() + timezone.timedelta(days=7))

        subscription_fake_duplicate = deepcopy(FAKE_SUBSCRIPTION)
        subscription_fake_duplicate["current_period_end"] = datetime_to_unix(
            timezone.now() + timezone.timedelta(days=7))
        subscription_fake_duplicate["id"] = "sub_6lsC8pt7IcF8jd"

        subscription_create_mock.side_effect = [
            subscription_fake,
            subscription_fake_duplicate,
        ]

        self.customer.subscribe(plan=plan, charge_immediately=False)
        self.customer.subscribe(plan=plan, charge_immediately=False)

        self.assertEqual(2, self.customer.subscriptions.count())

        with self.assertRaises(TypeError):
            self.customer.has_active_subscription()
Ejemplo n.º 10
0
    def test_stripe_metered_plan(self, plan_retrieve_mock):
        plan_data = deepcopy(FAKE_PLAN_METERED)
        plan = Plan.sync_from_stripe_data(plan_data)
        self.assertEqual(plan.id, plan_data["id"])
        self.assertEqual(plan.usage_type, PriceUsageType.metered)
        self.assertIsNotNone(plan.amount)

        self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
Ejemplo n.º 11
0
def sync_plans(apps, schema_editor):
    # This is okay, since we're only doing a forward migration.
    from djstripe.models import Plan

    from djstripe.context_managers import stripe_temporary_api_version

    with stripe_temporary_api_version("2016-03-07"):
        if Plan.objects.count():
            print("syncing plans. This may take a while.")

            for plan in tqdm(iterable=Plan.objects.all(), desc="Sync", unit=" plans"):
                try:
                    Plan.sync_from_stripe_data(plan)
                except InvalidRequestError:
                    tqdm.write("There was an error while syncing plan ({plan_id}).".format(transfer_id=plan.stripe_id))

            print("Transfer sync complete.")
Ejemplo n.º 12
0
 def setUp(self):
     self.plan_data = deepcopy(FAKE_PLAN)
     with patch(
             "stripe.Product.retrieve",
             return_value=deepcopy(FAKE_PRODUCT),
             autospec=True,
     ):
         self.plan = Plan.sync_from_stripe_data(self.plan_data)
Ejemplo n.º 13
0
    def test_stripe_tier_plan(self, plan_retrieve_mock):
        tier_plan_data = deepcopy(FAKE_TIER_PLAN)
        plan = Plan.sync_from_stripe_data(tier_plan_data)
        self.assertEqual(plan.id, tier_plan_data["id"])
        self.assertIsNone(plan.amount)
        self.assertIsNotNone(plan.tiers)

        self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
Ejemplo n.º 14
0
    def setUp(self):
        self.user = get_user_model().objects.create_user(
            username="******", email="*****@*****.**")
        self.customer = FAKE_CUSTOMER.create_for_user(self.user)

        with patch("stripe.Product.retrieve",
                   return_value=deepcopy(FAKE_PRODUCT)):
            self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
Ejemplo n.º 15
0
	def test_stripe_tier_plan(self, plan_retrieve_mock):
		tier_plan_data = deepcopy(FAKE_TIER_PLAN)
		plan = Plan.sync_from_stripe_data(tier_plan_data)
		self.assertEqual(plan.id, tier_plan_data["id"])
		self.assertIsNone(plan.amount)
		self.assertIsNotNone(plan.tiers)

		self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
Ejemplo n.º 16
0
 def test_stripe_plan(self, plan_retrieve_mock):
     stripe_plan = self.plan.api_retrieve()
     plan_retrieve_mock.assert_called_once_with(id=self.plan_data["id"],
                                                api_key=STRIPE_SECRET_KEY,
                                                expand=[])
     plan = Plan.sync_from_stripe_data(stripe_plan)
     assert plan.amount_in_cents == plan.amount * 100
     assert isinstance(plan.amount_in_cents, int)
Ejemplo n.º 17
0
	def test_stripe_metered_plan(self, plan_retrieve_mock):
		plan_data = deepcopy(FAKE_PLAN_METERED)
		plan = Plan.sync_from_stripe_data(plan_data)
		self.assertEqual(plan.id, plan_data["id"])
		self.assertEqual(plan.usage_type, PlanUsageType.metered)
		self.assertIsNotNone(plan.amount)

		self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
Ejemplo n.º 18
0
	def setUp(self):
		with patch(
			"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
		):
			self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

		self.site = AdminSite()
		self.plan_admin = PlanAdmin(Plan, self.site)
Ejemplo n.º 19
0
 def setUp(self):
     self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
     self.url = reverse("djstripe:cancel_subscription")
     self.user = get_user_model().objects.create_user(
         username="******",
         email="*****@*****.**",
         password="******"
     )
     self.assertTrue(self.client.login(username="******", password="******"))
Ejemplo n.º 20
0
 def get_queryset(self):
     stripe.api_key = settings.STRIPE_LIVE_PUBLIC_KEY
     try:
         plan_list = stripe.Plan.list()
         plans = [
             Plan.sync_from_stripe_data(pl) for pl in plan_list['data']
         ]
     except stripe.error.PermissionError:
         plans = []
     return plans
Ejemplo n.º 21
0
 def test_stripe_plan(self, plan_retrieve_mock):
     stripe_plan = self.plan.api_retrieve()
     plan_retrieve_mock.assert_called_once_with(
         id=self.plan_data["id"],
         api_key=settings.STRIPE_SECRET_KEY,
         expand=None
     )
     plan = Plan.sync_from_stripe_data(stripe_plan)
     assert plan.amount_in_cents == plan.amount * 100
     assert isinstance(plan.amount_in_cents, int)
Ejemplo n.º 22
0
	def test_stripe_plan(self, plan_retrieve_mock):
		stripe_plan = self.plan.api_retrieve()
		plan_retrieve_mock.assert_called_once_with(
			id=self.plan_data["id"], api_key=STRIPE_SECRET_KEY, expand=[]
		)
		plan = Plan.sync_from_stripe_data(stripe_plan)
		assert plan.amount_in_cents == plan.amount * 100
		assert isinstance(plan.amount_in_cents, int)

		self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
Ejemplo n.º 23
0
	def setUp(self):
		self.user = get_user_model().objects.create_user(
			username="******", email="*****@*****.**"
		)
		self.customer = FAKE_CUSTOMER.create_for_user(self.user)

		with patch(
			"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
		):
			self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
Ejemplo n.º 24
0
    def test_update_with_plan_model(self, customer_retrieve_mock, subscription_retrieve_mock, plan_retrieve_mock):
        subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
        subscription = Subscription.sync_from_stripe_data(subscription_fake)
        new_plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))

        self.assertEqual(FAKE_PLAN["id"], subscription.plan.stripe_id)

        new_subscription = subscription.update(plan=new_plan)

        self.assertEqual(FAKE_PLAN_II["id"], new_subscription.plan.stripe_id)
Ejemplo n.º 25
0
    def test_has_active_subscription_with_plan_string(self, customer_retrieve_mock, subscription_create_mock):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

        subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
        subscription_fake["current_period_end"] = datetime_to_unix(timezone.now() + timezone.timedelta(days=7))

        subscription_create_mock.return_value = subscription_fake

        self.customer.subscribe(plan=plan, charge_immediately=False)

        self.customer.has_active_subscription(plan=plan.stripe_id)
Ejemplo n.º 26
0
    def test_subscribe_not_charge_immediately(
        self,
        product_retrieve_mock,
        customer_retrieve_mock,
        subscription_create_mock,
        send_invoice_mock,
    ):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

        self.customer.subscribe(plan=plan, charge_immediately=False)
        self.assertFalse(send_invoice_mock.called)
Ejemplo n.º 27
0
    def test_has_active_subscription_with_plan_string(self, customer_retrieve_mock, subscription_create_mock):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

        subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
        subscription_fake["current_period_end"] = datetime_to_unix(timezone.now() + timezone.timedelta(days=7))

        subscription_create_mock.return_value = subscription_fake

        self.customer.subscribe(plan=plan, charge_immediately=False)

        self.customer.has_active_subscription(plan=plan.stripe_id)
Ejemplo n.º 28
0
	def test_subscribe_not_charge_immediately(
		self,
		product_retrieve_mock,
		customer_retrieve_mock,
		subscription_create_mock,
		send_invoice_mock,
	):
		plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

		self.customer.subscribe(plan=plan, charge_immediately=False)
		self.assertFalse(send_invoice_mock.called)
Ejemplo n.º 29
0
	def test_stripe_plan_null_product(self, product_retrieve_mock):
		"""
		assert that plan.Product can be null for backwards compatibility
		though note that it is a Stripe required field
		"""
		plan_data = deepcopy(FAKE_PLAN_II)
		del plan_data["product"]
		plan = Plan.sync_from_stripe_data(plan_data)

		self.assert_fks(
			plan, expected_blank_fks={"djstripe.Customer.coupon", "djstripe.Plan.product"}
		)
Ejemplo n.º 30
0
 def setUp(self):
     self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
     self.url = reverse("djstripe:cancel_subscription")
     self.user = get_user_model().objects.create_user(
         username="******",
         email="*****@*****.**",
         password="******"
     )
     self.assertTrue(self.client.login(username="******", password="******"))
     stripe_customer = Customer.sync_from_stripe_data(FAKE_CUSTOMER)
     stripe_customer.subscriber = self.user
     stripe_customer.save()
Ejemplo n.º 31
0
    def test_update_with_plan_model(self, customer_retrieve_mock,
                                    subscription_retrieve_mock,
                                    plan_retrieve_mock):
        subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
        subscription = Subscription.sync_from_stripe_data(subscription_fake)
        new_plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))

        self.assertEqual(FAKE_PLAN["id"], subscription.plan.stripe_id)

        new_subscription = subscription.update(plan=new_plan)

        self.assertEqual(FAKE_PLAN_II["id"], new_subscription.plan.stripe_id)
Ejemplo n.º 32
0
    def test_get_subscription(self):
        """Test a GET to the SubscriptionRestView.

        Should return the correct data.
        """
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
        subscription = Subscription.sync_from_stripe_data(deepcopy(FAKE_SUBSCRIPTION))

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["plan"], plan.id)
        self.assertEqual(response.data['status'], subscription.status)
        self.assertEqual(response.data['cancel_at_period_end'], subscription.cancel_at_period_end)
Ejemplo n.º 33
0
	def test_subscribe_plan_string(
		self,
		product_retrieve_mock,
		customer_retrieve_mock,
		subscription_create_mock,
		send_invoice_mock,
	):
		plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

		self.assert_fks(plan, expected_blank_fks={})

		self.customer.subscribe(plan=plan.id, charge_immediately=True)
		self.assertTrue(send_invoice_mock.called)
Ejemplo n.º 34
0
    def test_stripe_plan(self, plan_retrieve_mock):
        stripe_plan = self.plan.api_retrieve()
        plan_retrieve_mock.assert_called_once_with(id=self.plan_data["id"],
                                                   api_key=STRIPE_SECRET_KEY,
                                                   expand=[])
        plan = Plan.sync_from_stripe_data(stripe_plan)
        assert plan.amount_in_cents == plan.amount * 100
        assert isinstance(plan.amount_in_cents, int)

        self.assert_fks(plan,
                        expected_blank_fks={
                            "djstripe.Customer.coupon", "djstripe.Plan.product"
                        })
Ejemplo n.º 35
0
    def test_stripe_plan_null_product(self, product_retrieve_mock):
        """
		assert that plan.Product can be null for backwards compatibility
		though note that it is a Stripe required field
		"""
        plan_data = deepcopy(FAKE_PLAN_II)
        del plan_data["product"]
        plan = Plan.sync_from_stripe_data(plan_data)

        self.assert_fks(plan,
                        expected_blank_fks={
                            "djstripe.Customer.coupon", "djstripe.Plan.product"
                        })
Ejemplo n.º 36
0
    def test_subscribe_plan_string(
        self,
        product_retrieve_mock,
        customer_retrieve_mock,
        subscription_create_mock,
        send_invoice_mock,
    ):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

        self.assert_fks(plan, expected_blank_fks={})

        self.customer.subscribe(plan=plan.id, charge_immediately=True)
        self.assertTrue(send_invoice_mock.called)
Ejemplo n.º 37
0
    def test___str__null_product(self):
        plan_data = deepcopy(FAKE_PLAN_II)
        del plan_data["product"]
        plan = Plan.sync_from_stripe_data(plan_data)

        self.assertIsNone(plan.product)

        subscriptions = Subscription.objects.filter(plan__id=plan.id).count()

        self.assertEqual(
            f"{plan.human_readable_price} ({subscriptions} subscriptions)",
            str(plan),
        )
Ejemplo n.º 38
0
    def test_stripe_plan(self, plan_retrieve_mock):
        stripe_plan = self.plan.api_retrieve()
        plan_retrieve_mock.assert_called_once_with(
            id=self.plan_data["id"],
            api_key=djstripe_settings.STRIPE_SECRET_KEY,
            expand=["tiers"],
            stripe_account=self.plan.djstripe_owner_account.id,
        )
        plan = Plan.sync_from_stripe_data(stripe_plan)
        assert plan.amount_in_cents == plan.amount * 100
        assert isinstance(plan.amount_in_cents, int)

        self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
Ejemplo n.º 39
0
    def test_subscription_shortcut_with_multiple_subscriptions(self, customer_retrieve_mock, subscription_create_mock):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
        subscription_fake_duplicate = deepcopy(FAKE_SUBSCRIPTION)
        subscription_fake_duplicate["id"] = "sub_6lsC8pt7IcF8jd"

        subscription_create_mock.side_effect = [deepcopy(FAKE_SUBSCRIPTION), subscription_fake_duplicate]

        self.customer.subscribe(plan=plan, charge_immediately=False)
        self.customer.subscribe(plan=plan, charge_immediately=False)

        self.assertEqual(2, self.customer.subscriptions.count())

        with self.assertRaises(MultipleSubscriptionException):
            self.customer.subscription
Ejemplo n.º 40
0
    def test_subscription_shortcut_with_multiple_subscriptions(self, customer_retrieve_mock, subscription_create_mock):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
        subscription_fake_duplicate = deepcopy(FAKE_SUBSCRIPTION)
        subscription_fake_duplicate["id"] = "sub_6lsC8pt7IcF8jd"

        subscription_create_mock.side_effect = [deepcopy(FAKE_SUBSCRIPTION), subscription_fake_duplicate]

        self.customer.subscribe(plan=plan, charge_immediately=False)
        self.customer.subscribe(plan=plan, charge_immediately=False)

        self.assertEqual(2, self.customer.subscriptions.count())

        with self.assertRaises(MultipleSubscriptionException):
            self.customer.subscription
Ejemplo n.º 41
0
def plan_setup():
    remote_plan_cleanup()
    remote_product_cleanup()
    local_plan_cleanup()
    local_product_cleanup()
    for plan_definition in PLAN_DEFINITIONS:
        stripe_product = setup_remote_product(
            plan_definition[PlanDefinitionKey.NAME].value,
            plan_definition[PlanDefinitionKey.DESCRIPTION])
        stripe_plan = setup_remote_plan(stripe_product, plan_definition)

        product = Product.sync_from_stripe_data(stripe_product)
        Plan.sync_from_stripe_data(stripe_plan)

        try:
            pe = ProductExtension.objects.get(product=product)
        except ProductExtension.DoesNotExist:
            pe = ProductExtension(product=product)

        pe.allowances = json.dumps(plan_definition[PlanDefinitionKey.QUOTA])
        pe.tag_line = plan_definition[PlanDefinitionKey.TAG_LINE]
        pe.is_purchasable = plan_definition[PlanDefinitionKey.IS_PURCHASABLE]
        pe.save()
Ejemplo n.º 42
0
    def test_human_readable(self, fake_plan_data, expected_str, monkeypatch):
        def mock_product_get(*args, **kwargs):
            return deepcopy(FAKE_PRODUCT)

        def mock_price_get(*args, **kwargs):
            return fake_plan_data

        # monkeypatch stripe.Product.retrieve and stripe.Plan.retrieve calls to return
        # the desired json response.
        monkeypatch.setattr(stripe.Product, "retrieve", mock_product_get)
        monkeypatch.setattr(stripe.Plan, "retrieve", mock_price_get)

        plan = Plan.sync_from_stripe_data(fake_plan_data)

        assert plan.human_readable_price == expected_str
Ejemplo n.º 43
0
    def test_get_subscription(self):
        """Test a GET to the SubscriptionRestView.

        Should return the correct data.
        """
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
        subscription = Subscription.sync_from_stripe_data(
            deepcopy(FAKE_SUBSCRIPTION))

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["plan"], plan.id)
        self.assertEqual(response.data['status'], subscription.status)
        self.assertEqual(response.data['cancel_at_period_end'],
                         subscription.cancel_at_period_end)
Ejemplo n.º 44
0
	def test_get_subscription(self):
		"""Test a GET to the SubscriptionRestView.

		Should return the correct data.
		"""
		with patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT)):
			plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
		subscription = Subscription.sync_from_stripe_data(deepcopy(FAKE_SUBSCRIPTION))

		response = self.client.get(self.url)
		self.assertEqual(response.status_code, status.HTTP_200_OK)
		self.assertEqual(response.data["plan"], plan.djstripe_id)
		self.assertEqual(response.data["status"], subscription.status)
		self.assertEqual(
			response.data["cancel_at_period_end"], subscription.cancel_at_period_end
		)
Ejemplo n.º 45
0
	def test_get_subscription(self):
		"""Test a GET to the SubscriptionRestView.

		Should return the correct data.
		"""
		with patch(
			"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
		):
			plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
		subscription = Subscription.sync_from_stripe_data(deepcopy(FAKE_SUBSCRIPTION))

		response = self.client.get(self.url)
		self.assertEqual(response.status_code, status.HTTP_200_OK)
		self.assertEqual(response.data["plan"], plan.djstripe_id)
		self.assertEqual(response.data["status"], subscription.status)
		self.assertEqual(
			response.data["cancel_at_period_end"], subscription.cancel_at_period_end
		)
Ejemplo n.º 46
0
    def test_get_subscription(self):
        """Test a GET to the SubscriptionRestView.

        Should return the correct data.
        """
        Customer.objects.create(subscriber=self.user,
                                stripe_id=FAKE_CUSTOMER["id"],
                                livemode=False)
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
        subscription = Subscription.sync_from_stripe_data(
            deepcopy(FAKE_SUBSCRIPTION))

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["plan"], plan.id)
        self.assertEqual(response.data['status'], subscription.status)
        self.assertEqual(response.data['cancel_at_period_end'],
                         subscription.cancel_at_period_end)
Ejemplo n.º 47
0
	def test_update_with_plan_model(
		self,
		customer_retrieve_mock,
		subscription_retrieve_mock,
		product_retrieve_mock,
		plan_retrieve_mock,
	):
		subscription_fake = deepcopy(FAKE_SUBSCRIPTION)
		subscription = Subscription.sync_from_stripe_data(subscription_fake)
		new_plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))

		self.assertEqual(FAKE_PLAN["id"], subscription.plan.id)

		new_subscription = subscription.update(plan=new_plan)

		self.assertEqual(FAKE_PLAN_II["id"], new_subscription.plan.id)

		self.assert_fks(subscription, expected_blank_fks={"djstripe.Customer.coupon"})

		self.assert_fks(new_plan, expected_blank_fks={})
Ejemplo n.º 48
0
	def setUp(self):
		with patch(
			"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
		):
			Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
			Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))
Ejemplo n.º 49
0
	def setUp(self):
		# create customers and current subscription records
		period_start = datetime.datetime(2013, 4, 1, tzinfo=timezone.utc)
		period_end = datetime.datetime(2013, 4, 30, tzinfo=timezone.utc)
		start = datetime.datetime(
			2013, 1, 1, 0, 0, 1, tzinfo=timezone.utc
		)  # more realistic start

		with patch(
			"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
		):
			self.plan = Plan.sync_from_stripe_data(FAKE_PLAN)
			self.plan2 = Plan.sync_from_stripe_data(FAKE_PLAN_II)

		for i in range(10):
			user = get_user_model().objects.create_user(
				username="******".format(i), email="patrick{0}@example.com".format(i)
			)
			customer = Customer.objects.create(
				subscriber=user,
				id="cus_xxxxxxxxxxxxxx{0}".format(i),
				livemode=False,
				account_balance=0,
				delinquent=False,
			)

			Subscription.objects.create(
				id="sub_xxxxxxxxxxxxxx{0}".format(i),
				customer=customer,
				plan=self.plan,
				current_period_start=period_start,
				current_period_end=period_end,
				status="active",
				start=start,
				quantity=1,
			)

		user = get_user_model().objects.create_user(
			username="******".format(11), email="patrick{0}@example.com".format(11)
		)
		customer = Customer.objects.create(
			subscriber=user,
			id="cus_xxxxxxxxxxxxxx{0}".format(11),
			livemode=False,
			account_balance=0,
			delinquent=False,
		)
		Subscription.objects.create(
			id="sub_xxxxxxxxxxxxxx{0}".format(11),
			customer=customer,
			plan=self.plan,
			current_period_start=period_start,
			current_period_end=period_end,
			status="canceled",
			canceled_at=period_end,
			start=start,
			quantity=1,
		)

		user = get_user_model().objects.create_user(
			username="******".format(12), email="patrick{0}@example.com".format(12)
		)
		customer = Customer.objects.create(
			subscriber=user,
			id="cus_xxxxxxxxxxxxxx{0}".format(12),
			livemode=False,
			account_balance=0,
			delinquent=False,
		)
		Subscription.objects.create(
			id="sub_xxxxxxxxxxxxxx{0}".format(12),
			customer=customer,
			plan=self.plan2,
			current_period_start=period_start,
			current_period_end=period_end,
			status="active",
			start=start,
			quantity=1,
		)
Ejemplo n.º 50
0
	def setUp(self):
		self.plan_data = deepcopy(FAKE_PLAN)
		with patch(
			"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
		):
			self.plan = Plan.sync_from_stripe_data(self.plan_data)
Ejemplo n.º 51
0
 def setUp(self):
     self.plan_data = deepcopy(FAKE_PLAN)
     self.plan = Plan.sync_from_stripe_data(self.plan_data)
Ejemplo n.º 52
0
 def setUp(self):
     Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
     Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))
Ejemplo n.º 53
0
 def setUp(self):
     self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
     self.site = AdminSite()
     self.plan_admin = PlanAdmin(Plan, self.site)
Ejemplo n.º 54
0
    def test_subscribe_plan_string(self, customer_retrieve_mock, subscription_create_mock, send_invoice_mock):
        plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

        self.customer.subscribe(plan=plan.stripe_id, charge_immediately=True)
        self.assertTrue(send_invoice_mock.called)