Exemplo n.º 1
0
 def test_guest_user_exists(self):
     """
     Tests the case where a guest is invited to a course,
     and the user already has a Locus Tempus account.
     """
     addr = '*****@*****.**'
     UserFactory.create(first_name='Guest',
                        last_name='Student',
                        username='******',
                        email=addr)
     self.assertTrue(
         self.client.login(username=self.faculty.username, password='******'))
     response = self.client.post(reverse('course-roster-invite-user',
                                         args=[self.sandbox_course.pk]), {
                                             'uni-TOTAL_FORMS': '1',
                                             'uni-INITIAL_FORMS': '0',
                                             'uni-0-invitee': '',
                                             'email-TOTAL_FORMS': '1',
                                             'email-INITIAL_FORMS': '0',
                                             'email-0-invitee': addr,
                                         },
                                 follow=True)
     self.assertContains(
         response,
         'An email was sent to {} notifying the user.'.format(addr))
Exemplo n.º 2
0
    def test_has_permissions_put(self):
        """Tests POST permissions"""
        perm = IsResponseOwnerOrFaculty()
        p = ProjectFactory.create(course=self.sandbox_course)
        a = ActivityFactory.create(project=p)
        req = RequestFactory().put(reverse('api-response-list'),
                                   {'activity': a.pk})
        req.user = AnonymousUser()
        # Patch the request object, as it doesn't seem to create the
        # data attribute as expected
        req.data = {'activity': a.pk}

        # Anon
        self.assertFalse(perm.has_permission(req, None))

        # Faculty
        req.user = self.faculty
        self.assertFalse(perm.has_permission(req, None))

        # Unrelated Student
        unrelated_student = UserFactory.create(first_name='Student',
                                               last_name='Two',
                                               email='*****@*****.**')
        self.registrar_course.group.user_set.add(unrelated_student)
        req.user = unrelated_student
        self.assertFalse(perm.has_permission(req, None))

        # Related Student
        req.user = self.student
        self.assertTrue(perm.has_permission(req, None))
Exemplo n.º 3
0
    def test_has_object_permissions_patch(self):
        """Tests PATCH permissions"""
        perm = IsFeedbackFacultyOrStudentRecipient()
        p = ProjectFactory.create(course=self.sandbox_course)
        a = ActivityFactory.create(project=p)
        r = ResponseFactory.create(activity=a, owners=[self.student])
        r.status = r.SUBMITTED
        r.save()
        f = FeedbackFactory(response=r)
        req = RequestFactory().patch(
            reverse('api-feedback-detail', args=[f.pk]), {'feedback': 'foo'})

        # Anon
        req.user = AnonymousUser()
        self.assertFalse(perm.has_object_permission(req, None, r.feedback))

        # Related Faculty
        req.user = self.faculty
        self.assertTrue(perm.has_object_permission(req, None, r.feedback))

        # Unrelated Faculty
        # Checks that faculty in a different course can not
        # patch an object
        unrelated_faculty = UserFactory.create(first_name='Faculty',
                                               last_name='Two',
                                               email='*****@*****.**')
        self.registrar_course.faculty_group.user_set.add(unrelated_faculty)
        req.user = unrelated_faculty
        self.assertFalse(perm.has_object_permission(req, None, r.feedback))

        # Student
        req.user = self.student
        self.assertFalse(perm.has_object_permission(req, None, r.feedback))
Exemplo n.º 4
0
    def test_has_permissions_put(self):
        """Tests POST permissions"""
        perm = IsFeedbackFacultyOrStudentRecipient()
        p = ProjectFactory.create(course=self.sandbox_course)
        a = ActivityFactory.create(project=p)
        r = ResponseFactory.create(activity=a, owners=[self.student])
        req = RequestFactory().post(reverse('api-feedback-list'), {
            'response': r.pk,
            'body': 'foobar'
        })
        req.query_params = dict()
        req.data = {'response': r.pk, 'body': 'foobar'}

        # Anon
        req.user = AnonymousUser()
        self.assertFalse(perm.has_permission(req, None))

        # Faculty
        req.user = self.faculty
        self.assertTrue(perm.has_permission(req, None))

        # Unrelated Student
        unrelated_student = UserFactory.create(first_name='Student',
                                               last_name='Two',
                                               email='*****@*****.**')
        self.registrar_course.group.user_set.add(unrelated_student)
        req.user = unrelated_student
        self.assertFalse(perm.has_permission(req, None))

        # Related Student
        req.user = self.student
        self.assertFalse(perm.has_permission(req, None))
Exemplo n.º 5
0
    def test_non_course_user(self):
        user = UserFactory.create()
        self.assertTrue(
            self.client.login(username=user.username, password='******'))

        project = self.sandbox_course.projects.first()
        response = self.client.get(
            reverse('api-project-detail', args=[project.pk]))
        self.assertEqual(response.status_code, 403)
