Skip to content

Commit

Permalink
[#1548] add and regroup tests for profile deletion refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-sigma committed Jun 21, 2023
1 parent fd9ecd0 commit 09ba067
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 168 deletions.

This file was deleted.

92 changes: 0 additions & 92 deletions src/open_inwoner/accounts/tests/test_deleteinactiveusers.py

This file was deleted.

1 change: 0 additions & 1 deletion src/open_inwoner/accounts/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ def test_users_deactivation_is_logged(self):
self.assertEqual(
log_entry.timestamp.strftime("%m/%d/%Y, %H:%M:%S"), "10/18/2021, 13:00:00"
)
self.assertEqual(log_entry.content_object.id, self.user.id)
self.assertEqual(
log_entry.extra_data,
{
Expand Down
135 changes: 92 additions & 43 deletions src/open_inwoner/accounts/tests/test_profile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from open_inwoner.cms.profile.cms_appconfig import ProfileConfig
from open_inwoner.haalcentraal.tests.mixins import HaalCentraalMixin
from open_inwoner.pdc.tests.factories import CategoryFactory
from open_inwoner.plans.tests.factories import PlanFactory
from open_inwoner.utils.logentry import LOG_ACTIONS
from open_inwoner.utils.tests.helpers import AssertTimelineLogMixin, create_image_bytes

Expand All @@ -21,6 +22,7 @@
from ...questionnaire.tests.factories import QuestionnaireStepFactory
from ..choices import ContactTypeChoices, LoginTypeChoices
from ..forms import BrpUserForm, UserForm
from ..models import User
from .factories import ActionFactory, DigidUserFactory, DocumentFactory, UserFactory


Expand Down Expand Up @@ -92,49 +94,6 @@ def test_only_open_actions(self):
self.assertEquals(response.status_code, 200)
self.assertContains(response, "0 acties staan open.")

def test_deactivate_account(self):
response = self.app.get(self.url, user=self.user)
self.assertEquals(response.status_code, 200)
form = response.forms["deactivate-form"]
base_response = form.submit()
self.assertEquals(base_response.url, self.return_url)
followed_response = base_response.follow().follow()
self.assertEquals(followed_response.status_code, 200)
self.user.refresh_from_db()
self.assertFalse(self.user.is_active)
self.assertIsNotNone(self.user.deactivated_on)

def test_deactivate_account_staff(self):
self.user.is_staff = True
self.user.save()
response = self.app.get(self.url, user=self.user)
self.assertEquals(response.status_code, 200)
form = response.forms["deactivate-form"]
base_response = form.submit()
self.assertEquals(base_response.url, self.url)
followed_response = base_response.follow()
self.assertEquals(followed_response.status_code, 200)
self.user.refresh_from_db()
self.assertTrue(self.user.is_active)
self.assertIsNone(self.user.deactivated_on)

def test_deactivate_account_digid(self):
"""
check that user is redirected to digid:logout
"""
user = UserFactory.create(
login_type=LoginTypeChoices.digid, email="[email protected]"
)

response = self.app.get(self.url, user=user)
self.assertEquals(response.status_code, 200)
form = response.forms["deactivate-form"]

response = form.submit()

self.assertEquals(response.status_code, 302)
self.assertEquals(response.url, reverse("digid:logout"))

def test_get_documents_sorted(self):
"""
check that the new document is shown first
Expand Down Expand Up @@ -560,6 +519,96 @@ def test_modify_phone_updates_klant_api_but_skip_unchanged_phone(self, m):
)


@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class ProfileDeleteTest(WebTest):
csrf_checks = False

@classmethod
def setUpTestData(cls):
cls.url = reverse("profile:detail")

def test_delete_regular_user_success(self):
user = UserFactory()

# get profile page
response = self.app.get(self.url, user=user)
self.assertEqual(response.status_code, 200)

# check delete
response = response.forms["deactivate-form"].submit()
self.assertIsNone(User.objects.first())

# check redirect
self.assertRedirects(
self.app.get(response.url),
reverse("pages-root"),
status_code=302,
target_status_code=200,
fetch_redirect_response=True,
)

def test_delete_user_with_digid_login_success(self):
user = DigidUserFactory()

# get profile page
response = self.app.get(self.url, user=user)
self.assertEqual(response.status_code, 200)

# check user deleted
response = response.forms["deactivate-form"].submit()
self.assertIsNone(User.objects.first())

# check redirect
self.assertRedirects(
self.app.get(response.url),
reverse("pages-root"),
status_code=302,
target_status_code=200,
fetch_redirect_response=True,
)

def test_delete_regular_with_existing_plans_fail(self):
user = UserFactory()
PlanFactory.create(created_by=user)

# get profile page
response = self.app.get(self.url, user=user)
self.assertEqual(response.status_code, 200)

# check user not deleted
response = response.forms["deactivate-form"].submit()
self.assertEqual(User.objects.first(), user)

# check redirect
self.assertRedirects(
response,
reverse("profile:detail"),
status_code=302,
target_status_code=200,
fetch_redirect_response=True,
)

def test_delete_staff_user_via_frontend_does_not_work(self):
user = UserFactory(is_staff=True)

# get profile page
response = self.app.get(self.url, user=user)
self.assertEqual(response.status_code, 200)

# check staff user not deleted
response = response.forms["deactivate-form"].submit()
self.assertEqual(User.objects.first(), user)

# check redirect
self.assertRedirects(
response,
reverse("profile:detail"),
status_code=302,
target_status_code=200,
fetch_redirect_response=True,
)


@requests_mock.Mocker()
@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class MyDataTests(HaalCentraalMixin, WebTest):
Expand Down
14 changes: 13 additions & 1 deletion src/open_inwoner/accounts/views/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from open_inwoner.cms.utils.page_display import inbox_page_is_published
from open_inwoner.haalcentraal.utils import fetch_brp_data
from open_inwoner.plans.models import Plan
from open_inwoner.questionnaire.models import QuestionnaireStep
from open_inwoner.utils.mixins import ExportMixin
from open_inwoner.utils.views import CommonPageMixin, LogMixin
Expand Down Expand Up @@ -107,8 +108,19 @@ def post(self, request, *args, **kwargs):
if request.user.is_authenticated and not request.user.is_staff:
instance = User.objects.get(id=request.user.id)

self.log_user_action(instance, _("user was deactivated via frontend"))
# check if there are still plans associated witht the user
if Plan.objects.filter(created_by=instance).exists():
messages.warning(
request,
_(
"You still have plans associated with your profile. "
"Please delete those plans before deleting your profile."
),
)
return redirect("profile:detail")

# continue with delete
self.log_user_action(instance, _("user was deactivated via frontend"))
instance.delete()
request.session.flush()

Expand Down

0 comments on commit 09ba067

Please sign in to comment.