Exemple #1
0
    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 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
Exemple #3
0
    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__"],
        )
Exemple #4
0
    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())
Exemple #5
0
 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))
Exemple #6
0
    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())
Exemple #7
0
    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__"],
        )
Exemple #8
0
    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__"],
        )
Exemple #9
0
    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())
Exemple #10
0
    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())
Exemple #11
0
    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)
Exemple #12
0
 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())