backend_tests.py 3.24 KB
import os
import sys
import unittest
from shutil import copyfile

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):
        # initialise app
        self.app = create_app(TestConfig)

        # copy resource  demo db to test file
        appdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
        sqlite_file_name = os.path.abspath(os.path.join(appdir, 'resources', 'lesia-btp.sqlite'))
        if not os.path.isdir(self.app.instance_path):
            os.mkdir(self.app.instance_path)
        self.db_path = os.path.join(self.app.instance_path, 'test.db')
        copyfile(sqlite_file_name, self.db_path)

        # force db path to newly create file
        self.app.config.update(
            SQLALCHEMY_DATABASE_URI='sqlite:///' + self.db_path
        )

        # update flask context
        self.app_context = self.app.app_context()
        self.app_context.push()

    def tearDown(self):
        if os.path.isfile(self.db_path):
            os.remove(self.db_path)
        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(17, len(all_charges))

    def test_charges_by_agent_stacked(self):
        stacked_charges = db_mgr.charges_by_agent_stacked(60)
        # Waiting for 17 periods + headers line
        self.assertEqual(18, len(stacked_charges))

    def test_charges_by_project_stacked(self):
        stacked_charges = db_mgr.charges_by_project_stacked(60)
        # Waiting for 17 periods + headers line
        self.assertEqual(18, len(stacked_charges))


class AuthModelTestCase(BaseTestCase):

    def skip_if_no_sqlitememory(self):
        if 'memory' not in self.app.config['SQLALCHEMY_DATABASE_URI']:
            self.skipTest("Needs in memory sqlite")

    def get_admin(self):
        return User.query.filter(User.name == 'admin').one()

    def setUp(self):
        BaseTestCase.setUp(self)
        self.skip_if_no_sqlitememory()
        db.create_all()
        admin = User(email='admin@nowhere.org', name='admin', login='admin', role='admin')
        db.session.add(admin)
        db.session.commit()

    def test_in_memory(self):
        self.app.logger.info("In memory Sqlite DB for tests")
        self.assertTrue(True)

    def test_setrole(self):
        admin = self.get_admin()
        admin.set_role("ADMIN")
        db.session.commit()
        admin = self.get_admin()
        self.assertTrue(admin is not None)
        self.assertTrue(admin.has_role("ADMIN"))
        self.assertFalse(admin.has_role("SERVICE"))

    def test_setrole_valueerror(self):
        admin = self.get_admin()
        with self.assertRaises(ValueError) as ve:
            admin.set_role("NOSUCHROLE")

    def test_setcheckpassword(self):
        admin = self.get_admin()
        admin.set_password("hahaha")
        db.session.commit()
        admin2 = self.get_admin()
        self.assertTrue(admin2.check_password("hahaha"))