Exemplo n.º 1
0
    def test_courseschedules_feed(self):
        # GIVEN
        user1 = userhelper.given_a_user_exists()
        user1.settings.enable_private_slug()
        user2 = userhelper.given_a_user_exists(username='******', email='*****@*****.**')
        user2.settings.enable_private_slug()
        course_group1 = coursegrouphelper.given_course_group_exists(user1)
        course_group2 = coursegrouphelper.given_course_group_exists(user1)
        course_group3 = coursegrouphelper.given_course_group_exists(user2)
        course1 = coursehelper.given_course_exists(course_group1, room='SSC 123')
        course2 = coursehelper.given_course_exists(course_group2)
        course3 = coursehelper.given_course_exists(course_group3)
        courseschedulehelper.given_course_schedule_exists(course1)
        courseschedulehelper.given_course_schedule_exists(course2)
        courseschedulehelper.given_course_schedule_exists(course3)

        # WHEN
        response = self.client.get(
            reverse("feed_private_courseschedules_ical", kwargs={"slug": user1.settings.private_slug}))

        # THEN
        calendar = icalendar.Calendar.from_ical(response.content.decode('utf-8'))
        self.assertEqual(len(calendar.subcomponents), 106)
        self.assertEqual(calendar.subcomponents[0]['SUMMARY'], course1.title)
        self.assertEqual(str(calendar.subcomponents[0]['DTSTART'].dt), '2017-01-06 02:30:00-08:00')
        self.assertEqual(str(calendar.subcomponents[0]['DTEND'].dt), '2017-01-06 05:00:00-08:00')
        self.assertEqual(calendar.subcomponents[0]['DESCRIPTION'],
                         f'Comments: <a href="{course1.website}">{course1.title}</a> in {course1.room}')
Exemplo n.º 2
0
    def test_get_homework(self):
        user1 = userhelper.given_a_user_exists()
        user2 = userhelper.given_a_user_exists_and_is_authenticated(
            self.client, username='******', email='*****@*****.**')
        course_group1 = coursegrouphelper.given_course_group_exists(user1)
        course_group2 = coursegrouphelper.given_course_group_exists(user2)
        course1 = coursehelper.given_course_exists(course_group1)
        course2 = coursehelper.given_course_exists(course_group2)
        course3 = coursehelper.given_course_exists(course_group2)
        homeworkhelper.given_homework_exists(course1)
        homeworkhelper.given_homework_exists(course2)
        homeworkhelper.given_homework_exists(course3)
        homeworkhelper.given_homework_exists(course3)

        # WHEN
        response1 = self.client.get(reverse('planner_homework_list'))
        response2 = self.client.get(
            reverse('planner_coursegroups_courses_homework_list',
                    kwargs={
                        'course_group': course_group2.pk,
                        'course': course3.pk
                    }))

        # THEN
        self.assertEqual(response1.status_code, status.HTTP_200_OK)
        self.assertEqual(response2.status_code, status.HTTP_200_OK)
        self.assertEqual(Homework.objects.count(), 4)
        self.assertEqual(len(response1.data), 3)
        self.assertEqual(len(response2.data), 2)
Exemplo n.º 3
0
    def test_related_field_owned_by_another_user_forbidden(self):
        # GIVEN
        user1 = userhelper.given_a_user_exists_and_is_authenticated(
            self.client)
        user2 = userhelper.given_a_user_exists(username='******',
                                               email='*****@*****.**')
        course_group1 = coursegrouphelper.given_course_group_exists(user1)
        course_group2 = coursegrouphelper.given_course_group_exists(user2)
        course = coursehelper.given_course_exists(course_group1)

        # WHEN
        responses = [
            self.client.post(reverse('planner_coursegroups_courses_list',
                                     kwargs={'course_group':
                                             course_group2.pk}),
                             json.dumps({}),
                             content_type='application/json'),
            self.client.put(reverse('planner_coursegroups_courses_detail',
                                    kwargs={
                                        'course_group': course_group1.pk,
                                        'pk': course.pk
                                    }),
                            json.dumps({'course_group': course_group2.pk}),
                            content_type='application/json')
        ]

        # THEN
        for response in responses:
            self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Exemplo n.º 4
