Skip to content

Commit

Permalink
Remove TODO notes in sponsors app (#1692)
Browse files Browse the repository at this point in the history
* Refactor unit test by centralizing data dict in set up

* Only proceed to sponsor info form if Django can use cookies

* Remove outdated template tag (used with old Sponsors models)

* Update sponsors/views.py

* Update sponsors/tests/test_views.py

Co-authored-by: Ernest W. Durbin III <ernest@python.org>
  • Loading branch information
berinhard and ewdurbin committed Nov 30, 2020
1 parent af59bc0 commit 30a94fc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 58 deletions.
13 changes: 0 additions & 13 deletions sponsors/templatetags/sponsors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@
register = template.Library()


@register.inclusion_tag("sponsors/templatetags/featured_sponsor_rotation.html")
def featured_sponsor_rotation():
"""
Retrieve featured Sponsors for rotation
"""
# TODO remove this code completely if not necessary
# this templatetag logic was removed by the PR #1667
# the Sponsor model got updated and its previous use was deprecated
# this templated tag is used at https://www.python.org/psf-landing/ but since the prod
# DB doesn't have any published sponsor, the default message is being print
return {}


@register.inclusion_tag("sponsors/partials/full_sponsorship.txt")
def full_sponsorship(sponsorship):
return {
Expand Down
8 changes: 1 addition & 7 deletions sponsors/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
from companies.models import Company

from ..models import Sponsor
from ..templatetags.sponsors import featured_sponsor_rotation, full_sponsorship


class SponsorTemplatetagTests(TestCase):
def test_templatetag(self):
sponsors_context = featured_sponsor_rotation()
self.assertEqual({}, sponsors_context)
from ..templatetags.sponsors import full_sponsorship


class FullSponsorshipTemplatetagTests(TestCase):
Expand Down
71 changes: 39 additions & 32 deletions sponsors/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from model_bakery import baker
from itertools import chain

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import Group
from django.conf import settings
from django.contrib.messages import get_messages
from django.contrib.sessions.backends.base import SessionBase
from django.core import mail
from django.urls import reverse, reverse_lazy
from django.test import TestCase
from django.contrib.messages import get_messages
from django.urls import reverse, reverse_lazy

from .utils import get_static_image_file_as_upload
from ..models import (
Expand Down Expand Up @@ -42,11 +43,24 @@ def setUp(self):
self.program_2_benefits = baker.make(
SponsorshipBenefit, program=self.wk, _quantity=5
)
self.package = baker.make("sponsors.SponsorshipPackage")
for benefit in self.program_1_benefits:
benefit.packages.add(self.package)
self.user = baker.make(settings.AUTH_USER_MODEL, is_staff=True, is_active=True)
self.client.force_login(self.user)

self.group = Group(name="Sponsorship Preview")
self.group.save()
self.data = {
"benefits_psf": [b.id for b in self.program_1_benefits],
"benefits_working_group": [b.id for b in self.program_2_benefits],
"package": self.package.id,
}

def populate_test_cookie(self):
session = self.client.session
session[SessionBase.TEST_COOKIE_NAME] = SessionBase.TEST_COOKIE_VALUE
session.save()

def test_display_template_with_form_and_context(self):
psf_package = baker.make("sponsors.SponsorshipPackage")
Expand All @@ -59,45 +73,36 @@ def test_display_template_with_form_and_context(self):
self.assertTemplateUsed(r, "sponsors/sponsorship_benefits_form.html")
self.assertIsInstance(r.context["form"], SponsorshiptBenefitsForm)
self.assertEqual(r.context["benefit_model"], SponsorshipBenefit)
self.assertEqual(2, packages.count())
self.assertEqual(3, packages.count())
self.assertIn(psf_package, packages)
self.assertIn(extra_package, packages)
self.assertEqual(
r.client.session[SessionBase.TEST_COOKIE_NAME],
SessionBase.TEST_COOKIE_VALUE,
)

def test_display_form_with_errors_if_invalid_post(self):
self.populate_test_cookie()
r = self.client.post(self.url, {})
form = r.context["form"]

self.assertIsInstance(form, SponsorshiptBenefitsForm)
self.assertTrue(form.errors)

def test_valid_post_redirect_user_to_next_form_step_and_save_info_in_cookies(self):
package = baker.make("sponsors.SponsorshipPackage")
for benefit in self.program_1_benefits:
benefit.packages.add(package)

data = {
"benefits_psf": [b.id for b in self.program_1_benefits],
"benefits_working_group": [b.id for b in self.program_2_benefits],
"package": package.id,
}
response = self.client.post(self.url, data=data)
self.populate_test_cookie()
response = self.client.post(self.url, data=self.data)

self.assertRedirects(response, reverse("new_sponsorship_application"))
cookie_value = json.loads(
response.client.cookies["sponsorship_selected_benefits"].value
)
self.assertEqual(data, cookie_value)
self.assertEqual(self.data, cookie_value)

def test_populate_form_initial_with_values_from_cookie(self):
initial = {
"benefits_psf": [b.id for b in self.program_1_benefits],
"benefits_working_group": [b.id for b in self.program_2_benefits],
"package": "",
}
self.client.cookies["sponsorship_selected_benefits"] = json.dumps(initial)
self.client.cookies["sponsorship_selected_benefits"] = json.dumps(self.data)
r = self.client.get(self.url)

self.assertEqual(initial, r.context["form"].initial)
self.assertEqual(self.data, r.context["form"].initial)

def test_capacity_flag(self):
psf_package = baker.make("sponsors.SponsorshipPackage")
Expand All @@ -117,20 +122,22 @@ def test_redirect_to_login(self):
redirect_url = (
f"{settings.LOGIN_URL}?next={reverse('new_sponsorship_application')}"
)
package = baker.make("sponsors.SponsorshipPackage")
for benefit in self.program_1_benefits:
benefit.packages.add(package)

data = {
"benefits_psf": [b.id for b in self.program_1_benefits],
"benefits_working_group": [b.id for b in self.program_2_benefits],
"package": package.id,
}
self.client.logout()
response = self.client.post(self.url, data=data)
self.populate_test_cookie()
response = self.client.post(self.url, data=self.data)

self.assertRedirects(response, redirect_url, fetch_redirect_response=False)

def test_invalidate_post_even_if_valid_data_but_user_does_not_allow_cookies(self):
# do not set Django's test cookie
r = self.client.post(self.url, data=self.data)
form = r.context["form"]

self.assertIsInstance(form, SponsorshiptBenefitsForm)
msg = "You must allow cookies from python.org to proceed."
self.assertEqual(form.non_field_errors(), [msg])


class NewSponsorshipApplicationViewTests(TestCase):
url = reverse_lazy("new_sponsorship_application")
Expand Down
12 changes: 11 additions & 1 deletion sponsors/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db import transaction
from django.forms.utils import ErrorList
from django.utils.decorators import method_decorator
from django.http import JsonResponse
from django.views.generic import ListView, FormView
Expand Down Expand Up @@ -57,12 +58,21 @@ def get_initial(self):
return cookies.get_sponsorship_selected_benefits(self.request)

def form_valid(self, form):
if not self.request.session.test_cookie_worked():
error = ErrorList()
error.append("You must allow cookies from python.org to proceed.")
form._errors.setdefault("__all__", error)
return self.form_invalid(form)

response = super().form_valid(form)
self._set_form_data_cookie(form, response)
return response

def get(self, request, *args, **kwargs):
request.session.set_test_cookie()
return super().get(request, *args, **kwargs)

def _set_form_data_cookie(self, form, response):
# TODO: make sure user accepts cookies with set_test_cookie
data = {
"package": "" if not form.get_package() else form.get_package().id,
}
Expand Down
4 changes: 3 additions & 1 deletion templates/psf/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@

<div class="small-widget psf-widget4 last">
<h2 class="widget-title">Sponsors</h2>
{% featured_sponsor_rotation %}
<div id="sponsor-rotation" class="flex-slideshow">
<p>Without our sponsors we wouldn't be able to help the Python community grow and prosper.</p>
</div>
<p><a class="button" href="/psf/sponsorship/">Sponsorship Possibilities</a></p>
</div>

Expand Down

This file was deleted.

0 comments on commit 30a94fc

Please sign in to comment.