예제 #1
0
    def setUp(self):
        # create 2 users
        self.user1 = hydroshare.create_account('*****@*****.**',
                                               username='******',
                                               first_name='user1_first',
                                               last_name='user1_last',
                                               superuser=False,
                                               groups=[])

        self.user2 = hydroshare.create_account('*****@*****.**',
                                               username='******',
                                               first_name='user2_first',
                                               last_name='user2_last',
                                               superuser=False,
                                               groups=[])

        # create a resource
        self.res = hydroshare.create_resource('GenericResource', self.user1,
                                              'My Test Resource')

        # create comment from user 2
        self.comment = hydroshare.comment_on_resource(self.res.short_id,
                                                      "comment by user2",
                                                      user=self.user2)

        # endorse resource
        self.endorse1_res = hydroshare.endorse_resource(
            self.res.short_id, self.user2)
        self.endorse2_res = hydroshare.endorse_resource(
            self.res.short_id, self.user1)

        # endorse comment
        self.endorse1_com = hydroshare.endorse_comment(self.comment.id,
                                                       self.res.short_id,
                                                       self.user1)
        self.endorse2_com = hydroshare.endorse_comment(self.comment.id,
                                                       self.res.short_id,
                                                       self.user2)
예제 #2
0
    def setUp(self):
        # create 2 users
        self.user1 = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='user1_first',
            last_name='user1_last',
            superuser=False,
            groups=[]
        )

        self.user2 = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='user2_first',
            last_name='user2_last',
            superuser=False,
            groups=[]
        )

        # create a resource
        self.res = hydroshare.create_resource(
            'GenericResource',
            self.user1,
            'My Test Resource'
        )

        # create comment from user 2
        self.comment = hydroshare.comment_on_resource(self.res.short_id, "comment by user2", user=self.user2)

        # endorse resource
        self.endorse1_res = hydroshare.endorse_resource(self.res.short_id, self.user2)
        self.endorse2_res = hydroshare.endorse_resource(self.res.short_id, self.user1)

        # endorse comment
        self.endorse1_com = hydroshare.endorse_comment(self.comment.id, self.res.short_id, self.user1)
        self.endorse2_com = hydroshare.endorse_comment(self.comment.id, self.res.short_id, self.user2)
예제 #3
0
    def test_endorse_comment(self):
        # create a user to be used for creating the resource
        user_creator = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='Creator_FirstName',
            last_name='Creator_LastName',
            superuser=False,
            groups=[])
        user_commenter_1 = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='Commenter_1_FirstName',
            last_name='Commenter_1_LastName',
            superuser=False,
            groups=[])

        user_rater_1 = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='Rater_1_FirstName',
            last_name='Rater_1_LastName',
            superuser=False,
            groups=[])

        user_rater_2 = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='Rater_2_FirstName',
            last_name='Rater_2_LastName',
            superuser=False,
            groups=[])

        # create a resource
        new_resource = hydroshare.create_resource('GenericResource',
                                                  user_creator,
                                                  'My Test Resource')

        resource_2 = hydroshare.create_resource('GenericResource',
                                                user_creator,
                                                'My 2nd Test Resource')
        # first comment on resource
        comment_text = "comment by commenter_1"
        comment = hydroshare.comment_on_resource(new_resource.short_id,
                                                 comment_text,
                                                 user=user_commenter_1)

        # at this point there should not be any ratings for this new comment
        ratings = Rating.objects.filter(object_pk=comment.id)
        self.assertEqual(len(ratings), 0)

        # rate the comment - this is the api call we are testing
        rating = hydroshare.endorse_comment(comment.id, new_resource.short_id,
                                            user_rater_1)
        self.assertEqual(rating.content_object, comment)
        self.assertEqual(rating.user, user_rater_1)
        # test that the rating value is 1 (+1)
        self.assertEqual(rating.value, 1)

        # at this point there should be one ratings for this comment
        ratings = Rating.objects.filter(object_pk=comment.id)
        # test that the rating value is 1 (+1)
        self.assertEqual(len(ratings), 1)

        # test the user can't rate the same comment twice - no new rating object is created
        rating = hydroshare.endorse_comment(comment.id, new_resource.short_id,
                                            user_rater_1)
        self.assertEqual(rating.content_object, comment)
        self.assertEqual(rating.user, user_rater_1)
        # test that the rating value is 1 (+1)
        self.assertEqual(rating.value, 1)

        # at this point there should be one ratings for this comment
        ratings = Rating.objects.filter(object_pk=comment.id)
        self.assertEqual(len(ratings), 1)
        # test that the rating value is 1 (+1)
        self.assertEqual(ratings[0].value, 1)

        # rate the same comment by another user- this is the api call we are testing
        rating = hydroshare.endorse_comment(comment.id, new_resource.short_id,
                                            user_rater_2)
        self.assertEqual(rating.content_object, comment)
        self.assertEqual(rating.user, user_rater_2)
        # test that the rating value is 1 (+1)
        self.assertEqual(rating.value, 1)

        # at this point there should be two ratings for this comment
        ratings = Rating.objects.filter(object_pk=comment.id)
        self.assertEqual(len(ratings), 2)
        # test that the rating value is 1 (+1) for each of these ratings
        self.assertEqual(ratings[0].value, 1)
        self.assertEqual(ratings[1].value, 1)

        # test removing a rating from a comment
        hydroshare.endorse_comment(comment.id,
                                   new_resource.short_id,
                                   user_rater_2,
                                   endorse=False)

        # at this point there should be one ratings for this comment
        ratings = Rating.objects.filter(object_pk=comment.id)
        self.assertEqual(len(ratings), 1)
        # test that the rating value is 1 (+1)
        self.assertEqual(ratings[0].value, 1)

        # there should not be any ratings by user_rater_2 for this comment
        ratings = Rating.objects.filter(object_pk=comment.id,
                                        user=user_rater_2)
        self.assertEqual(len(ratings), 0)

        # test that ValueError exception raised if the comment does not exist for the specified resource
        self.assertRaises(
            ValueError, lambda: hydroshare.endorse_comment(
                comment.id, resource_2.short_id, user_rater_2))