Skip to content

Commit

Permalink
API - fix notification creation on appeal
Browse files Browse the repository at this point in the history
  • Loading branch information
SamR1 committed Nov 13, 2024
1 parent 596f583 commit 13980f4
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 8 deletions.
16 changes: 9 additions & 7 deletions fittrackee/reports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,13 +624,15 @@ def on_report_action_appeal_insert(
def receive_after_flush(session: Session, context: Connection) -> None:
from fittrackee.users.models import Notification, User

for admin in User.query.filter(
User.admin == True, # noqa
User.id != new_appeal.user_id,
User.is_active == True, # noqa
).all():
report_action = ReportAction.query.filter_by().first()
if report_action:
report_action = ReportAction.query.filter_by(
id=new_appeal.action_id
).first()
if report_action:
for admin in User.query.filter(
User.admin == True, # noqa
User.id != new_appeal.user_id,
User.is_active == True, # noqa
).all():
notification = Notification(
from_user_id=new_appeal.user_id,
to_user_id=admin.id,
Expand Down
114 changes: 113 additions & 1 deletion fittrackee/tests/reports/test_report_actions_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
ReportAction,
ReportActionAppeal,
)
from fittrackee.users.models import User
from fittrackee.users.models import Notification, User
from fittrackee.workouts.models import Sport, Workout

from ..comments.mixins import CommentMixin
Expand Down Expand Up @@ -989,6 +989,31 @@ def test_it_creates_appeal_for_user_suspension_action(
assert appeal.updated_at is None
assert appeal.user_id == user_2.id

def test_it_creates_notification_on_user_suspension_appeal(
self, app: Flask, user_1_admin: User, user_2_admin: User, user_3: User
) -> None:
user_warning = self.create_report_user_action(
user_1_admin, user_3, action_type='user_suspension'
)

appeal = ReportActionAppeal(
user_warning.id, user_3.id, self.random_string()
)
db.session.add(appeal)
db.session.commit()

notifications = Notification.query.filter_by(
event_type='suspension_appeal'
).all()
assert len(notifications) == 2
for notification in notifications:
assert notification.from_user_id == user_3.id
assert notification.to_user_id in [
user_1_admin.id,
user_2_admin.id,
]
assert notification.event_object_id == user_warning.id

def test_it_creates_appeal_for_user_warning_action(
self,
app: Flask,
Expand Down Expand Up @@ -1016,6 +1041,31 @@ def test_it_creates_appeal_for_user_warning_action(
assert appeal.updated_at is None
assert appeal.user_id == user_2.id

def test_it_creates_notification_on_user_warning_appeal(
self, app: Flask, user_1_admin: User, user_2_admin: User, user_3: User
) -> None:
user_warning = self.create_report_user_action(
user_1_admin, user_3, action_type='user_warning'
)

appeal = ReportActionAppeal(
user_warning.id, user_3.id, self.random_string()
)
db.session.add(appeal)
db.session.commit()

notifications = Notification.query.filter_by(
event_type='user_warning_appeal'
).all()
assert len(notifications) == 2
for notification in notifications:
assert notification.from_user_id == user_3.id
assert notification.to_user_id in [
user_1_admin.id,
user_2_admin.id,
]
assert notification.event_object_id == user_warning.id

def test_it_raises_error_when_workout_action_is_invalid(
self,
app: Flask,
Expand Down Expand Up @@ -1072,6 +1122,34 @@ def test_it_creates_appeal_for_workout_suspension_action(
assert appeal.updated_at is None
assert appeal.user_id == user_2.id

def test_it_creates_notification_on_workout_suspension_appeal(
self,
app: Flask,
user_1_admin: User,
user_2: User,
sport_1_cycling: Sport,
workout_cycling_user_2: Workout,
) -> None:
workout_suspension = self.create_report_workout_action(
user_1_admin, user_2, workout_cycling_user_2
)
db.session.add(workout_suspension)
db.session.flush()

appeal = ReportActionAppeal(
workout_suspension.id, user_2.id, self.random_string()
)
db.session.add(appeal)
db.session.commit()

notifications = Notification.query.filter_by(
event_type='suspension_appeal'
).all()
assert len(notifications) == 1
assert notifications[0].from_user_id == user_2.id
assert notifications[0].to_user_id == user_1_admin.id
assert notifications[0].event_object_id == workout_suspension.id

def test_it_raises_error_when_comment_action_is_invalid(
self,
app: Flask,
Expand Down Expand Up @@ -1136,6 +1214,40 @@ def test_it_creates_appeal_for_comment_suspension_action(
assert appeal.updated_at is None
assert appeal.user_id == user_2.id

def test_it_creates_notification_on_comment_suspension_appeal(
self,
app: Flask,
user_1_admin: User,
user_2: User,
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
workout_cycling_user_1.workout_visibility = PrivacyLevel.PUBLIC
comment = self.create_comment(
user_2,
workout_cycling_user_1,
text_visibility=PrivacyLevel.FOLLOWERS,
)
comment_suspension = self.create_report_comment_action(
user_1_admin, user_2, comment
)
db.session.add(comment_suspension)
db.session.flush()

appeal = ReportActionAppeal(
comment_suspension.id, user_2.id, self.random_string()
)
db.session.add(appeal)
db.session.commit()

notifications = Notification.query.filter_by(
event_type='suspension_appeal'
).all()
assert len(notifications) == 1
assert notifications[0].from_user_id == user_2.id
assert notifications[0].to_user_id == user_1_admin.id
assert notifications[0].event_object_id == comment_suspension.id

def test_it_creates_appeal_for_a_given_action_without_creation_date(
self,
app: Flask,
Expand Down

0 comments on commit 13980f4

Please sign in to comment.