0
    def test_get_course_schedules(self):
        # GIVEN
        user1 = userhelper.given_a_user_exists()
        user2 = userhelper.given_a_user_exists_and_is_authenticated(
            self.client, username='******', email='*****@*****.**')
        course_group1 = coursegrouphelper.given_course_group_exists(user1)
        course_group2 = coursegrouphelper.given_course_group_exists(user2)
        course_group3 = coursegrouphelper.given_course_group_exists(user2)
        course1 = coursehelper.given_course_exists(course_group1)
        course2 = coursehelper.given_course_exists(course_group2)
        course3 = coursehelper.given_course_exists(course_group3)
        course4 = coursehelper.given_course_exists(course_group3)
        courseschedulehelper.given_course_schedule_exists(course1)
        courseschedulehelper.given_course_schedule_exists(course2)
        courseschedulehelper.given_course_schedule_exists(course3)
        courseschedulehelper.given_course_schedule_exists(course4)
        courseschedulehelper.given_course_schedule_exists(course4)

        # WHEN
        response = self.client.get(
            reverse('planner_coursegroups_courses_courseschedules_list',
                    kwargs={
                        'course_group': course_group3.pk,
                        'course': course4.pk
                    }))

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(CourseSchedule.objects.count(), 5)
        self.assertEqual(len(response.data), 2)
Exemplo n.º 5
0
    def test_get_categories(self):
        # GIVEN
        user1 = userhelper.given_a_user_exists()
        user2 = userhelper.given_a_user_exists_and_is_authenticated(
            self.client, username='******', email='*****@*****.**')
        course_group1 = coursegrouphelper.given_course_group_exists(user1)
        course_group2 = coursegrouphelper.given_course_group_exists(user2)
        course1 = coursehelper.given_course_exists(course_group1)
        course2 = coursehelper.given_course_exists(course_group2)
        course3 = coursehelper.given_course_exists(course_group2)
        categoryhelper.given_category_exists(course1)
        categoryhelper.given_category_exists(course2, title='Test Category 2')
        categoryhelper.given_category_exists(course3, title='Test Category 3')
        categoryhelper.given_category_exists(course3, title='Test Category 4')

        # WHEN
        response1 = self.client.get(reverse('planner_categories_list'))
        response2 = self.client.get(
            reverse('planner_coursegroups_courses_categories_list',
                    kwargs={
                        'course_group': course_group2.pk,
                        'course': course3.pk
                    }))

        # THEN
        self.assertEqual(response1.status_code, status.HTTP_200_OK)
        self.assertEqual(response2.status_code, status.HTTP_200_OK)
        self.assertEqual(Category.objects.count(), 4)
        self.assertEqual(len(response1.data), 3)
        self.assertEqual(len(response2.data), 2)
