-
-
Notifications
You must be signed in to change notification settings - Fork 980
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #1782 -- Added page to delete one's user account
- Loading branch information
Showing
7 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from django.contrib.auth.models import User | ||
from django.contrib.auth.models import AnonymousUser, User | ||
from django.test import TestCase, override_settings | ||
from django_hosts.resolvers import reverse | ||
|
||
from accounts.forms import DeleteProfileForm | ||
from foundation import models as foundationmodels | ||
from tracdb.models import Revision, Ticket, TicketChange | ||
from tracdb.testutils import TracDBCreateDatabaseMixin | ||
|
||
|
@@ -169,3 +171,50 @@ def test_profile_view_reversal(self): | |
""" | ||
for username in ["asdf", "@asdf", "asd-f", "as.df", "as+df"]: | ||
reverse("user_profile", host="www", args=[username]) | ||
|
||
|
||
class UserDeletionTestCase(TestCase): | ||
def create_user_and_form(self, bound=True, **userkwargs): | ||
userkwargs.setdefault("username", "test") | ||
userkwargs.setdefault("email", "[email protected]") | ||
userkwargs.setdefault("password", "password") | ||
|
||
formkwargs = {"user": User.objects.create_user(**userkwargs)} | ||
if bound: | ||
formkwargs["data"] = {} | ||
|
||
return DeleteProfileForm(**formkwargs) | ||
|
||
def test_deletion(self): | ||
form = self.create_user_and_form() | ||
self.assertFormError(form, None, []) | ||
form.delete() | ||
self.assertQuerySetEqual(User.objects.all(), []) | ||
|
||
def test_anonymous_user_error(self): | ||
self.assertRaises(TypeError, DeleteProfileForm, user=AnonymousUser) | ||
|
||
def test_deletion_staff_forbidden(self): | ||
form = self.create_user_and_form(is_staff=True) | ||
self.assertFormError(form, None, ["Staff users cannot be deleted"]) | ||
|
||
def test_user_with_protected_data(self): | ||
form = self.create_user_and_form() | ||
form.user.boardmember_set.create( | ||
office=foundationmodels.Office.objects.create(name="test"), | ||
term=foundationmodels.Term.objects.create(year=2000), | ||
) | ||
form.delete() | ||
self.assertFormError( | ||
form, None, ["User has protected data and cannot be deleted"] | ||
) | ||
|
||
def test_form_delete_method_requires_valid_form(self): | ||
form = self.create_user_and_form(is_staff=True) | ||
self.assertRaises(form.InvalidFormError, form.delete) | ||
|
||
def test_view_deletion_also_logs_out(self): | ||
user = self.create_user_and_form().user | ||
self.client.force_login(user) | ||
self.client.post(reverse("delete_profile")) | ||
self.assertEqual(self.client.cookies["sessionid"].value, "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{% extends "base.html" %} | ||
{% load i18n %} | ||
|
||
{% block title %}{% translate "Confirmation: delete your profile" %}{% endblock %} | ||
|
||
{% block content %} | ||
{% if form.errors %} | ||
<h2>{% translate "Could not delete account" %}</h2> | ||
|
||
<p>{% blocktranslate trimmed %} | ||
Sorry, something went wrong when trying to delete your account. | ||
That means there's probably some protected data still associated | ||
with your account. | ||
Please contact | ||
<a href="mailto:[email protected]?{{ OPS_EMAIL_PRESETS }}">the operations team</a> | ||
and we'll sort it out for you. | ||
{% endblocktranslate %}</p> | ||
{% else %} | ||
<h2>{% translate "Are you sure?" %}</h2> | ||
|
||
<p>{% blocktranslate trimmed with username=request.user.username %} | ||
⚠️ You are about to delete all data associated with the username | ||
<strong>{{ username}}</strong>. | ||
{% endblocktranslate %}</p> | ||
|
||
<p>{% blocktranslate trimmed %} | ||
Deleting your account is permanent and <strong>cannot be reversed</strong>. | ||
Are you sure you want to continue? | ||
{% endblocktranslate %}</p> | ||
<form method="post"> | ||
{% csrf_token %} | ||
<div class="submit"> | ||
<button type="submit">{% translate "Yes, delete account" %}</button> | ||
<a href="{% url 'edit_profile' %}"> | ||
{% translate "No, cancel and go back" %} | ||
</a> | ||
</div> | ||
</form> | ||
{% endif %} | ||
{% endblock %} |
17 changes: 17 additions & 0 deletions
17
djangoproject/templates/accounts/delete_profile_success.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% extends "base.html" %} | ||
|
||
{% load i18n %} | ||
|
||
{% block content %} | ||
<h2>{% translate "Account deleted" %}</h2> | ||
<p> | ||
{% translate "Your account and its data was successfully deleted and you've been logged out." %} | ||
</p> | ||
<p> | ||
{% url "community-index" as community_index_url %} | ||
{% blocktranslate trimmed %} | ||
Thanks for spending your time with us, we hope we'll still see you | ||
around on our <a href="{{ community_index_url }}">various community spaces, online and off. | ||
{% endblocktranslate %} | ||
</p> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters