<commit_before>from datetime import timedelta
import re

from django.test import TestCase


from ....simplified import PermissionDenied, FilterValidationError, InvalidNumberOfResults
from ....simplified.utils import modelinstance_to_dict
from ...core import models, testhelper
from ..simplified import (SimplifiedNode, SimplifiedSubject, SimplifiedPeriod,
                          SimplifiedAssignment, SimplifiedAssignmentGroup,
                          SimplifiedDeadline, SimplifiedStaticFeedback,
                          SimplifiedFileMeta)


testhelper.TestHelper.set_memory_deliverystore()


class SimplifiedAdminTestBase(TestCase, testhelper.TestHelper):

    def setUp(self):
        self.create_superuser('superadminuser')

        # create a base structure
        self.add(nodes='uni:admin(admin1)',
                 subjects=['inf101', 'inf110'],
                 periods=['firstsem', 'secondsem:admin(admin2)'],
                 assignments=['a1', 'a2'])

        # add firstStud to the first and secondsem assignments
        self.add_to_path('uni;inf101.firstsem.a1.g1:candidate(firstStud):examiner(exam1,exam3)')
        self.add_to_path('uni;inf101.firstsem.a2.g1:candidate(firstStud):examiner(exam1)')
        self.add_to_path('uni;inf110.secondsem.a1.g1:candidate(firstStud):examiner(exam2)')
        self.add_to_path('uni;inf110.secondsem.a2.g1:candidate(firstStud):examiner(exam2)')

        # secondStud began secondsem
        self.add_to_path('uni;inf101.secondsem.a1.g2:candidate(secondStud):examiner(exam1)')
        self.add_to_path('uni;inf101.secondsem.a2.g2:candidate(secondStud):examiner(exam1)')


