Commit 7b74daf659f0419bcb61e35717ee33150d71e22a
1 parent
a5a365e8
Exists in
master
and in
4 other branches
New User role management
Showing
2 changed files
with
65 additions
and
1 deletions
Show diff stats
app/auth/models.py
1 | 1 | from flask_login import UserMixin |
2 | 2 | from app.models import db |
3 | 3 | |
4 | +# | |
5 | +# Roles | |
6 | +# | |
7 | + | |
8 | +ADMIN = 40 | |
9 | +SERVICE = 30 | |
10 | +AGENT = 10 | |
11 | +PUBLIC = 0 | |
12 | + | |
13 | +_roleToName = { | |
14 | + ADMIN: 'ADMIN', | |
15 | + SERVICE: 'SERVICE', | |
16 | + AGENT: 'AGENT', | |
17 | + PUBLIC: 'PUBLIC', | |
18 | +} | |
19 | +_nameToRole = { | |
20 | + 'ADMIN': ADMIN, | |
21 | + 'SERVICE': SERVICE, | |
22 | + 'AGENT': AGENT, | |
23 | + 'PUBLIC': PUBLIC, | |
24 | +} | |
25 | + | |
26 | + | |
27 | +def _checkRole(role): | |
28 | + if isinstance(role, int): | |
29 | + rv = role | |
30 | + elif str(role) == role: | |
31 | + role = role.upper() | |
32 | + if role not in _nameToRole: | |
33 | + raise ValueError("Unknown role: %r" % role) | |
34 | + rv = _nameToRole[role] | |
35 | + else: | |
36 | + raise TypeError("Role not an integer or a valid string: %r" % role) | |
37 | + return rv | |
38 | + | |
4 | 39 | |
5 | 40 | class User(UserMixin, db.Model): |
6 | 41 | id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy |
... | ... | @@ -8,7 +43,14 @@ class User(UserMixin, db.Model): |
8 | 43 | name = db.Column(db.String(100)) |
9 | 44 | login = db.Column(db.String(100), unique=True) |
10 | 45 | password = db.Column(db.String(100)) |
46 | + role = db.Column(db.Integer) | |
11 | 47 | |
12 | 48 | def __repr__(self): |
13 | 49 | return "i: {}, n: {}, e: {}, l: {}".format(self.id, self.name, self.email, self.login) |
14 | 50 | |
51 | + def setRole(self, role): | |
52 | + self.role = _checkRole(role) | |
53 | + | |
54 | + def hasRole(self, role): | |
55 | + role = _checkRole(role) | |
56 | + return role == self.role | ... | ... |
tests/backend_tests.py
1 | 1 | import unittest |
2 | 2 | from pdc_config import TestConfig |
3 | -from app import create_app, db_mgr | |
3 | +from app import create_app, db_mgr, db | |
4 | +from app.auth.models import User | |
4 | 5 | |
5 | 6 | |
6 | 7 | class BaseTestCase(unittest.TestCase): |
... | ... | @@ -9,6 +10,10 @@ class BaseTestCase(unittest.TestCase): |
9 | 10 | self.app = create_app(TestConfig) |
10 | 11 | self.app_context = self.app.app_context() |
11 | 12 | self.app_context.push() |
13 | + db.create_all() | |
14 | + admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') | |
15 | + db.session.add(admin) | |
16 | + db.session.commit() | |
12 | 17 | |
13 | 18 | def tearDown(self): |
14 | 19 | self.app_context.pop() |
... | ... | @@ -27,3 +32,20 @@ class DbMgrTestCase(BaseTestCase): |
27 | 32 | def test_charges_by_agent(self): |
28 | 33 | all_charges = db_mgr.charges_by_agent(355) |
29 | 34 | self.assertEqual(6, len(all_charges)) |
35 | + | |
36 | + | |
37 | +class AuthModelTestCase(BaseTestCase): | |
38 | + | |
39 | + def test_setrole(self): | |
40 | + admin = User.query.filter(User.name == 'admin').one_or_none() | |
41 | + admin.setRole("ADMIN") | |
42 | + db.session.commit() | |
43 | + admin = User.query.filter(User.name == 'admin').one_or_none() | |
44 | + self.assertTrue(admin is not None) | |
45 | + self.assertTrue(admin.hasRole("ADMIN")) | |
46 | + self.assertFalse(admin.hasRole("SERVICE")) | |
47 | + | |
48 | + def test_setrole_valueerror(self): | |
49 | + admin = User(email='me@nowhere.org', name='me', login='me', password='me') | |
50 | + with self.assertRaises(ValueError) as ve: | |
51 | + admin.setRole("NOSUCHROLE") | ... | ... |