Skip to content

Commit

Permalink
Fix: Allow to create more than one api key if the user has more than one
Browse files Browse the repository at this point in the history
  • Loading branch information
acasajus committed Jul 25, 2023
1 parent 04d92b7 commit 562d753
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/dashboard/views/api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class NewApiKeyForm(FlaskForm):

def clean_up_unused_or_old_api_keys(user_id: int):
total_keys = ApiKey.filter_by(user_id=user_id).count()
if total_keys <= config.MAX_API_KEYS:
return
# Remove oldest unused
for api_key in (
ApiKey.filter_by(user_id=user_id, last_used=None)
Expand Down
13 changes: 12 additions & 1 deletion tests/dashboard/test_api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,25 @@ def test_create_delete_api_key(flask_client):
assert ApiKey.filter(ApiKey.user_id == user.id).count() == 1
assert api_key.name == "for test"

# create second api_key
create_r = flask_client.post(
url_for("dashboard.api_key"),
data={"form-name": "create", "name": "for test 2"},
follow_redirects=True,
)
assert create_r.status_code == 200
api_key_2 = ApiKey.filter_by(user_id=user.id).order_by(ApiKey.id.desc()).first()
assert ApiKey.filter(ApiKey.user_id == user.id).count() == 2
assert api_key_2.name == "for test 2"

# delete api_key
delete_r = flask_client.post(
url_for("dashboard.api_key"),
data={"form-name": "delete", "api-key-id": api_key.id},
follow_redirects=True,
)
assert delete_r.status_code == 200
assert ApiKey.count() == nb_api_key
assert ApiKey.count() == nb_api_key + 1


def test_delete_all_api_keys(flask_client):
Expand Down

0 comments on commit 562d753

Please sign in to comment.