class TestSimplifiedNode(SimplifiedAdminTestBase):
    allExtras = SimplifiedNode._meta.resultfields.additional_aslist()

    def setUp(self):
        self.add(nodes='uni:admin(admin1).mat.inf')
        self.add(nodes='uni.fys')
        self.create_superuser('superadminuser')


    def test_search_filters(self):
        qrywrap = SimplifiedNode.search(self.admin1)
        self.assertEquals(len(qrywrap), 4)
        qrywrap = SimplifiedNode.search(self.admin1,
                                        filters=[dict(field='parentnode__short_name', comp='exact', value='uni')])
        self.assertEquals(len(qrywrap), 2)
        qrywrap = SimplifiedNode.search(self.admin1,
                                        filters=[dict(field='parentnode__parentnode__short_name', comp='exact', value='uni')])
        self.assertEquals(len(qrywrap), 1)

        with self.assertRaises(FilterValidationError):
            SimplifiedNode.search(self.admin1,
                                  filters=[dict(field='parentnode__INVALID__short_name', comp='exact', value='uni')])
        with self.assertRaises(FilterValidationError):
            SimplifiedNode.search(self.admin1,
                                  filters=[dict(field='INVALIDparentnode__short_name', comp='exact', value='uni')])
        with self.assertRaises(FilterValidationError):
            SimplifiedNode.search(self.admin1,
                                  filters=[dict(field='parentnode__short_nameINVALID', comp='exact', value='uni')])

    def test_search_exact_number_of_results(self):
        qrywrap = SimplifiedNode.search(self.admin1, exact_number_of_results=4)
        self.assertEquals(len(qrywrap), 4)
        qrywrap = SimplifiedNode.search(self.admin1, exact_number_of_results=None)
        self.assertEquals(len(qrywrap), 4)
        with self.assertRaises(InvalidNumberOfResults):
            SimplifiedNode.search(self.admin1, exact_number_of_results=3)
        with self.assertRaises(InvalidNumberOfResults):
            SimplifiedNode.search(self.admin1, exact_number_of_results=5)
        with self.assertRaises(InvalidNumberOfResults):
            SimplifiedNode.search(self.admin1, exact_number_of_results=0)

    def test_create_asadmin(self):
        kw = dict(
                long_name='TestOne',
                parentnode = self.uni)

        newpk = SimplifiedNode.create(self.admin1, short_name='test1', **kw)
        create_res = models.Node.objects.get(pk=newpk)
        self.assertEquals(create_res.short_name, 'test1')
        self.assertEquals(create_res.long_name, 'TestOne')
        self.assertEquals(create_res.parentnode, self.uni)

    def test_create_assuperadmin(self):
        kw = dict(
                long_name='TestOne',
                parentnode = None)

        newpk = SimplifiedNode.create(self.superadminuser, short_name='test1', **kw)
        create_res = models.Node.objects.get(pk=newpk)
        self.assertEquals(create_res.short_name, 'test1')
        self.assertEquals(create_res.long_name, 'TestOne')
        self.assertEquals(create_res.parentnode, None)

    def test_read(self):
        # do a read with no extra fields
        read_res = SimplifiedNode.read(self.admin1, self.uni.id)
        expected_res = modelinstance_to_dict(self.uni, SimplifiedNode._meta.resultfields.aslist())
        self.assertEquals(read_res, expected_res)

        # do a read with all extras
        read_res = SimplifiedNode.read(self.admin1, self.uni.id, result_fieldgroups=self.allExtras)
        expected_res = modelinstance_to_dict(self.uni, SimplifiedNode._meta.resultfields.aslist(self.allExtras))
        self.assertEquals(read_res, expected_res)

    def test_update(self):
        self.assertEquals(self.uni.short_name, 'uni')

        kw = dict(short_name = 'testuni',
                    long_name = 'Test')

        pk = SimplifiedNode.update(self.admin1,
                            pk = self.uni.id,
                            **kw)
        update_res = models.Node.objects.get(pk=pk)
        self.assertEquals(update_res.short_name, 'testuni')

        self.assertEquals(self.uni.short_name, 'uni')
        self.refresh_var(self.uni)
        self.assertEquals(self.uni.short_name, 'testuni')

    def test_search_noextras(self):
        # search with no query and no extra fields
        search_res = SimplifiedNode.search(self.admin1)
        expected_res = [modelinstance_to_dict(self.uni, SimplifiedNode._meta.resultfields.aslist()),
                        modelinstance_to_dict(self.uni_mat, SimplifiedNode._meta.resultfields.aslist()),
                        modelinstance_to_dict(self.uni_fys, SimplifiedNode._meta.resultfields.aslist()),
                        modelinstance_to_dict(self.uni_mat_inf, SimplifiedNode._meta.resultfields.aslist())]

        # assert that all search results are as expected
        self.assertEquals(search_res.count(), len(expected_res))
        for s in search_res:
            self.assertTrue(s in expected_res)

    def test_search_allextras(self):
        # search with no querys, but all extra fields
        search_res = SimplifiedNode.search(self.admin1, result_fieldgroups=self.allExtras)
        expected_res = [modelinstance_to_dict(self.uni, SimplifiedNode._meta.resultfields.aslist(self.allExtras)),
                        modelinstance_to_dict(self.uni_mat, SimplifiedNode._meta.resultfields.aslist(self.allExtras)),
                        modelinstance_to_dict(self.uni_fys, SimplifiedNode._meta.resultfields.aslist(self.allExtras)),
                        modelinstance_to_dict(self.uni_mat_inf, SimplifiedNode._meta.resultfields.aslist(self.allExtras))]

        # assert that all search results are as expected
        self.assertEquals(search_res.count(), len(expected_res))
        for s in search_res:
            self.assertTrue(s in expected_res)

    def test_search_withquery(self):
        # search with query
        search_res = SimplifiedNode.search(self.admin1, query='inf')
        expected_res = [modelinstance_to_dict(self.uni_mat_inf, SimplifiedNode._meta.resultfields.aslist())]

        # assert that all search results are as expected
        self.assertEquals(search_res.count(), len(expected_res))
        for s in search_res:
            self.assertTrue(s in expected_res)

    def test_search_queryandextras(self):
        # with query and extra fields.
        search_res = SimplifiedNode.search(self.admin1, query='inf', result_fieldgroups = self.allExtras)
        expected_res = [modelinstance_to_dict(self.uni_mat_inf, SimplifiedNode._meta.resultfields.aslist(self.allExtras))]

        # assert that all search results are as expected
        self.assertEquals(search_res.count(), len(expected_res))
        for s in search_res:
            self.assertTrue(s in expected_res)

    def test_search_security(self):
        self.add_to_path('uni;inf110.firstsem.a2.g1:candidate(testPerson)')

        search_res = SimplifiedNode.search(self.testPerson)
        self.assertEquals(search_res.count(), 0)

    def test_delete_asnodeadmin(self):
        # this node has children and should raise permissiondenied
        with self.assertRaises(PermissionDenied):
            SimplifiedNode.delete(self.admin1, self.uni.id)

    def test_delete_assuperadmin(self):
        SimplifiedNode.delete(self.superadminuser, self.uni.id)

        with self.assertRaises(PermissionDenied):
            SimplifiedNode.delete(self.superadminuser, self.uni.id)

    def test_delete_noperm(self):
        self.add_to_path('uni;inf101.secondsem.a1.g2:candidate(secondStud):examiner(exam1)')
        with self.assertRaises(PermissionDenied):
            SimplifiedNode.delete(self.secondStud, self.uni.id)
        with self.assertRaises(PermissionDenied):
            SimplifiedNode.delete(self.exam1, self.uni.id)

