test_models.py 10.6 KB
Newer Older
1
2
3
4
5
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
6
from django.utils import timezone, translation
7
8
9
from django.utils.translation import ugettext_lazy as _

from members.models import Member, Membership, Profile
10
from registrations.models import Entry, Registration, Renewal
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27


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',
28
            address_country='NL',
29
30
31
32
33
34
35
36
37
38
39
40
41
42
            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,
        )

43
    def test_str(self):
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
        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))


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',
70
            address_country='NL',
71
            phone_number='06123456789',
72
            student_number='s1234567',
73
74
75
76
77
78
79
80
            birthday=timezone.now().replace(year=1990),
            language='en',
            length=Entry.MEMBERSHIP_YEAR,
            membership_type=Membership.MEMBER,
            status=Entry.STATUS_CONFIRM,
        )

    def setUp(self):
81
        translation.activate('en')
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        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):
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        self.registration.clean()
        user = get_user_model().objects.create_user('johnnydoe',
                                                    'johndoe@example.com')

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

        user.delete()
        self.registration.clean()
        Registration.objects.create(
            first_name='John',
            last_name='Doe',
            birthday=timezone.now().replace(year=1990),
            email='johndoe@example.com',
        )
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

        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()

129
130
131
132
133
134
135
136
137
138
139
140
        user.delete()
        self.registration.clean()
        Registration.objects.create(
            first_name='John',
            last_name='Doe',
            birthday=timezone.now().replace(year=1990),
            student_number='s1234567'
        )

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

141
142
143
144
145
146
147
148
149
150
151
    def test_require_student_number_members(self):
        with self.subTest('No student number entered'):
            self.registration.student_number = None
            with self.assertRaisesMessage(
                    ValidationError,
                    "{'student_number': ['This field is required.']}"
            ):
                self.registration.clean()

        with self.subTest('Type is benefactor'):
            self.registration.student_number = None
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
152
            self.registration.membership_type = Membership.BENEFACTOR
153
154
            self.registration.clean()

155
156
157
158
159
160
161
162
163
164
165
166
    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
167
168
        with self.assertRaisesMessage(
                ValidationError,
169
                "{'programme': ['This field is required.']}"
170
        ):
171
            self.registration.clean()
172
        self.registration.membership_type = Membership.BENEFACTOR
173
174
175
176
        self.registration.clean()

    def test_require_starting_year_members(self):
        self.registration.starting_year = None
177
178
        with self.assertRaisesMessage(
                ValidationError,
179
                "{'starting_year': ['This field is required.']}"
180
        ):
181
            self.registration.clean()
182
        self.registration.membership_type = Membership.BENEFACTOR
183
184
185
186
187
188
189
190
191
192
193
194
195
196
        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',
197
            address_country='NL',
198
199
200
201
202
203
204
            phone_number='06123456789',
            birthday=timezone.now().replace(year=1990),
            language='en',
            length=Entry.MEMBERSHIP_YEAR,
            membership_type=Membership.MEMBER,
        )

205
206
207
        with self.subTest(f'Create confirmation mail'
                          f'on save with status confirm'):
            self.assertEqual(len(mail.outbox), 1)
208
209
210
211
212
            confirm_url = (
                'https://thalia.localhost' +
                reverse('registrations:confirm-email',
                        args=[registration.pk])
            )
213
214
215
216
217
218
219
220
            self.assertTrue(confirm_url in mail.outbox[0].body)
            mail.outbox.clear()

        with self.subTest('No emails in outbox when status is not confirm'):
            registration.status = Entry.STATUS_REVIEW
            registration.save()

            self.assertEqual(len(mail.outbox), 0)
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
279
280
281
282
283
284
285
286


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()

287
    def test_benefactor_no_study_length(self):
288
        self.renewal.length = Entry.MEMBERSHIP_STUDY
289
        self.renewal.membership_type = Membership.BENEFACTOR
290
291
292
293
294
295
296
297
298
299
300
        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, {
301
                'length': 'Benefactors cannot have a membership '
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
                          '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.',
            })