Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1 from tswicegood/fix-missing-kwargs

Updated to make sure kwargs and context are handled properly
  • Loading branch information...
commit 079751f177287f237cd27bf2d450d647d642d787 2 parents 951d8f6 + 1993afe
Chris Chang crccheck authored
117 armstrong/apps/donations/tests/views.py
View
@@ -121,6 +121,21 @@ def get_response(self):
self.assertEqual(200, response.status_code, msg="sanity check")
return response
+ def get_fake_post_request(self, confirmed=False):
+ d = {} if not confirmed else {"confirmed": u"1"}
+ return self.factory.post(self.url, d)
+
+ @property
+ def fake_get_request(self):
+ return self.factory.get(self.url)
+
+ def get_post_view(self, confirmed=False):
+ v = views.DonationFormView(confirm=True)
+ v.request = self.get_fake_post_request(confirmed=confirmed)
+ return v
+
+ post_view = property(get_post_view)
+
# TODO: move to armstrong.dev
def get_response(func):
@@ -150,6 +165,49 @@ def test_get_donation_form_returns_credit_card_form_by_default(self):
donation_form = view.get_donation_form()
self.assertIsA(donation_form, forms.CreditCardDonationForm)
+ def test_get_context_turns_kwargs_into_params(self):
+ r = lambda: random.randint(100, 200)
+ random_kwargs = {
+ "slug%d" % r(): "foo-%d" % r(),
+ }
+ view = self.get_view_object()
+ context = view.get_context_data(**random_kwargs)
+ self.assertEqual(len(context["params"]), len(random_kwargs),
+ msg="verify context.params is the same length")
+ for key in context["params"].keys():
+ self.assert_(key in random_kwargs)
+
+ def test_form_is_invalid_passes_kwargs_to_get_context_data(self):
+ r = lambda: random.randint(100, 200)
+ random_kwargs = {
+ "slug%d" % r(): "foo-%d" % r(),
+ }
+
+ get_context_data = fudge.Fake()
+ get_context_data.expects_call().with_args(**random_kwargs)
+
+ view = self.post_view
+ with fudge.patched_context(view, "get_context_data", get_context_data):
+ view.post({}, **random_kwargs)
+
+ fudge.verify()
+
+ def test_form_is_valid_passes_kwargs_to_get_context_data(self):
+ donation, donation_form = self.random_donation_and_form
+ r = lambda: random.randint(100, 200)
+ random_kwargs = {
+ "slug%d" % r(): "foo-%d" % r(),
+ }
+
+ get_context_data = fudge.Fake()
+ get_context_data.expects_call().with_args(**random_kwargs)
+
+ view = self.post_view
+ with fudge.patched_context(view, "get_context_data", get_context_data):
+ view.form_is_valid(donation_form, **random_kwargs)
+
+ fudge.verify()
+
def form_is_valid_response(confirmed=False):
def outer(func):
@@ -168,21 +226,6 @@ def inner(self, *args, **kwargs):
class DonationFormViewPostWithConfirmTestCase(BaseDonationFormViewTestCase):
view_name = "donations_form_confirm"
- def get_fake_post_request(self, confirmed=False):
- d = {} if not confirmed else {"confirmed": u"1"}
- return self.factory.post(self.url, d)
-
- @property
- def fake_get_request(self):
- return self.factory.get(self.url)
-
- def get_post_view(self, confirmed=False):
- v = views.DonationFormView(confirm=True)
- v.request = self.get_fake_post_request(confirmed=confirmed)
- return v
-
- post_view = property(get_post_view)
-
def test_use_confirm_template_false_by_default(self):
v = views.DonationFormView()
self.assertFalse(v.use_confirm_template)
@@ -242,6 +285,50 @@ def test_contains_confirmation_required_in_context(self, r):
def test_redirects_on_confirmed(self, r):
self.assertIsA(r, HttpResponseRedirect)
+ def test_form_is_invalid_receives_kwargs_from_post(self):
+ r = lambda: random.randint(100, 200)
+ random_kwargs = {
+ "slug%d" % r(): "foo-%d" % r(),
+ }
+
+ donation_form = fudge.Fake()
+ donation_form.provides("is_valid").returns(False)
+ get_donation_form = fudge.Fake()
+ get_donation_form.is_callable().returns(donation_form)
+
+ form_is_invalid = fudge.Fake()
+ form_is_invalid.expects_call().with_args(**random_kwargs)
+
+ view = self.post_view
+ with fudge.patched_context(view, "get_donation_form",
+ get_donation_form):
+ with fudge.patched_context(view, "form_is_invalid",
+ form_is_invalid):
+ view.post({}, **random_kwargs)
+
+ fudge.verify()
+
+ def test_post_passes_kwargs_to_form_is_valid(self):
+ r = lambda: random.randint(100, 200)
+ random_kwargs = {
+ "slug%d" % r(): "foo-%d" % r(),
+ }
+ donation_form = fudge.Fake()
+ donation_form.provides("is_valid").returns(True)
+ get_donation_form = fudge.Fake()
+ get_donation_form.is_callable().returns(donation_form)
+
+ form_is_valid = fudge.Fake()
+ form_is_valid.expects_call().with_args(donation_form=donation_form,
+ **random_kwargs)
+ view = self.post_view
+ with fudge.patched_context(view, "get_donation_form",
+ get_donation_form):
+ with fudge.patched_context(view, "form_is_valid", form_is_valid):
+ view.post({}, **random_kwargs)
+
+ fudge.verify()
+
class DonationFormViewPostTestCase(BaseDonationFormViewTestCase):
@property
20 armstrong/apps/donations/views.py
View
@@ -81,29 +81,29 @@ def get_donation_form(self):
return donation_form_class(**self.get_donation_form_kwargs())
def get_context_data(self, **kwargs):
+ context = super(DonationFormView, self).get_context_data(**kwargs)
donation_form = self.get_donation_form()
- context = {
+ context.update({
"form_action_url": self.form_action_url,
"donor_form": donation_form.donor_form,
"donation_form": donation_form,
- }
- context.update(kwargs)
+ "confirmation_required": self.requires_confirmation,
+ })
return context
def post(self, request, *args, **kwargs):
donation_form = self.get_donation_form()
if not donation_form.is_valid():
- return self.form_is_invalid()
- return self.form_is_valid(donation_form=donation_form)
+ return self.form_is_invalid(**kwargs)
+ return self.form_is_valid(donation_form=donation_form, **kwargs)
- def form_is_invalid(self):
+ def form_is_invalid(self, **kwargs):
self.form_validation_failed = True
- return self.render_to_response(self.get_context_data())
+ return self.render_to_response(self.get_context_data(**kwargs))
- def form_is_valid(self, donation_form):
+ def form_is_valid(self, donation_form, **kwargs):
if self.requires_confirmation:
- context = {"confirmation_required": True}
- context.update(self.get_context_data())
+ context = self.get_context_data(**kwargs)
return self.render_to_response(context)
donation = donation_form.save()
response = backends.get_backend().purchase(donation, donation_form)
Please sign in to comment.
Something went wrong with that request. Please try again.