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/admin/test_views.py
from flask import url_for
from flask_babel import ngettext as _n
from flask_babel import gettext as _

from buyercall.tests.lib.util import ViewTestMixin
from buyercall.tests.lib.assertions import assert_status_with_message
from buyercall.blueprints.user.models import User
from buyercall.blueprints.issue.models import Issue


class TestDashboard(ViewTestMixin):

    def test_dashboard_page(self, users):
        self.login()
        response = self.client.get(url_for('admin.dashboard'))
        assert b'Billing' not in response.data
        assert b'Users' in response.data

    def test_dashboard_page_for_partner(self, users):
        self.login(identity='partner@localhost.com')
        response = self.client.get(url_for('admin.dashboard'))
        assert b'Billing' in response.data
        assert b'Subscriptions' in response.data
        assert b'User' in response.data
        assert b'Issue' in response.data


class TestUsers(ViewTestMixin):
    def test_index_page(self):
        """ Index renders successfully. """
        self.login()
        response = self.client.get(url_for('admin.users'))

        assert response.status_code == 200
        assert b'Account User Table' in response.data

    def test_edit_page(self):
        """ Edit page renders successfully. """

        self.login()
        user = User.find_by_identity('admin@localhost.com')
        response = self.client.get(url_for('admin.users_edit', id=user.id))

        assert_status_with_message(200, response, 'admin@localhost.com')

    def test_edit_resource(self):
        """ Edit this resource successfully. """
        params = {
            'role': 'admin',
            'username': 'foo',
            'active': True
        }

        self.login()
        user = User.find_by_identity('admin@localhost.com')
        response = self.client.post(url_for('admin.users_edit', id=user.id),
                                    data=params, follow_redirects=True)

        assert_status_with_message(200, response,
                                   _('User has been saved successfully.'))

    def test_user_delete(self, users):
        """ Last admin account should not get deleted. """
        old_count = User.query.count()
        user = User.find_by_identity('disabled@localhost.com')
        self.login()
        response = self.client.delete(
            url_for('admin.user_delete', **{'id': user.id}),
            follow_redirects=True
        )

        new_count = User.query.count()
        assert response.status_code == 200
        assert user.is_deactivated == True

    def test_cancel_subscription(self, subscriptions, mock_stripe):
        """ User subscription gets cancelled.. """
        user = User.find_by_identity('subscriber@localhost.com')
        params = {
            'id': user.id
        }

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

        assert response.status_code == 200
        assert user.subscription.cancelled_subscription_on is not None


class TestIssues(ViewTestMixin):
    def test_index_page(self, users):
        """ Index renders successfully. """
        self.login()
        response = self.client.get(url_for('admin.issues'))

        assert response.status_code == 200

    def test_edit_page(self, issues):
        """ Edit page renders successfully. """
        issue = Issue.query.first()
        self.login()
        response = self.client.get(url_for('admin.issues_edit', id=issue.id))

        assert_status_with_message(200, response, 'admin@localhost.com')

    def test_edit_resource(self):
        """ Edit this resource successfully. """
        params = {
            'label': 'login',
            'email': 'foo@bar.com',
            'question': 'Cool.',
            'status': 'open',
        }

        issue = Issue(**params)
        issue.save()

        params_edit = {
            'label': 'other',
            'email': 'foo@bar.com',
            'question': 'Cool.',
            'status': 'closed',
        }

        self.login()
        response = self.client.post(url_for('admin.issues_edit', id=issue.id),
                                    data=params_edit, follow_redirects=True)

        assert_status_with_message(200, response,
                                   _('Issue has been saved successfully.'))