class TestSimplifiedSubject(SimplifiedAdminTestBase):
    allExtras = SimplifiedSubject._meta.resultfields.additional_aslist()
    def setUp(self):
        super(TestSimplifiedSubject,self).setUp()<commit_msg>apps.administrator.simplified: added tests<commit_after>from datetime import timedelta
import re

from django.test import TestCase


from ....simplified import PermissionDenied, FilterValidationError, InvalidNumberOfResults
from ....simplified.utils import modelinstance_to_dict
from ...core import models, testhelper
from ..simplified import (SimplifiedNode, SimplifiedSubject, SimplifiedPeriod,
                          SimplifiedAssignment, SimplifiedAssignmentGroup,
                          SimplifiedDeadline, SimplifiedStaticFeedback,
                          SimplifiedFileMeta)


testhelper.TestHelper.set_memory_deliverystore()


class SimplifiedAdminTestBase(TestCase, testhelper.TestHelper):

    def setUp(self):
        self.create_superuser('superadminuser')

        # create a base structure
        self.add(nodes='uni:admin(admin1)',
                 subjects=['inf101', 'inf110'],
                 periods=['firstsem', 'secondsem:admin(admin2)'],
                 assignments=['a1', 'a2'])

        # add firstStud to the first and secondsem assignments
        self.add_to_path('uni;inf101.firstsem.a1.g1:candidate(firstStud):examiner(exam1,exam3)')
        self.add_to_path('uni;inf101.firstsem.a2.g1:candidate(firstStud):examiner(exam1)')
        self.add_to_path('uni;inf110.secondsem.a1.g1:candidate(firstStud):examiner(exam2)')
        self.add_to_path('uni;inf110.secondsem.a2.g1:candidate(firstStud):examiner(exam2)')

        # secondStud began secondsem
        self.add_to_path('uni;inf101.secondsem.a1.g2:candidate(secondStud):examiner(exam1)')
        self.add_to_path('uni;inf101.secondsem.a2.g2:candidate(secondStud):examiner(exam1)')


