def test_create_comment_content_does_not_exceed_max_length_complex(self, node, user, auth): Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content=''.join(['c' for c in range(settings.COMMENT_MAXLENGTH - 12)]) + '[@George Ant](http://localhost:5000/' + user._id + '/)' )
def test_create_comment_content_cannot_exceed_max_length_simple(self, node, user, auth): with pytest.raises(ValidationError): Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content=''.join(['c' for c in range(settings.COMMENT_MAXLENGTH + 3)]) )
def test_create_sends_mention_added_signal_if_mentions(self, node, user, auth): with capture_signals() as mock_signals: Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content='This is a comment with a bad mention [@Unconfirmed User](http://localhost:5000/' + user._id + '/).' ) assert mock_signals.signals_sent() == ({comment_added, mention_added})
def test_create_sends_comment_added_signal(self, node, user, auth): with capture_signals() as mock_signals: Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content='This is a comment.' ) assert mock_signals.signals_sent() == ({comment_added})
def test_create_comment_content_cannot_be_whitespace(self, node, user, auth): with pytest.raises(ValidationError) as error: Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content=' ' ) assert error.value.messages[0] == 'Value must not be empty.'
def test_create_comment_content_cannot_be_empty(self, node, user, auth): with pytest.raises(ValidationError) as error: Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content='' ) assert error.value.messages[0] == 'This field cannot be blank.'
def test_create_does_not_send_mention_added_signal_if_nonuser_mentioned(self, node, user, auth): with pytest.raises(ValidationError) as error: with capture_signals() as mock_signals: Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content='This is a comment with a bad mention [@Not a User](http://localhost:5000/qwert/).' ) assert mock_signals.signals_sent() == set([]) assert error.value.message == 'User does not exist or is not active.'
def test_create_does_not_send_mention_added_signal_if_noncontributor_mentioned(self, node, user, auth): with pytest.raises(ValidationError) as error: with capture_signals() as mock_signals: user = UserFactory() Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content='This is a comment with a bad mention [@Non-contributor User](http://localhost:5000/' + user._id + '/).' ) assert mock_signals.signals_sent() == set([]) assert error.value.message == 'Mentioned user is not a contributor.'
def test_create_sends_mention_added_signal_if_group_member_mentions(self, node, user, auth): manager = AuthUserFactory() group = OSFGroupFactory(creator=manager) node.add_osf_group(group) assert node.is_contributor_or_group_member(manager) is True with capture_signals() as mock_signals: Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content='This is a comment with a group member mention [@Group Member](http://localhost:5000/' + manager._id + '/).' ) assert mock_signals.signals_sent() == ({comment_added, mention_added})
def test_create_does_not_send_mention_added_signal_if_unconfirmed_contributor_mentioned(self, node, user, auth): with pytest.raises(ValidationError) as error: with capture_signals() as mock_signals: user = UnregUserFactory() user.save() node.add_unregistered_contributor(user.fullname, user.email, Auth(node.creator), permissions=[permissions.READ], save=True) Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content='This is a comment with a bad mention [@Unconfirmed User](http://localhost:5000/' + user._id + '/).' ) assert mock_signals.signals_sent() == ({contributor_added}) assert error.value.message == 'User does not exist or is not active.'
def test_create_does_not_send_mention_added_signal_if_unconfirmed_contributor_mentioned(self, node, user, auth): with pytest.raises(ValidationError) as error: with capture_signals() as mock_signals: user = UserFactory() user.is_registered = False user.is_claimed = False user.save() node.add_contributor(user, visible=False, permissions=[permissions.READ], save=True) Comment.create( auth=auth, user=user, node=node, target=node.guids.all()[0], content='This is a comment with a bad mention [@Unconfirmed User](http://localhost:5000/' + user._id + '/).' ) assert mock_signals.signals_sent() == ({contributor_added}) assert error.value.message == 'User does not exist or is not active.'
def test_create_comment(self, request, user, project, comment_content, expected_signals, expected_error_msg): if hasattr(comment_content, '_pytestfixturefunction'): comment_content = request.getfixturevalue(comment_content.__name__) auth = Auth(user) error_msg = None with capture_signals() as mock_signals: try: Comment.create( auth=auth, user=user, node=project, target=project.guids.all()[0], content=comment_content ) except Exception as e: error_msg = str(e) assert expected_signals == mock_signals.signals_sent() assert error_msg == expected_error_msg
def test_create(self): first_comment = CommentFactory() auth = Auth(user=first_comment.user) comment = Comment.create( auth=auth, user=first_comment.user, node=first_comment.node, target=first_comment.target, root_target=first_comment.root_target, page='node', content='This is a comment, and ya cant teach that.' ) assert comment.user == first_comment.user assert comment.node == first_comment.node assert comment.target == first_comment.target assert comment.node.logs.count() == 2 assert comment.node.logs.latest().action == NodeLog.COMMENT_ADDED assert not first_comment.ever_mentioned.exists()
def create(self, validated_data): user = validated_data['user'] auth = Auth(user) node = validated_data['node'] target_id = self.context['request'].data.get('id') try: target = self.get_target(node._id, target_id) except ValueError: raise InvalidModelValueError( source={'pointer': '/data/relationships/target/data/id'}, detail='Invalid comment target \'{}\'.'.format(target_id) ) validated_data['target'] = target validated_data['content'] = validated_data.pop('get_content') try: comment = Comment.create(auth=auth, **validated_data) except PermissionsError: raise PermissionDenied('Not authorized to comment on this project.') except ModelValidationError as err: raise ValidationError(err.messages[0]) return comment
def create(self, validated_data): user = validated_data['user'] auth = Auth(user) node = validated_data['node'] target_id = self.context['request'].data.get('id') try: target = self.get_target(node._id, target_id) except ValueError: raise InvalidModelValueError( source={'pointer': '/data/relationships/target/data/id'}, detail='Invalid comment target \'{}\'.'.format(target_id)) validated_data['target'] = target validated_data['content'] = validated_data.pop('get_content') try: comment = Comment.create(auth=auth, **validated_data) except PermissionsError: raise PermissionDenied( 'Not authorized to comment on this project.') except ModelValidationError as err: raise ValidationError(err.messages[0]) return comment