Blame view

tests/backend_tests.py 1.63 KB
506d2f02   hitier   Initialize unit t...
1
2
import unittest
from pdc_config import TestConfig
7b74daf6   hitier   New User role man...
3
4
from app import create_app, db_mgr, db
from app.auth.models import User
506d2f02   hitier   Initialize unit t...
5
6
7
8
9


class BaseTestCase(unittest.TestCase):
    def setUp(self):
        # configure data base
506d2f02   hitier   Initialize unit t...
10
11
12
        self.app = create_app(TestConfig)
        self.app_context = self.app.app_context()
        self.app_context.push()
9c7c1dfa   hitier   Fix test with new...
13
14
15
16
17
        # TODO: shall we always copy db from resources ?
        #db.create_all()
        #admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin')
        #db.session.add(admin)
        #db.session.commit()
506d2f02   hitier   Initialize unit t...
18
19
20
21
22
23

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

    def test_always_true(self):
        self.assertTrue(True)
1aeda847   hitier   Two db request wr...
24
25
26
27
28
29
30
31
32
33
34


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)
9c7c1dfa   hitier   Fix test with new...
35
        self.assertEqual(17, len(all_charges))
7b74daf6   hitier   New User role man...
36
37
38
39
40
41


class AuthModelTestCase(BaseTestCase):

    def test_setrole(self):
        admin = User.query.filter(User.name == 'admin').one_or_none()
42c07429   hitier   Default role valu...
42
        admin.set_role("ADMIN")
7b74daf6   hitier   New User role man...
43
44
45
        db.session.commit()
        admin = User.query.filter(User.name == 'admin').one_or_none()
        self.assertTrue(admin is not None)
42c07429   hitier   Default role valu...
46
47
        self.assertTrue(admin.has_role("ADMIN"))
        self.assertFalse(admin.has_role("SERVICE"))
7b74daf6   hitier   New User role man...
48
49

    def test_setrole_valueerror(self):
42c07429   hitier   Default role valu...
50
        admin = User(email='me@nowhere.org', name='me', login='me', password='me', role='admin')
7b74daf6   hitier   New User role man...
51
        with self.assertRaises(ValueError) as ve:
42c07429   hitier   Default role valu...
52
            admin.set_role("NOSUCHROLE")