Skip to content

Commit f808c6c

Browse files
fix(UserApi): Fixed pre_update issue (#2347)
* fix(userapi): fixed pre_update issue * feat(test): adding unittests for the fix * feat(test): adding unittests for the fix
1 parent 3cc9b96 commit f808c6c

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

flask_appbuilder/security/sqla/apis/user/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ class UserApi(ModelRestApi):
6565
add_model_schema = UserPostSchema()
6666
edit_model_schema = UserPutSchema()
6767

68-
def pre_update(self, item):
68+
def pre_update(self, item, data):
6969
item.changed_on = datetime.now()
7070
item.changed_by_fk = g.user.id
71-
if item.password:
71+
if "password" in data and data["password"]:
7272
item.password = generate_password_hash(
73-
password=item.password,
73+
password=data["password"],
7474
method=self.appbuilder.get_app.config.get(
7575
"FAB_PASSWORD_HASH_METHOD", "scrypt"
7676
),
@@ -220,7 +220,7 @@ def put(self, pk):
220220
if "roles" in item.keys():
221221
model.roles = roles
222222

223-
self.pre_update(model)
223+
self.pre_update(model, item)
224224
self.datamodel.edit(model, raise_exception=True)
225225
return self.response(
226226
200,

tests/test_security_api.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def test_create_user_with_invalid_role(self):
313313
token = self.login(client, USERNAME_ADMIN, PASSWORD_ADMIN)
314314

315315
uri = "api/v1/security/users/"
316+
316317
create_user_payload = {
317318
"active": True,
318319
"email": "fab@test_create_user_1.com",
@@ -393,6 +394,72 @@ def test_edit_user(self):
393394
self.session.delete(r)
394395
self.session.commit()
395396

397+
def test_edit_user_check_password(self):
398+
client = self.app.test_client()
399+
token = self.login(client, USERNAME_ADMIN, PASSWORD_ADMIN)
400+
role_id = self.appbuilder.sm.find_role("Admin").id
401+
uri = "api/v1/security/users/"
402+
create_user_payload = {
403+
"active": True,
404+
"email": "[email protected]",
405+
"first_name": "test",
406+
"last_name": "test",
407+
"password": "password",
408+
"roles": [role_id],
409+
"username": "test_password",
410+
}
411+
rv = self.auth_client_post(client, token, uri, create_user_payload)
412+
self.assertEqual(rv.status_code, 201)
413+
414+
user = self.appbuilder.sm.find_user(username="test_password")
415+
self.assertIsNotNone(user)
416+
user_id = user.id
417+
old_password_hash = user.password
418+
419+
update_payload = {"username": "test_password_renamed"}
420+
rv = self.auth_client_put(client, token, f"{uri}{user_id}", update_payload)
421+
self.assertEqual(rv.status_code, 200)
422+
423+
updated_user = self.appbuilder.sm.find_user(username="test_password_renamed")
424+
self.assertIsNotNone(updated_user)
425+
self.assertEqual(updated_user.password, old_password_hash)
426+
427+
self.session.delete(updated_user)
428+
self.session.commit()
429+
430+
def test_edit_user_change_password(self):
431+
client = self.app.test_client()
432+
token = self.login(client, USERNAME_ADMIN, PASSWORD_ADMIN)
433+
role_id = self.appbuilder.sm.find_role("Admin").id
434+
uri = "api/v1/security/users/"
435+
436+
create_user_payload = {
437+
"active": True,
438+
"email": "[email protected]",
439+
"first_name": "test",
440+
"last_name": "test",
441+
"password": "initial_password",
442+
"roles": [role_id],
443+
"username": "test_change_password",
444+
}
445+
rv = self.auth_client_post(client, token, uri, create_user_payload)
446+
self.assertEqual(rv.status_code, 201)
447+
448+
user = self.appbuilder.sm.find_user(username="test_change_password")
449+
self.assertIsNotNone(user)
450+
user_id = user.id
451+
old_password_hash = user.password
452+
453+
update_payload = {"password": "new_secure_password"}
454+
rv = self.auth_client_put(client, token, f"{uri}{user_id}", update_payload)
455+
self.assertEqual(rv.status_code, 200)
456+
457+
updated_user = self.appbuilder.sm.find_user(username="test_change_password")
458+
self.assertIsNotNone(updated_user)
459+
self.assertNotEqual(updated_user.password, old_password_hash)
460+
461+
self.appbuilder.sm.del_register_user(updated_user)
462+
396463
def test_delete_user(self):
397464
client = self.app.test_client()
398465
token = self.login(client, USERNAME_ADMIN, PASSWORD_ADMIN)

0 commit comments

Comments
 (0)