Example #1
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(timestamp_to_datetime(user_json['last_login_at']), first_login_timestamp,
                               delta=timedelta(milliseconds=100))
        self.assertAlmostEqual(timestamp_to_datetime(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'], '')
Example #2
0
 def test_read_user(self):
     user = User('username', 'asdfghjkl;')
     db.session.add(user)
     db.session.commit()
     response = self.get_with_status_check('/api/users/{}'.format(user.id), headers=self.headers,
                                           status_code=SUCCESS)
     self.assertDictEqual(response, user.as_json())
 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'], '')
Example #4
0
 def test_read_user(self):
     user = User('username', 'asdfghjkl;')
     db.session.add(user)
     db.session.commit()
     response = self.get_with_status_check('/api/users/{}'.format(user.id), headers=self.headers,
                                           status_code=SUCCESS)
     self.assertDictEqual(response, user.as_json())
Example #5
0
 def test_update_user_password_only(self):
     user = User('username', 'asdfghjkl;')
     db.session.add(user)
     db.session.commit()
     data = {'id': user.id, 'old_password': '******', 'password': '******'}
     response = self.put_with_status_check('/api/users', headers=self.headers, content_type='application/json',
                                           data=json.dumps(data), status_code=SUCCESS)
     self.assertDictEqual(response, user.as_json())
     self.assertTrue(user.verify_password('changed!'))
Example #6
0
 def test_update_username(self):
     user = User('username', 'whisperDieselEngine')
     db.session.add(user)
     db.session.commit()
     data = {'id': user.id, 'username': '******'}
     response = self.put_with_status_check('/api/users', headers=self.headers, content_type='application/json',
                                           data=json.dumps(data), status_code=SUCCESS)
     self.assertEqual(user.username, 'new_name')
     self.assertDictEqual(response, user.as_json())
Example #7
0
 def test_update_user_password_only(self):
     user = User('username', 'asdfghjkl;')
     db.session.add(user)
     db.session.commit()
     data = {'id': user.id, 'old_password': '******', 'password': '******'}
     response = self.put_with_status_check('/api/users', headers=self.headers, content_type='application/json',
                                           data=json.dumps(data), status_code=SUCCESS)
     self.assertDictEqual(response, user.as_json())
     self.assertTrue(user.verify_password('changed!'))
Example #8
0
 def test_update_username(self):
     user = User('username', 'whisperDieselEngine')
     db.session.add(user)
     db.session.commit()
     data = {'id': user.id, 'username': '******'}
     response = self.put_with_status_check('/api/users', headers=self.headers, content_type='application/json',
                                           data=json.dumps(data), status_code=SUCCESS)
     self.assertEqual(user.username, 'new_name')
     self.assertDictEqual(response, user.as_json())
Example #9
0
 def put_patch_update_user_with_roles(self, verb):
     send_func = self.put_with_status_check if verb == 'put' else self.patch_with_status_check
     role = Role('role1')
     db.session.add(role)
     db.session.commit()
     user = User('username', 'supersecretshhhhh')
     db.session.add(user)
     db.session.commit()
     data = {'id': user.id, 'roles': [{'id': role.id}]}
     response = send_func('/api/users', headers=self.headers, content_type='application/json',
                          data=json.dumps(data), status_code=SUCCESS)
     self.assertDictEqual(response, user.as_json())
     self.assertSetEqual({role.name for role in user.roles}, {'role1'})
Example #10
0
 def put_patch_update_user_with_roles(self, verb):
     send_func = self.put_with_status_check if verb == 'put' else self.patch_with_status_check
     role = Role('role1')
     db.session.add(role)
     db.session.commit()
     user = User('username', 'supersecretshhhhh')
     db.session.add(user)
     db.session.commit()
     data = {'id': user.id, 'roles': [{'id': role.id}]}
     response = send_func('/api/users', headers=self.headers, content_type='application/json',
                          data=json.dumps(data), status_code=SUCCESS)
     self.assertDictEqual(response, user.as_json())
     self.assertSetEqual({role.name for role in user.roles}, {'role1'})