Exemplo n.º 6
0
    def test_update_course_by_id(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group1 = coursegrouphelper.given_course_group_exists(user)
        course_group2 = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group1)

        # WHEN
        data = {
            'title': 'some title',
            'room': 'my room',
            'credits': '3.00',
            'color': '#7bd148',
            'website': 'http://www.mywebsite.com',
            'is_online': True,
            'teacher_name': 'my teacher',
            'teacher_email': '*****@*****.**',
            'start_date': '2015-03-05',
            'end_date': '2015-07-09',
            'course_group': course_group2.pk
        }
        response = self.client.put(reverse(
            'planner_coursegroups_courses_detail',
            kwargs={
                'course_group': course_group1.pk,
                'pk': course.pk
            }),
                                   json.dumps(data),
                                   content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertDictContainsSubset(data, response.data)
        course = Course.objects.get(pk=course.pk)
        coursehelper.verify_course_matches_data(self, course, response.data)
Exemplo n.º 7
0
    def test_export_success(self):
        # GIVEN
        user1 = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        user2 = userhelper.given_a_user_exists(username='******', email='*****@*****.**')
        external_calendar = externalcalendarhelper.given_external_calendar_exists(user1)
        event1 = eventhelper.given_event_exists(user1)
        event2 = eventhelper.given_event_exists(user1)
        eventhelper.given_event_exists(user2)
        course_group1 = coursegrouphelper.given_course_group_exists(user1)
        course_group2 = coursegrouphelper.given_course_group_exists(user1)
        course_group3 = coursegrouphelper.given_course_group_exists(user2)
        course1 = coursehelper.given_course_exists(course_group1, room='')
        course2 = coursehelper.given_course_exists(course_group2)
        course3 = coursehelper.given_course_exists(course_group3)
        course_schedule1 = courseschedulehelper.given_course_schedule_exists(course1)
        course_schedule2 = courseschedulehelper.given_course_schedule_exists(course2)
        courseschedulehelper.given_course_schedule_exists(course3)
        category1 = categoryhelper.given_category_exists(course1, title='Uncategorized')
        category2 = categoryhelper.given_category_exists(course2)
        category3 = categoryhelper.given_category_exists(course3)
        material_group1 = materialgrouphelper.given_material_group_exists(user1)
        material_group2 = materialgrouphelper.given_material_group_exists(user2)
        material1 = materialhelper.given_material_exists(material_group1)
        materialhelper.given_material_exists(material_group2)
        homework1 = homeworkhelper.given_homework_exists(course1, category=category1, completed=True,
                                                         current_grade="20/30", materials=[material1])
        homework2 = homeworkhelper.given_homework_exists(course2, category=category2, current_grade="-1/100")
        homeworkhelper.given_homework_exists(course3, category=category3, completed=True, current_grade="-1/100")
        reminder = reminderhelper.given_reminder_exists(user1, homework=homework1)

        # WHEN
        response = self.client.get(reverse('importexport_resource_export'))
        data = json.loads(response.content.decode('utf-8'))

        # THEN
        course_group1 = CourseGroup.objects.get(pk=course_group1.pk)
        course_group2 = CourseGroup.objects.get(pk=course_group2.pk)
        course1 = Course.objects.get(pk=course1.pk)
        course2 = Course.objects.get(pk=course2.pk)
        category1 = Category.objects.get(pk=category1.pk)
        category2 = Category.objects.get(pk=category2.pk)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        externalcalendarhelper.verify_externalcalendar_matches_data(self, external_calendar,
                                                                    data['external_calendars'][0])
        eventhelper.verify_event_matches_data(self, event1, data['events'][0])
        eventhelper.verify_event_matches_data(self, event2, data['events'][1])
        coursegrouphelper.verify_course_group_matches_data(self, course_group1, data['course_groups'][0])
        coursegrouphelper.verify_course_group_matches_data(self, course_group2, data['course_groups'][1])
        coursehelper.verify_course_matches_data(self, course1, data['courses'][0])
        coursehelper.verify_course_matches_data(self, course2, data['courses'][1])
        courseschedulehelper.verify_course_schedule_matches(self, course_schedule1, data['course_schedules'][0])
        courseschedulehelper.verify_course_schedule_matches(self, course_schedule2, data['course_schedules'][1])
        categoryhelper.verify_category_matches_data(self, category1, data['categories'][1])
        categoryhelper.verify_category_matches_data(self, category2, data['categories'][0])
        homeworkhelper.verify_homework_matches_data(self, homework1, data['homework'][0])
        homeworkhelper.verify_homework_matches_data(self, homework2, data['homework'][1])
        reminderhelper.verify_reminder_matches_data(self, reminder, data['reminders'][0])
Exemplo n.º 8
0
    def test_homework_feed(self):
        # GIVEN
        user1 = userhelper.given_a_user_exists()
        user1.settings.enable_private_slug()
        user2 = userhelper.given_a_user_exists(username='******',
                                               email='*****@*****.**')
        user2.settings.enable_private_slug()
        course_group1 = coursegrouphelper.given_course_group_exists(user1)
        course_group2 = coursegrouphelper.given_course_group_exists(user1)
        course_group3 = coursegrouphelper.given_course_group_exists(user2)
        course1 = coursehelper.given_course_exists(course_group1, room='')
        course2 = coursehelper.given_course_exists(course_group2)
        course3 = coursehelper.given_course_exists(course_group3)
        category1 = categoryhelper.given_category_exists(course1,
                                                         title='Uncategorized')
        category2 = categoryhelper.given_category_exists(course2)
        category3 = categoryhelper.given_category_exists(course3)
        homework1 = homeworkhelper.given_homework_exists(course1,
                                                         category=category1,
                                                         completed=True,
                                                         current_grade="20/30")
        homework2 = homeworkhelper.given_homework_exists(
            course2, category=category2, current_grade="-1/100")
        homeworkhelper.given_homework_exists(course3,
                                             category=category3,
                                             completed=True,
                                             current_grade="-1/100")

        # WHEN
        response = self.client.get(
            reverse("feed_private_homework_ical",
                    kwargs={"slug": user1.settings.private_slug}))

        # THEN
        calendar = icalendar.Calendar.from_ical(
            response.content.decode('utf-8'))
        self.assertEqual(len(calendar.subcomponents), 2)
        self.assertEqual(calendar.subcomponents[0]['SUMMARY'], homework1.title)
        self.assertEqual(
            calendar.subcomponents[0]['DESCRIPTION'],
            'Class Info: {}\nGrade: {}\nComments: {}'.format(
                homework1.course.title, homework1.current_grade,
                homework1.comments))
        self.assertEqual(calendar.subcomponents[0]['DTSTART'].dt,
                         homework1.start)
        self.assertEqual(calendar.subcomponents[0]['DTEND'].dt, homework1.end)
        self.assertEqual(calendar.subcomponents[1]['SUMMARY'], homework2.title)
        self.assertEqual(calendar.subcomponents[1]['DTSTART'].dt,
                         homework2.start)
        self.assertEqual(calendar.subcomponents[1]['DTEND'].dt, homework2.end)
        self.assertEqual(
            calendar.subcomponents[1]['DESCRIPTION'],
            'Class Info: {} for {} in {}\nComments: {}'.format(
                homework2.category.title, homework2.course.title,
                homework2.course.room, homework2.comments))
Exemplo n.º 9
0
    def test_create_material(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        material_group = materialgrouphelper.given_material_group_exists(user)

        # WHEN
        data = {
            'title': 'some title',
            'status': enums.TO_SELL,
            'condition': enums.USED_POOR,
            'website': 'http://www.some-material.com',
            'price': '500.27',
            'details': 'N/A',
            'material_group': material_group.pk,
            'courses': [course.pk]
        }
        response = self.client.post(reverse(
            'planner_materialgroups_materials_list',
            kwargs={'material_group': material_group.pk}),
                                    json.dumps(data),
                                    content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Material.objects.count(), 1)
        material = Material.objects.get(pk=response.data['id'])
        materialhelper.verify_material_matches_data(self, material,
                                                    response.data)
Exemplo n.º 10
0
    def test_create_course(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)

        # WHEN
        data = {
            'title': 'some title',
            'room': 'my room',
            'credits': 3,
            'color': '#7bd148',
            'website': 'http://www.mywebsite.com',
            'is_online': True,
            'teacher_name': 'my teacher',
            'teacher_email': '*****@*****.**',
            'start_date': '2015-03-05',
            'end_date': '2015-07-09',
            'course_group': course_group.pk,
            # Read-only fields, unused in the POST but used in the validation of this dict afterward
            'current_grade': -1,
            'trend': None,
        }
        response = self.client.post(reverse(
            'planner_coursegroups_courses_list',
            kwargs={'course_group': course_group.pk}),
                                    json.dumps(data),
                                    content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Course.objects.count(), 1)
        course = Course.objects.get(pk=response.data['id'])
        coursehelper.verify_course_matches_data(self, course, data)
        coursehelper.verify_course_matches_data(self, course, response.data)
Exemplo n.º 11
0
    def test_update_multiple_courses(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course1 = coursehelper.given_course_exists(course_group)
        course2 = coursehelper.given_course_exists(course_group)
        material_group1 = materialgrouphelper.given_material_group_exists(user)
        material = materialhelper.given_material_exists(material_group1)

        # WHEN
        data = {
            'courses': [course1.pk, course2.pk],
            # Intentionally NOT changing these value
            'title': material.title,
            'material_group': material.material_group.pk
        }
        response = self.client.put(reverse(
            'planner_materialgroups_materials_detail',
            kwargs={
                'material_group': material_group1.pk,
                'pk': material.pk
            }),
                                   json.dumps(data),
                                   content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['courses'], data['courses'])
        material = Material.objects.get(pk=material.pk)
        materialhelper.verify_material_matches_data(self, material,
                                                    response.data)
Exemplo n.º 12
0
    def test_update_read_only_field_does_nothing(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        current_grade = course.current_grade
        trend = course.trend

        # WHEN
        data = {
            'current_grade': 23,
            'trend': 1.5,
            # Intentionally NOT changing these value
            'title': course.title,
            'credits': course.credits,
            'start_date': course.start_date.isoformat(),
            'end_date': course.end_date.isoformat(),
            'course_group': course.course_group.pk
        }
        response = self.client.put(reverse(
            'planner_coursegroups_courses_detail',
            kwargs={
                'course_group': course_group.pk,
                'pk': course.pk
            }),
                                   json.dumps(data),
                                   content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        course = Course.objects.get(pk=course.id)
        self.assertEqual(course.current_grade, current_grade)
        self.assertEqual(course.trend, trend)
Exemplo n.º 13
0
    def test_range_query(self):
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        coursehelper.given_course_exists(course_group,
                                         start_date=datetime.date(2016, 5, 8),
                                         end_date=datetime.date(2016, 8, 15))
        course2 = coursehelper.given_course_exists(
            course_group,
            start_date=datetime.date(2016, 8, 15),
            end_date=datetime.date(2016, 11, 20))
        course3 = coursehelper.given_course_exists(
            course_group,
            start_date=datetime.date(2017, 1, 8),
            end_date=datetime.date(2017, 3, 25))
        coursehelper.given_course_exists(course_group,
                                         start_date=datetime.date(2017, 3, 25),
                                         end_date=datetime.date(2017, 8, 15))

        response = self.client.get(
            reverse('planner_coursegroups_courses_list',
                    kwargs={'course_group': course_group.pk}) +
            '?start_date__gte={}&end_date__lte={}'.format(
                course2.start_date.isoformat(), course3.end_date.isoformat()))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 2)
Exemplo n.º 14
0
    def test_create_course_attachment(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        tmp_file = attachmenthelper.given_file_exists()

        # WHEN
        with open(tmp_file.name) as fp:
            data = {'course': course.pk, 'file[]': [fp]}
            response = self.client.post(reverse('planner_attachments_list'),
                                        data)

        # THEN
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['title'],
                         os.path.basename(tmp_file.name))
        self.assertEqual(response.data[0]['size'],
                         os.path.getsize(tmp_file.name))
        self.assertEqual(response.data[0]['course'], data['course'])
        self.assertEqual(Attachment.objects.count(), 1)
        attachment = Attachment.objects.get(pk=response.data[0]['id'])
        attachmenthelper.verify_attachment_matches_data(
            self, attachment, response.data[0])
Exemplo n.º 15
0
    def test_not_found(self):
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        coursehelper.given_course_exists(course_group)
        tmp_file = attachmenthelper.given_file_exists()

        # WHEN
        with open(tmp_file.name) as fp:
            data = {'course': '9999', 'file[]': [fp]}
            responses = [
                self.client.post(reverse('planner_attachments_list'), data),
                self.client.get(
                    reverse('planner_attachments_detail',
                            kwargs={'pk': '9999'})),
                self.client.delete(
                    reverse('planner_attachments_detail',
                            kwargs={'pk': '9999'}))
            ]

        # THEN
        for response in responses:
            if isinstance(response.data, list):
                self.assertEqual(response.status_code, status.HTTP_200_OK)
                self.assertEqual(len(response.data), 0)
            else:
                self.assertEqual(response.status_code,
                                 status.HTTP_404_NOT_FOUND)
                self.assertIn('not found', response.data['detail'].lower())
Exemplo n.º 16
0
    def test_patch_assumes_naive_datetime_to_utc(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        user.settings.time_zone = 'America/New_York'
        user.settings.save()
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        homework = homeworkhelper.given_homework_exists(course)

        # WHEN
        data = {
            'start': '2016-05-08 12:00:00',
            'end': '2016-05-08 14:00:00',
        }
        response = self.client.patch(reverse(
            'planner_coursegroups_courses_homework_detail',
            kwargs={
                'course_group': course_group.pk,
                'course': course.pk,
                'pk': homework.pk
            }),
                                     json.dumps(data),
                                     content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        homework = Homework.objects.get(pk=homework.pk)

        start = timezone.make_aware(parser.parse(data['start']), timezone.utc)
        end = timezone.make_aware(parser.parse(data['end']), timezone.utc)
        self.assertEquals(homework.start.isoformat(), start.isoformat())
        self.assertEquals(homework.end.isoformat(), end.isoformat())
Exemplo n.º 17
0
    def test_get_category_by_id(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        category = categoryhelper.given_category_exists(course)
        homeworkhelper.given_homework_exists(course, category=category)
        homeworkhelper.given_homework_exists(course,
                                             completed=True,
                                             category=category)
        homeworkhelper.given_homework_exists(course,
                                             completed=True,
                                             current_grade='25/30',
                                             category=category)

        # WHEN
        response = self.client.get(
            reverse('planner_coursegroups_courses_categories_detail',
                    kwargs={
                        'course_group': course_group.pk,
                        'course': course.pk,
                        'pk': category.pk
                    }))

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        category = Category.objects.get(pk=category.pk)
        categoryhelper.verify_category_matches_data(self, category,
                                                    response.data)
        self.assertEqual(response.data['num_homework'], 3)
        self.assertEqual(response.data['num_homework_graded'], 1)
Exemplo n.º 18
0
    def test_update_start_before_end_fails(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        homework = homeworkhelper.given_homework_exists(course)

        # WHEN
        data = {
            'start': '2016-05-08T12:00:00Z',
            'end': '2016-05-07T14:00:00Z',
        }
        response = self.client.patch(reverse(
            'planner_coursegroups_courses_homework_detail',
            kwargs={
                'course_group': course_group.pk,
                'course': course.pk,
                'pk': homework.pk
            }),
                                     json.dumps(data),
                                     content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertIn('must be before', response.data['non_field_errors'][0])
Exemplo n.º 19
0
    def test_weighted_grade_imbalanced(self):
        # GIVEN
        user = userhelper.given_a_user_exists()
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        category1 = categoryhelper.given_category_exists(course)
        category2 = categoryhelper.given_category_exists(course, title='Test Category 2')
        category3 = categoryhelper.given_category_exists(course, title='Test Category 3')

        # WHEN
        homeworkhelper.given_homework_exists(course, category=category1, completed=True, current_grade='10/10')
        homeworkhelper.given_homework_exists(course, category=category1, completed=True, current_grade='50/100')
        homeworkhelper.given_homework_exists(course, category=category2, completed=True, current_grade='40/50')
        homeworkhelper.given_homework_exists(course, category=category2, completed=True, current_grade='60/100')
        homeworkhelper.given_homework_exists(course, category=category3, completed=True, current_grade='200/200')

        # THEN
        course_group = CourseGroup.objects.get(pk=course_group.pk)
        course = Course.objects.get(pk=course.pk)
        category1 = Category.objects.get(pk=category1.pk)
        category2 = Category.objects.get(pk=category2.pk)
        category3 = Category.objects.get(pk=category3.pk)
        # 360/460 total points
        self.assertEqual(float(course_group.average_grade), 78.2609)
        self.assertEqual(float(course.current_grade), 78.2609)
        # 60/110 total points
        self.assertEqual(float(category1.average_grade), 54.5454)
        # 100/150 total points
        self.assertEqual(float(category2.average_grade), 66.6667)
        # 200/200 total points
        self.assertEqual(float(category3.average_grade), 100)
Exemplo n.º 20
0
    def test_create_category(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)

        # WHEN
        data = {
            'title': 'some title',
            'weight': 25,
            'color': '#7bd148',
            # Read-only fields, unused in the POST but used in the validation of this dict afterward
            'average_grade': -1,
            'grade_by_weight': 0,
            'trend': None
        }
        response = self.client.post(reverse(
            'planner_coursegroups_courses_categories_list',
            kwargs={
                'course_group': course_group.pk,
                'course': course.pk
            }),
                                    json.dumps(data),
                                    content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Category.objects.count(), 1)
        category = Category.objects.get(pk=response.data['id'])
        categoryhelper.verify_category_matches_data(self, category, data)
        categoryhelper.verify_category_matches_data(self, category,
                                                    response.data)
Exemplo n.º 21
0
    def test_update_category_by_id(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        category = categoryhelper.given_category_exists(course)

        # WHEN
        data = {'title': 'some title', 'weight': '25.00', 'color': '#7bd148'}
        response = self.client.put(reverse(
            'planner_coursegroups_courses_categories_detail',
            kwargs={
                'course_group': course_group.pk,
                'course': course.pk,
                'pk': category.pk
            }),
                                   json.dumps(data),
                                   content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertDictContainsSubset(data, response.data)
        category = Category.objects.get(pk=category.pk)
        categoryhelper.verify_category_matches_data(self, category,
                                                    response.data)
Exemplo n.º 22
0
    def test_update_category_exceeds_weight_course_100_fails(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        categoryhelper.given_category_exists(course, weight=25)
        categoryhelper.given_category_exists(course,
                                             title='Test Category 2',
                                             weight=25)
        categoryhelper.given_category_exists(course,
                                             title='Test Category 3',
                                             weight=25)
        category = categoryhelper.given_category_exists(
            course, title='Test Category 4', weight=25)

        # WHEN
        data = {
            'title': 'some title',
            'weight': 25.0000000001,
            'color': '#7bd148'
        }
        response = self.client.put(reverse(
            'planner_coursegroups_courses_categories_detail',
            kwargs={
                'course_group': course_group.pk,
                'course': course.pk,
                'pk': category.pk
            }),
                                   json.dumps(data),
                                   content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertIn('weight', response.data)
Exemplo n.º 23
0
    def test_trend_negative(self):
        # GIVEN
        user = userhelper.given_a_user_exists()
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        category = categoryhelper.given_category_exists(course)

        # WHEN
        homeworkhelper.given_homework_exists(course, category=category, completed=True,
                                             start=datetime.datetime(2017, 4, 8, 20, 0, tzinfo=pytz.utc),
                                             end=datetime.datetime(2017, 4, 8, 20, 30, tzinfo=pytz.utc),
                                             current_grade='400/100')
        homeworkhelper.given_homework_exists(course, category=category, completed=True,
                                             start=datetime.datetime(2017, 4, 9, 20, 0, tzinfo=pytz.utc),
                                             end=datetime.datetime(2017, 4, 9, 20, 30, tzinfo=pytz.utc),
                                             current_grade='200/100')
        homeworkhelper.given_homework_exists(course, category=category, completed=True,
                                             start=datetime.datetime(2017, 4, 10, 20, 0, tzinfo=pytz.utc),
                                             end=datetime.datetime(2017, 4, 10, 20, 30, tzinfo=pytz.utc),
                                             current_grade='0/100')

        # THEN
        course_group = CourseGroup.objects.get(pk=course_group.pk)
        course = Course.objects.get(pk=course.pk)
        category = Category.objects.get(pk=category.pk)
        self.assertEqual(float(course_group.trend), -1)
        self.assertEqual(float(course.trend), -1)
        self.assertEqual(float(category.trend), -1)
    def test_update_read_only_field_does_nothing(self):
        # GIVEN
        user1 = userhelper.given_a_user_exists()
        user2 = userhelper.given_a_user_exists_and_is_authenticated(self.client, username='******',
                                                                    email='*****@*****.**')
        course_group = coursegrouphelper.given_course_group_exists(user2)
        average_grade = course_group.average_grade
        trend = course_group.trend
        private_slug = course_group.private_slug

        # WHEN
        data = {
            'average_grade': 23,
            'trend': 1.5,
            'private_slug': 'new_slug',
            'user': user1.pk,
            # Intentionally NOT changing these value
            'title': course_group.title,
            'start_date': course_group.start_date.isoformat(),
            'end_date': course_group.end_date.isoformat()
        }
        response = self.client.put(reverse('planner_coursegroups_detail', kwargs={'pk': course_group.pk}),
                                   json.dumps(data), content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        course_group = CourseGroup.objects.get(pk=course_group.id)
        self.assertEqual(course_group.average_grade, average_grade)
        self.assertEqual(course_group.trend, trend)
        self.assertEqual(course_group.private_slug, private_slug)
        self.assertEqual(course_group.get_user().pk, user2.pk)
Exemplo n.º 25
0
    def test_patch_converts_to_utc(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        homework = homeworkhelper.given_homework_exists(course)

        # WHEN
        data = {
            'start': '2016-05-08T12:00:00-0500',
            'end': '2016-05-08T14:00:00-0500',
        }
        response = self.client.patch(reverse(
            'planner_coursegroups_courses_homework_detail',
            kwargs={
                'course_group': course_group.pk,
                'course': course.pk,
                'pk': homework.pk
            }),
                                     json.dumps(data),
                                     content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        homework = Homework.objects.get(pk=homework.pk)
        self.assertEquals(
            homework.start.isoformat(),
            parser.parse(data['start']).astimezone(timezone.utc).isoformat())
        self.assertEquals(
            homework.end.isoformat(),
            parser.parse(data['end']).astimezone(timezone.utc).isoformat())
Exemplo n.º 26
0
    def test_get_course_by_id(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        homeworkhelper.given_homework_exists(course)
        homeworkhelper.given_homework_exists(course, completed=True)
        homeworkhelper.given_homework_exists(course,
                                             completed=True,
                                             current_grade='25/30')

        # WHEN
        response = self.client.get(
            reverse('planner_coursegroups_courses_detail',
                    kwargs={
                        'course_group': course_group.pk,
                        'pk': course.pk
                    }))

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        course = Course.objects.get(pk=course.pk)
        coursehelper.verify_course_matches_data(self, course, response.data)
        self.assertEqual(response.data['num_days'], 122)
        self.assertEqual(response.data['num_days_completed'],
                         (datetime.datetime.now().date() -
                          course.start_date).days)
        self.assertEqual(response.data['num_homework'], 3)
        self.assertEqual(response.data['num_homework_completed'], 2)
        self.assertEqual(response.data['num_homework_graded'], 1)
Exemplo n.º 27
0
    def test_update_invalid_fraction_fails(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        homework = homeworkhelper.given_homework_exists(course)

        # WHEN
        response1 = self.client.patch(
            reverse('planner_coursegroups_courses_homework_detail',
                    kwargs={
                        'course_group': course_group.pk,
                        'course': course.pk,
                        'pk': homework.pk
                    }),
            json.dumps({'current_grade': 'not-a-fraction'}),
            content_type='application/json')
        response2 = self.client.patch(
            reverse('planner_coursegroups_courses_homework_detail',
                    kwargs={
                        'course_group': course_group.pk,
                        'course': course.pk,
                        'pk': homework.pk
                    }),
            json.dumps({'current_grade': 'invalid/4'}),
            content_type='application/json')

        # THEN
        self.assertEqual(response1.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertIn('current_grade', response1.data)
        self.assertIn('current_grade', response2.data)
Exemplo n.º 28
0
    def test_update_start_before_end_fails(self):
        # GIVEN
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)

        # WHEN
        data = {
            'start_date': '2017-08-12',
            'end_date': '2017-07-12',
            # Intentionally NOT changing these value
            'title': course.title,
            'credits': course.credits,
            'course_group': course.course_group.pk,
        }
        response = self.client.put(reverse(
            'planner_coursegroups_courses_detail',
            kwargs={
                'course_group': course_group.pk,
                'pk': course.pk
            }),
                                   json.dumps(data),
                                   content_type='application/json')

        # THEN
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertIn('must be before', response.data['non_field_errors'][0])
Exemplo n.º 29
0
    def test_range_query(self):
        user = userhelper.given_a_user_exists_and_is_authenticated(self.client)
        course_group = coursegrouphelper.given_course_group_exists(user)
        course = coursehelper.given_course_exists(course_group)
        homeworkhelper.given_homework_exists(
            course,
            start=datetime.datetime(2017, 5, 8, 16, 0, 0, tzinfo=timezone.utc),
            end=datetime.datetime(2017, 5, 8, 17, 0, 0, tzinfo=timezone.utc))
        homework2 = homeworkhelper.given_homework_exists(
            course,
            start=datetime.datetime(2017, 5, 8, 17, 0, 0, tzinfo=timezone.utc),
            end=datetime.datetime(2017, 5, 8, 18, 0, 0, tzinfo=timezone.utc))
        homeworkhelper.given_homework_exists(
            course,
            start=datetime.datetime(2017, 5, 8, 18, 30, 0,
                                    tzinfo=timezone.utc),
            end=datetime.datetime(2017, 5, 8, 19, 0, 0, tzinfo=timezone.utc))
        homework4 = homeworkhelper.given_homework_exists(
            course,
            start=datetime.datetime(2017, 5, 8, 19, 30, 0,
                                    tzinfo=timezone.utc),
            end=datetime.datetime(2017, 5, 8, 21, 0, 0, tzinfo=timezone.utc))

        response = self.client.get(
            reverse('planner_homework_list') +
            '?start__gte={}&end__lt={}'.format(
                quote(homework2.start.isoformat()),
                quote(homework4.end.isoformat())))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 2)
Exemplo n.º 30
0
    def test_error_on_object_owned_by_another_user(self):
        # GIVEN
        user1 = userhelper.given_a_user_exists()
        userhelper.given_a_user_exists_and_is_authenticated(
            self.client, username='******', email='*****@*****.**')
        course_group = coursegrouphelper.given_course_group_exists(user1)

        # WHEN
        responses = [
            self.client.get(
                reverse('planner_coursegroups_detail',
                        kwargs={'pk': course_group.pk})),
            self.client.put(
                reverse('planner_coursegroups_detail',
                        kwargs={'pk': course_group.pk})),
            self.client.delete(
                reverse('planner_coursegroups_detail',
                        kwargs={'pk': course_group.pk}))
        ]

        # THEN
        self.assertTrue(
            CourseGroup.objects.filter(pk=course_group.pk,
                                       user_id=user1.pk).exists())
        for response in responses:
            self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)