backend_tests.py 1.57 KB
import unittest
from pdc_config import TestConfig
from app import create_app, db_mgr, db
from app.auth.models import User


class BaseTestCase(unittest.TestCase):
    def setUp(self):
        # configure data base
        self.app = create_app(TestConfig)
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin')
        db.session.add(admin)
        db.session.commit()

    def tearDown(self):
        self.app_context.pop()

    def test_always_true(self):
        self.assertTrue(True)


class DbMgrTestCase(BaseTestCase):

    def test_agents(self):
        all_agents = db_mgr.agents()
        print(len(all_agents))
        self.assertEqual(548, len(all_agents))

    def test_charges_by_agent(self):
        all_charges = db_mgr.charges_by_agent(355)
        self.assertEqual(6, len(all_charges))


class AuthModelTestCase(BaseTestCase):

    def test_setrole(self):
        admin = User.query.filter(User.name == 'admin').one_or_none()
        admin.set_role("ADMIN")
        db.session.commit()
        admin = User.query.filter(User.name == 'admin').one_or_none()
        self.assertTrue(admin is not None)
        self.assertTrue(admin.has_role("ADMIN"))
        self.assertFalse(admin.has_role("SERVICE"))

    def test_setrole_valueerror(self):
        admin = User(email='me@nowhere.org', name='me', login='me', password='me', role='admin')
        with self.assertRaises(ValueError) as ve:
            admin.set_role("NOSUCHROLE")