def setup(self, db):
     self.site = Site.objects.first()
     self.course_overview = CourseOverviewFactory()
     self.users = [UserFactory(), UserFactory()]
     self.course_access_roles = [
         CourseAccessRoleFactory(user=self.users[0],
                                 course_id=self.course_overview.id,
                                 role='staff'),
         CourseAccessRoleFactory(user=self.users[1],
                                 course_id=self.course_overview.id,
                                 role='administrator'),
     ]
     self.serializer = GeneralCourseDataSerializer(
         instance=self.course_overview)
     self.expected_fields = [
         'course_id',
         'course_name',
         'course_code',
         'org',
         'start_date',
         'end_date',
         'self_paced',
         'staff',
         'metrics',
     ]
Example #2
0
 def retrieve(self, request, *args, **kwargs):
     course_id_str = kwargs.get('pk', '')
     course_key = CourseKey.from_string(course_id_str.replace(' ', '+'))
     site = django.contrib.sites.shortcuts.get_current_site(request)
     if figures.helpers.is_multisite():
         if site != figures.sites.get_site_for_course(course_key):
             # Raising NotFound instead of PermissionDenied
             raise NotFound()
     course_overview = get_object_or_404(CourseOverview, pk=course_key)
     return Response(GeneralCourseDataSerializer(course_overview).data)
class TestGeneralCourseDataSerializer(object):
    '''
    TODO: Verify that learner roles are NOT in CourseAccessRole
    If learner roles can be in this model, then we need to add test for verifying
    that learner roles are not in the staff list of the general course data
    '''
    @pytest.fixture(autouse=True)
    def setup(self, db):
        self.site = Site.objects.first()
        self.course_overview = CourseOverviewFactory()
        self.users = [UserFactory(), UserFactory()]
        self.course_access_roles = [
            CourseAccessRoleFactory(user=self.users[0],
                                    course_id=self.course_overview.id,
                                    role='staff'),
            CourseAccessRoleFactory(user=self.users[1],
                                    course_id=self.course_overview.id,
                                    role='administrator'),
        ]
        self.serializer = GeneralCourseDataSerializer(
            instance=self.course_overview)
        self.expected_fields = [
            'course_id',
            'course_name',
            'course_code',
            'org',
            'start_date',
            'end_date',
            'self_paced',
            'staff',
            'metrics',
        ]

    def test_has_fields(self):
        '''Tests that the serialized general course  data has specific keys and values
        '''
        data = self.serializer.data
        assert set(data.keys()) == set(self.expected_fields)

        # This is to make sure that the serializer retrieves the correct nested
        # model (UserProfile) data
        assert data['course_id'] == str(self.course_overview.id)
        assert data['course_name'] == self.course_overview.display_name
        assert data['course_code'] == self.course_overview.number
        assert data['org'] == self.course_overview.org
        assert parse(data['start_date']) == self.course_overview.start
        assert parse(data['end_date']) == self.course_overview.end
        assert data['self_paced'] == self.course_overview.self_paced

    def test_get_metrics_with_cdm_records(self):
        '''Tests we get the data for the latest CourseDailyMetrics object
        '''
        dates = [
            '2018-01-01',
            '2018-02-01',
        ]
        [
            CourseDailyMetricsFactory(site=self.site,
                                      course_id=self.course_overview.id,
                                      date_for=date) for date in dates
        ]
        data = self.serializer.get_metrics(self.course_overview)
        assert data['date_for'] == dates[-1]

    def test_get_metrics_with_no_cdm_records(self):
        data = self.serializer.get_metrics(self.course_overview)
        assert not data