def test_invalidate_form_without_benefits(self): form = SponsorshiptBenefitsForm(data={}) self.assertFalse(form.is_valid()) self.assertIn("__all__", form.errors) form = SponsorshiptBenefitsForm( data={"benefits_psf": [self.program_1_benefits[0].id]}) self.assertTrue(form.is_valid())
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) data = {"benefits_psf": [b.id for b in self.program_1_benefits]} form = SponsorshiptBenefitsForm(data=data) self.assertTrue(form.is_valid()) data["benefits_working_group"] = [benefit_1.id] form = SponsorshiptBenefitsForm(data=data) self.assertFalse(form.is_valid()) self.assertIn( "The application has 1 or more benefits that conflicts.", form.errors["__all__"], )
def form_valid(self, form): benefits_form = SponsorshiptBenefitsForm(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(), 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]]} form = SponsorshiptBenefitsForm(data=data) self.assertTrue(form.is_valid())
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 = SponsorshiptBenefitsForm(data=data) self.assertFalse(form.is_valid()) self.assertIn( "The application has 1 or more package only benefits and no package.", form.errors["__all__"], )
def test_benefit_with_no_capacity_should_not_validate(self): SponsorshipBenefit.objects.all().update(capacity=0) data = {"benefits_psf": [self.program_1_benefits[0]]} form = SponsorshiptBenefitsForm(data=data) self.assertFalse(form.is_valid()) self.assertIn( "The application has 1 or more benefits with no capacity.", form.errors["__all__"], )
def form_valid(self, form): benefits_form = SponsorshiptBenefitsForm(data=self.benefits_data) if not benefits_form.is_valid(): return self._redirect_back_to_benefits() sponsor = form.save() Sponsorship.new(sponsor, benefits_form.get_benefits(), benefits_form.get_package()) response = super().form_valid(form) cookies.delete_sponsorship_selected_benefits(response) return response
def test_package_only_benefit_with_wrong_package_should_not_validate(self): SponsorshipBenefit.objects.all().update(package_only=True) package = baker.make("sponsors.SponsorshipPackage") package.benefits.add(*SponsorshipBenefit.objects.all()) data = { "benefits_psf": [self.program_1_benefits[0]], "package": baker.make("sponsors.SponsorshipPackage").id, # other package } form = SponsorshiptBenefitsForm(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__"], ) data = { "benefits_psf": [self.program_1_benefits[0]], "package": package.id, } form = SponsorshiptBenefitsForm(data=data) self.assertTrue(form.is_valid())