<s> """<STR_LIT>""" <EOL> import json <EOL> from django . test import TestCase <EOL> from django . contrib . auth . models import AnonymousUser <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . exceptions import PermissionDenied <EOL> from nose . tools import raises <EOL> from rest_framework . test import APITestCase <EOL> from rest_framework . test import APIRequestFactory , force_authenticate <EOL> from rest_framework import status <EOL> from rest_framework . renderers import JSONRenderer <EOL> from geokey . projects . tests . model_factories import UserFactory , ProjectFactory <EOL> from geokey . projects . models import Project <EOL> from geokey . contributions . models import Comment , Observation <EOL> from geokey . users . models import User <EOL> from geokey . users . tests . model_factories import UserGroupFactory <EOL> from . . model_factories import ObservationFactory , CommentFactory <EOL> from geokey . contributions . views . comments import ( <EOL> CommentsAPIView , <EOL> SingleCommentAPIView , <EOL> CommentAbstractAPIView <EOL> ) <EOL> class CommentAbstractAPIViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . factory = APIRequestFactory ( ) <EOL> self . admin = UserFactory . create ( ) <EOL> self . creator = UserFactory . create ( ) <EOL> self . moderator = UserFactory . create ( ) <EOL> self . commenter = UserFactory . create ( ) <EOL> self . project = ProjectFactory ( <EOL> add_admins = [ self . admin ] , <EOL> add_contributors = [ self . creator , self . commenter ] <EOL> ) <EOL> self . moderators = UserGroupFactory ( add_users = [ self . moderator ] , ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : True <EOL> } ) <EOL> self . contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . creator , <EOL> '<STR_LIT:status>' : '<STR_LIT>' <EOL> } ) <EOL> self . comment = CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution , <EOL> '<STR_LIT>' : self . commenter <EOL> } ) <EOL> def render ( self , response ) : <EOL> response . accepted_renderer = JSONRenderer ( ) <EOL> response . accepted_media_type = '<STR_LIT:application/json>' <EOL> response . renderer_context = { '<STR_LIT>' : '<STR_LIT>' } <EOL> return response . render ( ) <EOL> def test_create_comment_with_admin ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id <EOL> } ) <EOL> request = self . factory . post ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . admin <EOL> request . data = { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . create_and_respond ( request , self . contribution ) <EOL> ) <EOL> self . assertEqual ( json . loads ( response . content ) . get ( '<STR_LIT:text>' ) , '<STR_LIT>' ) <EOL> def test_create_reviewcomment_with_admin ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id <EOL> } ) <EOL> request = self . factory . post ( <EOL> url , { '<STR_LIT:text>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> request . user = self . admin <EOL> request . data = { '<STR_LIT:text>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . create_and_respond ( request , self . contribution ) <EOL> ) <EOL> self . assertEqual ( json . loads ( response . content ) . get ( '<STR_LIT:text>' ) , '<STR_LIT>' ) <EOL> ref = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertEqual ( ref . status , '<STR_LIT>' ) <EOL> def test_create_reviewcomment_to_empty_obs_with_admin ( self ) : <EOL> self . contribution . properties = None <EOL> self . contribution . save ( ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id <EOL> } ) <EOL> request = self . factory . post ( <EOL> url , { '<STR_LIT:text>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> request . user = self . admin <EOL> request . data = { '<STR_LIT:text>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . create_and_respond ( request , self . contribution ) <EOL> ) <EOL> self . assertEqual ( json . loads ( response . content ) . get ( '<STR_LIT:text>' ) , '<STR_LIT>' ) <EOL> ref = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertEqual ( ref . status , '<STR_LIT>' ) <EOL> def test_update_comment_with_admin ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . admin <EOL> request . data = { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> ) <EOL> self . assertEqual ( json . loads ( response . content ) . get ( '<STR_LIT:text>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> Comment . objects . get ( pk = self . comment . id ) . text , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_update_comment_with_commenter ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . commenter <EOL> request . data = { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> ) <EOL> self . assertEqual ( json . loads ( response . content ) . get ( '<STR_LIT:text>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> Comment . objects . get ( pk = self . comment . id ) . text , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_update_comment_with_moderator ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . moderator <EOL> request . data = { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> ) <EOL> self . assertEqual ( json . loads ( response . content ) . get ( '<STR_LIT:text>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> Comment . objects . get ( pk = self . comment . id ) . text , <EOL> '<STR_LIT>' <EOL> ) <EOL> @ raises ( PermissionDenied ) <EOL> def test_update_comment_with_creator ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . creator <EOL> request . data = { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> ) <EOL> self . assertEqual ( <EOL> json . loads ( response . content ) . get ( '<STR_LIT:text>' ) , <EOL> self . comment . text <EOL> ) <EOL> self . assertEqual ( <EOL> Comment . objects . get ( pk = self . comment . id ) . text , <EOL> self . comment . text <EOL> ) <EOL> def test_update_invalid_comment ( self ) : <EOL> self . project . isprivate = False <EOL> self . project . save ( ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( <EOL> url , { '<STR_LIT:text>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> force_authenticate ( request , user = self . commenter ) <EOL> view = SingleCommentAPIView . as_view ( ) <EOL> response = view ( <EOL> request , <EOL> project_id = self . project . id , <EOL> contribution_id = self . contribution . id , <EOL> comment_id = self . comment . id <EOL> ) . render ( ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> class CommentAbstractAPIViewResolveTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . factory = APIRequestFactory ( ) <EOL> self . admin = UserFactory . create ( ) <EOL> self . creator = UserFactory . create ( ) <EOL> self . moderator = UserFactory . create ( ) <EOL> self . commenter = UserFactory . create ( ) <EOL> self . project = ProjectFactory ( <EOL> add_admins = [ self . admin ] , <EOL> add_contributors = [ self . creator , self . commenter ] <EOL> ) <EOL> self . moderators = UserGroupFactory ( add_users = [ self . moderator ] , ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : True <EOL> } ) <EOL> self . contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . creator , <EOL> '<STR_LIT:status>' : '<STR_LIT>' <EOL> } ) <EOL> self . comment = CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution , <EOL> '<STR_LIT>' : self . commenter , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> def render ( self , response ) : <EOL> response . accepted_renderer = JSONRenderer ( ) <EOL> response . accepted_media_type = '<STR_LIT:application/json>' <EOL> response . renderer_context = { '<STR_LIT>' : '<STR_LIT>' } <EOL> return response . render ( ) <EOL> def test_resolve_comment_with_admin ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . admin <EOL> request . data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> ) <EOL> self . assertEqual ( <EOL> json . loads ( response . content ) . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> reference = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertEqual ( reference . status , '<STR_LIT>' ) <EOL> self . assertIsNotNone ( reference . properties ) <EOL> def test_resolve_comment_with_invalid_review_status ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . admin <EOL> request . data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> ) <EOL> ref = Comment . objects . get ( pk = self . comment . id ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( ref . review_status , '<STR_LIT>' ) <EOL> def test_resolve_one_of_two_comment_with_admin ( self ) : <EOL> CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution , <EOL> '<STR_LIT>' : self . creator , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . admin <EOL> request . data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> ) <EOL> self . assertEqual ( <EOL> json . loads ( response . content ) . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( <EOL> Observation . objects . get ( pk = self . contribution . id ) . status , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_resolve_comment_with_moderator ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . moderator <EOL> request . data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> response = self . render ( <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> ) <EOL> self . assertEqual ( <EOL> json . loads ( response . content ) . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( <EOL> Observation . objects . get ( pk = self . contribution . id ) . status , <EOL> '<STR_LIT>' <EOL> ) <EOL> @ raises ( PermissionDenied ) <EOL> def test_resolve_comment_with_creator ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . creator <EOL> request . data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> @ raises ( PermissionDenied ) <EOL> def test_resolve_comment_with_commenter ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = self . commenter <EOL> request . data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> @ raises ( PermissionDenied ) <EOL> def test_resolve_comment_with_anonymous ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : self . contribution . id , <EOL> '<STR_LIT>' : self . comment . id <EOL> } ) <EOL> request = self . factory . patch ( url , { '<STR_LIT:text>' : '<STR_LIT>' } ) <EOL> request . user = AnonymousUser ( ) <EOL> request . data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = CommentAbstractAPIView ( ) <EOL> view . update_and_respond ( request , self . contribution , self . comment ) <EOL> class SingleCommentAPIViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . admin = UserFactory . create ( ) <EOL> self . creator = UserFactory . create ( ) <EOL> self . project = ProjectFactory ( <EOL> add_admins = [ self . admin ] , <EOL> add_contributors = [ self . creator ] <EOL> ) <EOL> self . contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . creator <EOL> } ) <EOL> def test_get_contribution_with_admin ( self ) : <EOL> view = SingleCommentAPIView ( ) <EOL> contribution = view . get_contribution ( <EOL> self . admin , self . project . id , self . contribution . id ) <EOL> self . assertEqual ( contribution , self . contribution ) <EOL> def test_get_contribution_with_creator ( self ) : <EOL> view = SingleCommentAPIView ( ) <EOL> view . get_contribution ( <EOL> self . creator , <EOL> self . project . id , <EOL> self . contribution . id <EOL> ) <EOL> @ raises ( Project . DoesNotExist ) <EOL> def test_get_contribution_with_some_dude ( self ) : <EOL> some_dude = UserFactory . create ( ) <EOL> view = SingleCommentAPIView ( ) <EOL> view . get_contribution ( <EOL> some_dude , <EOL> self . project . id , <EOL> self . contribution . id <EOL> ) <EOL> class GetProjectComments ( APITestCase ) : <EOL> def setUp ( self ) : <EOL> self . contributor = UserFactory . create ( ) <EOL> self . admin = UserFactory . create ( ) <EOL> self . non_member = UserFactory . create ( ) <EOL> self . project = ProjectFactory ( <EOL> add_admins = [ self . admin ] , <EOL> add_contributors = [ self . contributor ] <EOL> ) <EOL> self . contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . contributor <EOL> } ) <EOL> comment = CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution <EOL> } ) <EOL> response = CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution , <EOL> '<STR_LIT>' : comment <EOL> } ) <EOL> CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution , <EOL> '<STR_LIT>' : response <EOL> } ) <EOL> CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution , <EOL> '<STR_LIT>' : comment <EOL> } ) <EOL> CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution <EOL> } ) <EOL> def get_response ( self , user ) : <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . get ( <EOL> '<STR_LIT>' % <EOL> ( self . project . id , self . contribution . id ) <EOL> ) <EOL> force_authenticate ( request , user = user ) <EOL> view = CommentsAPIView . as_view ( ) <EOL> return view ( <EOL> request , <EOL> project_id = self . project . id , <EOL> contribution_id = self . contribution . id <EOL> ) . render ( ) <EOL> def test_get_comments_with_admin ( self ) : <EOL> response = self . get_response ( self . admin ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_get_comments_with_contributor ( self ) : <EOL> response = self . get_response ( self . contributor ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_get_comments_with_non_member ( self ) : <EOL> response = self . get_response ( self . non_member ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> class AddCommentToPrivateProjectTest ( APITestCase ) : <EOL> def setUp ( self ) : <EOL> self . contributor = UserFactory . create ( ) <EOL> self . admin = UserFactory . create ( ) <EOL> self . non_member = UserFactory . create ( ) <EOL> self . project = ProjectFactory ( <EOL> add_admins = [ self . admin ] , <EOL> add_contributors = [ self . contributor ] <EOL> ) <EOL> self . contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . contributor <EOL> } ) <EOL> def get_response ( self , user ) : <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . post ( <EOL> '<STR_LIT>' % <EOL> ( self . project . id , self . contribution . id ) , <EOL> { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> ) <EOL> force_authenticate ( request , user = user ) <EOL> view = CommentsAPIView . as_view ( ) <EOL> return view ( <EOL> request , <EOL> project_id = self . project . id , <EOL> contribution_id = self . contribution . id <EOL> ) . render ( ) <EOL> def test_add_comment_to_contribution_with_admin ( self ) : <EOL> response = self . get_response ( self . admin ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> def test_add_comment_to_contribution_with_contributor ( self ) : <EOL> response = self . get_response ( self . contributor ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> def test_add_review_comment_to_contribution_with_contributor ( self ) : <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . post ( <EOL> '<STR_LIT>' % <EOL> ( self . project . id , self . contribution . id ) , <EOL> { <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> force_authenticate ( request , user = self . contributor ) <EOL> view = CommentsAPIView . as_view ( ) <EOL> response = view ( <EOL> request , <EOL> project_id = self . project . id , <EOL> contribution_id = self . contribution . id <EOL> ) . render ( ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> self . assertEqual ( <EOL> Observation . objects . get ( pk = self . contribution . id ) . status , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_add_closed_review_comment_to_contribution_with_contributor ( self ) : <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . post ( <EOL> '<STR_LIT>' % <EOL> ( self . project . id , self . contribution . id ) , <EOL> { <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> force_authenticate ( request , user = self . contributor ) <EOL> view = CommentsAPIView . as_view ( ) <EOL> response = view ( <EOL> request , <EOL> project_id = self . project . id , <EOL> contribution_id = self . contribution . id <EOL> ) . render ( ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> self . assertEqual ( <EOL> Observation . objects . get ( pk = self . contribution . id ) . status , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_add_comment_to_contribution_with_non_member ( self ) : <EOL> response = self . get_response ( self . non_member ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> def test_add_comment_to_draft ( self ) : <EOL> self . contribution . status = '<STR_LIT>' <EOL> self . contribution . save ( ) <EOL> response = self . get_response ( self . contribution . creator ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> class AddCommentToPublicProjectTest ( APITestCase ) : <EOL> def setUp ( self ) : <EOL> self . contributor = UserFactory . create ( ) <EOL> self . admin = UserFactory . create ( ) <EOL> self . non_member = UserFactory . create ( ) <EOL> self . project = ProjectFactory ( <EOL> add_admins = [ self . admin ] , <EOL> add_contributors = [ self . contributor ] , <EOL> ** { '<STR_LIT>' : False } <EOL> ) <EOL> self . contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . contributor <EOL> } ) <EOL> def get_response ( self , user ) : <EOL> if user . is_anonymous and not User . objects . filter ( <EOL> display_name = '<STR_LIT>' ) . exists ( ) : <EOL> UserFactory . create ( display_name = '<STR_LIT>' ) <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . post ( <EOL> '<STR_LIT>' % <EOL> ( self . project . id , self . contribution . id ) , <EOL> { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> ) <EOL> force_authenticate ( request , user = user ) <EOL> view = CommentsAPIView . as_view ( ) <EOL> return view ( <EOL> request , <EOL> project_id = self . project . id , <EOL> contribution_id = self . contribution . id <EOL> ) . render ( ) <EOL> def test_add_comment_to_contribution_with_admin ( self ) : <EOL> response = self . get_response ( self . admin ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> def test_add_comment_to_contribution_with_contributor ( self ) : <EOL> response = self . get_response ( self . contributor ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> def test_add_comment_to_contribution_with_non_member ( self ) : <EOL> response = self . get_response ( self . non_member ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> def test_add_comment_to_contribution_with_anonymous ( self ) : <EOL> response = self . get_response ( AnonymousUser ( ) ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> class AddCommentToWrongProjectContribution ( APITestCase ) : <EOL> def test ( self ) : <EOL> admin = UserFactory . create ( ) <EOL> project = ProjectFactory ( add_admins = [ admin ] ) <EOL> contribution = ObservationFactory . create ( ) <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . post ( <EOL> '<STR_LIT>' % <EOL> ( project . id , contribution . id ) , <EOL> { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> ) <EOL> force_authenticate ( request , user = admin ) <EOL> view = CommentsAPIView . as_view ( ) <EOL> response = view ( <EOL> request , <EOL> project_id = project . id , <EOL> contribution_id = contribution . id <EOL> ) . render ( ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> class AddResponseToProjectCommentTest ( APITestCase ) : <EOL> def test ( self ) : <EOL> admin = UserFactory . create ( ) <EOL> project = ProjectFactory ( add_admins = [ admin ] ) <EOL> contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : project <EOL> } ) <EOL> comment = CommentFactory . create ( ** { <EOL> '<STR_LIT>' : contribution <EOL> } ) <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . post ( <EOL> '<STR_LIT>' % <EOL> ( project . id , contribution . id ) , <EOL> { <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : comment . id <EOL> } <EOL> ) <EOL> force_authenticate ( request , user = admin ) <EOL> view = CommentsAPIView . as_view ( ) <EOL> response = view ( <EOL> request , <EOL> project_id = project . id , <EOL> contribution_id = contribution . id <EOL> ) . render ( ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> self . assertEqual ( <EOL> json . loads ( response . content ) . get ( '<STR_LIT>' ) , <EOL> comment . id <EOL> ) <EOL> class AddResponseToWrongProjectCommentTest ( APITestCase ) : <EOL> def test ( self ) : <EOL> admin = UserFactory . create ( ) <EOL> project = ProjectFactory ( add_admins = [ admin ] ) <EOL> contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : project <EOL> } ) <EOL> comment = CommentFactory . create ( ) <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . post ( <EOL> '<STR_LIT>' % <EOL> ( project . id , contribution . id ) , <EOL> { <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : comment . id <EOL> } <EOL> ) <EOL> force_authenticate ( request , user = admin ) <EOL> view = CommentsAPIView . as_view ( ) <EOL> response = view ( <EOL> request , <EOL> project_id = project . id , <EOL> contribution_id = contribution . id <EOL> ) . render ( ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( <EOL> json . loads ( response . content ) . get ( '<STR_LIT:error>' ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> class DeleteProjectCommentTest ( APITestCase ) : <EOL> def setUp ( self ) : <EOL> self . contributor = UserFactory . create ( ) <EOL> self . admin = UserFactory . create ( ) <EOL> self . non_member = UserFactory . create ( ) <EOL> self . project = ProjectFactory ( <EOL> add_admins = [ self . admin ] , <EOL> add_contributors = [ self . contributor ] , <EOL> ** { '<STR_LIT>' : False } <EOL> ) <EOL> self . contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . contributor <EOL> } ) <EOL> self . comment = CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution <EOL> } ) <EOL> self . comment_to_remove = CommentFactory . create ( ** { <EOL> '<STR_LIT>' : self . contribution , <EOL> '<STR_LIT>' : self . contributor <EOL> } ) <EOL> def get_response ( self , user ) : <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . delete ( <EOL> '<STR_LIT>' % <EOL> ( self . project . id , self . contribution . id , self . comment_to_remove . id ) , <EOL> { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> ) <EOL> force_authenticate ( request , user = user ) <EOL> view = SingleCommentAPIView . as_view ( ) <EOL> return view ( <EOL> request , <EOL> project_id = self . project . id , <EOL> contribution_id = self . contribution . id , <EOL> comment_id = self . comment_to_remove . id <EOL> ) . render ( ) <EOL> def test_delete_comment_with_admin ( self ) : <EOL> response = self . get_response ( self . admin ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> contribution = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertIn ( self . comment , contribution . comments . all ( ) ) <EOL> self . assertNotIn ( self . comment_to_remove , contribution . comments . all ( ) ) <EOL> def test_delete_comment_with_comment_creator ( self ) : <EOL> response = self . get_response ( self . contributor ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> contribution = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertIn ( self . comment , contribution . comments . all ( ) ) <EOL> self . assertNotIn ( self . comment_to_remove , contribution . comments . all ( ) ) <EOL> def test_delete_review_comment_with_comment_creator ( self ) : <EOL> self . comment_to_remove . review_status = '<STR_LIT>' <EOL> self . comment_to_remove . save ( ) <EOL> self . contribution . status = '<STR_LIT>' <EOL> self . contribution . save ( ) <EOL> response = self . get_response ( self . contributor ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> contribution = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertEqual ( contribution . status , '<STR_LIT>' ) <EOL> self . assertIn ( self . comment , contribution . comments . all ( ) ) <EOL> self . assertNotIn ( self . comment_to_remove , contribution . comments . all ( ) ) <EOL> def test_delete_comment_but_not_change_status_from_pending ( self ) : <EOL> self . contribution . status = '<STR_LIT>' <EOL> self . contribution . save ( ) <EOL> response = self . get_response ( self . contributor ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> contribution = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertEqual ( contribution . status , '<STR_LIT>' ) <EOL> def test_delete_comment_and_change_status_from_review ( self ) : <EOL> self . contribution . status = '<STR_LIT>' <EOL> self . contribution . save ( ) <EOL> response = self . get_response ( self . contributor ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> contribution = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertEqual ( contribution . status , '<STR_LIT>' ) <EOL> def test_delete_one_review_comment_with_comment_creator ( self ) : <EOL> self . comment . review_status = '<STR_LIT>' <EOL> self . comment . save ( ) <EOL> self . comment_to_remove . review_status = '<STR_LIT>' <EOL> self . comment_to_remove . save ( ) <EOL> self . contribution . status = '<STR_LIT>' <EOL> self . contribution . save ( ) <EOL> response = self . get_response ( self . contributor ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> contribution = Observation . objects . get ( pk = self . contribution . id ) <EOL> self . assertEqual ( contribution . status , '<STR_LIT>' ) <EOL> self . assertIn ( self . comment , contribution . comments . all ( ) ) <EOL> self . assertNotIn ( self . comment_to_remove , contribution . comments . all ( ) ) <EOL> def test_resolve_nested_comment_with_admin ( self ) : <EOL> self . comment . respondsto = self . comment_to_remove <EOL> self . comment . review_status = '<STR_LIT>' <EOL> self . comment . save ( ) <EOL> self . comment_to_remove . review_status = None <EOL> self . comment_to_remove . save ( ) <EOL> self . contribution . status = '<STR_LIT>' <EOL> self . contribution . save ( ) <EOL> response = self . get_response ( self . admin ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> self . assertEqual ( <EOL> Observation . objects . get ( pk = self . contribution . id ) . status , <EOL> '<STR_LIT>' <EOL> ) <EOL> class DeleteWrongProjectComment ( APITestCase ) : <EOL> def test ( self ) : <EOL> admin = UserFactory . create ( ) <EOL> project = ProjectFactory ( add_admins = [ admin ] ) <EOL> contribution = ObservationFactory . create ( ** { <EOL> '<STR_LIT>' : project <EOL> } ) <EOL> comment = CommentFactory . create ( ) <EOL> factory = APIRequestFactory ( ) <EOL> request = factory . delete ( <EOL> '<STR_LIT>' % <EOL> ( project . id , contribution . id , comment . id ) , <EOL> { '<STR_LIT:text>' : '<STR_LIT>' } <EOL> ) <EOL> force_authenticate ( request , user = admin ) <EOL> view = SingleCommentAPIView . as_view ( ) <EOL> response = view ( <EOL> request , <EOL> project_id = project . id , <EOL> contribution_id = contribution . id , <EOL> comment_id = comment . id <EOL> ) . render ( ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) </s>
