HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/tests/billing/test_views.py
from flask import url_for, json
from flask_babel import gettext as _

from buyercall.blueprints.billing.models.coupon import Coupon
from buyercall.tests.lib.util import ViewTestMixin
from buyercall.tests.lib.assertions import assert_status_with_message


class TestBilling(ViewTestMixin):
    def test_pricing_page(self):
        """ Pricing page renders successfully. """
        response = self.client.get(url_for('billing.pricing'), follow_redirects=True)
        assert_status_with_message(200, response, 'Sign in')
        assert b'/subscription/pricing' in response.data

    def test_pricing_page_logged_in(self, billing):
        """ Pricing page renders successfully. """
        self.login()
        response = self.client.get(url_for('billing.pricing'))
        assert_status_with_message(200, response, 'Continue')

    def test_pricing_page_as_subscriber(self, subscriptions):
        """ Pricing page for subscribers should redirect to update. """
        self.login(identity='subscriber@localhost.com')

        response = self.client.get(url_for('billing.pricing'),
                                   follow_redirects=True)
        assert_status_with_message(200, response, 'Current plan')

    def test_coupon_code_not_valid(self):
        """ Coupon code should not be processed, """
        self.login()

        params = {'coupon_code': None}
        response = self.client.post(url_for('billing.coupon_code'),
                                    data=params, follow_redirects=True)

        data = json.loads(response.data)

        assert response.status_code == 422
        assert data['error'] == _('Discount code cannot be processed.')

    def test_coupon_code_not_redeemable(self):
        """ Coupon code should be redeemable. """
        self.login()

        params = {'coupon_code': 'foo'}
        response = self.client.post(url_for('billing.coupon_code'),
                                    data=params, follow_redirects=True)

        data = json.loads(response.data)

        assert response.status_code == 404
        assert data['error'] == _('Discount code not found.')

    def test_subscription_create_page(self):
        """ Subscription create page renders successfully. """
        self.login()

        response = self.client.get(url_for('billing.create'),
                                   follow_redirects=True)

        assert response.status_code == 200

    def test_subscription_create_as_subscriber(self, subscriptions):
        """ Subscribers should not be allowed to create a subscription. """
        self.login(identity='subscriber@localhost.com')

        response = self.client.get(url_for('billing.create'),
                                   follow_redirects=True)

        assert_status_with_message(200, response,
                                   _('You already have an active subscription'
                                     '.'))

    def test_subscription_create(self, billing, mock_stripe):
        """ Subscription create requires javascript. """
        self.login()

        params = {
            'stripe_key': 'cus_000',
            'plan': 'gold',
            'name': 'Foobar Johnson'
        }

        response = self.client.post(url_for('billing.create'),
                                    data=params, follow_redirects=True)

        assert_status_with_message(200, response,
                                   _('My Account Settings'))

    def test_subscription_update_page_without_subscription(self):
        """ Subscription update page redirects to pricing page. """
        self.login()

        response = self.client.get(url_for('billing.update'),
                                   follow_redirects=True)
        assert_status_with_message(200, response, "Continue")

    def test_subscription_update_page(self, subscriptions):
        """ Subscription update page renders successfully. """
        self.login(identity='subscriber@localhost.com')

        response = self.client.get(url_for('billing.update'),
                                   follow_redirects=True)

        assert_status_with_message(200, response,
                                   "Current plan")

    # def test_subscription_update(self, subscriptions, mock_stripe):
    #     """ Subscription create adds a new subscription. """
    #     self.login(identity='subscriber@localhost.com')
    #
    #     params = {
    #         '3': ''
    #     }
    #     import pdb
    #     pdb.set_trace()
    #     response = self.client.post(url_for('billing.update'),
    #                                 data=params, follow_redirects=True)
    #
    #     assert response.status_code == 200

    def test_subscription_cancel_page_without_subscription(self):
        """ Subscription cancel page redirects to settings. """
        self.login()

        response = self.client.get(url_for('billing.cancel'),
                                   follow_redirects=True)

        assert_status_with_message(200, response,
                                   _('You do not have an active subscription'
                                     '.'))

    def test_subscription_cancel_page(self, subscriptions):
        """ Subscription cancel page renders successfully. """
        self.login(identity='subscriber@localhost.com')

        response = self.client.get(url_for('billing.cancel'),
                                   follow_redirects=True)

        assert response.status_code == 200

    def test_subscription_cancel(self, subscriptions, mock_stripe):
        """ Subscription cancel is successful. """
        self.login(identity='subscriber@localhost.com')

        response = self.client.post(url_for('billing.cancel'),
                                    data={}, follow_redirects=True)

        assert_status_with_message(200, response,
                                   _('Sorry to see you go, your subscription '
                                     'has been cancelled.'))

    def test_subscription_update_payment_method_without_card(self, subscriptions):
        """ Subscription update method without card should fail. """
        self.login(identity='subscriber@localhost.com')
        response = self.client.post(url_for('billing.update_payment_method'),
                                    data={}, follow_redirects=True)
        assert_status_with_message(200, response,
                                   _('Payment Information'))

    def test_subscription_update_payment_method(self, subscriptions,
                                                mock_stripe):
        """ Subscription update payment requires javascript. """
        self.login(identity='subscriber@localhost.com')
        response = self.client.post(url_for('billing.update_payment_method'),
                                    data={}, follow_redirects=True)
        assert_status_with_message(200, response,
                                   _(''))

    def test_subscription_billing_history(self, billing, mock_stripe):
        """ Subscription billing history should render successfully. """
        self.login(identity='subscriber@localhost.com')
        response = self.client.get(url_for('billing.billing_history'))

        assert_status_with_message(200, response,
                                   'Billing details and history')

    def test_subscription_billing_history_without_sub(self, mock_stripe):
        """ Subscription billing history without sub should still work. """
        self.login()
        response = self.client.get(url_for('billing.billing_history'))

        assert_status_with_message(200, response,
                                   'Billing details and history')


