Skip to content

Commit

Permalink
Make sure only events for pctx.course get updated, fix invalid tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhuang committed Jun 1, 2018
1 parent 09b5d5d commit 22b7e29
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
7 changes: 5 additions & 2 deletions course/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,7 @@ def update_event(pctx, event_id):
instance_to_update.shown_in_calendar = (
temp_instance.shown_in_calendar)

assert instance_to_update.course == pctx.course
instance_to_update.save()

if original_str == str(temp_instance):
Expand All @@ -1406,14 +1407,15 @@ def update_event(pctx, event_id):

if "update_all" in request.POST:
events_to_update = (
Event.objects.filter(kind=instance_to_update.kind))
events_of_same_kind_and_weekday_time.filter(
kind=instance_to_update.kind))
message = string_concat(
_("All '%(kind)s' events updated"
% {"kind": instance_to_update.kind}),
changes)

elif "update_this_and_following" in request.POST:
events_to_update = Event.objects.filter(
events_to_update = events_of_same_kind_and_weekday_time.filter(
kind=instance_to_update.kind,
time__gte=instance_to_update.time)
message = string_concat(
Expand Down Expand Up @@ -1451,6 +1453,7 @@ def update_event(pctx, event_id):
temp_instance.ordinal - instance_to_update.ordinal)

for event in events_to_update:
assert event.course == pctx.course
event.kind = temp_instance.kind

# This might result in IntegrityError
Expand Down
57 changes: 50 additions & 7 deletions tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def get_prefixed_form_data(form_klass, form_data):
return prefixed_form_data


def get_object_dict(obj):
return dict((k,v) for (k, v) in six.iteritems(obj.__dict__)
if not k.startswith("_"))


class CalendarTestMixin(SingleCourseTestMixin, HackRepoMixin):

default_faked_now = datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC)
Expand Down Expand Up @@ -99,14 +104,14 @@ def create_recurring_events(
hours=staring_time_offset_hours,
minutes=staring_time_offset_minutes)
for i in range(n):
now_time += timedelta(weeks=1)
kwargs = {"kind": self.default_event_kind,
"ordinal": i + staring_ordinal,
"time": now_time}
if end_time_minute_duration is not None:
kwargs["end_time"] = (
now_time + timedelta(minutes=end_time_minute_duration))
factories.EventFactory(**kwargs)
now_time += timedelta(weeks=1)

return list(Event.objects.exclude(pk__in=exist_events_pks))

Expand Down Expand Up @@ -2240,25 +2245,29 @@ def setUp(self):
# an event in another course, which should not be edited
self.another_course_event = factories.EventFactory(
course=factories.CourseFactory(
identifier="another-course"), kind=self.default_event_kind)
identifier="another-course"),
kind=self.default_event_kind,
time=self.default_faked_now)

# an event with another kind, which should not be edited
self.another_kind_event = factories.EventFactory(
course=self.course, kind="another_kind")

# this is to make sure other events are not affected during update
self.another_course_event_dict = self.another_course_event.__dict__
self.another_kind_event_dict = self.another_kind_event.__dict__
self.another_course_event_dict = get_object_dict(self.another_course_event)
self.another_kind_event_dict = get_object_dict(self.another_kind_event)

self.c.force_login(self.instructor_participation.user)

def assertOtherEventNotAffected(self): # noqa
self.another_kind_event.refresh_from_db()
self.another_course_event.refresh_from_db()
self.another_kind_event.refresh_from_db()
self.assertDictEqual(
self.another_course_event_dict, self.another_course_event.__dict__)
self.another_course_event_dict,
get_object_dict(self.another_course_event))
self.assertDictEqual(
self.another_kind_event_dict, self.another_kind_event.__dict__)
self.another_kind_event_dict,
get_object_dict(self.another_kind_event))

def create_event(self, **kwargs):
data = {
Expand Down Expand Up @@ -2462,6 +2471,40 @@ def test_update_single_within_single_series_success(self):
"Event '%s' updated." % str(instance_to_update))
self.assertOtherEventNotAffected()

def test_update_all_no_endtime_success(self):
instance_to_update, another_event = self.create_recurring_events(2)

original_time1 = instance_to_update.time
original_time2 = another_event.time
new_time = as_local_time(instance_to_update.time + timedelta(hours=2))

resp = self.post_update_event_view(
instance_to_update.id,
self.get_default_post_data(
instance_to_update, time=new_time, operation="update_all")
)
self.assertEqual(resp.status_code, 200)

instance_to_update.refresh_from_db()
self.assertEqual(
instance_to_update.time - original_time1,
timedelta(hours=2)
)

another_event.refresh_from_db()
self.assertEqual(
another_event.time - original_time2,
timedelta(hours=2)
)

json_response = json.loads(resp.content.decode())
self.assertEqual(json_response["message"],
"All '%s' events updated."
% instance_to_update.kind)

self.assertOtherEventNotAffected()
self.another_course_event.refresh_from_db()

def test_update_all_success(self):
instance_to_update, another_event = self.create_recurring_events(2)

Expand Down

0 comments on commit 22b7e29

Please sign in to comment.