Exemplo n.º 6
0
 def setUp(self):
     self.setup_course()
     self.layer_permission_helper = \
         LayerPermission().layer_permission_helper
     self.anon = AnonymousUser()
     self.classmate = UserFactory.create(first_name='Student',
                                         last_name='Two',
                                         email='*****@*****.**')
     self.registrar_course.group.user_set.add(self.classmate)
     self.classmate_response = ResponseFactory.create(
         activity=self.sandbox_course_activity, owners=[self.classmate])
     self.classmate_layer = LayerFactory.create(
         title='Classmate layer', content_object=self.classmate_response)
Exemplo n.º 7
0
    def test_has_permissions_obj_get(self):
        """Tests GET permissions"""
        perm = IsFeedbackFacultyOrStudentRecipient()
        self.sandbox_course_response.status = \
            self.sandbox_course_response.SUBMITTED
        self.sandbox_course_response.save()
        feedback = FeedbackFactory(response=self.sandbox_course_response)
        req = RequestFactory().get(
            reverse('api-feedback-detail', args=[feedback.pk]))

        # Anon
        req.user = AnonymousUser()
        self.assertFalse(perm.has_object_permission(req, None, feedback))

        # Faculty
        req.user = self.faculty
        self.assertTrue(perm.has_object_permission(req, None, feedback))

        # Student
        req.user = self.student
        self.assertTrue(perm.has_object_permission(req, None, feedback))

        # Student in course, but not related to the response
        classmate = UserFactory.create(first_name='Student',
                                       last_name='Two',
                                       email='*****@*****.**')
        self.sandbox_course.group.user_set.add(classmate)
        req.user = classmate
        self.assertFalse(perm.has_object_permission(req, None, feedback))

        # Non-course user
        unrelated_student = UserFactory.create(first_name='Student',
                                               last_name='Two',
                                               email='*****@*****.**')
        req.user = unrelated_student
        self.assertFalse(perm.has_object_permission(req, None, feedback))
Exemplo n.º 8
0
    def test_unique_owner_per_activity(self):
        """
        Test that a user can not be added as an owner to multiple responses
        for a single activity.
        """
        # Add a new activity response to an existing activity
        student: User = UserFactory.create(first_name='Student',
                                           last_name='One',
                                           email='*****@*****.**')
        course = RegistrarCourseFactory.create()
        project = ProjectFactory.create(course=course)
        activity = ActivityFactory.create(project=project)
        ResponseFactory.create(activity=activity, owners=[student])

        # Try adding self.student to this new response
        with self.assertRaises(IntegrityError):
            ResponseFactory.create(activity=activity, owners=[student])
Exemplo n.º 9
0
    def test_guest_user_course_affil(self):
        """
        Tests that a guest user is added to the
        course upon registering for an account
        """
        affil = GuestUserAffil(guest_email='*****@*****.**',
                               course=self.sandbox_course,
                               invited_by=self.faculty)
        affil.save()
        guest = UserFactory.create(first_name='Guest',
                                   last_name='User',
                                   username='******',
                                   email='*****@*****.**')

        mock_sender = MagicMock()
        user_activated.send(sender=mock_sender, user=guest)
        self.assertTrue(self.sandbox_course.is_true_member(guest))
Exemplo n.º 10
0
    def test_unique_owner_per_response(self):
        """
        Test that a user can not be added as an owner to response
        multiple times.

        Noting that Django's default behavior is to prevent
        multiple instances of many to many relation.
        """
        student: User = UserFactory.create(first_name='Student',
                                           last_name='One',
                                           email='*****@*****.**')
        course = RegistrarCourseFactory.create()
        project = ProjectFactory.create(course=course)
        activity = ActivityFactory.create(project=project)
        response = ResponseFactory.create(activity=activity, owners=[student])
        self.assertEqual(response.owners.count(), 1)
        response.owners.add(student)
        self.assertEqual(response.owners.count(), 1)
Exemplo n.º 11
0
    def test_submitted_by_on(self):
        student: User = UserFactory.create(
            first_name='Student',
            last_name='One',
            email='*****@*****.**'
        )
        course = RegistrarCourseFactory.create()
        project = ProjectFactory.create(course=course)
        activity = ActivityFactory.create(project=project)
        response = ResponseFactory.create(
            activity=activity, owners=[student])

        # Assert that submitted_* are null
        self.assertIsNone(response.submitted_at)

        # Then submit the assignment
        response.status = response.SUBMITTED
        response.save()

        # Check that the fields have updated
        self.assertIsNot(response.submitted_at, None)
Exemplo n.º 12
0
 def test_no_course(self):
     """
     Tests that the courses created in the setup method
     do not appear for a new user not in those courses.
     """
     student = UserFactory.create(
         first_name='Student',
         last_name='Two',
         username='******',
         email='*****@*****.**'
     )
     self.assertTrue(
         self.client.login(
             username=student.username,
             password='******'
         )
     )
     response = self.client.get("/dashboard/")
     self.assertEqual(response.status_code, 200)
     self.assertEqual(
         response.context['courses'].count(), 0)
