Skip to content

Commit

Permalink
Editing task should not change its completed status
Browse files Browse the repository at this point in the history
  • Loading branch information
shacker committed Sep 19, 2019
1 parent 2d40ef4 commit 4f9f379
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ django-todo uses pytest exclusively for testing. The best way to run the suite i

## Version History

**2.4.9** Fixed: Editing a task should not change its completed/incomplete status

**2.4.8** Fix bug when setting default values for unspecified settings

**2.4.7** Support custom user model in external_add
Expand Down
2 changes: 1 addition & 1 deletion todo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
A multi-user, multi-group task management and assignment system for Django.
"""
__version__ = "2.4.8"
__version__ = "2.4.9"

__author__ = "Scot Hacker"
__email__ = "shacker@birdhouse.org"
Expand Down
2 changes: 2 additions & 0 deletions todo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def __init__(self, user, *args, **kwargs):

note = forms.CharField(widget=forms.Textarea(), required=False)

completed = forms.BooleanField(required=False)

def clean_created_by(self):
"""Keep the existing created_by regardless of anything coming from the submitted form.
If creating a new task, then created_by will be None, but we set it before saving."""
Expand Down
1 change: 1 addition & 0 deletions todo/templates/todo/include/task_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<input type="hidden" name="priority"
value="{% if form.priority.value %}{{ form.priority.value }}{% else %}999{% endif %}" id="id_priority">
<input type="hidden" name="task_list" value="{{ form.task_list.value }}" id="id_task_list">
<input type="hidden" name="completed" class="form-check-input" type="checkbox" checked="{%if task.completed%}checked{% endif %}" id="id_completed">

<p>
<input type="submit" name="add_edit_task" value="Submit" class="btn btn-primary">
Expand Down
48 changes: 41 additions & 7 deletions todo/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_no_javascript_in_task_note(todo_setup, client):

@pytest.mark.django_db
def test_created_by_unchanged(todo_setup, client):

task_list = TaskList.objects.first()
u2 = get_user_model().objects.get(username="u2")
title = "Some Unique String with unique chars: ab78539e"
Expand All @@ -150,7 +150,9 @@ def test_created_by_unchanged(todo_setup, client):
}

client.login(username="u2", password="password")
url_add_task = reverse("todo:list_detail", kwargs={"list_id": task_list.id, "list_slug": task_list.slug})
url_add_task = reverse(
"todo:list_detail", kwargs={"list_id": task_list.id, "list_slug": task_list.slug}
)

response = client.post(url_add_task, data)
assert response.status_code == 302
Expand All @@ -167,26 +169,58 @@ def test_created_by_unchanged(todo_setup, client):

url_edit_task = reverse("todo:task_detail", kwargs={"task_id": task.id})

dataTwo = {
dataTwo = {
"task_list": task.task_list.id,
"created_by": extra_g2_user.id, # this submission is attempting to change created_by
"created_by": extra_g2_user.id, # this submission is attempting to change created_by
"priority": 10,
"title": task.title,
"note": "the note was changed",
"add_edit_task": "Submit",
}

response = client.post(url_edit_task, dataTwo)
assert response.status_code == 302

task.refresh_from_db()

# Proof that the task was saved:
assert task.note == "the note was changed"

# client was unable to modify created_by:
assert task.created_by == u2



@pytest.mark.django_db
@pytest.mark.parametrize("test_input, expected", [(True, True), (False, False)])
def test_completed_unchanged(test_input, expected, todo_setup, client):
"""Tasks are marked completed/uncompleted by buttons,
not via checkbox on the task edit form. Editing a task should
not change its completed status. Test with both completed and incomplete Tasks."""

task = Task.objects.get(title="Task 1", created_by__username="u1")
task.completed = test_input
task.save()
assert task.completed == expected

url_edit_task = reverse("todo:task_detail", kwargs={"task_id": task.id})

data = {
"task_list": task.task_list.id,
"title": "Something",
"note": "the note was changed",
"add_edit_task": "Submit",
"completed": task.completed,
}

client.login(username="u1", password="password")
response = client.post(url_edit_task, data)
assert response.status_code == 302

# Prove the task is still marked complete/incomplete
# (despite the default default state for completed being False)
task.refresh_from_db()
assert task.completed == expected


@pytest.mark.django_db
def test_no_javascript_in_comments(todo_setup, client):
Expand Down

0 comments on commit 4f9f379

Please sign in to comment.