Skip to content

Commit

Permalink
fix task notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Aug 29, 2024
1 parent d9b9b48 commit 9e3dfe4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
76 changes: 72 additions & 4 deletions breathecode/assignments/tests/urls/tests_task_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,13 @@ def test_task_id__put__with_one_task__with_revision_status(self):
@patch("breathecode.assignments.tasks.teacher_task_notification", MagicMock())
@patch("django.db.models.signals.pre_delete.send_robust", MagicMock(return_value=None))
@patch("breathecode.admissions.signals.student_edu_status_updated.send_robust", MagicMock(return_value=None))
def test_task_id__put__with_one_task__with_revision_status__teacher_auth(self):
def test_task_id__put__with_one_task__with_revision_status__teacher_auth__good_statuses(self):
from breathecode.assignments.tasks import student_task_notification, teacher_task_notification

statuses = ["PENDING", "APPROVED", "REJECTED", "IGNORED"]
for index in range(0, 4):
statuses = ["PENDING", "APPROVED", "REJECTED"]
for index in range(0, 3):
current_status = statuses[index]
next_status = statuses[index - 1 if index > 0 else 3]
next_status = statuses[index - 1 if index > 0 else 2]
task = {"revision_status": current_status, "user_id": (index * 2) + 1, "task_status": "DONE"}
cohort_users = [
{
Expand Down Expand Up @@ -438,6 +438,74 @@ def test_task_id__put__with_one_task__with_revision_status__teacher_auth(self):
student_task_notification.delay.call_args_list = []
activity_tasks.add_activity.delay.call_args_list = []

@patch("breathecode.assignments.tasks.student_task_notification", MagicMock())
@patch("breathecode.assignments.tasks.teacher_task_notification", MagicMock())
@patch("django.db.models.signals.pre_delete.send_robust", MagicMock(return_value=None))
@patch("breathecode.admissions.signals.student_edu_status_updated.send_robust", MagicMock(return_value=None))
def test_task_id__put__with_one_task__with_revision_status__teacher_auth__bad_statuses(self):
from breathecode.assignments.tasks import student_task_notification, teacher_task_notification

another_statuses = ["PENDING", "APPROVED", "REJECTED"]
statuses = ["IGNORED"]
statuses = ["PENDING", "APPROVED", "REJECTED"]
for index in range(0, 3):
current_status = statuses[index]
next_status = "IGNORED"
task = {"revision_status": current_status, "user_id": (index * 2) + 1, "task_status": "DONE"}
cohort_users = [
{
"role": "STUDENT",
"user_id": (index * 2) + 1,
},
{
"role": "TEACHER",
"user_id": (index * 2) + 2,
},
]
model = self.bc.database.create(user=2, task=task, cohort=1, cohort_user=cohort_users)
model2 = self.bc.database.create(cohort=1)
self.bc.request.authenticate(model.user[1])

url = reverse_lazy("assignments:task_id", kwargs={"task_id": index + 1})
data = {
"title": "They killed kenny",
"revision_status": next_status,
}
start = self.bc.datetime.now()
response = self.client.put(url, data, format="json")
end = self.bc.datetime.now()

json = response.json()
updated_at = self.bc.datetime.from_iso_string(json["updated_at"])
self.bc.check.datetime_in_range(start, end, updated_at)

del json["updated_at"]

expected = put_serializer(self, model.task, data=data)

self.assertEqual(json, expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(self.bc.database.list_of("assignments.Task"), [task_row(self, model.task, data=data)])
self.assertEqual(student_task_notification.delay.call_args_list, [])
self.assertEqual(teacher_task_notification.delay.call_args_list, [])

self.bc.check.calls(
activity_tasks.add_activity.delay.call_args_list,
[
call(
(index * 2) + 2,
"assignment_review_status_updated",
related_type="assignments.Task",
related_id=index + 1,
),
],
)

# teardown
self.bc.database.delete("assignments.Task")
activity_tasks.add_activity.delay.call_args_list = []

@patch("breathecode.assignments.tasks.student_task_notification", MagicMock())
@patch("breathecode.assignments.tasks.teacher_task_notification", MagicMock())
@patch("django.db.models.signals.pre_delete.send_robust", MagicMock(return_value=None))
Expand Down
5 changes: 3 additions & 2 deletions breathecode/assignments/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hashlib
import logging
import os
from slugify import slugify

from adrf.views import APIView
from asgiref.sync import sync_to_async
from circuitbreaker import CircuitBreakerError
Expand All @@ -15,6 +15,7 @@
from rest_framework.decorators import api_view
from rest_framework.exceptions import PermissionDenied
from rest_framework.response import Response
from slugify import slugify

import breathecode.activity.tasks as tasks_activity
import breathecode.assignments.tasks as tasks
Expand Down Expand Up @@ -713,7 +714,7 @@ def update(_req, data, _id=None, only_validate=True):
if serializer.is_valid():
if not only_validate:
serializer.save()
if _req.user.id != item.user.id:
if _req.user.id != item.user.id and item.revision_status != "IGNORED":
tasks.student_task_notification.delay(item.id)
return status.HTTP_200_OK, serializer.data
return status.HTTP_400_BAD_REQUEST, serializer.errors
Expand Down

0 comments on commit 9e3dfe4

Please sign in to comment.