<commit_before># Python imports
import shutil
import unittest

from django.conf import settings
from django.core.files.base import ContentFile
from django.http import Http404
from django.test import TestCase
from django_cradmin.cradmin_testhelpers import TestCaseMixin
from model_mommy import mommy

from devilry.devilry_dbcache.customsql import AssignmentGroupDbCacheCustomSql
from devilry.devilry_group import devilry_group_mommy_factories
from devilry.devilry_group import models as group_models
from devilry.devilry_group.views.download_files import batch_download_files


class AbstractTestCase(TestCase):
    def setUp(self):
        # Sets up a directory where files can be added. Is removed by tearDown.
        self.backend_path = 'devilry_testfiles/devilry_compressed_archives/'

    def tearDown(self):
        # Ignores errors if the path is not created.
        shutil.rmtree(self.backend_path, ignore_errors=True)
        shutil.rmtree('devilry_testfiles/filestore/', ignore_errors=True)


class TestFileDownloadFeedbackfeedView(TestCase, TestCaseMixin):
    """
    Test single file download
    """
    viewclass = batch_download_files.FileDownloadFeedbackfeedView

    def setUp(self):
        AssignmentGroupDbCacheCustomSql().initialize()

    def test_single_file_download(self):
        # Test download of single file
        testgroup = mommy.make('core.AssignmentGroup')
        testuser = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        testcomment = mommy.make('devilry_group.GroupComment',
                                 feedback_set=testgroup.feedbackset_set.first(),
                                 user=testuser,
                                 user_role='student')
        commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
        commentfile.file.save('testfile.txt', ContentFile('testcontent'))
        mockresponse = self.mock_getrequest(
                requestuser=testuser,
                cradmin_role=testgroup,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })
        self.assertEquals(mockresponse.response.content, 'testcontent')

    def test_single_file_download_two_users(self):
        # Test download of single file
        testgroup = mommy.make('core.AssignmentGroup')
        testuser1 = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        testuser2 = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        candidate1 = mommy.make('core.Candidate', assignment_group=testgroup, relatedstudent__user=testuser1)
        candidate2 = mommy.make('core.Candidate', assignment_group=testgroup, relatedstudent__user=testuser2)
        testcomment = mommy.make('devilry_group.GroupComment',
                                 feedback_set=testgroup.feedbackset_set.first(),
                                 user=testuser1,
                                 user_role='student')
        commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
        commentfile.file.save('testfile.txt', ContentFile('testcontent'))
        mockresponse1 = self.mock_getrequest(
                requestuser=candidate1.relatedstudent.user,
                cradmin_role=candidate1.assignment_group,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })
        mockresponse2 = self.mock_getrequest(
                requestuser=candidate2.relatedstudent.user,
                cradmin_role=candidate2.assignment_group,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })
        self.assertEquals(mockresponse1.response.content, 'testcontent')
        self.assertEquals(mockresponse2.response.content, 'testcontent')

    def test_file_download_user_not_in_group_404(self):
        # Test user can't download if not part of AssignmentGroup
        testgroup = mommy.make('core.AssignmentGroup')
        testuser = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        testcomment = mommy.make('devilry_group.GroupComment',
                                 user_role='examiner',
                                 feedback_set=devilry_group_mommy_factories.make_first_feedbackset_in_group())
        commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
        commentfile.file.save('testfile.txt', ContentFile('testcontent'))

        with self.assertRaises(Http404):
            self.mock_getrequest(
                requestuser=testuser,
                cradmin_role=testgroup,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })

    def test_file_download_private_comment_404(self):
        # User can't download file if the comment it belongs to is private unless the user created it.
        testgroup = mommy.make('core.AssignmentGroup')
        testuser = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        testcomment = mommy.make('devilry_group.GroupComment',
                                 user_role='examiner',
                                 feedback_set=testgroup.feedbackset_set.first(),
                                 visibility=group_models.GroupComment.VISIBILITY_PRIVATE)
        commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
        commentfile.file.save('testfile.txt', ContentFile('testcontent'))

        with self.assertRaises(Http404):
            self.mock_getrequest(
                requestuser=testuser,
                cradmin_role=testgroup,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })


class TestCompressedFeedbackSetFileDownload(AbstractTestCase, TestCaseMixin):
    """
    Test FeedbackSet files download.
    """
    viewclass = batch_download_files.CompressedFeedbackSetFileDownloadView

    @unittest.skip('Skipped for new batch download api, will probably be removed')
    def test_feedbackset_files_download(self):
        with self.settings(DEVILRY_COMPRESSED_ARCHIVES_DIRECTORY=self.backend_path):
            # Test download feedbackset files.
            testuser = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
            testfeedbackset = mommy.make('devilry_group.FeedbackSet')
            testcomment = mommy.make('devilry_group.GroupComment', feedback_set=testfeedbackset, user=testuser,
                                     user_role='student')
            commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
            commentfile.file.save('testfile.txt', ContentFile('testcontent'))

            mockresponse = self.mock_getrequest(
                    cradmin_role=testfeedbackset.group,
                    viewkwargs={
                        'feedbackset_id': testfeedbackset.id
                    }
            )
            self.assertEquals(mockresponse.response.status_code, 302)<commit_msg>devilry_group/tests: removed test.<commit_after># Python imports
