Skip to content

Commit f3629d3

Browse files
committed
fixes for updating password and unit tests
1 parent 84440ef commit f3629d3

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lib/pbench/server/database/models/users.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from sqlalchemy import Column, Integer, String, DateTime, LargeBinary
44
from pbench.server.database.database import Database
55
from sqlalchemy.orm import relationship
6-
from sqlalchemy.orm import validates
76

87

98
class User(Database.Base):
@@ -17,6 +16,7 @@ class User(Database.Base):
1716
last_name = Column(String(255), unique=False, nullable=False)
1817
password = Column(LargeBinary(500), nullable=False)
1918
registered_on = Column(DateTime, nullable=False)
19+
bcrypt_log_rounds = Column(Integer, nullable=False)
2020
email = Column(String(255), unique=True, nullable=False)
2121
auth_tokens = relationship("ActiveTokens", backref="users")
2222

@@ -25,6 +25,7 @@ def __init__(self, bcrypt_log_rounds, **kwargs):
2525
self.username = kwargs.get("username")
2626
self.first_name = kwargs.get("first_name")
2727
self.last_name = kwargs.get("last_name")
28+
self.bcrypt_log_rounds = bcrypt_log_rounds
2829
self.password = generate_password_hash(
2930
kwargs.get("password"), bcrypt_log_rounds
3031
)
@@ -63,14 +64,6 @@ def add(self):
6364
Database.db_session.rollback()
6465
raise e
6566

66-
# Prevent update on "registered_on" field
67-
@validates("registered_on")
68-
def validates_user_registration(self, key, value):
69-
if self.registered_on: # Field already exists
70-
raise ValueError("registered_on cannot be modified.")
71-
72-
return value
73-
7467
def update(self, **kwargs):
7568
"""
7669
Update the current user object with given keyword arguments
@@ -81,6 +74,13 @@ def update(self, **kwargs):
8174
# Insert the auth token
8275
self.auth_tokens.append(value)
8376
Database.db_session.add(value)
77+
elif key == "password":
78+
setattr(
79+
self, key, generate_password_hash(value, self.bcrypt_log_rounds)
80+
)
81+
# Prevent update on "registered_on" and "bcrypt_log_rounds" fields
82+
elif key in ["registered_on", "bcrypt_log_rounds"]:
83+
continue
8484
else:
8585
setattr(self, key, value)
8686
Database.db_session.commit()

lib/pbench/test/unit/server/test_user_auth.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import datetime
23
from pbench.server.database.models.users import User
34
from pbench.server.database.models.active_tokens import ActiveTokens
45
from pbench.server.database.database import Database
@@ -283,17 +284,33 @@ def test_update_user(client, server_config):
283284
data_login = response.json
284285
assert data_login["status"] == "success"
285286
assert data_login["message"] == "Successfully logged in."
287+
288+
new_registration_time = datetime.datetime.now()
286289
response = client.put(
287290
f"{server_config.rest_uri}/user/username",
288-
json={"first_name": "newname"},
291+
json={"registered_on": new_registration_time, "first_name": "newname"},
289292
headers=dict(Authorization="Bearer " + data_login["auth_token"]),
290293
)
291294
data = response.json
292295
assert data["status"] == "success"
293296
assert data["data"] is not None
294297
assert data["data"]["first_name"] == "newname"
298+
# registered_on is not updatable
299+
assert data["data"]["registered_on"] != new_registration_time
295300
assert response.status_code == 200
296301

302+
# Test password update
303+
response = client.put(
304+
f"{server_config.rest_uri}/user/username",
305+
json={"password": "newpass"},
306+
headers=dict(Authorization="Bearer " + data_login["auth_token"]),
307+
)
308+
time.sleep(1)
309+
response = login_user(client, server_config, "username", "newpass")
310+
data_login = response.json
311+
assert data_login["status"] == "success"
312+
assert data_login["message"] == "Successfully logged in."
313+
297314
@staticmethod
298315
def test_malformed_auth_token(client, server_config):
299316
""" Test for user status for malformed auth token"""

0 commit comments

Comments
 (0)