def test_no_change(self):
        """Nothing should happen if the payment matches the current tier."""
        tier = self.tiers[20]
        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(self.tier_info.tier, tier)

        set_tier(tier.price)

        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(self.tier_info.tier, tier)
        self.assertFalse(self._enforce_tier.called)
    def test_no_change(self):
        """Nothing should happen if the payment matches the current tier."""
        tier = self.tiers[20]
        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(self.tier_info.tier, tier)

        set_tier(tier.price)

        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(self.tier_info.tier, tier)
        self.assertFalse(self._enforce_tier.called)
    def test_invalid_change(self):
        """
        If the change is invalid, Tier.DoesNotExist should be raised.

        """
        tier = self.tier_info.tier
        self.assertEqual(len(mail.outbox), 0)

        with self.assertRaises(Tier.DoesNotExist):
            set_tier(40)

        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(self.tier_info.tier, tier)
        self.assertFalse(self._enforce_tier.called)
    def test_invalid_change(self):
        """
        If the change is invalid, Tier.DoesNotExist should be raised.

        """
        tier = self.tier_info.tier
        self.assertEqual(len(mail.outbox), 0)

        with self.assertRaises(Tier.DoesNotExist):
            set_tier(40)

        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(self.tier_info.tier, tier)
        self.assertFalse(self._enforce_tier.called)
    def test_valid_change(self):
        """
        If the change is valid, the tier should change and be enforced, and
        admins should be notified.

        """
        tier = self.tiers[30]
        self.assertEqual(len(mail.outbox), 0)
        self.assertNotEqual(self.tier_info.tier, tier)

        set_tier(tier.price)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, ["manager@localhost"])
        self.assertEqual(self.tier_info.tier, tier)
        self._enforce_tier.assert_called_with(tier)
    def test_valid_change(self):
        """
        If the change is valid, the tier should change and be enforced, and
        admins should be notified.

        """
        tier = self.tiers[30]
        self.assertEqual(len(mail.outbox), 0)
        self.assertNotEqual(self.tier_info.tier, tier)

        set_tier(tier.price)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, ['manager@localhost'])
        self.assertEqual(self.tier_info.tier, tier)
        self._enforce_tier.assert_called_with(tier)
Exemple #7
0
    def get_context_data(self, **kwargs):
        context = super(TierView, self).get_context_data(**kwargs)
        tier_info = SiteTierInfo.objects.get_current()

        if tier_info.enforce_payments:
            price = (0 if tier_info.subscription is None else
                     tier_info.subscription.signup_or_modify.amount3)
            try:
                set_tier(price)
            except Tier.DoesNotExist:
                logging.error('No tier matching current subscription.',
                              exc_info=True)
            except Tier.MultipleObjectsReturned:
                logging.error(
                    'Multiple tiers found matching current'
                    'subscription.',
                    exc_info=True)

        forms = SortedDict()
        tiers = tier_info.available_tiers.order_by('price')
        for tier in tiers:
            if tier.price < tier_info.tier.price:
                forms[tier] = DowngradeConfirmationForm(tier)
            else:
                if tier_info.enforce_payments:
                    forms[tier] = PayPalSubscriptionForm(tier)
                else:
                    forms[tier] = TierChangeForm(initial={'tier': tier})

        # Here we build a list of prices of current subscriptions, sorted
        # by the date the subscription started. This lets us check in the
        # template whether a tier's subscription should be considered
        # "upcoming" or "old".
        subscriptions = sorted([
            subscr
            for subscr in tier_info.subscriptions if not subscr.is_cancelled
        ],
                               key=lambda s: s.signup_or_modify.subscr_date,
                               reverse=True)
        subscription_prices = [subscr.price for subscr in subscriptions]
        context.update({
            'subscription_prices': subscription_prices,
            'cancellation_form': PayPalCancellationForm(),
            'forms': forms,
            'tier_info': tier_info,
        })
        return context
    def test_duplicate_change(self):
        """
        If the price matches the current tier, Tier.MultipleObjectsReturned
        should be raised.

        """
        tier = self.tier_info.tier
        self.assertEqual(len(mail.outbox), 0)

        duplicate_tier = self.create_tier(price=30, slug="tier30-2")
        self.tier_info.available_tiers.add(duplicate_tier)

        with self.assertRaises(Tier.MultipleObjectsReturned):
            set_tier(30)

        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(self.tier_info.tier, tier)
        self.assertFalse(self._enforce_tier.called)
    def test_duplicate_change(self):
        """
        If the price matches the current tier, Tier.MultipleObjectsReturned
        should be raised.

        """
        tier = self.tier_info.tier
        self.assertEqual(len(mail.outbox), 0)

        duplicate_tier = self.create_tier(price=30, slug='tier30-2')
        self.tier_info.available_tiers.add(duplicate_tier)

        with self.assertRaises(Tier.MultipleObjectsReturned):
            set_tier(30)

        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(self.tier_info.tier, tier)
        self.assertFalse(self._enforce_tier.called)
Exemple #10
0
    def get_context_data(self, **kwargs):
        context = super(TierView, self).get_context_data(**kwargs)
        tier_info = SiteTierInfo.objects.get_current()

        if tier_info.enforce_payments:
            price = (0 if tier_info.subscription is None
                     else tier_info.subscription.signup_or_modify.amount3)
            try:
                set_tier(price)
            except Tier.DoesNotExist:
                logging.error('No tier matching current subscription.',
                              exc_info=True)
            except Tier.MultipleObjectsReturned:
                logging.error('Multiple tiers found matching current'
                              'subscription.', exc_info=True)

        forms = SortedDict()
        tiers = tier_info.available_tiers.order_by('price')
        for tier in tiers:
            if tier.price < tier_info.tier.price:
                forms[tier] = DowngradeConfirmationForm(tier)
            else:
                if tier_info.enforce_payments:
                    forms[tier] = PayPalSubscriptionForm(tier)
                else:
                    forms[tier] = TierChangeForm(initial={'tier': tier})

        # Here we build a list of prices of current subscriptions, sorted
        # by the date the subscription started. This lets us check in the
        # template whether a tier's subscription should be considered
        # "upcoming" or "old".
        subscriptions = sorted([subscr for subscr in tier_info.subscriptions
                                if not subscr.is_cancelled],
                               key=lambda s: s.signup_or_modify.subscr_date,
                               reverse=True)
        subscription_prices = [subscr.price for subscr in subscriptions]
        context.update({
            'subscription_prices': subscription_prices,
            'cancellation_form': PayPalCancellationForm(),
            'forms': forms,
            'tier_info': tier_info,
        })
        return context