Esempio n. 1
0
 def test_set_roles_some_in_user_all_in_db(self):
     role_ids = TestUserRolesDatabase.add_roles_to_db(3)
     x = role_ids.pop()
     user = User('username', 'password')
     user.set_roles(role_ids)
     user.set_roles({x - 1, x})
     self.assertUserRolesEqual(user, {x - 1, x})
Esempio n. 2
0
 def test_as_json(self):
     role_ids = TestUserRolesDatabase.add_roles_to_db(3)
     user = User('username', 'password')
     db.session.add(user)
     user.set_roles(role_ids)
     user.login('192.168.0.1')
     user.login('192.168.0.2')
     user_json = user.as_json()
     expected = {
         "id":
         1,
         "username":
         '******',
         "active":
         True,
         "roles": [{
             'name': role,
             'description': '',
             'resources': []
         } for role in ['role1', 'role2', 'role3']]
     }
     self.assertSetEqual(set(user_json.keys()), set(expected.keys()))
     self.assertEqual(user_json['username'], 'username')
     self.assertEqual(user_json['active'], True)
     for role in user_json['roles']:
         self.assertIn('id', role)
         self.assertIn(role['name'], ['role1', 'role2', 'role3'])
         self.assertListEqual(role['resources'], [])
         self.assertEqual(role['description'], '')
Esempio n. 3
0
 def test_has_role_user_with_role(self):
     role_ids = TestUserRolesDatabase.add_roles_to_db(3)
     user = User('username', 'password')
     user.set_roles(role_ids)
     db.session.commit()
     for role in role_ids:
         self.assertTrue(user.has_role(role))
Esempio n. 4
0
 def test_set_roles_none_in_user_some_in_db(self):
     role_ids = TestUserRolesDatabase.add_roles_to_db(3)
     user = User('username', 'password')
     added_roles = set(role_ids)
     added_roles.add(30)
     user.set_roles(added_roles)
     db.session.commit()
     self.assertUserRolesEqual(user, set(role_ids))
Esempio n. 5
0
 def test_set_roles_to_none_with_some_in_user(self):
     role_ids = TestUserRolesDatabase.add_roles_to_db(3)
     user = User('username', 'password')
     user.set_roles(role_ids)
     db.session.commit()
     user.set_roles([])
     db.session.commit()
     self.assertUserRolesEqual(user, set())
Esempio n. 6
0
    def test_as_json_with_user_history(self):
        role_ids = TestUserRolesDatabase.add_roles_to_db(3)
        user = User('username', 'password')
        db.session.add(user)
        user.set_roles(role_ids)
        user.login('192.168.0.1')
        first_login_timestamp = datetime.utcnow()
        user.login('192.168.0.2')
        second_login_timestamp = datetime.utcnow()
        user_json = user.as_json(with_user_history=True)
        expected = {
            "id":
            1,
            "username":
            '******',
            "roles": [{
                'name': role,
                'description': '',
                'resources': []
            } for role in ['role1', 'role2', 'role3']],
            "active":
            True,
            "last_login_at":
            first_login_timestamp,
            "current_login_at":
            second_login_timestamp,
            "last_login_ip":
            '192.168.0.1',
            "current_login_ip":
            '192.168.0.2',
            "login_count":
            2
        }
        self.assertSetEqual(set(user_json.keys()), set(expected.keys()))
        for key in [
                'username', 'active', 'last_login_ip', 'current_login_ip',
                'login_count'
        ]:
            self.assertEqual(user_json[key], expected[key],
                             '{} for user\'s json in incorrect'.format(key))

        self.assertAlmostEqual(user_json['last_login_at'],
                               first_login_timestamp,
                               delta=timedelta(milliseconds=100))
        self.assertAlmostEqual(user_json['current_login_at'],
                               second_login_timestamp,
                               delta=timedelta(milliseconds=100))
        for role in user_json['roles']:
            self.assertIn('id', role)
            self.assertIn(role['name'], ['role1', 'role2', 'role3'])
            self.assertListEqual(role['resources'], [])
            self.assertEqual(role['description'], '')
Esempio n. 7
0
 def test_roles_as_json_with_users_one_user(self):
     role = Role('role1')
     db.session.add(role)
     db.session.commit()
     user = User('username', 'password')
     db.session.add(user)
     user.set_roles([role.id])
     expected = {
         'name': 'role1',
         'description': '',
         'resources': [],
         'users': ['username']
     }
     role_json = role.as_json(with_users=True)
     role_json.pop('id')
     self.assertDictEqual(role_json, expected)
Esempio n. 8
0
 def test_has_role_user_without_role(self):
     role_ids = TestUserRolesDatabase.add_roles_to_db(3)
     user = User('username', 'password')
     user.set_roles(role_ids)
     self.assertFalse(user.has_role('invalid'))
Esempio n. 9
0
 def test_set_roles_none_in_user_all_in_db(self):
     role_ids = TestUserRolesDatabase.add_roles_to_db(3)
     user = User('username', 'password')
     user.set_roles(role_ids)
     self.assertUserRolesEqual(user, set(role_ids))
Esempio n. 10
0
 def test_set_roles_to_none_with_none_in_user(self):
     user = User('username', 'password')
     user.set_roles([])
     self.assertUserRolesEqual(user, set())
Esempio n. 11
0
 def test_set_roles_none_in_user_none_in_db(self):
     user = User('username', 'password')
     user.set_roles([10, 20, 30])
     self.assertUserRolesEqual(user, set())