def test_comment_to_json(self): review = models.Review.create_review(**self.default_review_kwargs) thread = models.CommentThread.create_comment_thread(review.revision) attachments = [{ 'attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), 'sort_order': 1, 'description': 'Test Description' }] comment = models.Comment.create_comment( commenter=self.user, review=review.revision, comment="test comment", thread=thread, attachments=attachments, ) self.assertEqual( comment.to_json(), { 'id': comment.pk, 'thread': comment.thread.pk, 'name': comment.commenter.userprofile.name, 'comment': comment.comment, 'attachment_count': comment.attachments.count(), 'attachments': [comment.attachments.get().to_json()], 'url': comment.get_absolute_url(), 'thread_url': comment.get_absolute_thread_url(), 'issue': {}, 'created': comment.created.isoformat(), 'modified': comment.modified.isoformat(), })
def test_comment_json_reply_comment_as_issue(self): thread = self.review.revision.commentthread_set.latest() response = self.client.post( self.api_url, { 'comment': "test_comment_json_reply_comment", '0-attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), '0-description': 'Test Description', '0-sort_order': 1, 'thread': thread.pk, 'is_issue': True }) self.assertStatusCode(response, 200) comment = models.Comment.objects.latest('created') self.assertIsNotNone(comment.issue) self.assertTrue( comment.events.filter( event_type__code=models.EventType.COMMENT_ADDED).exists()) self.assertEqual(json.loads(response.content.decode('utf8')), { 'status': 'success', 'errors': '', 'comment': comment.to_json() }) self.assertEqual(comment.comment, 'test_comment_json_reply_comment') self.assertEqual(comment.thread, thread) self.assertEqual(comment.attachments.count(), 1)
def setUp(self): super(TestCommentAPIViews, self).setUp() assert self.client.login(username=self.user.username, password='******') # Sample review self.review = models.Review.create_review(**self.default_review_kwargs) attachments = [{ 'attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), 'description': 'Test Description', }] self.comment = models.Comment.create_comment( commenter=self.user, review=self.review.revision, comment='Test Comment', attachments=attachments, ) self.api_url = reverse('comments-api', kwargs={ 'review_pk': self.review.pk, 'proj_slug': self.review.project.slug, 'rev_num': self.review.revision.number, }) # Reset out mail queue mail.outbox = []
def test_comment_json_create_comment_thread(self): response = self.client.post( self.api_url, { 'comment': "test_comment_json_create_comment_thread", '0-attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), '0-description': 'Test Description', }) self.assertStatusCode(response, 200) comment = models.Comment.objects.get( comment='test_comment_json_create_comment_thread') self.assertTrue( comment.events.filter( event_type__code=models.EventType.COMMENT_ADDED).exists()) self.assertEqual(json.loads(response.content.decode('utf8')), { 'status': 'success', 'errors': '', 'comment': comment.to_json() }) self.assertIsNone(comment.issue) comment = self.review.revision.commentthread_set.latest( ).comment_set.get() self.assertTrue( comment.events.filter( event_type__code=models.EventType.COMMENT_ADDED).exists()) self.assertEqual(comment.comment, 'test_comment_json_create_comment_thread') self.assertEqual(comment.attachments.count(), 1)
def test_comment_with_multiple_attachments(self): response = self.client.post( self.api_url, { 'comment': 'Test comment with multiple attachments', '0-attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), '0-description': 'Image Attachment', '1-attachment': File(BytesIO(b'test_file_2'), name='test_file_2.mov'), '1-description': 'Movie Attachment', '2-attachment': File(BytesIO(b'test_file_3'), name='test_file_3.wav'), '2-description': 'Audio Attachment', }) self.assertStatusCode(response, 200) comment = models.Comment.objects.latest('created') self.assertTrue( comment.events.filter( event_type__code=models.EventType.COMMENT_ADDED).exists()) self.assertEqual(comment.comment, 'Test comment with multiple attachments') self.assertEqual(comment.attachments.count(), 3) attachment_one, attachment_two, attachment_three = comment.attachments.all( ).order_by('sort_order') # One self.assertEqual(attachment_one.attachment.file.read(), b'test_file_1') self.assertEqual(attachment_one.attachment_type, 'image') self.assertEqual(attachment_one.description, 'Image Attachment') self.assertEqual(attachment_one.sort_order, 0) # Two self.assertEqual(attachment_two.attachment.file.read(), b'test_file_2') self.assertEqual(attachment_two.attachment_type, 'movie') self.assertEqual(attachment_two.description, 'Movie Attachment') self.assertEqual(attachment_two.sort_order, 1) # Three self.assertEqual(attachment_three.attachment.file.read(), b'test_file_3') self.assertEqual(attachment_three.attachment_type, 'audio') self.assertEqual(attachment_three.description, 'Audio Attachment') self.assertEqual(attachment_three.sort_order, 2)
def test_create_attachment(self): attachment = models.Attachment.create_attachment( attachment=File(BytesIO(b'test_file_1'), name='test_file_1.mov'), description='Create Attachment', sort_order=5, content_object=self.review, ) self.assertEqual(attachment.description, 'Create Attachment') self.assertEqual(attachment.attachment_type, constants.MOVIE) self.assertEqual(attachment.sort_order, 5) self.assertEqual(attachment.content_object, self.review)
def test_comment_creation_without_comment(self): response = self.client.post( self.api_url, { 'comment': "", '0-attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), '0-description': 'Test Description', }) self.assertStatusCode(response, 200) comment = models.Comment.objects.latest('created') self.assertTrue( comment.events.filter( event_type__code=models.EventType.COMMENT_ADDED).exists()) self.assertIsNone(comment.issue) self.assertEqual(comment.comment, '') self.assertEqual(comment.attachments.count(), 1)
def setUp(self): super(TestCommentViews, self).setUp() assert self.client.login(username=self.user.username, password='******') # Sample review self.review = models.Review.create_review(**self.default_review_kwargs) attachments = [{ 'attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), 'description': 'Test Description', }] self.comment = models.Comment.create_comment( commenter=self.user, review=self.review.revision, comment='Test Comment', attachments=attachments, ) # Reset out mail queue mail.outbox = []
def test_create_comment_on_draft(self): self.review.state = constants.DRAFT self.review.save(update_fields=['state']) models.UserReviewStatus.objects.filter(review=self.review).update( read=True) mail.outbox = [] comment = models.Comment.create_comment( commenter=self.user, review=self.review.revision, comment='Test Comment', attachments=[ { 'attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), 'description': 'Image 1', }, ]) self.assertEqual( comment.get_absolute_url(), '{}#{}'.format( reverse('review-rev-detail', kwargs={ 'proj_slug': self.review.project.slug, 'pk': self.review.pk, 'rev_num': self.review.revision.number, }), comment.thread.pk)) self.assertEqual(comment.thread.review_revision, self.review.revision) self.assertEqual(comment.attachments.count(), 1) attachment = comment.attachments.get() self.assertEqual(attachment.description, 'Image 1') self.assertEqual(attachment.attachment_type, 'image') self.assertEqual(attachment.sort_order, 0) self.assertEqual(comment.commenter, self.user) self.assertEqual(comment.comment, 'Test Comment') self.assertEqual(len(mail.outbox), 0) self.assertEqual( comment.__str__(), 'Comment by {} on Review: {}'.format(comment.commenter.username, self.review.title)) self.assertFalse(self.task_patch.delay.called) self.assertFalse(comment.events.exists())
def test_create_comment_with_thread(self): review = models.Review.create_review(**self.default_review_kwargs) thread = models.CommentThread.create_comment_thread(review.revision) models.UserReviewStatus.objects.filter(review=review).update(read=True) attachments = [{ 'attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), 'sort_order': 1, 'description': 'Test Description' }] mail.outbox = [] comment = models.Comment.create_comment( commenter=self.user, review=review.revision, comment='Test Comment', attachments=attachments, thread=thread, ) self.assertEqual(comment.thread, thread) self.assertEqual(comment.thread.review_revision, review.revision) self.assertEqual(comment.attachments.count(), 1) attachment = comment.attachments.get() self.assertEqual(attachment.description, 'Test Description') self.assertEqual(attachment.attachment_type, 'image') self.assertEqual(comment.commenter, self.user) self.assertEqual(comment.comment, 'Test Comment') event = comment.events.get() self.assertEqual(event.event_type.code, event.event_type.COMMENT_ADDED) self.assertEqual(event.related_object, comment) self.assertEqual(event.user, comment.commenter) self.assertEqual(len(mail.outbox), 5) statuses = models.UserReviewStatus.objects.filter(review=review) self.assertEqual(statuses.count(), 6) self.assertEqual(statuses.filter(read=True).count(), 1) self.assertEqual(statuses.filter(read=False).count(), 5)
def test_create_comment(self): dropped_reviewer = self.review.reviewer_set.all()[0] dropped_follower = self.review.follower_set.all()[0] dropped_reviewer.drop_reviewer( self.review.creator_set.active().get().user) dropped_follower.drop_follower( self.review.creator_set.active().get().user) mail.outbox = [] models.UserReviewStatus.objects.filter(review=self.review).update( read=True) attachments = [ { 'attachment': File(BytesIO(b'test_file_1'), name='test_file_1.png'), 'description': 'Image 1', }, { 'attachment': File(BytesIO(b'test_file_2'), name='test_file_2.jpg'), 'description': 'Image 2', }, ] comment = models.Comment.create_comment( commenter=self.user, review=self.review.revision, comment='Test Comment', attachments=attachments, ) self.assertEqual( comment.get_absolute_url(), '{}#{}'.format( reverse('review-rev-detail', kwargs={ 'proj_slug': self.review.project.slug, 'pk': self.review.pk, 'rev_num': self.review.revision.number, }), comment.pk)) self.assertIsNone(comment.issue) self.assertEqual(comment.thread.review_revision, self.review.revision) self.assertEqual(comment.attachments.count(), 2) attachment_one, attachment_two = comment.attachments.all() self.assertEqual(attachment_one.description, 'Image 1') self.assertEqual(attachment_one.attachment_type, 'image') self.assertEqual(attachment_one.sort_order, 0) self.assertEqual(attachment_two.description, 'Image 2') self.assertEqual(attachment_two.attachment_type, 'image') self.assertEqual(attachment_two.sort_order, 1) self.assertEqual(comment.commenter, self.user) self.assertEqual(comment.comment, 'Test Comment') self.assertEqual(len(mail.outbox), 3) statuses = models.UserReviewStatus.objects.filter(review=self.review) self.assertEqual(statuses.count(), 6) # Dropped people aren't updated self.assertEqual(statuses.filter(read=True).count(), 3) self.assertEqual(statuses.filter(read=False).count(), 3) self.assertEqual( comment.__str__(), 'Comment by {} on Review: {}'.format(comment.commenter.username, self.review.title)) self.task_patch.delay.assert_called_with( self.review.pk, self.hook.pk, {'comment': comment.to_json()}, ) event = comment.events.get() self.assertEqual(event.event_type.code, event.event_type.COMMENT_ADDED) self.assertEqual(event.related_object, comment) self.assertEqual(event.user, comment.commenter)
def setUp(self): self.user = User.objects.create_user(username='******') self.user.set_password('testing') self.user.email = '*****@*****.**' self.user.save() self.co_owner = User.objects.create_user(username='******') self.co_owner.set_password('testing') self.co_owner.email = '*****@*****.**' self.co_owner.save() self.system_user = User.objects.get(username='******') self.group = models.Group.objects.get(slug='default-group') self.project = models.Project.objects.get(slug='default-project') models.GroupMember.objects.create(user=self.user, group=self.group) models.GroupMember.objects.create(user=self.co_owner, group=self.group) for count in range(0, 3): u = User.objects.create_user(username='******'.format(count)) u.set_password('testing') u.email = '{}@example.com'.format(u.username) u.save() models.GroupMember.objects.create(user=u, group=self.group) for count in range(0, 2): u = User.objects.create_user(username='******'.format(count)) u.set_password('testing') u.email = '{}@example.com'.format(u.username) u.save() models.GroupMember.objects.create(user=u, group=self.group) self.test_users = User.objects.filter( username__startswith="test_user_") self.followers = User.objects.filter(username__startswith='follower_') self.default_review_kwargs = { 'creators': [self.user], 'title': 'Test Title', 'description': 'Test Description', 'case_link': 'http://example.org/', 'reviewers': self.test_users, 'followers': self.followers, 'project': self.project, 'state': constants.OPEN, 'attachments': [ { 'attachment': File(BytesIO(b'test_file_1'), name='test_file_1.jpeg'), 'description': 'Testing', 'sort_order': 1, }, { 'attachment': File(BytesIO(b'test_file_2'), name='test_file_2.png'), 'description': 'Testing', 'sort_order': 2, }, ], }