Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IAC][ENG-6998] Infinite Resubmits For IAC #10937

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions api/requests/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.db import IntegrityError
from django.db import IntegrityError, transaction
from rest_framework import exceptions
from rest_framework import serializers as ser

Expand Down Expand Up @@ -209,17 +209,32 @@ def _create_node_request(self, node, validated_data) -> NodeRequest:
comment = validated_data.get('comment', '')
requested_permissions = validated_data.get('requested_permissions')
try:
node_request = NodeRequest.objects.create(
target=node,
creator=creator,
comment=comment,
machine_state=DefaultStates.INITIAL.value,
request_type=request_type,
requested_permissions=requested_permissions,
)
node_request.save()
with transaction.atomic():
node_request = NodeRequest.objects.create(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the error would happen mid-same, you need this transaction to be able to rollback on error.

target=node,
creator=creator,
comment=comment,
machine_state=DefaultStates.INITIAL.value,
request_type=request_type,
requested_permissions=requested_permissions,
)
node_request.save()
except IntegrityError:
raise Conflict(f"Users may not have more than one {request_type} request per node.")
# if INSTITUTIONAL_REQUEST updates and restarts the request
with transaction.atomic():
if request_type == NodeRequestTypes.INSTITUTIONAL_REQUEST.value:
node_request = NodeRequest.objects.filter(
target=node,
creator=creator,
request_type=NodeRequestTypes.INSTITUTIONAL_REQUEST.value,
).first()
else:
raise Conflict(f"Users may not have more than one {request_type} request per node.")
if node_request:
node_request.comment = comment
node_request.machine_state = DefaultStates.INITIAL.value
node_request.requested_permissions = requested_permissions
node_request.save()

node_request.run_submit(creator)
return node_request
Expand Down
17 changes: 17 additions & 0 deletions api_tests/requests/views/test_node_request_institutional_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,20 @@ def test_access_requests_disabled_raises_permission_denied(
)
assert res.status_code == 403
assert f"{node_with_disabled_access_requests._id} does not have Access Requests enabled" in res.json['errors'][0]['detail']

def test_requester_can_resubmit(self, app, project, institutional_admin, url, create_payload):
"""
Test that a requester cannot submit another access request for the same node.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Test that a requester cannot submit another access request for the same node.
Test that a requester can submit another access request for the same node.

"""
# Create the first request
app.post_json_api(url, create_payload, auth=institutional_admin.auth)
node_request = project.requests.get()
node_request.run_reject(project.creator, 'test comment2')
node_request.refresh_from_db()
assert node_request.machine_state == 'rejected'

# Attempt to create a second request
res = app.post_json_api(url, create_payload, auth=institutional_admin.auth)
assert res.status_code == 201
node_request.refresh_from_db()
assert node_request.machine_state == 'pending'
Loading