Skip to content

Commit

Permalink
Using try except to catch errors + added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunandadadi committed Oct 26, 2023
1 parent 7e7dfc9 commit eaff3df
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
33 changes: 21 additions & 12 deletions endpoints/api/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
AdministerOrganizationPermission,
OrganizationMemberPermission,
)
from data.model import organization, user
from endpoints.api import (
ApiResource,
allow_if_superuser,
Expand Down Expand Up @@ -121,12 +122,16 @@ def put(self, robot_shortname):
"""
parent = get_authenticated_user()
create_data = request.get_json(silent=True) or {}
robot = model.create_user_robot(
robot_shortname,
parent,
create_data.get("description"),
create_data.get("unstructured_metadata"),
)

try:
robot = model.create_user_robot(
robot_shortname,
parent,
create_data.get("description"),
create_data.get("unstructured_metadata"),
)
except Exception as e:
raise request_error(message=str(e))
log_action(
"create_robot",
parent.username,
Expand Down Expand Up @@ -237,12 +242,16 @@ def put(self, orgname, robot_shortname):
permission = AdministerOrganizationPermission(orgname)
if permission.can() or allow_if_superuser():
create_data = request.get_json(silent=True) or {}
robot = model.create_org_robot(
robot_shortname,
orgname,
create_data.get("description"),
create_data.get("unstructured_metadata"),
)

try:
robot = model.create_org_robot(
robot_shortname,
orgname,
create_data.get("description"),
create_data.get("unstructured_metadata"),
)
except Exception as e:
raise request_error(message=str(e))
log_action(
"create_robot",
orgname,
Expand Down
28 changes: 28 additions & 0 deletions endpoints/api/test/test_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,31 @@ def test_retrieve_robots_token_permission(username, is_admin, with_permissions,
for robot in result.json["robots"]:
assert (robot.get("token") is not None) == is_admin
assert (robot.get("repositories") is not None) == (is_admin and with_permissions)


# @pytest.mark.parametrize(
# "endpoint, params",
# [
# (UserRobot, {"robot_shortname": "dtrobot"}),
# (OrgRobot, {"orgname": "buynlarge", "robot_shortname": "coolrobot"}),
# ],
# )
def test_duplicate_robot_creation(app):
with client_with_identity("devtable", app) as cl:
resp = conduct_api_call(
cl,
UserRobot,
"PUT",
{"robot_shortname": "dtrobot"},
expected_code=400,
)
assert resp.json["error_message"] == "Existing robot with name: devtable+dtrobot"

resp = conduct_api_call(
cl,
OrgRobot,
"PUT",
{"orgname": "buynlarge", "robot_shortname": "coolrobot"},
expected_code=400,
)
assert resp.json["error_message"] == "Existing robot with name: buynlarge+coolrobot"
3 changes: 2 additions & 1 deletion web/src/components/modals/CreateRobotAccountModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
selectedRobotReposPermissionState,
} from 'src/atoms/RobotAccountState';
import {useRepositories} from 'src/hooks/UseRepositories';
import {addDisplayError} from 'src/resources/ErrorHandling';
import {useOrganizations} from 'src/hooks/UseOrganizations';
import {Entity} from 'src/resources/UserResource';

Expand Down Expand Up @@ -60,7 +61,7 @@ export default function CreateRobotAccountModal(
handleModalToggle();
},
onError: (err) => {
props.showErrorAlert(err);
props.showErrorAlert(addDisplayError('Error', err));
},
});

Expand Down
2 changes: 1 addition & 1 deletion web/src/hooks/useRobotAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function useCreateRobotAccount({namespace, onSuccess, onError}) {
queryClient.invalidateQueries(['Namespace', namespace, 'robots']);
},
onError: (err) => {
onError('Error creating robot account');
onError(err);
},
},
);
Expand Down

0 comments on commit eaff3df

Please sign in to comment.