def test_create_comment_content_cannot_be_whitespace(self): with assert_raises(ValidationValueError) as error: comment = Comment.create(auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content=' ') assert_equal(error.exception.message, 'Value must not be empty.')
def test_create_comment_content_cannot_be_none(self): with assert_raises(ValidationError) as error: comment = Comment.create(auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content=None) assert_equal(error.exception.message, 'Value <content> is required.')
def test_create_comment_content_does_not_exceed_max_length_complex(self): comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content=''.join(['c' for c in range(settings.COMMENT_MAXLENGTH - 12)]) + '[@George Ant](http://localhost:5000/' + self.comment.user._id + '/)' )
def test_create_sends_comment_added_signal(self): with capture_signals() as mock_signals: comment = Comment.create(auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content='This is a comment.') assert_equal(mock_signals.signals_sent(), set([comment_added]))
def test_create_comment_content_cannot_exceed_max_length(self): with assert_raises(ValidationValueError): comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content=''.join(['c' for c in range(settings.COMMENT_MAXLENGTH + 1)]) )
def test_create_comment_content_cannot_exceed_max_length(self): with assert_raises(ValidationValueError): comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content=''.join( ['c' for c in range(settings.COMMENT_MAXLENGTH + 1)]))
def test_create_sends_mention_added_signal_if_mentions(self): with capture_signals() as mock_signals: comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content='This is a comment with a bad mention [@Unconfirmed User](http://localhost:5000/' + self.comment.user._id + '/).' ) assert_equal(mock_signals.signals_sent(), set([comment_added, mention_added]))
def test_create_comment_content_cannot_be_none(self): with assert_raises(ValidationError) as error: comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content=None ) assert_equal(error.exception.message, 'Value <content> is required.')
def test_create_comment_content_cannot_be_whitespace(self): with assert_raises(ValidationValueError) as error: comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content=' ' ) assert_equal(error.exception.message, 'Value must not be empty.')
def test_create_sends_comment_added_signal(self): with capture_signals() as mock_signals: comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content='This is a comment.' ) assert_equal(mock_signals.signals_sent(), set([comment_added]))
def create(self, validated_data): user = validated_data['user'] auth = Auth(user) node = validated_data['node'] validated_data['content'] = validated_data.pop('get_content') if node and node.can_comment(auth): comment = Comment.create(auth=auth, **validated_data) else: raise PermissionDenied("Not authorized to comment on this project.") return comment
def test_create_comment_content_does_not_exceed_max_length_complex(self): comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content="".join(["c" for c in range(settings.COMMENT_MAXLENGTH - 12)]) + "[@George Ant](http://localhost:5000/" + self.comment.user._id + "/)", )
def test_create_does_not_send_mention_added_signal_if_nonuser_mentioned(self): with assert_raises(ValidationValueError) as error: with capture_signals() as mock_signals: comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content="This is a comment with a bad mention [@Not a User](http://localhost:5000/qwert/).", ) assert_equal(mock_signals.signals_sent(), set([])) assert_equal(error.exception.message, "User does not exist or is not active.")
def test_create(self): comment = Comment.create(auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, page='node', is_public=True, content='This is a comment.') assert_equal(comment.user, self.comment.user) assert_equal(comment.node, self.comment.node) assert_equal(comment.target, self.comment.target) assert_equal(len(comment.node.logs), 2) assert_equal(comment.node.logs[-1].action, NodeLog.COMMENT_ADDED)
def test_create_does_not_send_mention_added_signal_if_nonuser_mentioned(self): with assert_raises(ValidationValueError) as error: with capture_signals() as mock_signals: comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content='This is a comment with a bad mention [@Not a User](http://localhost:5000/qwert/).' ) assert_equal(mock_signals.signals_sent(), set([])) assert_equal(error.exception.message, 'User does not exist or is not active.')
def test_create_sends_mention_added_signal_if_mentions(self): with capture_signals() as mock_signals: comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content="This is a comment with a bad mention [@Unconfirmed User](http://localhost:5000/" + self.comment.user._id + "/).", ) assert_equal(mock_signals.signals_sent(), set([comment_added, mention_added]))
def test_create_does_not_send_mention_added_signal_if_noncontributor_mentioned(self): with assert_raises(ValidationValueError) as error: with capture_signals() as mock_signals: user = UserFactory() comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content='This is a comment with a bad mention [@Non-contributor User](http://localhost:5000/' + user._id + '/).' ) assert_equal(mock_signals.signals_sent(), set([])) assert_equal(error.exception.message, 'Mentioned user is not a contributor.')
def test_create(self): comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, page='node', is_public=True, content='This is a comment.' ) assert_equal(comment.user, self.comment.user) assert_equal(comment.node, self.comment.node) assert_equal(comment.target, self.comment.target) assert_equal(len(comment.node.logs), 2) assert_equal(comment.node.logs[-1].action, NodeLog.COMMENT_ADDED)
def test_create_does_not_send_mention_added_signal_if_noncontributor_mentioned(self): with assert_raises(ValidationValueError) as error: with capture_signals() as mock_signals: user = UserFactory() comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content="This is a comment with a bad mention [@Non-contributor User](http://localhost:5000/" + user._id + "/).", ) assert_equal(mock_signals.signals_sent(), set([])) assert_equal(error.exception.message, "Mentioned user is not a contributor.")
def test_create(self): comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, root_target=self.comment.root_target, page="node", is_public=True, content="This is a comment.", ) assert_equal(comment.user, self.comment.user) assert_equal(comment.node, self.comment.node) assert_equal(comment.target, self.comment.target) assert_equal(len(comment.node.logs), 2) assert_equal(comment.node.logs[-1].action, NodeLog.COMMENT_ADDED) assert_equal([], self.comment.ever_mentioned)
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.') return comment
def test_create_does_not_send_mention_added_signal_if_unconfirmed_contributor_mentioned(self): with assert_raises(ValidationValueError) as error: with capture_signals() as mock_signals: user = UserFactory() user.is_registered = False user.is_claimed = False user.save() self.comment.node.add_contributor(user, visible=False,permissions=[permissions.READ]) self.comment.node.save() comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content='This is a comment with a bad mention [@Unconfirmed User](http://localhost:5000/' + user._id + '/).' ) assert_equal(mock_signals.signals_sent(), set([contributor_added])) assert_equal(error.exception.message, 'User does not exist or is not active.')
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.") return comment
def test_create_does_not_send_mention_added_signal_if_unconfirmed_contributor_mentioned(self): with assert_raises(ValidationValueError) as error: with capture_signals() as mock_signals: user = UserFactory() user.is_registered = False user.is_claimed = False user.save() self.comment.node.add_contributor(user, visible=False, permissions=[permissions.READ]) self.comment.node.save() comment = Comment.create( auth=self.auth, user=self.comment.user, node=self.comment.node, target=self.comment.target, is_public=True, content="This is a comment with a bad mention [@Unconfirmed User](http://localhost:5000/" + user._id + "/).", ) assert_equal(mock_signals.signals_sent(), set([contributor_added])) assert_equal(error.exception.message, "User does not exist or is not active.")