Skip to content

Commit

Permalink
Merge branch 'release/2.024.36'
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Dec 9, 2024
2 parents c290b8e + 953f2c9 commit f4dc7d2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
from kobo.apps.openrosa.apps.logger.models import Attachment
from kobo.apps.openrosa.libs.constants import CAN_ADD_SUBMISSIONS
from kobo.apps.openrosa.libs.utils.guardian import assign_perm
from kobo.apps.openrosa.libs.utils.logger_tools import OpenRosaTemporarilyUnavailable
from kobo.apps.openrosa.libs.utils.logger_tools import (
OpenRosaResponseNotAllowed,
OpenRosaTemporarilyUnavailable,
)


class TestXFormSubmissionApi(TestAbstractViewSet):
Expand Down Expand Up @@ -377,6 +380,42 @@ def test_post_submission_json_without_submission_key(self):
response = self.view(request)
self.assertContains(response, 'No submission key provided.', status_code=400)

def test_submission_account_inactive(self):
"""
Verify that submissions are blocked when the owning user has
`is_active = False`
"""
self.xform.user.is_active = False
self.xform.user.save()

# No need auth for this test
self.xform.require_auth = False
self.xform.save(update_fields=['require_auth'])

s = self.surveys[0]
username = self.user.username
submission_path = os.path.join(
self.main_directory,
'fixtures',
'transportation',
'instances',
s,
s + '.xml',
)
with open(submission_path) as sf:
request = self.factory.post(
f'/{username}/submission', {'xml_submission_file': sf}
)
request.user = AnonymousUser()

# Ensure that submissions are not accepted since the owning user is
# inactive
response = self.view(request, username=username)
self.assertEqual(
response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED
)
self.assertTrue(isinstance(response, OpenRosaResponseNotAllowed))

def test_submission_blocking_flag(self):
# Set 'submissions_suspended' True in the profile to test if
# submission do fail with the flag set
Expand Down
4 changes: 4 additions & 0 deletions kobo/apps/openrosa/apps/logger/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class BuildDbQueriesNoConfirmationProvidedError(Exception):
pass


class AccountInactiveError(Exception):
pass


class DuplicateUUIDError(Exception):
pass

Expand Down
5 changes: 4 additions & 1 deletion kobo/apps/openrosa/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from kobo.apps.kobo_auth.shortcuts import User
from kobo.apps.openrosa.apps.logger.exceptions import (
AccountInactiveError,
FormInactiveError,
TemporarilyUnavailableError,
)
Expand Down Expand Up @@ -131,7 +132,9 @@ def check_active(self, force):
profile, created = UserProfile.objects.get_or_create(user=self.xform.user)
if not created and profile.submissions_suspended:
raise TemporarilyUnavailableError()
return

if not self.xform.user.is_active:
raise AccountInactiveError()

def _set_geom(self):
xform = self.xform
Expand Down
4 changes: 4 additions & 0 deletions kobo/apps/openrosa/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from rest_framework.exceptions import NotAuthenticated

from kobo.apps.openrosa.apps.logger.exceptions import (
AccountInactiveError,
DuplicateUUIDError,
FormInactiveError,
TemporarilyUnavailableError,
Expand Down Expand Up @@ -310,6 +311,9 @@ def status_code(self):
except TemporarilyUnavailableError:
result.error = t('Temporarily unavailable')
result.http_error_response = OpenRosaTemporarilyUnavailable(result.error)
except AccountInactiveError:
result.error = t('Account is not active')
result.http_error_response = OpenRosaResponseNotAllowed(result.error)
except XForm.DoesNotExist:
result.error = t('Form does not exist on this account')
result.http_error_response = OpenRosaResponseNotFound(result.error)
Expand Down

0 comments on commit f4dc7d2

Please sign in to comment.