Exemplo n.º 13
0
    def test_has_object_permissions_patch(self):
        """Tests PATCH permissions"""
        perm = IsResponseOwnerOrFaculty()
        req = RequestFactory().patch(
            reverse('api-response-detail',
                    args=[self.sandbox_course_response.pk]),
            {'reflection': 'foo'})

        # Anon
        req.user = AnonymousUser()
        self.assertFalse(
            perm.has_object_permission(req, None,
                                       self.sandbox_course_response))

        # Related Faculty
        req.user = self.faculty
        self.assertFalse(
            perm.has_object_permission(req, None,
                                       self.sandbox_course_response))

        # Unrelated Faculty
        # Checks that faculty in a different course can not
        # patch an object
        unrelated_faculty = UserFactory.create(first_name='Faculty',
                                               last_name='Two',
                                               email='*****@*****.**')
        self.registrar_course.faculty_group.user_set.add(unrelated_faculty)
        req.user = unrelated_faculty
        self.assertFalse(
            perm.has_object_permission(req, None,
                                       self.sandbox_course_response))

        # Student
        req.user = self.student
        self.assertTrue(
            perm.has_object_permission(req, None,
                                       self.sandbox_course_response))
Exemplo n.º 14
0
def reset_test_models():
    call_command('flush', verbosity=0, interactive=False)
    # Create some models
    UserFactory.create(username='******',
                       first_name='Super',
                       last_name='User',
                       email='*****@*****.**',
                       is_superuser=True)
    # Sandbox Course
    c1: Course = SandboxCourseFactory.create()
    s1: User = UserFactory.create(username='******',
                                  first_name='Student',
                                  last_name='One',
                                  email='*****@*****.**')
    c1.group.user_set.add(s1)
    f1: User = UserFactory.create(username='******',
                                  first_name='Faculty',
                                  last_name='One',
                                  email='*****@*****.**')
    c1.group.user_set.add(f1)
    c1.faculty_group.user_set.add(f1)

    project = ProjectFactory.create(course=c1)
    activity = ActivityFactory.create(project=project)
    ResponseFactory.create(activity=activity, owners=[s1])

    # Registrar Course
    c2: Course = RegistrarCourseFactory.create()
    s2: User = UserFactory.create(username='******',
                                  first_name='Student',
                                  last_name='Two',
                                  email='*****@*****.**')
    c2.group.user_set.add(s2)
    f2: User = UserFactory.create(username='******',
                                  first_name='Faculty',
                                  last_name='Two',
                                  email='*****@*****.**')
    c2.group.user_set.add(f2)
    c2.faculty_group.user_set.add(f2)
Exemplo n.º 15
0
def reset_test_models():
    call_command('flush', verbosity=0, interactive=False)
    # Enable some flags
    Flag.objects.create(name='share_response_layers', everyone=True)

    # Create some models
    UserFactory.create(username='******',
                       first_name='Super',
                       last_name='User',
                       email='*****@*****.**',
                       is_superuser=True,
                       is_staff=True)
    # Sandbox Course
    c1: Course = SandboxCourseFactory.create(title='Sandbox Workspace')
    s1: User = UserFactory.create(username='******',
                                  first_name='Student',
                                  last_name='One',
                                  email='*****@*****.**')
    c1.group.user_set.add(s1)
    s1: User = UserFactory.create(username='******',
                                  first_name='Student',
                                  last_name='Three',
                                  email='*****@*****.**')
    c1.group.user_set.add(s1)

    f1: User = UserFactory.create(username='******',
                                  first_name='Faculty',
                                  last_name='One',
                                  email='*****@*****.**')
    c1.group.user_set.add(f1)
    c1.faculty_group.user_set.add(f1)

    a1: User = UserFactory.create(username='******',
                                  first_name='Author',
                                  last_name='One',
                                  email='*****@*****.**')
    c1.group.user_set.add(a1)
    c1.faculty_group.user_set.add(a1)

    project = ProjectFactory.create(course=c1, title='Activity One')
    activity = ActivityFactory.create(project=project)
    ResponseFactory.create(activity=activity, owners=[s1])

    # Registrar Course - generates a project too
    c2: Course = RegistrarCourseFactory.create()
    s2: User = UserFactory.create(username='******',
                                  first_name='Student',
                                  last_name='Two',
                                  email='*****@*****.**')
    c2.group.user_set.add(s2)
    f2: User = UserFactory.create(username='******',
                                  first_name='Faculty',
                                  last_name='Two',
                                  email='*****@*****.**')
    c2.group.user_set.add(f2)
    c2.faculty_group.user_set.add(f2)

    # No affiliations
    UserFactory.create(username='******',
                       first_name='Faculty',
                       last_name='Three',
                       email='*****@*****.**')
Exemplo n.º 16
0
 def setUp(self):
     self.user = UserFactory.create()
     self.course = SandboxCourseFactory.create()