<commit_before>from datetime import datetime

from django.test import TestCase
from django.core import mail
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError

from devilry.project.develop.testhelpers.corebuilder import UserBuilder
from devilry.project.develop.testhelpers.corebuilder import PeriodBuilder
from devilry.project.develop.testhelpers.datebuilder import DateTimeBuilder
from devilry.apps.core.models import GroupInvite
from devilry.apps.core.models import AssignmentGroup


class TestGroupInvite(TestCase):
    def setUp(self):
        self.testuser1 = UserBuilder('testuser1').user
        self.testuser2 = UserBuilder('testuser2').user
        self.testuser3 = UserBuilder('testuser3').user

    def test_create_sanity(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        before = datetime.now()
        invite = GroupInvite(
            group=group,
            sent_by=self.testuser1,
            sent_to=self.testuser2)
        invite.full_clean()
        invite.save()
        after = datetime.now()
        self.assertTrue(invite.sent_datetime >= before and invite.sent_datetime <= after)

    def test_only_groupmember_can_invite(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The user sending an invite must be a Candiate on the group.*$'):
            GroupInvite(group=group, sent_by=self.testuser2, sent_to=self.testuser3).clean()

    def test_can_not_invite_groupmember(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1, self.testuser2]).group
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The student is already a member of the group.*$'):
            GroupInvite(group=group, sent_by=self.testuser1, sent_to=self.testuser2).clean()

    def test_can_not_invite_self(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser1) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The student is already a member of the group.*$'):
            GroupInvite(group=group, sent_by=self.testuser1, sent_to=self.testuser1).clean()

    def test_can_not_invite_someone_that_already_has_invite_for_group(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        GroupInvite(group=group, sent_by=self.testuser1, sent_to=self.testuser2).save()
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The student is already invited to join the group, but they have not responded yet.*$'):
            GroupInvite(group=group, sent_by=self.testuser1, sent_to=self.testuser2).clean()

    def test_can_not_invite_someone_that_is_already_in_projectgroup(self):
        periodbuilder = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2)
        assignment1builder = periodbuilder.add_assignment('assignment1',
                                                          students_can_create_groups=True)
        assignment2builder = periodbuilder.add_assignment('assignment2',
                                                          students_can_create_groups=True)

        assignment1builder.add_group(students=[self.testuser2, self.testuser3]).group
        group1 = assignment1builder.add_group(students=[self.testuser1]).group
        group2 = assignment2builder.add_group(students=[self.testuser1]).group

        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The invited student is already in a project group.*$'):
            GroupInvite(group=group1, sent_by=self.testuser1, sent_to=self.testuser2,
                        accepted=True).clean()

        # Ensure we are not affected by having groups in other assignments
        GroupInvite(group=group2, sent_by=self.testuser1, sent_to=self.testuser2,
                    accepted=True).clean()

    def test_only_when_allowed_on_assignment(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=False) \
            .add_group(students=[self.testuser1]).group
        invite = GroupInvite(
            group=group,
            sent_by=self.testuser1,
            sent_to=self.testuser2)
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*This assignment does not allow students to form project groups on their own.*$'):
            invite.full_clean()

    def test_students_can_not_create_groups_after(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_not_create_groups_after=DateTimeBuilder.now().minus(days=1),
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        invite = GroupInvite(
            group=group,
            sent_by=self.testuser1,
            sent_to=self.testuser2)
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*Creating project groups without administrator approval is not allowed on this assignment anymore.*$'):<commit_msg>core...test_groupinvite: Fixed pep8 errors.<commit_after>from datetime import datetime

from django.test import TestCase
from django.core import mail
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError

from devilry.project.develop.testhelpers.corebuilder import UserBuilder
from devilry.project.develop.testhelpers.corebuilder import PeriodBuilder
from devilry.project.develop.testhelpers.datebuilder import DateTimeBuilder
from devilry.apps.core.models import GroupInvite
from devilry.apps.core.models import AssignmentGroup


class TestGroupInvite(TestCase):
    def setUp(self):
        self.testuser1 = UserBuilder('testuser1').user
        self.testuser2 = UserBuilder('testuser2').user
        self.testuser3 = UserBuilder('testuser3').user

    def test_create_sanity(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        before = datetime.now()
        invite = GroupInvite(
            group=group,
            sent_by=self.testuser1,
            sent_to=self.testuser2)
        invite.full_clean()
        invite.save()
        after = datetime.now()
        self.assertTrue(invite.sent_datetime >= before and invite.sent_datetime <= after)

    def test_only_groupmember_can_invite(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The user sending an invite must be a Candiate on the group.*$'):
            GroupInvite(group=group, sent_by=self.testuser2, sent_to=self.testuser3).clean()

    def test_can_not_invite_groupmember(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1, self.testuser2]).group
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The student is already a member of the group.*$'):
            GroupInvite(group=group, sent_by=self.testuser1, sent_to=self.testuser2).clean()

    def test_can_not_invite_self(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser1) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The student is already a member of the group.*$'):
            GroupInvite(group=group, sent_by=self.testuser1, sent_to=self.testuser1).clean()

    def test_can_not_invite_someone_that_already_has_invite_for_group(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        GroupInvite(group=group, sent_by=self.testuser1, sent_to=self.testuser2).save()
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The student is already invited to join the group, '
                                     r'but they have not responded yet.*$'):
            GroupInvite(group=group, sent_by=self.testuser1, sent_to=self.testuser2).clean()

    def test_can_not_invite_someone_that_is_already_in_projectgroup(self):
        periodbuilder = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2)
        assignment1builder = periodbuilder.add_assignment('assignment1',
                                                          students_can_create_groups=True)
        assignment2builder = periodbuilder.add_assignment('assignment2',
                                                          students_can_create_groups=True)

        assignment1builder.add_group(students=[self.testuser2, self.testuser3])
        group1 = assignment1builder.add_group(students=[self.testuser1]).group
        group2 = assignment2builder.add_group(students=[self.testuser1]).group

        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*The invited student is already in a project group.*$'):
            GroupInvite(group=group1, sent_by=self.testuser1, sent_to=self.testuser2,
                        accepted=True).clean()

        # Ensure we are not affected by having groups in other assignments
        GroupInvite(group=group2, sent_by=self.testuser1, sent_to=self.testuser2,
                    accepted=True).clean()

    def test_only_when_allowed_on_assignment(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_create_groups=False) \
            .add_group(students=[self.testuser1]).group
        invite = GroupInvite(
            group=group,
            sent_by=self.testuser1,
            sent_to=self.testuser2)
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*This assignment does not allow students to form '
                                     r'project groups on their own.*$'):
            invite.full_clean()

    def test_students_can_not_create_groups_after(self):
        group = PeriodBuilder.quickadd_ducku_duck1010_active() \
            .add_relatedstudents(self.testuser2) \
            .add_assignment('assignment1',
                            students_can_not_create_groups_after=DateTimeBuilder.now().minus(days=1),
                            students_can_create_groups=True) \
            .add_group(students=[self.testuser1]).group
        invite = GroupInvite(
            group=group,
            sent_by=self.testuser1,
            sent_to=self.testuser2)
        with self.assertRaisesRegexp(ValidationError,
                                     r'^.*Creating project groups without administrator approval is '
                                     r'not allowed on this assignment anymore.*$'):
{'fraction_non_alphanumeric': 0.08560840958935188, 'fraction_numerical': 0.014747031687211677, 'mean_word_length': 2.357197258187357, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '359053', 'n_tokens_mistral': 3716, 'n_tokens_neox': 3657, 'n_words': 562}