Skip to content

Commit

Permalink
add middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pydanny committed Aug 11, 2013
1 parent 4d1ba76 commit 4b75318
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 21 deletions.
6 changes: 5 additions & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
STRIPE_PUBLIC_KEY="",
STRIPE_SECRET_KEY="",
DJSTRIPE_PLANS={},
DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS=("(admin)", )
DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS=(
"(admin)",
"test_url_name",
"testapp_namespaced:test_url_namespaced"
)
)

from django_nose import NoseTestSuiteRunner
Expand Down
Empty file added tests/apps/__init__.py
Empty file.
Empty file added tests/apps/testapp/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions tests/apps/testapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
16 changes: 16 additions & 0 deletions tests/apps/testapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import unicode_literals
from django.conf.urls import patterns, url

from django.http import HttpResponse


def testview(request):
return HttpResponse()

urlpatterns = patterns("",
url(
r"^$",
testview,
name="test_url_name"
),
)
1 change: 1 addition & 0 deletions tests/apps/testapp_content/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Represents protected content
Empty file.
3 changes: 3 additions & 0 deletions tests/apps/testapp_content/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
20 changes: 20 additions & 0 deletions tests/apps/testapp_content/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Represents protected content
"""

from __future__ import unicode_literals
from django.conf.urls import patterns, url

from django.http import HttpResponse


def testview(request):
return HttpResponse()

urlpatterns = patterns("",
url(
r"^$",
testview,
name="test_url_content"
),
)
Empty file.
3 changes: 3 additions & 0 deletions tests/apps/testapp_namespaced/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
16 changes: 16 additions & 0 deletions tests/apps/testapp_namespaced/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import unicode_literals
from django.conf.urls import patterns, url

from django.http import HttpResponse


def testview(request):
return HttpResponse()

urlpatterns = patterns("",
url(
r"^$",
testview,
name="test_url_namespaced",
),
)
63 changes: 44 additions & 19 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
import datetime
import decimal

from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import timezone


from djstripe.models import Customer, CurrentSubscription
from djstripe import middleware
from djstripe.settings import User


class Request(object):
# TODO - Switch to RequestFactory

def __init__(self, user, path):
self.user = user
self.path = path


class MiddlewareTest(TestCase):
class MiddlewareURLTest(TestCase):
urls = 'tests.test_urls'

def setUp(self):
self.user = User.objects.create_user(username="pydanny")
self.middleware = middleware.SubscriptionPaymentMiddleware()

def test_appname(self):
request = Request(self.user, "/admin/")
response = self.middleware.process_request(request)
self.assertEqual(response, None)

def test_namespace(self):
request = Request(self.user, "/djstripe/")
response = self.middleware.process_request(request)
self.assertEqual(response, None)

def test_namespace_and_url(self):
request = Request(self.user, "/testapp_namespaced/")
response = self.middleware.process_request(request)
self.assertEqual(response, None)

def test_url(self):
request = Request(self.user, "/testapp/")
response = self.middleware.process_request(request)
self.assertEqual(response, None)


class MiddlewareLogicTest(TestCase):
urls = 'tests.test_urls'

def setUp(self):
Expand All @@ -44,32 +72,29 @@ def setUp(self):
start=start,
quantity=1
)
self.spm = middleware.SubscriptionPaymentMiddleware()
self.middleware = middleware.SubscriptionPaymentMiddleware()

def test_anonymous(self):
request = Request(AnonymousUser(), "clarg")
response = self.spm.process_request(request)
response = self.middleware.process_request(request)
self.assertEqual(response, None)

def test_is_staff(self):
self.user.is_staff = True
self.user.save()
request = Request(self.user, "nonsense")
response = self.spm.process_request(request)
response = self.middleware.process_request(request)
self.assertEqual(response, None)

def test_appname(self):
request = Request(self.user, "/admin/")
response = self.spm.process_request(request)
self.assertEqual(response, None)
def test_customer_has_inactive_subscription(self):
request = Request(self.user, "/testapp_content/")
response = self.middleware.process_request(request)
self.assertEqual(response.status_code, 302)

def test_namespace(self):
request = Request(self.user, "/djstripe/")
response = self.spm.process_request(request)
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 = Request(self.user, "/testapp_content/")
response = self.middleware.process_request(request)
self.assertEqual(response, None)

def test_namespace_and_url(self):
pass

def test_url(self):
pass
13 changes: 12 additions & 1 deletion tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,16 @@
urlpatterns = patterns('',

url(r'^admin/', include(admin.site.urls)),
url(r'^djstripe/', include('djstripe.urls', namespace="djstripe")),
url(r'^djstripe/', include('djstripe.urls',
namespace="djstripe", app_name="djstripe")),
url(r'^testapp/', include('tests.apps.testapp.urls')),
url(
r'^testapp_namespaced/',
include('tests.apps.testapp_namespaced.urls',
namespace="testapp_namespaced",
app_name="testapp_namespaced")),

# Represents protected content
url(r'^testapp_content/', include('tests.apps.testapp_content.urls')),
)

0 comments on commit 4b75318

Please sign in to comment.