test_models.py 9.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
from unittest import mock

from django.contrib.auth import get_user_model
from django.core import mail
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from members.models import Member, Membership, Profile
12
from registrations.models import Entry, Registration, Renewal
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131


class EntryTest(TestCase):
    fixtures = ['members.json']

    @classmethod
    def setUpTestData(cls):
        cls.registration = Registration.objects.create(
            first_name='John',
            last_name='Doe',
            email='johndoe@example.com',
            programme='computingscience',
            starting_year=2014,
            address_street='Heyendaalseweg 135',
            address_street2='',
            address_postal_code='6525AJ',
            address_city='Nijmegen',
            phone_number='06123456789',
            birthday=timezone.now().replace(year=1990),
            language='en',
            length=Entry.MEMBERSHIP_YEAR,
            membership_type=Membership.MEMBER,
            status=Entry.STATUS_CONFIRM,
        )
        cls.member = Member.objects.filter(last_name="Wiggers").first()
        cls.renewal = Renewal(
            member=cls.member,
            length=Entry.MEMBERSHIP_STUDY,
            membership_type=Membership.MEMBER,
        )

    @mock.patch('django.db.models.Model.__str__')
    def test_str(self, str_mock):
        entry = Entry(registration=self.registration)
        self.assertEqual(str(entry), '{} {} ({})'.format(
            self.registration.first_name, self.registration.last_name,
            self.registration.email))

        entry = Entry(renewal=self.renewal)
        self.assertEqual(str(entry), '{} {} ({})'.format(
            self.member.first_name, self.member.last_name,
            self.member.email))

        str_mock.return_value = 'return str'
        entry = Entry()
        str(entry)
        str_mock.assert_called_once_with()


class RegistrationTest(TestCase):
    """Tests registrations"""

    @classmethod
    def setUpTestData(cls):
        cls.registration = Registration.objects.create(
            first_name='John',
            last_name='Doe',
            email='johndoe@example.com',
            programme='computingscience',
            starting_year=2014,
            address_street='Heyendaalseweg 135',
            address_street2='',
            address_postal_code='6525AJ',
            address_city='Nijmegen',
            phone_number='06123456789',
            birthday=timezone.now().replace(year=1990),
            language='en',
            length=Entry.MEMBERSHIP_YEAR,
            membership_type=Membership.MEMBER,
            status=Entry.STATUS_CONFIRM,
        )

    def setUp(self):
        self.registration.refresh_from_db()

    def test_str(self):
        self.assertEqual(str(self.registration), '{} {} ({})'.format(
            self.registration.first_name, self.registration.last_name,
            self.registration.email))

    def test_full_clean_works(self):
        self.registration.full_clean()

    def test_clean_works(self):
        self.registration.clean()

    def test_unique_email_user(self):
        get_user_model().objects.create_user('johnnydoe',
                                             'johndoe@example.com')

        with self.assertRaises(ValidationError):
            self.registration.clean()

    def test_unique_student_number_user(self):
        self.registration.student_number = 's1234567'
        self.registration.clean()

        user = get_user_model().objects.create_user(
            'johnnydoe', 'johndoe2@example.com')
        Profile.objects.create(
            user=user,
            student_number='s1234567'
        )

        with self.assertRaises(ValidationError):
            self.registration.clean()

    def test_unique_username_user(self):
        self.registration.username = 'johndoe'
        self.registration.clean()

        get_user_model().objects.create_user('johndoe',
                                             'johndoe@example.com')

        with self.assertRaises(ValidationError):
            self.registration.clean()

    def test_require_programme_members(self):
        self.registration.programme = None
132 133 134 135
        with self.assertRaisesMessage(
                ValidationError,
                "{'programme': ['Dit veld is vereist.']}"
        ):
136 137 138 139 140 141
            self.registration.clean()
        self.registration.membership_type = Membership.SUPPORTER
        self.registration.clean()

    def test_require_starting_year_members(self):
        self.registration.starting_year = None
