def form_valid(self, form): benefits_form = SponsorshipsBenefitsForm(data=self.benefits_data) if not benefits_form.is_valid(): return self._redirect_back_to_benefits() sponsor = form.save() uc = use_cases.CreateSponsorshipApplicationUseCase.build() sponsorship = uc.execute( self.request.user, sponsor, benefits_form.get_benefits(include_add_ons=True, include_a_la_carte=True), benefits_form.get_package(), request=self.request, ) notified = uc.notifications[1].get_recipient_list({ "user": self.request.user, "sponsorship": sponsorship }) response = render( self.request, "sponsors/sponsorship_application_finished.html", context={ "sponsorship": sponsorship, "notified": notified }, ) cookies.delete_sponsorship_selected_benefits(response) return response
def test_benefit_with_soft_capacity_should_validate(self): SponsorshipBenefit.objects.all().update(capacity=0, soft_capacity=True) data = {"benefits_psf": [self.program_1_benefits[0]], "package": self.package.id} form = SponsorshipsBenefitsForm(data=data) self.assertTrue(form.is_valid())
def test_validate_form_without_package_but_with_a_la_carte_benefits(self): benefit = self.a_la_carte[0] form = SponsorshipsBenefitsForm( data={"a_la_carte_benefits": [benefit.id]} ) self.assertTrue(form.is_valid()) self.assertEqual([], form.get_benefits()) self.assertEqual([benefit], form.get_benefits(include_a_la_carte=True))
def test_should_not_validate_form_without_package_with_add_ons_and_a_la_carte_benefits(self): data = { "a_la_carte_benefits": [self.a_la_carte[0]], "add_ons_benefits": [self.add_ons[0]], } form = SponsorshipsBenefitsForm(data=data) self.assertFalse(form.is_valid())
def test_benefit_with_no_capacity_should_not_validate(self): SponsorshipBenefit.objects.all().update(capacity=0) data = {"benefits_psf": [self.program_1_benefits[0]], "package": self.package.id} form = SponsorshipsBenefitsForm(data=data) self.assertFalse(form.is_valid()) self.assertIn( "The application has 1 or more benefits with no capacity.", form.errors["__all__"], )
def test_package_only_benefit_without_package_should_not_validate(self): SponsorshipBenefit.objects.all().update(package_only=True) data = {"benefits_psf": [self.program_1_benefits[0]]} form = SponsorshipsBenefitsForm(data=data) self.assertFalse(form.is_valid()) self.assertIn( "You must pick a package to include the selected benefits.", form.errors["__all__"], )
def test_package_list_only_advertisable_ones(self): ads_pkgs = baker.make('SponsorshipPackage', advertise=True, _quantity=2) baker.make('SponsorshipPackage', advertise=False) form = SponsorshipsBenefitsForm() field = form.fields.get("package") self.assertEqual(3, field.queryset.count())
def test_specific_field_to_select_a_la_carte_benefits(self): form = SponsorshipsBenefitsForm() choices = list(form.fields["a_la_carte_benefits"].choices) self.assertEqual(len(self.a_la_carte), len(choices)) for benefit in self.a_la_carte: self.assertIn(benefit.id, [c[0] for c in choices])
def test_get_benefits_from_cleaned_data(self): benefit = self.program_1_benefits[0] data = {"benefits_psf": [benefit.id], "add_ons_benefits": [b.id for b in self.add_ons], "package": self.package.id} form = SponsorshipsBenefitsForm(data=data) self.assertTrue(form.is_valid()) benefits = form.get_benefits() self.assertEqual(1, len(benefits)) self.assertIn(benefit, benefits) benefits = form.get_benefits(include_add_ons=True) self.assertEqual(3, len(benefits)) self.assertIn(benefit, benefits) for add_on in self.add_ons: self.assertIn(add_on, benefits)
def test_invalidate_form_without_benefits(self): form = SponsorshipsBenefitsForm(data={}) self.assertFalse(form.is_valid()) self.assertIn("__all__", form.errors) form = SponsorshipsBenefitsForm( data={"benefits_psf": [self.program_1_benefits[0].id], "package": self.package.id} ) self.assertTrue(form.is_valid())
def test_benefits_organized_by_program(self): form = SponsorshipsBenefitsForm() field1, field2 = sorted(form.benefits_programs, key=lambda f: f.name) self.assertEqual("benefits_psf", field1.name) self.assertEqual("PSF Benefits", field1.label) choices = list(field1.field.choices) self.assertEqual(len(self.program_1_benefits), len(choices)) for benefit in self.program_1_benefits: self.assertIn(benefit.id, [c[0] for c in choices]) self.assertEqual("benefits_working_group", field2.name) self.assertEqual("Working Group Benefits", field2.label) choices = list(field2.field.choices) self.assertEqual(len(self.program_2_benefits), len(choices)) for benefit in self.program_2_benefits: self.assertIn(benefit.id, [c[0] for c in choices])
def test_get_package_get_or_create_a_la_carte_only_package(self): data = {"a_la_carte_benefits": [self.a_la_carte[0].id]} form = SponsorshipsBenefitsForm(data=data) self.assertTrue(form.is_valid()) self.assertEqual(1, SponsorshipPackage.objects.count()) # should create package if it doesn't exist yet package = form.get_package() self.assertEqual("A La Carte Only", package.name) self.assertEqual("a-la-carte-only", package.slug) self.assertEqual(175, package.logo_dimension) self.assertEqual(0, package.sponsorship_amount) self.assertFalse(package.advertise) self.assertEqual(2, SponsorshipPackage.objects.count()) # re-use previously created package for subsequent applications data = {"a_la_carte_benefits": [self.a_la_carte[0].id]} form = SponsorshipsBenefitsForm(data=data) self.assertTrue(form.is_valid()) self.assertEqual(package, form.get_package()) self.assertEqual(2, SponsorshipPackage.objects.count())
def test_invalid_form_if_any_conflict(self): benefit_1 = baker.make("sponsors.SponsorshipBenefit", program=self.wk) benefit_1.conflicts.add(*self.program_1_benefits) self.package.benefits.add(benefit_1) data = {"benefits_psf": [b.id for b in self.program_1_benefits], "package": self.package.id} form = SponsorshipsBenefitsForm(data=data) self.assertTrue(form.is_valid()) data["benefits_working_group"] = [benefit_1.id] form = SponsorshipsBenefitsForm(data=data) self.assertFalse(form.is_valid()) self.assertIn( "The application has 1 or more benefits that conflicts.", form.errors["__all__"], )
def test_benefits_conflicts_helper_property(self): benefit_1, benefit_2 = baker.make("sponsors.SponsorshipBenefit", _quantity=2) benefit_1.conflicts.add(*self.program_1_benefits) benefit_2.conflicts.add(*self.program_2_benefits) form = SponsorshipsBenefitsForm() map = form.benefits_conflicts # conflicts are symmetrical relationships self.assertEqual( 2 + len(self.program_1_benefits) + len(self.program_2_benefits), len(map) ) self.assertEqual( sorted(map[benefit_1.id]), sorted(b.id for b in self.program_1_benefits) ) self.assertEqual( sorted(map[benefit_2.id]), sorted(b.id for b in self.program_2_benefits) ) for b in self.program_1_benefits: self.assertEqual(map[b.id], [benefit_1.id]) for b in self.program_2_benefits: self.assertEqual(map[b.id], [benefit_2.id])
def test_package_only_benefit_with_wrong_package_should_not_validate(self): SponsorshipBenefit.objects.all().update(package_only=True) package = baker.make("sponsors.SponsorshipPackage", advertise=True) package.benefits.add(*SponsorshipBenefit.objects.all()) data = { "benefits_psf": [self.program_1_benefits[0]], "package": baker.make("sponsors.SponsorshipPackage", advertise=True).id, # other package } form = SponsorshipsBenefitsForm(data=data) self.assertFalse(form.is_valid()) self.assertIn( "The application has 1 or more package only benefits but wrong sponsor package.", form.errors["__all__"][0], ) data = { "benefits_psf": [self.program_1_benefits[0]], "package": package.id, } form = SponsorshipsBenefitsForm(data=data) self.assertTrue(form.is_valid())
def test_get_package_return_selected_package(self): data = {"benefits_psf": [self.program_1_benefits[0]], "package": self.package.id} form = SponsorshipsBenefitsForm(data=data) self.assertTrue(form.is_valid()) self.assertEqual(self.package, form.get_package())