Skip to content

Commit

Permalink
Rename "reset_api_key" to "generate_api_key"
Browse files Browse the repository at this point in the history
This method is now also used to generate an initial key for users who do not yet have an API key.
  • Loading branch information
tillprochaska committed May 1, 2024
1 parent b997ef2 commit d42f7a2
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion aleph/model/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def check_password(self, secret):
digest = self.password_digest or ""
return check_password_hash(digest, secret)

def reset_api_key(self):
def generate_api_key(self):
"""Resets the API key"""
self.api_key = make_token()

Expand Down
2 changes: 1 addition & 1 deletion aleph/tests/test_role_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_remove_role(self):

def test_role_by_api_key(self):
role_ = self.create_user()
role_.reset_api_key()
role_.generate_api_key()
db.session.add(role_)
db.session.commit()
assert role_.api_key is not None
Expand Down
10 changes: 5 additions & 5 deletions aleph/tests/test_roles_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_view_api_key(self):
assert res.status_code == 200
assert res.json["has_api_key"] is False

role.reset_api_key()
role.generate_api_key()
db.session.add(role)
db.session.commit()

Expand Down Expand Up @@ -201,8 +201,8 @@ def test_create_on_existing_email(self):

self.assertEqual(res.status_code, 409)

def test_reset_api_key_auth(self):
url = f"/api/2/roles/{self.rolex.id}/reset_api_key"
def test_generate_api_key_auth(self):
url = f"/api/2/roles/{self.rolex.id}/generate_api_key"

# Anonymous request
res = self.client.post(url)
Expand All @@ -213,11 +213,11 @@ def test_reset_api_key_auth(self):
res = self.client.post(url, headers=headers)
self.assertEqual(res.status_code, 403)

def test_reset_api_key(self):
def test_generate_api_key(self):
role, headers = self.login()
old_key = role.api_key

url = f"/api/2/roles/{role.id}/reset_api_key"
url = f"/api/2/roles/{role.id}/generate_api_key"
res = self.client.post(url, headers=headers)
new_key = res.json["api_key"]

Expand Down
2 changes: 1 addition & 1 deletion aleph/tests/test_view_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setUp(self):
super().setUp()
self.role = self.create_user(email="[email protected]")
self.role.set_password("12345678")
self.role.reset_api_key()
self.role.generate_api_key()

self.other_role = self.create_user(
foreign_id="other",
Expand Down
6 changes: 3 additions & 3 deletions aleph/views/roles_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ def update(id):
return RoleSerializer.jsonify(role)


@blueprint.route("/api/2/roles/<int:id>/reset_api_key", methods=["POST"])
def reset_api_key(id):
@blueprint.route("/api/2/roles/<int:id>/generate_api_key", methods=["POST"])
def generate_api_key(id):
"""Reset the role’s API key.
---
post:
Expand Down Expand Up @@ -270,7 +270,7 @@ def reset_api_key(id):
role = obj_or_404(Role.by_id(id))
require(request.authz.can_write_role(role.id))

role.reset_api_key()
role.generate_api_key()
db.session.add(role)
db.session.commit()
update_role(role)
Expand Down
2 changes: 1 addition & 1 deletion ui/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export {
fetchRole,
suggestRoles,
updateRole,
resetApiKey,
generateApiKey,
} from './roleActions';
export { createAlert, deleteAlert, queryAlerts } from './alertActions';
export { queryNotifications } from './notificationActions';
Expand Down
6 changes: 3 additions & 3 deletions ui/src/actions/roleActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export const updateRole = asyncActionCreator(
{ name: 'UPDATE_ROLE' }
);

export const resetApiKey = asyncActionCreator(
export const generateApiKey = asyncActionCreator(
(role) => async () => {
const response = await endpoint.post(`roles/${role.id}/reset_api_key`);
const response = await endpoint.post(`roles/${role.id}/generate_api_key`);
return { id: role.id, data: response.data };
},
{ name: 'RESET_API_KEY' }
{ name: 'GENERATE_API_KEY' }
);
4 changes: 2 additions & 2 deletions ui/src/components/Settings/ApiKeySettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSelector, useDispatch } from 'react-redux';
import { Button, Intent, Dialog, DialogBody, Card } from '@blueprintjs/core';
import { ClipboardInput } from 'components/common';
import { selectCurrentRole } from 'selectors';
import { resetApiKey } from 'actions';
import { generateApiKey as generateApiKeyAction } from 'actions';
import { FormattedMessage } from 'react-intl';

const GENERATE_MESSAGE = (
Expand Down Expand Up @@ -79,7 +79,7 @@ export default function ApiKeySettings() {
const [{ state, apiKey }, dispatch] = useReducer(reducer, { state: 'START' });

const generateApiKey = async () => {
const { data } = await reduxDispatch(resetApiKey(role));
const { data } = await reduxDispatch(generateApiKeyAction(role));
return data.api_key;
};

Expand Down
4 changes: 2 additions & 2 deletions ui/src/reducers/roles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createReducer } from 'redux-act';

import { queryRoles, fetchRole, updateRole, resetApiKey } from 'actions';
import { queryRoles, fetchRole, updateRole, generateApiKey } from 'actions';
import {
resultObjects,
objectLoadStart,
Expand All @@ -20,7 +20,7 @@ export default createReducer(
objectLoadComplete(state, id, data),
[updateRole.COMPLETE]: (state, { id, data }) =>
objectLoadComplete(state, id, data),
[resetApiKey.COMPLETE]: (state, { id, data }) =>
[generateApiKey.COMPLETE]: (state, { id, data }) =>
objectLoadComplete(state, id, { ...data, api_key: null }),
},
initialState
Expand Down

0 comments on commit d42f7a2

Please sign in to comment.