class TestCoupon(ViewTestMixin):
    def test_index_page_logged_out(self, users):
        """Index redirects to home page."""
        self.logout()
        response = self.client.get(url_for('admin.coupons'))
        assert_status_with_message(302, response, _('/login'))

    def test_index_page_admin(self, users):
        """Index redirects to home page."""
        self.login()
        response = self.client.get(url_for('admin.coupons'))
        assert_status_with_message(302, response, _('href="/"'))

    def test_index_page_sysadmin(self):
        """ Index renders successfully. """
        self.login(identity='sysadmin@localhost.com')
        response = self.client.get(url_for('admin.coupons'))
        assert_status_with_message(200, response, _('buyercall Admin - Coupons List'))

    def test_new_page(self, coupons):
        """ New page renders successfully. """
        self.login(identity='sysadmin@localhost.com')
        response = self.client.get(url_for('admin.coupons_new'))

        assert response.status_code == 200
        assert b'Add' in response.data

    def test_new_resource(self, mock_stripe):
        """ Edit this resource successfully. """

        params = {
            'code': '1337',
            'duration': 'repeating',
            'percent_off': 5,
            'amount_off': None,
            'currency': 'usd',
            'redeem_by': None,
            'max_redemptions': 10,
            'duration_in_months': 5,
        }

        self.login(identity='sysadmin@localhost.com')
        response = self.client.post(url_for('admin.coupons_new'),
                                    data=params, follow_redirects=True)

        assert_status_with_message(200, response,
                                   _('Coupon has been created successfully.'))

    def test_delete(self, coupons, mock_stripe):
        """ Resource gets bulk deleted. """
        self.login(identity='sysadmin@localhost.com')
        old_count = Coupon.query.count()
        if not old_count:
            params = {
                'code': '1337',
                'duration': 'repeating',
                'percent_off': 5,
                'amount_off': None,
                'currency': 'usd',
                'redeem_by': None,
                'max_redemptions': 10,
                'duration_in_months': 5,
            }
            coupon = Coupon(**params)
        else:
            coupon = Coupon.query.first()
        response = self.client.post(
            url_for('admin.coupons_delete', id=coupon.id),
            follow_redirects=True
        )

        new_count = Coupon.query.count()
        assert old_count == new_count + 1
        assert response.status_code == 200