<commit_before>from tests import OkTestCase

from server.models import db, Assignment, Backup, Message, User
from server import utils

class TestDownload(OkTestCase):
    def _add_file(self, filename, contents):
        self.setup_course()
        
        email = '<EMAIL>'
        self.login(email)
        self.user = User.lookup(email)
        
        self.backup = Backup(
            submitter=self.user,
            assignment=self.assignment,
            submit=True)
            
        self.message = Message(
            backup=self.backup,
            contents={
                filename: contents,
                'submit': True
            },
            kind='file_contents')
            
        db.session.add(self.backup)
        db.session.add(self.message)
        db.session.commit()
        
    def test_simple(self):
        filename = "test.py"
        contents = "x = 4"
        self._add_file(filename, contents)
        encoded_id = utils.encode_id(self.backup.id)
        submit_str = "submissions" if self.backup.submit else "backups"
        url = "/{0}/{1}/{2}/download/{3}".format(self.assignment.name, submit_str, encoded_id, filename)
        response = self.client.get(url)
        self.assert_200(response)
        self.assertEqual(contents, response.data.decode('UTF-8'))
        
    def test_unicode(self):
        filename = "test.py"
        contents = "⚡️ 🔥 💥 ❄️"
        self._add_file(filename, contents)
        encoded_id = utils.encode_id(self.backup.id)
        submit_str = "submissions" if self.backup.submit else "backups"
        url = "/{0}/{1}/{2}/download/{3}".format(self.assignment.name, submit_str, encoded_id, filename)
        response = self.client.get(url)
        self.assert_200(response)
        self.assertEqual(contents, response.data.decode('UTF-8'))
        
    def test_wrong_student(self):
        filename = "test.py"
        contents = "x = 4"
        self._add_file(filename, contents)
        self.login('<EMAIL>')
        encoded_id = utils.encode_id(self.backup.id)
        submit_str = "submissions" if self.backup.submit else "backups"
        url = "/{0}/{1}/{2}/download/{3}".format(self.assignment.name, submit_str, encoded_id, filename)
        response = self.client.get(url)
        self.assert_404(response)

    def test_staff(self):
        filename = "test.py"
        contents = "x = 4"
        self._add_file(filename, contents)
        self.login(self.staff1.email)
<commit_msg>add tests for downloading file in folder<commit_after>from tests import OkTestCase

from server.models import db, Assignment, Backup, Message, User
from server import utils

class TestDownload(OkTestCase):
    def _add_file(self, filename, contents):
        self.setup_course()
        
        email = '<EMAIL>'
        self.login(email)
        self.user = User.lookup(email)
        
        self.backup = Backup(
            submitter=self.user,
            assignment=self.assignment,
            submit=True)
            
        self.message = Message(
            backup=self.backup,
            contents={
                filename: contents,
                'submit': True
            },
            kind='file_contents')
            
        db.session.add(self.backup)
        db.session.add(self.message)
        db.session.commit()
        
    def test_simple(self):
        filename = "test.py"
        contents = "x = 4"
        self._add_file(filename, contents)
        encoded_id = utils.encode_id(self.backup.id)
        submit_str = "submissions" if self.backup.submit else "backups"
        url = "/{0}/{1}/{2}/download/{3}".format(self.assignment.name, submit_str, encoded_id, filename)
        response = self.client.get(url)
        self.assert_200(response)
        self.assertEqual(contents, response.data.decode('UTF-8'))
        
    def test_unicode(self):
        filename = "test.py"
        contents = "⚡️ 🔥 💥 ❄️"
        self._add_file(filename, contents)
        encoded_id = utils.encode_id(self.backup.id)
        submit_str = "submissions" if self.backup.submit else "backups"
        url = "/{0}/{1}/{2}/download/{3}".format(self.assignment.name, submit_str, encoded_id, filename)
        response = self.client.get(url)
        self.assert_200(response)
        self.assertEqual(contents, response.data.decode('UTF-8'))

    def test_folders(self):
        filename = "tests/hof.py"
        contents = "tests = {\nstatus: 'locked'\n}"
        self._add_file(filename, contents)
        encoded_id = utils.encode_id(self.backup.id)
        submit_str = "submissions" if self.backup.submit else "backups"
        url = "/{0}/{1}/{2}/download/{3}".format(self.assignment.name, submit_str, encoded_id, filename)
        response = self.client.get(url)
        self.assert_200(response)
        self.assertEqual(contents, response.data.decode('UTF-8'))

    def test_wrong_student(self):
        filename = "test.py"
        contents = "x = 4"
        self._add_file(filename, contents)
        self.login('<EMAIL>')
        encoded_id = utils.encode_id(self.backup.id)
        submit_str = "submissions" if self.backup.submit else "backups"
        url = "/{0}/{1}/{2}/download/{3}".format(self.assignment.name, submit_str, encoded_id, filename)
        response = self.client.get(url)
        self.assert_404(response)

    def test_staff(self):
        filename = "test.py"
        contents = "x = 4"
        self._add_file(filename, contents)
        self.login(self.staff1.email)

{'fraction_non_alphanumeric': 0.12720655141037307, 'fraction_numerical': 0.011282984531392174, 'mean_word_length': 2.2367491166077738, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5073652', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1677, 'n_words': 308}