示例#1
0
class MiddlewareURLTest(TestCase):

    def setUp(self):
        self.settings(ROOT_URLCONF='tests.test_urls')
        self.factory = RequestFactory()
        self.user = get_user_model().objects.create_user(username="******",
                                                         email="*****@*****.**")
        self.middleware = SubscriptionPaymentMiddleware()

    def test_appname(self):
        request = self.factory.get("/admin/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_namespace(self):
        request = self.factory.get("/djstripe/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_namespace_and_url(self):
        request = self.factory.get("/testapp_namespaced/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_url(self):
        request = self.factory.get("/testapp/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    @override_settings(DEBUG=True)
    def test_djdt(self):
        request = self.factory.get("/__debug__/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)
示例#2
0
class MiddlewareURLTest(TestCase):
    def setUp(self):
        self.settings(ROOT_URLCONF='tests.test_urls')
        self.factory = RequestFactory()
        self.user = get_user_model().objects.create_user(
            username="******", email="*****@*****.**")
        self.middleware = SubscriptionPaymentMiddleware()

    def test_appname(self):
        request = self.factory.get("/admin/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_namespace(self):
        request = self.factory.get("/djstripe/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_namespace_and_url(self):
        request = self.factory.get("/testapp_namespaced/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_url(self):
        request = self.factory.get("/testapp/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    @override_settings(DEBUG=True)
    def test_djdt(self):
        request = self.factory.get("/__debug__/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)
示例#3
0
class MiddlewareLogicTest(TestCase):
    urlconf = "tests.urls"

    def setUp(self):
        self.settings(ROOT_URLCONF=self.urlconf)
        self.factory = RequestFactory()
        self.user = get_user_model().objects.create_user(
            username="******", email="*****@*****.**")
        self.customer = Customer.sync_from_stripe_data(FAKE_CUSTOMER)
        self.customer.subscriber = self.user
        self.customer.save()

        with patch(
                "stripe.Product.retrieve",
                return_value=deepcopy(FAKE_PRODUCT),
                autospec=True,
        ):
            self.subscription = Subscription.sync_from_stripe_data(
                FAKE_SUBSCRIPTION)

        self.middleware = SubscriptionPaymentMiddleware()

    def test_anonymous(self):
        request = self.factory.get("/djstripe/webhook/")
        request.user = AnonymousUser()
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_is_staff(self):
        self.user.is_staff = True
        self.user.save()

        request = self.factory.get("/djstripe/webhook/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_is_superuser(self):
        self.user.is_superuser = True
        self.user.save()

        request = self.factory.get("/djstripe/webhook/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_customer_has_inactive_subscription(self):
        with patch(
                "stripe.Product.retrieve",
                return_value=deepcopy(FAKE_PRODUCT),
                autospec=True,
        ):
            self.subscription = Subscription.sync_from_stripe_data(
                FAKE_SUBSCRIPTION_NOT_PERIOD_CURRENT)

        request = self.factory.get("/testapp_content/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response.status_code, 302)

    def test_customer_has_active_subscription(self):
        self.subscription.current_period_end = FUTURE_DATE
        self.subscription.save()

        request = self.factory.get("/testapp_content/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)
示例#4
0
class MiddlewareURLTest(TestCase):
    urlconf = "tests.urls"

    def setUp(self):
        self.settings(ROOT_URLCONF=self.urlconf)
        self.factory = RequestFactory()
        self.user = get_user_model().objects.create_user(
            username="******", email="*****@*****.**")
        self.middleware = SubscriptionPaymentMiddleware()

    def test_appname(self):
        request = self.factory.get("/admin/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_namespace(self):
        request = self.factory.get("/djstripe/webhook/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_namespace_and_url(self):
        request = self.factory.get("/testapp_namespaced/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_url(self):
        request = self.factory.get("/testapp/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    @override_settings(DEBUG=True)
    def test_djdt(self):
        request = self.factory.get("/__debug__/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_fnmatch(self):
        request = self.factory.get("/test_fnmatch/extra_text/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    @override_settings(DEBUG=True)
    @modify_settings(MIDDLEWARE={
        "append": ["djstripe.middleware.SubscriptionPaymentMiddleware"]
    })
    def test_middleware_loads(self):
        """Check that the middleware can be loaded by django's
        middleware handlers. This is to check for compatibility across
        the change to django's middleware class structure. See
        https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
        """
        self.client.get("/__debug__")
示例#5
0
class MiddlewareLogicTest(TestCase):
	urlconf = "tests.urls"

	def setUp(self):
		self.settings(ROOT_URLCONF=self.urlconf)
		self.factory = RequestFactory()
		self.user = get_user_model().objects.create_user(
			username="******", email="*****@*****.**"
		)
		self.customer = Customer.sync_from_stripe_data(FAKE_CUSTOMER)
		self.customer.subscriber = self.user
		self.customer.save()

		with patch(
			"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
		):
			self.subscription = Subscription.sync_from_stripe_data(FAKE_SUBSCRIPTION)

		self.middleware = SubscriptionPaymentMiddleware()

	def test_anonymous(self):
		request = self.factory.get("/djstripe/webhook/")
		request.user = AnonymousUser()
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	def test_is_staff(self):
		self.user.is_staff = True
		self.user.save()

		request = self.factory.get("/djstripe/webhook/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	def test_is_superuser(self):
		self.user.is_superuser = True
		self.user.save()

		request = self.factory.get("/djstripe/webhook/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	def test_customer_has_inactive_subscription(self):
		request = self.factory.get("/testapp_content/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response.status_code, 302)

	def test_customer_has_active_subscription(self):
		self.subscription.current_period_end = FUTURE_DATE
		self.subscription.save()

		request = self.factory.get("/testapp_content/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)
示例#6
0
class MiddlewareURLTest(TestCase):
	urlconf = "tests.urls"

	def setUp(self):
		self.settings(ROOT_URLCONF=self.urlconf)
		self.factory = RequestFactory()
		self.user = get_user_model().objects.create_user(
			username="******", email="*****@*****.**"
		)
		self.middleware = SubscriptionPaymentMiddleware()

	def test_appname(self):
		request = self.factory.get("/admin/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	def test_namespace(self):
		request = self.factory.get("/djstripe/webhook/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	def test_namespace_and_url(self):
		request = self.factory.get("/testapp_namespaced/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	def test_url(self):
		request = self.factory.get("/testapp/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	@override_settings(DEBUG=True)
	def test_djdt(self):
		request = self.factory.get("/__debug__/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	def test_fnmatch(self):
		request = self.factory.get("/test_fnmatch/extra_text/")
		request.user = self.user
		request.urlconf = self.urlconf

		response = self.middleware.process_request(request)
		self.assertEqual(response, None)

	@override_settings(DEBUG=True)
	@modify_settings(
		MIDDLEWARE={"append": ["djstripe.middleware.SubscriptionPaymentMiddleware"]}
	)
	def test_middleware_loads(self):
		"""Check that the middleware can be loaded by django's
		middleware handlers. This is to check for compatibility across
		the change to django's middleware class structure. See
		https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
		"""
		self.client.get("/__debug__")
示例#7
0
class MiddlewareLogicTest(TestCase):
    urlconf = 'tests.test_urls'

    def setUp(self):
        self.settings(ROOT_URLCONF=self.urlconf)
        self.factory = RequestFactory()
        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.subscription = Subscription.sync_from_stripe_data(
            FAKE_SUBSCRIPTION)
        self.middleware = SubscriptionPaymentMiddleware()

    def test_anonymous(self):
        request = self.factory.get("/djstripe/webhook/")
        request.user = AnonymousUser()
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_is_staff(self):
        self.user.is_staff = True
        self.user.save()

        request = self.factory.get("/djstripe/webhook/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_is_superuser(self):
        self.user.is_superuser = True
        self.user.save()

        request = self.factory.get("/djstripe/webhook/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_customer_has_inactive_subscription(self):
        request = self.factory.get("/testapp_content/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response.status_code, 302)

    def test_customer_has_active_subscription(self):
        self.subscription.current_period_end = FUTURE_DATE
        self.subscription.save()

        request = self.factory.get("/testapp_content/")
        request.user = self.user
        request.urlconf = self.urlconf

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)
示例#8
0
class MiddlewareLogicTest(TestCase):
    urls = 'tests.test_urls'

    def setUp(self):
        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, tzinfo=timezone.utc)

        self.factory = RequestFactory()
        self.user = get_user_model().objects.create_user(username="******",
                                                         email="*****@*****.**")
        self.customer = Customer.objects.create(
            subscriber=self.user,
            stripe_id="cus_xxxxxxxxxxxxxxx",
            card_fingerprint="YYYYYYYY",
            card_last_4="2342",
            card_kind="Visa"
        )
        self.subscription = CurrentSubscription.objects.create(
            customer=self.customer,
            plan="test",
            current_period_start=period_start,
            current_period_end=period_end,
            amount=(500 / decimal.Decimal("100.0")),
            status="active",
            start=start,
            quantity=1,
            cancel_at_period_end=True
        )
        self.middleware = SubscriptionPaymentMiddleware()

    def test_anonymous(self):
        request = self.factory.get("/djstripe/")
        request.user = AnonymousUser()

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_is_staff(self):
        self.user.is_staff = True
        self.user.save()

        request = self.factory.get("/djstripe/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_is_superuser(self):
        self.user.is_superuser = True
        self.user.save()

        request = self.factory.get("/djstripe/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)

    def test_customer_has_inactive_subscription(self):
        request = self.factory.get("/testapp_content/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response.status_code, 302)

    def test_customer_has_active_subscription(self):
        end_date = datetime.datetime(2100, 4, 30, tzinfo=timezone.utc)
        self.subscription.current_period_end = end_date
        self.subscription.save()

        request = self.factory.get("/testapp_content/")
        request.user = self.user

        response = self.middleware.process_request(request)
        self.assertEqual(response, None)