import shutil
import unittest

from django.conf import settings
from django.core.files.base import ContentFile
from django.http import Http404
from django.test import TestCase
from django_cradmin.cradmin_testhelpers import TestCaseMixin
from model_mommy import mommy

from devilry.devilry_dbcache.customsql import AssignmentGroupDbCacheCustomSql
from devilry.devilry_group import devilry_group_mommy_factories
from devilry.devilry_group import models as group_models
from devilry.devilry_group.views.download_files import batch_download_files


class AbstractTestCase(TestCase):
    def setUp(self):
        # Sets up a directory where files can be added. Is removed by tearDown.
        self.backend_path = 'devilry_testfiles/devilry_compressed_archives/'

    def tearDown(self):
        # Ignores errors if the path is not created.
        shutil.rmtree(self.backend_path, ignore_errors=True)
        shutil.rmtree('devilry_testfiles/filestore/', ignore_errors=True)


class TestFileDownloadFeedbackfeedView(TestCase, TestCaseMixin):
    """
    Test single file download
    """
    viewclass = batch_download_files.FileDownloadFeedbackfeedView

    def setUp(self):
        AssignmentGroupDbCacheCustomSql().initialize()

    def test_single_file_download(self):
        # Test download of single file
        testgroup = mommy.make('core.AssignmentGroup')
        testuser = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        testcomment = mommy.make('devilry_group.GroupComment',
                                 feedback_set=testgroup.feedbackset_set.first(),
                                 user=testuser,
                                 user_role='student')
        commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
        commentfile.file.save('testfile.txt', ContentFile('testcontent'))
        mockresponse = self.mock_getrequest(
                requestuser=testuser,
                cradmin_role=testgroup,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })
        self.assertEquals(mockresponse.response.content, 'testcontent')

    def test_single_file_download_two_users(self):
        # Test download of single file
        testgroup = mommy.make('core.AssignmentGroup')
        testuser1 = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        testuser2 = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        candidate1 = mommy.make('core.Candidate', assignment_group=testgroup, relatedstudent__user=testuser1)
        candidate2 = mommy.make('core.Candidate', assignment_group=testgroup, relatedstudent__user=testuser2)
        testcomment = mommy.make('devilry_group.GroupComment',
                                 feedback_set=testgroup.feedbackset_set.first(),
                                 user=testuser1,
                                 user_role='student')
        commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
        commentfile.file.save('testfile.txt', ContentFile('testcontent'))
        mockresponse1 = self.mock_getrequest(
                requestuser=candidate1.relatedstudent.user,
                cradmin_role=candidate1.assignment_group,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })
        mockresponse2 = self.mock_getrequest(
                requestuser=candidate2.relatedstudent.user,
                cradmin_role=candidate2.assignment_group,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })
        self.assertEquals(mockresponse1.response.content, 'testcontent')
        self.assertEquals(mockresponse2.response.content, 'testcontent')

    def test_file_download_user_not_in_group_404(self):
        # Test user can't download if not part of AssignmentGroup
        testgroup = mommy.make('core.AssignmentGroup')
        testuser = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        testcomment = mommy.make('devilry_group.GroupComment',
                                 user_role='examiner',
                                 feedback_set=devilry_group_mommy_factories.make_first_feedbackset_in_group())
        commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
        commentfile.file.save('testfile.txt', ContentFile('testcontent'))

        with self.assertRaises(Http404):
            self.mock_getrequest(
                requestuser=testuser,
                cradmin_role=testgroup,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })

    def test_file_download_private_comment_404(self):
        # User can't download file if the comment it belongs to is private unless the user created it.
        testgroup = mommy.make('core.AssignmentGroup')
        testuser = mommy.make(settings.AUTH_USER_MODEL, shortname='<EMAIL>', fullname='<NAME>')
        testcomment = mommy.make('devilry_group.GroupComment',
                                 user_role='examiner',
                                 feedback_set=testgroup.feedbackset_set.first(),
                                 visibility=group_models.GroupComment.VISIBILITY_PRIVATE)
        commentfile = mommy.make('devilry_comment.CommentFile', comment=testcomment, filename='testfile.txt')
        commentfile.file.save('testfile.txt', ContentFile('testcontent'))

        with self.assertRaises(Http404):
            self.mock_getrequest(
                requestuser=testuser,
                cradmin_role=testgroup,
                viewkwargs={
                    'commentfile_id': commentfile.id
                })
{'fraction_non_alphanumeric': 0.08817681248088101, 'fraction_numerical': 0.004817987152034261, 'mean_word_length': 2.6619994399327918, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 58, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4742777', 'n_tokens_mistral': 3583, 'n_tokens_neox': 3489, 'n_words': 573}