Skip to content

Commit

Permalink
feat: add "active" field to output and align on field name for username
Browse files Browse the repository at this point in the history
- update tests to use username
- add some new checks on user.active
  • Loading branch information
pieterlukasse committed Nov 6, 2024
1 parent 24b357c commit baef648
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
8 changes: 5 additions & 3 deletions fence/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def create_user():
Returns a json object
"""
username = request.get_json().get("name", None)
username = request.get_json().get("username", None)
role = request.get_json().get("role", None)
email = request.get_json().get("email", None)
display_name = request.get_json().get("display_name", None)
Expand Down Expand Up @@ -110,11 +110,13 @@ def update_user(username):
Returns a json object
"""
name = request.get_json().get("name", None)
new_username = request.get_json().get("username", None)
role = request.get_json().get("role", None)
email = request.get_json().get("email", None)
return jsonify(
admin.update_user(current_app.scoped_session(), username, role, email, name)
admin.update_user(
current_app.scoped_session(), username, role, email, new_username
)
)


Expand Down
6 changes: 3 additions & 3 deletions fence/resources/admin/admin_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_all_users(current_session):
users_names = []
for user in users:
new_user = {}
new_user["name"] = user.username
new_user["username"] = user.username
if user.is_admin:
new_user["role"] = "admin"
else:
Expand Down Expand Up @@ -124,7 +124,7 @@ def create_user(
except NotFound:
logger.debug(f"User not found for: {username}. Checking again ignoring case...")
user_list = [
user["name"].upper() for user in get_all_users(current_session)["users"]
user["username"].upper() for user in get_all_users(current_session)["users"]
]
if username.upper() in user_list:
logger.debug(f"User already exists for: {username}")
Expand Down Expand Up @@ -168,7 +168,7 @@ def create_user(
def update_user(current_session, username, role, email, new_name):
usr = us.get_user(current_session, username)
user_list = [
user["name"].upper() for user in get_all_users(current_session)["users"]
user["username"].upper() for user in get_all_users(current_session)["users"]
]
if (
new_name
Expand Down
1 change: 1 addition & 0 deletions fence/resources/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def get_user_info(current_session, username):
"phone_number": user.phone_number,
"email": user.email,
"is_admin": user.is_admin,
"active": user.active,
"role": role,
"project_access": dict(user.project_access),
"certificates_uploaded": [],
Expand Down
5 changes: 3 additions & 2 deletions tests/admin/test_admin_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_get_user(db_session, awg_users):
assert "test_group_1" in info["groups"]
assert "test_group_2" in info["groups"]
assert info["message"] == ""
assert info["email"] == None
assert info["email"] is None
assert info["certificates_uploaded"] == []
assert info["resources_granted"] == []
assert info["project_access"]["phs_project_1"] == ["read"]
Expand All @@ -34,6 +34,7 @@ def test_create_user(db_session, oauth_client):
assert user.phone_number is None
assert user.identity_provider is None
assert len(user.tags) == 0
assert user.active == True


def test_create_user_with_all_fields_set(db_session, oauth_client):
Expand Down Expand Up @@ -133,7 +134,7 @@ def test_create_already_existing_user(db_session, awg_users):

def test_get_all_users(db_session, awg_users):
user_list = adm.get_all_users(db_session)
user_name_list = [item["name"] for item in user_list["users"]]
user_name_list = [item["username"] for item in user_list["users"]]
assert "awg_user" in user_name_list
assert "awg_user_2" in user_name_list

Expand Down
24 changes: 15 additions & 9 deletions tests/admin/test_admin_users_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def test_get_user(
assert r.status_code == 200
# should at least have the users added from above (may have more from other tests)
assert len(r.json["users"]) >= 4
usernames = [user["name"] for user in r.json["users"]]
usernames = [user["username"] for user in r.json["users"]]
assert "test_a" in usernames
assert "test_b" in usernames
assert "test_amazing_user_with_an_fancy_but_extremely_long_name" in usernames
Expand All @@ -274,7 +274,11 @@ def test_post_user(client, admin_user, encoded_admin_jwt, db_session):
"Content-Type": "application/json",
},
data=json.dumps(
{"name": "new_test_user", "role": "user", "email": "new_test_user@fake.com"}
{
"username": "new_test_user",
"role": "user",
"email": "new_test_user@fake.com",
}
),
)
assert r.status_code == 200
Expand All @@ -284,10 +288,12 @@ def test_post_user(client, admin_user, encoded_admin_jwt, db_session):
assert r.json["email"] == "new_test_user@fake.com"
assert r.json["project_access"] == {}
assert r.json["groups"] == []
assert r.json["active"] == True
new_test_user = db_session.query(User).filter_by(username="new_test_user").one()
assert new_test_user.username == "new_test_user"
assert new_test_user.is_admin == False
assert new_test_user.email == "new_test_user@fake.com"
assert new_test_user.active == True


def test_post_user_no_fields_defined(client, admin_user, encoded_admin_jwt, db_session):
Expand Down Expand Up @@ -341,7 +347,7 @@ def test_post_user_only_username_defined(
"Authorization": "Bearer " + encoded_admin_jwt,
"Content-Type": "application/json",
},
data=json.dumps({"name": "new_test_user"}),
data=json.dumps({"username": "new_test_user"}),
)
assert r.status_code == 200
assert r.json["username"] == "new_test_user"
Expand All @@ -366,7 +372,7 @@ def test_post_user_already_exists(
"Authorization": "Bearer " + encoded_admin_jwt,
"Content-Type": "application/json",
},
data=json.dumps({"name": "test_a"}),
data=json.dumps({"username": "test_a"}),
)
assert r.status_code == 400

Expand All @@ -392,7 +398,7 @@ def test_put_user_username(
},
data=json.dumps(
{
"name": "test_a_updated",
"username": "test_a_updated",
"role": "admin",
"email": "test_a_updated@fake.com",
}
Expand Down Expand Up @@ -422,7 +428,7 @@ def test_put_user_username_nonexistent(
"Authorization": "Bearer " + encoded_admin_jwt,
"Content-Type": "application/json",
},
data=json.dumps({"name": "test_nonexistent_updated"}),
data=json.dumps({"username": "test_nonexistent_updated"}),
)
assert r.status_code == 404
assert (
Expand All @@ -442,7 +448,7 @@ def test_put_user_username_already_exists(
"Authorization": "Bearer " + encoded_admin_jwt,
"Content-Type": "application/json",
},
data=json.dumps({"name": "test_b"}),
data=json.dumps({"username": "test_b"}),
)
assert r.status_code == 400
assert db_session.query(User).filter_by(username="test_a").one()
Expand All @@ -455,7 +461,7 @@ def test_put_user_username_try_delete_username(
"""PUT /users/<username>: [update_user] try to delete username"""
"""
This probably shouldn't be allowed. Conveniently, the code flow ends up
the same as though the user had not tried to update 'name' at all,
the same as though the user had not tried to update 'username' at all,
since they pass in None. Right now, this just returns a 200 without
updating anything or sending any message to the user. So the test has
been written to ensure this behavior, but maybe it should be noted that
Expand All @@ -467,7 +473,7 @@ def test_put_user_username_try_delete_username(
"Authorization": "Bearer " + encoded_admin_jwt,
"Content-Type": "application/json",
},
data=json.dumps({"name": None}),
data=json.dumps({"username": None}),
)
assert r.status_code == 200
user = db_session.query(User).filter_by(username="test_a").one()
Expand Down

0 comments on commit baef648

Please sign in to comment.