142 143 144 145
        with self.assertRaisesMessage(
                ValidationError,
                "{'starting_year': ['Dit veld is vereist.']}"
        ):
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
            self.registration.clean()
        self.registration.membership_type = Membership.SUPPORTER
        self.registration.clean()

    def test_save(self):
        registration = Registration.objects.create(
            first_name='John',
            last_name='Doe',
            email='johndoe@example.com',
            programme='computingscience',
            student_number='s1234567',
            starting_year=2014,
            address_street='Heyendaalseweg 135',
            address_street2='',
            address_postal_code='6525AJ',
            address_city='Nijmegen',
            phone_number='06123456789',
            birthday=timezone.now().replace(year=1990),
            language='en',
            length=Entry.MEMBERSHIP_YEAR,
            membership_type=Membership.MEMBER,
        )

        self.assertEqual(len(mail.outbox), 1)
        confirm_url = '{}{}'.format('https://thalia.nu',
                                    reverse('registrations:confirm-email',
                                            args=[registration.pk]))
        self.assertTrue(confirm_url in mail.outbox[0].body)
        mail.outbox.clear()

        registration.status = Entry.STATUS_REVIEW
        registration.save()

        self.assertEqual(len(mail.outbox), 0)


class RenewalTest(TestCase):
    fixtures = ['members.json']

    def setUp(self):
        self.member = Member.objects.filter(last_name="Wiggers").first()
        self.renewal = Renewal(
            member=self.member,
            length=Entry.MEMBERSHIP_STUDY,
            membership_type=Membership.MEMBER,
        )

    def test_str(self):
        self.assertEqual(str(self.renewal), '{} {} ({})'.format(
            self.member.first_name, self.member.last_name,
            self.member.email))

    def test_clean_works(self):
        self.member.membership_set.all().delete()
        self.renewal.clean()

    def test_existing_renewal_in_review(self):
        Renewal.objects.create(
            member=self.member,
            length=Entry.MEMBERSHIP_STUDY,
            membership_type=Membership.MEMBER,
        )

        with self.assertRaises(ValidationError):
            self.renewal.clean()

        try:
            self.renewal.clean()
        except ValidationError as e:
            self.assertEqual(e.message, _('You already have a renewal '
                                          'request queued for review.'))

    def test_not_within_renew_period(self):
        membership = self.member.latest_membership
        membership.until = (timezone.now().date() +
                            timezone.timedelta(days=32))
        membership.save()

        self.renewal.length = Entry.MEMBERSHIP_YEAR

        with self.assertRaises(ValidationError):
            self.renewal.clean()

        try:
            self.renewal.clean()
        except ValidationError as e:
            self.assertCountEqual(e.error_dict, {
                'length': 'You cannot renew your membership at this moment.',
            })

    def test_within_renew_period(self):
        self.renewal.length = Entry.MEMBERSHIP_YEAR

        membership = self.member.latest_membership
        membership.until = (timezone.now().date() +
                            timezone.timedelta(days=31))
        membership.save()

        self.renewal.clean()

    def test_supporter_no_study_length(self):
        self.renewal.length = Entry.MEMBERSHIP_STUDY
        self.renewal.membership_type = Membership.SUPPORTER
        membership = self.member.latest_membership
        membership.until = timezone.now()
        membership.save()

        with self.assertRaises(ValidationError):
            self.renewal.clean()

        try:
            self.renewal.clean()
        except ValidationError as e:
            self.assertCountEqual(e.error_dict, {
                'length': 'Supporters cannot have a membership '
                          'that lasts their entire study duration.',
            })

    def test_has_active_membership(self):
        membership = self.member.current_membership
        membership.until = None
        membership.save()

        with self.assertRaises(ValidationError):
            self.renewal.clean()

        try:
            self.renewal.clean()
        except ValidationError as e:
            self.assertCountEqual(e.error_dict, {
                'length': 'You currently have an active membership.',
                'membership_type': 'You currently have an active membership.',
            })