def test_enough_donations(self):
        donation = DonationFactory.create(activity=self.funding,
                                          amount=Money(300, 'EUR'))
        PledgePaymentFactory.create(donation=donation)
        donation = DonationFactory.create(activity=self.funding,
                                          amount=Money(450, 'EUR'))
        PledgePaymentFactory.create(donation=donation)
        self.assertEqual(len(mail.outbox), 4)
        self.assertEqual(donation.status, 'succeeded')
        self.funding.deadline = now() - timedelta(days=1)
        self.funding.save()

        # Run scheduled task
        tenant = connection.tenant
        funding_tasks()
        with LocalTenant(tenant, clear_tenant=True):
            self.funding.refresh_from_db()

        self.assertEqual(len(mail.outbox), 5)
        self.assertEqual(
            mail.outbox[4].subject,
            u'Your campaign "{}" has been successfully completed! \U0001f389'.
            format(self.funding.title))
        self.assertTrue('Hi Jean Baptiste,' in mail.outbox[4].body)
        self.assertTrue(self.funding.title in mail.outbox[4].body)
        url = 'http://testserver/en/initiatives/activities/details/funding/{}/{}'.format(
            self.funding.id, self.funding.slug)
        self.assertTrue(url in mail.outbox[4].body)

        organizer = self.funding.contributions.instance_of(Organizer).get()
        self.assertEqual(organizer.status, organizer.states.succeeded.value)
 def test_funding_scheduled_task_expired(self):
     mail.outbox = []
     tenant = connection.tenant
     with mock.patch.object(timezone,
                            'now',
                            return_value=(timezone.now() +
                                          timedelta(days=20))):
         funding_tasks()
     with LocalTenant(tenant, clear_tenant=True):
         self.funding.refresh_from_db()
     self.assertEqual(self.funding.status, 'cancelled')
     self.assertEqual(len(mail.outbox), 1)
     self.assertEqual(mail.outbox[0].subject,
                      u'Your crowdfunding campaign has expired')
    def test_extend_past_deadline(self):
        donation = DonationFactory.create(activity=self.funding,
                                          amount=Money(1000, 'EUR'))
        PledgePaymentFactory.create(donation=donation)

        self.funding.deadline = now() - timedelta(days=1)
        self.funding.save()

        # Run scheduled task
        tenant = connection.tenant
        funding_tasks()
        with LocalTenant(tenant, clear_tenant=True):
            self.funding.refresh_from_db()

        self.assertEqual(self.funding.status, 'succeeded')

        with self.assertRaises(TransitionNotPossible):
            self.funding.states.extend(save=True)
    def test_no_donations(self):
        self.assertEqual(self.funding.initiative.status, 'approved')
        self.assertEqual(self.funding.status, 'open')

        # Run scheduled task
        tenant = connection.tenant
        future = now() + timedelta(weeks=12)
        with mock.patch.object(timezone, 'now', return_value=future):
            funding_tasks()
        with LocalTenant(tenant, clear_tenant=True):
            self.funding.refresh_from_db()

        self.assertEqual(self.funding.status, 'cancelled')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject,
                         'Your crowdfunding campaign has expired')
        self.assertTrue(self.funding.title in mail.outbox[0].body)
        self.assertTrue('Hi Jean Baptiste,' in mail.outbox[0].body)
 def test_funding_scheduled_task_partial(self):
     donation = DonationFactory.create(activity=self.funding,
                                       user=BlueBottleUserFactory.create(),
                                       amount=Money(500, 'EUR'))
     PledgePaymentFactory.create(donation=donation)
     self.funding.deadline = now() - timedelta(days=1)
     mail.outbox = []
     tenant = connection.tenant
     with mock.patch.object(timezone,
                            'now',
                            return_value=(timezone.now() +
                                          timedelta(days=20))):
         funding_tasks()
     with LocalTenant(tenant, clear_tenant=True):
         self.funding.refresh_from_db()
     self.assertEqual(self.funding.status, 'partially_funded')
     self.assertEqual(len(mail.outbox), 1)
     self.assertEqual(mail.outbox[0].subject,
                      u'Your crowdfunding campaign deadline passed')
 def test_funding_scheduled_task_succeed(self):
     donation = DonationFactory.create(activity=self.funding,
                                       user=BlueBottleUserFactory.create(),
                                       amount=Money(1000, 'EUR'))
     PledgePaymentFactory.create(donation=donation)
     self.funding.deadline = now() - timedelta(days=1)
     mail.outbox = []
     tenant = connection.tenant
     with mock.patch.object(timezone,
                            'now',
                            return_value=(timezone.now() +
                                          timedelta(days=20))):
         funding_tasks()
     with LocalTenant(tenant, clear_tenant=True):
         self.funding.refresh_from_db()
     self.assertEqual(self.funding.status, 'succeeded')
     self.assertEqual(len(mail.outbox), 1)
     self.assertEqual(
         mail.outbox[0].subject,
         u'Your campaign "{}" has been successfully completed! \U0001f389'.
         format(self.funding.title))
    def test_extend(self):
        donation = DonationFactory.create(activity=self.funding,
                                          amount=Money(1000, 'EUR'))
        PledgePaymentFactory.create(donation=donation)

        self.funding.deadline = now() + timedelta(days=1)
        self.funding.save()

        # Run scheduled task
        tenant = connection.tenant
        future = now() + timedelta(days=2)
        with mock.patch.object(timezone, 'now', return_value=future):
            funding_tasks()
        with LocalTenant(tenant, clear_tenant=True):
            self.funding.refresh_from_db()

        self.assertEqual(self.funding.status, 'succeeded')

        self.funding.deadline = now() + timedelta(days=1)
        self.funding.save()

        # self.funding.states.extend()
        self.assertEqual(self.funding.status, 'open')
    def test_some_donations(self):
        user = BlueBottleUserFactory.create(first_name='Bill')
        donation = DonationFactory.create(user=user,
                                          activity=self.funding,
                                          amount=Money(50, 'EUR'))
        donation.states.succeed(save=True)
        PledgePaymentFactory.create(donation=donation)
        self.assertEqual(donation.status, donation.states.succeeded.value)
        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(mail.outbox[0].subject,
                         u'You have a new donation!💰')
        self.assertEqual(mail.outbox[1].subject, 'Thanks for your donation!')
        self.assertTrue('Hi Jean Baptiste,' in mail.outbox[0].body)
        self.assertTrue('Hi Bill,' in mail.outbox[1].body)

        # Donation amount should appear in both emails
        self.assertTrue(u'50.00 €' in mail.outbox[0].body)
        self.assertTrue(u'50.00 €' in mail.outbox[1].body)

        self.funding.deadline = now() - timedelta(days=1)
        self.funding.save()

        # Run scheduled task
        tenant = connection.tenant
        funding_tasks()
        with LocalTenant(tenant, clear_tenant=True):
            self.funding.refresh_from_db()

        self.assertEqual(self.funding.status, 'partially_funded')
        self.assertEqual(len(mail.outbox), 3)
        self.assertEqual(mail.outbox[2].subject,
                         'Your crowdfunding campaign deadline passed')
        self.assertTrue('Hi Jean Baptiste,' in mail.outbox[0].body)
        self.assertTrue(self.funding.title in mail.outbox[0].body)
        url = 'http://testserver/en/initiatives/activities/details/funding/{}/{}'.format(
            self.funding.id, self.funding.slug)
        self.assertTrue(url in mail.outbox[0].body)
    def test_refund(self):
        donation = DonationFactory.create(activity=self.funding,
                                          amount=Money(50, 'EUR'))
        PledgePaymentFactory.create(donation=donation)

        self.funding.deadline = now() - timedelta(days=1)
        self.funding.save()

        tenant = connection.tenant
        funding_tasks()

        with LocalTenant(tenant, clear_tenant=True):
            self.funding.refresh_from_db()

        self.funding.refresh_from_db()
        self.assertEqual(self.funding.status, 'partially_funded')
        self.funding.states.refund(save=True)

        for contribution in self.funding.donations.all():
            self.assertEqual(contribution.status,
                             contribution.states.activity_refunded.value)

        self.funding.update_amounts()
        self.assertEqual(self.funding.amount_raised, donation.amount)