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

Avoiding the multiple clicks on Application Submission. #286

Merged
merged 2 commits into from
Oct 23, 2024
Merged
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
8 changes: 7 additions & 1 deletion request_a_govuk_domain/request/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def save_data_in_database(reference, request):
:param request: Request object
"""

# get the application here and return status as False.
# since the application already is created in DB.
application = Application.objects.filter(reference=reference)
if application:
return False

session_data = get_registration_data(request)
registration_data = sanitised_registration_data(
session_data, request.session.session_key
Expand Down Expand Up @@ -164,7 +170,7 @@ def save_data_in_database(reference, request):
"Saved application %d with reference %s", application.id, reference
)
Review.objects.create(application=application)
return application
return True
except Exception as e:
logger.error(
f"""Exception while saving data. Exception: {type(e).__name__} - {str(e)} ,
Expand Down
12 changes: 8 additions & 4 deletions request_a_govuk_domain/request/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,24 +384,28 @@ def post(self, request):
:return:
"""
reference_ = request.session.get(APPLICATION_REFERENCE)
self.save_application_to_database_and_send_confirmation_email(
status = self.save_application_to_database_and_send_confirmation_email(
reference_, request
)
return redirect("success")
if status:
return redirect("success")
else:
return HttpResponseNotFound()
jinnashravan marked this conversation as resolved.
Show resolved Hide resolved

@transaction.atomic # This ensures that any failure during email send will not save data in db either
def save_application_to_database_and_send_confirmation_email(
self, reference: str, request: HttpRequest
) -> None:
):
jinnashravan marked this conversation as resolved.
Show resolved Hide resolved
"""
Saves data in the db and sends confirmation email

:param reference: Application reference
:param request: request object
"""
logger.info(f"Saving form {request.session.session_key}")
save_data_in_database(reference, request)
status = save_data_in_database(reference, request)
self.send_confirmation_email(reference, request)
return status

def send_confirmation_email(self, reference: str, request) -> None:
"""
Expand Down
6 changes: 4 additions & 2 deletions tests/test_admin_approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def test_last_modified_is_set_correctly(self, owner, changed_by, expected_owner)
request.session = SessionDict({"registration_data": self.registration_data})

# Simulate application creation through client screens
application = db.save_data_in_database("ABCDEFGHIJK", request)
db.save_data_in_database("ABCDEFGHIJK", request)
application = Application.objects.filter(reference="ABCDEFGHIJK")[0]

# Application retrieval, check original condition
response = self.admin_client.get(get_admin_change_view_url(application))
Expand Down Expand Up @@ -98,7 +99,8 @@ def test_create_approval_works(self, status, reason, action):
# Make the application email unique so that we are sure when we compare the data later
self.registration_data["registrar_email"] = "[email protected]"
# Simulate application creation through client screens
application_to_approve = db.save_data_in_database("ABCDEFGHIJG", request)
db.save_data_in_database("ABCDEFGHIJG", request)
application_to_approve = Application.objects.filter(reference="ABCDEFGHIJG")[0]

# Now we review the last application created by the client
review = Review.objects.filter(
Expand Down
5 changes: 3 additions & 2 deletions tests/test_admin_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.test import TestCase

from request_a_govuk_domain.request import db
from request_a_govuk_domain.request.models import Review
from request_a_govuk_domain.request.models import Review, Application
from request_a_govuk_domain.request.templatetags.admin_tags import format_date
from tests.util import (
AdminScreenTestMixin,
Expand All @@ -27,7 +27,8 @@ def test_application_history(self):
request.session = SessionDict({"registration_data": self.registration_data})

# Simulate application creation through client screens
application = db.save_data_in_database("ABCDEFGHIJK", request)
db.save_data_in_database("ABCDEFGHIJK", request)
application = Application.objects.filter(reference="ABCDEFGHIJK")[0]
sleep(1)

# Simulate 1st review user interaction.
Expand Down
30 changes: 29 additions & 1 deletion tests/test_application_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import functools
from concurrent.futures import ThreadPoolExecutor
from typing import Any
from unittest.mock import Mock
from unittest.mock import Mock, patch

from django.db import connection
from django.test import TransactionTestCase
Expand Down Expand Up @@ -183,3 +183,31 @@ def __init__(self, *k, **kwargs):
# Try to save the application without any session data
mock_request.POST = {}
return ConfirmView().post(mock_request)

def test_application_success(self):
"""
successfull response
jinnashravan marked this conversation as resolved.
Show resolved Hide resolved
:return:
"""

with patch(
"request_a_govuk_domain.request.views.ConfirmView.save_application_to_database_and_send_confirmation_email"
) as mock_save:
mock_save.return_value = True
res = self.client.post("/confirm/", data={})

self.assertEqual(302, res.status_code)

def test_application_failure(self):
"""
failure response
jinnashravan marked this conversation as resolved.
Show resolved Hide resolved
:return:
"""
with patch(
"request_a_govuk_domain.request.views.ConfirmView.save_application_to_database_and_send_confirmation_email"
) as mock_save:
mock_save.return_value = False
# self.client.raise_request_exception = False
res = self.client.post("/confirm/", data={})

self.assertEqual(404, res.status_code)
5 changes: 3 additions & 2 deletions tests/test_model_admins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from request_a_govuk_domain.request import db
from request_a_govuk_domain.request.admin.model_admins import convert_to_local_time
from request_a_govuk_domain.request.models import Registrar
from request_a_govuk_domain.request.models import Registrar, Application


class ModelAdminTestCase(TestCase):
Expand Down Expand Up @@ -70,7 +70,8 @@ def test_gmt_time_is_converted_correctly(self):
def test_file_is_uploaded_from_admin_screen(self, file_name, expected_name):
request = Mock()
request.session = SessionDict({"registration_data": self.registration_data})
app = db.save_data_in_database("ABCDEFGHIJK", request)
db.save_data_in_database("ABCDEFGHIJK", request)
app = Application.objects.filter(reference="ABCDEFGHIJK")[0]
c = Client()
c.login(username="superuser", password="secret") # pragma: allowlist secret

Expand Down
7 changes: 5 additions & 2 deletions tests/test_model_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ApplicationStatus,
)
from tests.util import get_admin_change_view_url, SessionDict, AdminScreenTestMixin
from request_a_govuk_domain.request.models import Application


class CheckHistoryTestCase(AdminScreenTestMixin, TestCase):
Expand All @@ -21,7 +22,8 @@ def test_application_history(self):
request.session = SessionDict({"registration_data": self.registration_data})

# Simulate application creation through client screens
application = db.save_data_in_database("ABCDEFGHIJK", request)
db.save_data_in_database("ABCDEFGHIJK", request)
application = Application.objects.filter(reference="ABCDEFGHIJK")[0]

# see the records in history table
history = application.history.all() # type: ignore
Expand All @@ -46,7 +48,8 @@ def test_review_history(self):
request.session = SessionDict({"registration_data": self.registration_data})

# Simulate application creation through client screens
application = db.save_data_in_database("ABCDEFGHIJL", request)
db.save_data_in_database("ABCDEFGHIJL", request)
application = Application.objects.filter(reference="ABCDEFGHIJL")[0]

# Now we review the last application created by the client
review = Review.objects.filter(
Expand Down