class TestSimplifiedNode(SimplifiedAdminTestBase):
    allExtras = SimplifiedNode._meta.resultfields.additional_aslist()

    def setUp(self):
        self.add(nodes='uni:admin(admin1).mat.inf')
        self.add(nodes='uni.fys')
        self.create_superuser('superadminuser')
        self.add_to_path('uni;inf101:admin(testadmin).firstsem.a1.g1:candidate(teststud):examiner(testexam)')


    def test_search_filters(self):
        qrywrap = SimplifiedNode.search(self.admin1)
        self.assertEquals(len(qrywrap), 4)
        qrywrap = SimplifiedNode.search(self.admin1,
                                        filters=[dict(field='parentnode__short_name', comp='exact', value='uni')])
        self.assertEquals(len(qrywrap), 2)
        qrywrap = SimplifiedNode.search(self.admin1,
                                        filters=[dict(field='parentnode__parentnode__short_name', comp='exact', value='uni')])
        self.assertEquals(len(qrywrap), 1)

        with self.assertRaises(FilterValidationError):
            SimplifiedNode.search(self.admin1,
                                  filters=[dict(field='parentnode__INVALID__short_name', comp='exact', value='uni')])
        with self.assertRaises(FilterValidationError):
            SimplifiedNode.search(self.admin1,
                                  filters=[dict(field='INVALIDparentnode__short_name', comp='exact', value='uni')])
        with self.assertRaises(FilterValidationError):
            SimplifiedNode.search(self.admin1,
                                  filters=[dict(field='parentnode__short_nameINVALID', comp='exact', value='uni')])

    def test_search_exact_number_of_results(self):
        qrywrap = SimplifiedNode.search(self.admin1, exact_number_of_results=4)
        self.assertEquals(len(qrywrap), 4)
        qrywrap = SimplifiedNode.search(self.admin1, exact_number_of_results=None)
        self.assertEquals(len(qrywrap), 4)
        with self.assertRaises(InvalidNumberOfResults):
            SimplifiedNode.search(self.admin1, exact_number_of_results=3)
        with self.assertRaises(InvalidNumberOfResults):
            SimplifiedNode.search(self.admin1, exact_number_of_results=5)
        with self.assertRaises(InvalidNumberOfResults):
            SimplifiedNode.search(self.admin1, exact_number_of_results=0)

    def test_create_asadmin(self):
        kw = dict(
                long_name='TestOne',
                parentnode = self.uni)

        newpk = SimplifiedNode.create(self.admin1, short_name='test1', **kw)
        create_res = models.Node.objects.get(pk=newpk)
        self.assertEquals(create_res.short_name, 'test1')
        self.assertEquals(create_res.long_name, 'TestOne')
        self.assertEquals(create_res.parentnode, self.uni)

    def test_create_assuperadmin(self):
        kw = dict(
                long_name='TestOne',
                parentnode = None)

        newpk = SimplifiedNode.create(self.superadminuser, short_name='test1', **kw)
        create_res = models.Node.objects.get(pk=newpk)
        self.assertEquals(create_res.short_name, 'test1')
        self.assertEquals(create_res.long_name, 'TestOne')
        self.assertEquals(create_res.parentnode, None)

    def test_create_security_asstudent(self):
        # test that a student cant create a node
        kw = dict(
                long_name='TestOne',
                parentnode = self.uni)

        with self.assertRaises(PermissionDenied):
            SimplifiedNode.create(self.teststud, short_name='test1', **kw)

    def test_create_security_asexaminer(self):
        # test that an examiner cant create a node
        kw = dict(
                long_name='TestOne',
                parentnode = self.uni)

        with self.assertRaises(PermissionDenied):
            SimplifiedNode.create(self.testexam, short_name='test1', **kw)

    def test_create_security_assubjectadmin(self):
        # test that an admin for a subject cant create a node
        kw = dict(
                long_name='TestOne',
                parentnode = self.uni)

        with self.assertRaises(PermissionDenied):
            SimplifiedNode.create(self.testadmin, short_name='test1', **kw)

    def test_read(self):
        # do a read with no extra fields
        read_res = SimplifiedNode.read(self.admin1, self.uni.id)
        expected_res = modelinstance_to_dict(self.uni, SimplifiedNode._meta.resultfields.aslist())
        self.assertEquals(read_res, expected_res)

        # do a read with all extras
        read_res = SimplifiedNode.read(self.admin1, self.uni.id, result_fieldgroups=self.allExtras)
        expected_res = modelinstance_to_dict(self.uni, SimplifiedNode._meta.resultfields.aslist(self.allExtras))
        self.assertEquals(read_res, expected_res)

    def test_read_security_asstudent(self):
        # test that a student cant read a node
        with self.assertRaises(PermissionDenied):
            read_res = SimplifiedNode.read(self.teststud, self.uni.id)

    def test_read_security_asexam(self):
        # test that a student cant read a node
        with self.assertRaises(PermissionDenied):
            read_res = SimplifiedNode.read(self.testexam, self.uni.id)

    def test_read_security_assubjectadmin(self):
        # test that a student cant read a node
        with self.assertRaises(PermissionDenied):
            read_res = SimplifiedNode.read(self.testadmin, self.uni.id)

    def test_update(self):
        self.assertEquals(self.uni.short_name, 'uni')

        kw = dict(short_name = 'testuni',
                    long_name = 'Test')

        pk = SimplifiedNode.update(self.admin1,
                            pk = self.uni.id,
                            **kw)
        update_res = models.Node.objects.get(pk=pk)
        self.assertEquals(update_res.short_name, 'testuni')

        self.assertEquals(self.uni.short_name, 'uni')
        self.refresh_var(self.uni)
        self.assertEquals(self.uni.short_name, 'testuni')

    def test_update_security_asstudent(self):
        # test that an admin for a subject cant create a node
        kw = dict(
                short_name='test1',
                long_name='TestOne')

        with self.assertRaises(PermissionDenied):
            SimplifiedNode.update(self.teststud, pk=self.uni.id, **kw)

    def test_update_security_asexaminer(self):
        # test that an admin for a subject cant create a node
        kw = dict(
                short_name='test1',
                long_name='TestOne')

        with self.assertRaises(PermissionDenied):
            SimplifiedNode.update(self.testexam, pk=self.uni.id, **kw)

    def test_update_security_assubjectadmin(self):
        # test that an admin for a subject cant create a node
        kw = dict(
                short_name='test1',
                long_name='TestOne')

        with self.assertRaises(PermissionDenied):
            SimplifiedNode.update(self.testadmin, pk=self.uni.id, **kw)

    def test_search_noextras(self):
        # search with no query and no extra fields
        search_res = SimplifiedNode.search(self.admin1)
        expected_res = [modelinstance_to_dict(self.uni, SimplifiedNode._meta.resultfields.aslist()),
                        modelinstance_to_dict(self.uni_mat, SimplifiedNode._meta.resultfields.aslist()),
                        modelinstance_to_dict(self.uni_fys, SimplifiedNode._meta.resultfields.aslist()),
                        modelinstance_to_dict(self.uni_mat_inf, SimplifiedNode._meta.resultfields.aslist())]

        # assert that all search results are as expected
        self.assertEquals(search_res.count(), len(expected_res))
        for s in search_res:
            self.assertTrue(s in expected_res)

    def test_search_allextras(self):
        # search with no querys, but all extra fields
        search_res = SimplifiedNode.search(self.admin1, result_fieldgroups=self.allExtras)
        expected_res = [modelinstance_to_dict(self.uni, SimplifiedNode._meta.resultfields.aslist(self.allExtras)),
                        modelinstance_to_dict(self.uni_mat, SimplifiedNode._meta.resultfields.aslist(self.allExtras)),
                        modelinstance_to_dict(self.uni_fys, SimplifiedNode._meta.resultfields.aslist(self.allExtras)),
                        modelinstance_to_dict(self.uni_mat_inf, SimplifiedNode._meta.resultfields.aslist(self.allExtras))]

        # assert that all search results are as expected
        self.assertEquals(search_res.count(), len(expected_res))
        for s in search_res:
            self.assertTrue(s in expected_res)

    def test_search_withquery(self):
        # search with query
        search_res = SimplifiedNode.search(self.admin1, query='inf')
        expected_res = [modelinstance_to_dict(self.uni_mat_inf, SimplifiedNode._meta.resultfields.aslist())]

        # assert that all search results are as expected
        self.assertEquals(search_res.count(), len(expected_res))
        for s in search_res:
            self.assertTrue(s in expected_res)

    def test_search_queryandextras(self):
        # with query and extra fields.
        search_res = SimplifiedNode.search(self.admin1, query='inf', result_fieldgroups = self.allExtras)
        expected_res = [modelinstance_to_dict(self.uni_mat_inf, SimplifiedNode._meta.resultfields.aslist(self.allExtras))]

        # assert that all search results are as expected
        self.assertEquals(search_res.count(), len(expected_res))
        for s in search_res:
            self.assertTrue(s in expected_res)

    def test_search_security_asstudent(self):
        search_res = SimplifiedNode.search(self.teststud)
        self.assertEquals(search_res.count(), 0)

    def test_search_security_asexaminer(self):
        search_res = SimplifiedNode.search(self.testexam)
        self.assertEquals(search_res.count(), 0)

    def test_search_security_assubjectadmin(self):
        search_res = SimplifiedNode.search(self.testadmin)
        self.assertEquals(search_res.count(), 0)

    def test_delete_asnodeadmin(self):
        # this node has children and should raise permissiondenied
        with self.assertRaises(PermissionDenied):
            SimplifiedNode.delete(self.admin1, self.uni.id)

    def test_delete_assuperadmin(self):
        SimplifiedNode.delete(self.superadminuser, self.uni.id)

        with self.assertRaises(PermissionDenied):
            SimplifiedNode.delete(self.superadminuser, self.uni.id)

    def test_delete_noperm(self):
        self.add_to_path('uni;inf101.secondsem.a1.g2:candidate(secondStud):examiner(exam1)')
        with self.assertRaises(PermissionDenied):
            SimplifiedNode.delete(self.secondStud, self.uni.id)
        with self.assertRaises(PermissionDenied):
            SimplifiedNode.delete(self.exam1, self.uni.id)

class TestSimplifiedSubject(SimplifiedAdminTestBase):
    allExtras = SimplifiedSubject._meta.resultfields.additional_aslist()
    def setUp(self):
        super(TestSimplifiedSubject,self).setUp()
{'fraction_non_alphanumeric': 0.0997035839396389, 'fraction_numerical': 0.00880265876223839, 'mean_word_length': 3.012072072072072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3098499', 'n_tokens_mistral': 6787, 'n_tokens_neox': 6580, 'n_words': 1025}