test_models.py 16.6 KB
Newer Older
1
2
3
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division

David Haynes's avatar
David Haynes committed
4
# Django Imports
David Haynes's avatar
David Haynes committed
5
from django.test import TestCase
6
from django.contrib.auth.models import User
7
from django.db.utils import DataError, IntegrityError
David Haynes's avatar
David Haynes committed
8
9
from django.utils import timezone

David Haynes's avatar
David Haynes committed
10
11
# App Imports
from go.models import URL, RegisteredUser
David Haynes's avatar
David Haynes committed
12

David Haynes's avatar
David Haynes committed
13
class RegisteredUserTest(TestCase):
14
    """
David Haynes's avatar
David Haynes committed
15
    Test cases for the RegisteredUser Model
16
    """
David Haynes's avatar
David Haynes committed
17

18
    def setUp(self):
19
        """
David Haynes's avatar
David Haynes committed
20
21
        Set up any variables such as dummy objects that will be utilised in
        testing methods
22
23
        """

24
        User.objects.create(username='dhaynes', password='password')
25
26

    # user ---------------------------------------------------------------------
27
28
29

    def test_registereduser_creation(self):
        """
David Haynes's avatar
David Haynes committed
30
        check if RegisteredUsers are actually made
31
32
        """

33
34
35
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        self.assertTrue(get_registered_user)
36

37
    # full_name ----------------------------------------------------------------
38

39
40
    def test_full_name(self):
        """
David Haynes's avatar
David Haynes committed
41
        check if full_name char field functions as intentioned
42
43
44
45
46
47
48
49
50
51
52
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.full_name = "David Haynes"
        get_registered_user.save()

        self.assertEqual(get_registered_user.full_name, "David Haynes")

    def test_full_name_length(self):
        """
David Haynes's avatar
David Haynes committed
53
        check if full_name char field functions as intentioned
54
55
56
57
58
59
60
61
62
63
64
65
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.full_name = """
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        """
66
67
68
        try:
            get_registered_user.save()
        except DataError as ex:
David Haynes's avatar
David Haynes committed
69
            self.assertTrue(ex)
70
71
72

    # blank=False is purely form validation related

73
    # organization -------------------------------------------------------------
74

75
76
    def test_organization(self):
        """
David Haynes's avatar
David Haynes committed
77
        check if organization char field functions as intentioned
78
79
80
81
82
83
84
85
86
87
88
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.organization = "SRCT"
        get_registered_user.save()

        self.assertEqual(get_registered_user.organization, "SRCT")

    def test_organization_length(self):
        """
David Haynes's avatar
David Haynes committed
89
        check if organization char field functions as intentioned
90
91
92
93
94
95
96
97
98
99
100
101
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.organization = """
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        """
David Haynes's avatar
David Haynes committed
102
103
104
105

        try:
            get_registered_user.save()
        except DataError as ex:
David Haynes's avatar
David Haynes committed
106
            self.assertTrue(ex)
David Haynes's avatar
David Haynes committed
107

108
109
110

    # blank=False is purely form validation related

111
    # description --------------------------------------------------------------
David Haynes's avatar
David Haynes committed
112

113
114
    def test_description_blank(self):
        """
David Haynes's avatar
David Haynes committed
115
        - add in description (blank)
116
117
        """

118
119
120
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        self.assertEqual(get_registered_user.description, "")
121
122
123

    def test_description_text(self):
        """
David Haynes's avatar
David Haynes committed
124
        - add in description (text)
125
126
        """

127
128
129
130
131
132
133
134
135
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.description = "We're going to build a big beautiful testcase"
        get_registered_user.save()

        self.assertEqual(
            get_registered_user.description,
            "We're going to build a big beautiful testcase"
        )
136
137


138
    # registered ---------------------------------------------------------------
139

140
141
    def test_registered(self):
        """
David Haynes's avatar
David Haynes committed
142
        test the registered bool
143
144
145
146
147
148
149
150
151
152
153
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.registered = True
        get_registered_user.save()

        self.assertTrue(get_registered_user.registered)

    def test_registered_default(self):
        """
David Haynes's avatar
David Haynes committed
154
        test the registered bool
155
156
157
158
159
160
161
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)

        self.assertFalse(get_registered_user.registered)

162
    # approved -----------------------------------------------------------------
163

164
165
    def test_approved(self):
        """
David Haynes's avatar
David Haynes committed
166
        test the approved bool
167
168
169
170
171
172
173
174
175
176
177
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.approved = True
        get_registered_user.save()

        self.assertTrue(get_registered_user.approved)

    def test_approved_default(self):
        """
David Haynes's avatar
David Haynes committed
178
        test the approved bool default
179
180
181
182
183
184
185
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)

        self.assertFalse(get_registered_user.approved)

186
    # blocked ------------------------------------------------------------------
187

188
189
    def test_blocked(self):
        """
David Haynes's avatar
David Haynes committed
190
        test the blocked bool
191
192
193
194
195
196
197
198
199
200
201
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.blocked = True
        get_registered_user.save()

        self.assertTrue(get_registered_user.blocked)

    def test_approved_default(self):
        """
David Haynes's avatar
David Haynes committed
202
        test the blocked bool default
203
204
205
206
207
208
209
210
        """

        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)

        self.assertFalse(get_registered_user.blocked)


211
    # __str__ ------------------------------------------------------------------
212
213
214

    def test_check_str(self):
        """
David Haynes's avatar
David Haynes committed
215
        check printing
216
        """
217

218
219
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
220
        expected = '<Registered User: dhaynes - Approval Status: False>'
221
        actual = str(get_registered_user)
222
223
224
225
        self.assertEqual(expected, actual)


class URLTest(TestCase):
226
    """
David Haynes's avatar
David Haynes committed
227
    Test cases for the URL Model
228
    """
229

230
231
    def setUp(self):
        """
David Haynes's avatar
David Haynes committed
232
233
        Set up any variables such as dummy objects that will be utilised in
        testing methods
234
235
        """

David Haynes's avatar
David Haynes committed
236
        # Setup a blank URL object with an owner
237
238
239
240
        User.objects.create(username='dhaynes', password='password')
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        URL.objects.create(owner=get_registered_user)
David Haynes's avatar
David Haynes committed
241
242

        # Create a dummy User object
243
244
        User.objects.create(username='evildhaynes', password='password')

245
    # owner --------------------------------------------------------------------
246

247
248
    def test_change_owner(self):
        """
David Haynes's avatar
David Haynes committed
249
        Test the ability to change the owner of a URL
250
251
252
253
254
255
256
257
258
259
260
261
262
        """

        # Original owner
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # New Owner
        get_user = User.objects.get(username='evildhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)

        # Change original owner to new owner 
        current_url.owner = get_registered_user
263
        current_url.save()
264
265
266

        self.assertEqual(current_url.owner, get_registered_user)

267
    # date_created -------------------------------------------------------------
268

David Haynes's avatar
David Haynes committed
269
270
    def test_date_created(self):
        """
David Haynes's avatar
David Haynes committed
271
        Test that the timedate is set properly on URL creation
David Haynes's avatar
David Haynes committed
272
273
        """

David Haynes's avatar
David Haynes committed
274
        # Get a date
275
        now = timezone.now()
David Haynes's avatar
David Haynes committed
276

David Haynes's avatar
David Haynes committed
277
        # Get the URL to apply it to
David Haynes's avatar
David Haynes committed
278
279
280
281
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

David Haynes's avatar
David Haynes committed
282
        # Apply the date
David Haynes's avatar
David Haynes committed
283
        current_url.date_created = now
284
        current_url.save()
David Haynes's avatar
David Haynes committed
285
286
287
288

        self.assertEqual(current_url.date_created, now)


289
    # target -------------------------------------------------------------------
290

David Haynes's avatar
David Haynes committed
291
292
    def test_target(self):
        """
David Haynes's avatar
David Haynes committed
293
        Test that the target field properly accepts a URL
David Haynes's avatar
David Haynes committed
294
295
296
297
298
299
300
301
302
303
304
305
        """

        # Get a URL
        test_url = "https://dhaynes.xyz"

        # Get the URL to apply it to
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Apply the URL
        current_url.target = test_url
306
        current_url.save()
David Haynes's avatar
David Haynes committed
307
308
309
310
311

        self.assertEqual(current_url.target, test_url)

    def test_target_length(self):
        """
David Haynes's avatar
David Haynes committed
312
        Test that we can't input a URL longer than 1000 chars
David Haynes's avatar
David Haynes committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
        """

        # Get a URL
        test_url = "https://ymzvakaamyamelmshikymeodyqogjbmrxfgjsjowjjluzbhmgaahkoflhftnicprokfsmkwzoczfowboagwvarbtozszvumruvjlnmxcyhzltgijfatiacihrnbennvvuiwpjpredeyrqdqvkhyjixohrhpyrhrzaptzfeacvkopzkvxcxapknoelcfapjiwlvwnhulmsadiuzhvjevywwvkjordyhyrqntfueycgasyantpcnartxappzmmhbhtyplatqylunvdfkpcrvjjuvpnprxrgcxzbatfcvipvhetoiuknlnwscrgtwruatjazkrmsbyvrkxjiggejxormncbrxwajhhmuvsmzaclaehievayhtjbublhrljdfrudxcmnmokmlpdvhbgkicbfezdjyxhhspdnnufevvcncdbqkmqbubvrtaeiniowpjuqyuvxpjqfuejubjbphempwgvhlrvmtjuqafsopppjqujpinphyslfyyoiysoozblpjtigjaaqiwwoggjspbotzgwzzjvhgeztcnkzwjeejjzrjrhiqvjurrncoluwmcxmfmhngaqovpxocishflcfklyoowqlgnjsmagadlpgaphptpeoojqkyhsfcyhoxjnfwczhnunyhvlnzcdauydaipefedqalakkfexbkddcyjxofxgvrhriryrjzrnvoudkvuehbrhfwudgsrxktflglkqdqptxeadlhpvgwobwrbyrynbljuzjrogjgpkgfkhaawcykwzpqeahkigkmldxkrzavoqhivlebfhkmwvxgfgveaqdkgxtaixzdlhbdgcygeuwqfquqaojutlrybdrlfvxitectjyfdjtsinsuahnxsfovecymnuswkrcptpkgjreccmhznbxngzhzarmaxenhkfncmmzqyqpiccugfnxdiyifzyjawykpgheayboekztyitvajbwgrnmhrpprmuteofemxtcfqcekwbkqgggggggggggggggg.xyz"

        # Get the URL to apply it to
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Apply the URL
        current_url.target = test_url

        try:
            current_url.save()
        except DataError as ex:
            self.assertTrue(ex)


332
    # short --------------------------------------------------------------------
333

334
335
    def test_short(self):
        """
David Haynes's avatar
David Haynes committed
336
        Test that the short field functions as intended
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
        """

        # Get a short 
        test_short = "dhaynes"

        # Get the URL to apply it to
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Apply the short
        current_url.short = test_short
        current_url.save()

        self.assertEqual(current_url.short, test_short)

    def test_short_dupe(self):
        """
David Haynes's avatar
David Haynes committed
355
        Test that the short field primary key functions as intended
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
        """

        # Get a short 
        test_short = "dhaynes"

        # Get the URL to apply it to
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Apply the short
        current_url.short = test_short
        current_url.save()

        try:
            new_url = URL.objects.create(owner=get_registered_user, short="dhaynes")
            new_url.save()
        except IntegrityError as ex:
            self.assertTrue(ex)

    def test_short_length(self):
        """
David Haynes's avatar
David Haynes committed
378
        Test that a short field can be no longer than 20 characters
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
        """

        # Get a invalid short
        test_short = "ggggggggggggggggggggg"

        # Get the URL to apply it to
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Apply the URL
        current_url.short = test_short

        try:
            current_url.save()
        except DataError as ex:
            self.assertTrue(ex)

397
    # clicks -------------------------------------------------------------------
398

David Haynes's avatar
David Haynes committed
399
400
    def test_clicks(self):
        """
David Haynes's avatar
David Haynes committed
401
        Test that clicks incremention works
David Haynes's avatar
David Haynes committed
402
403
404
405
406
407
408
409
        """

        # Get the URL to test
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Increment
David Haynes's avatar
David Haynes committed
410
411
        current_url.clicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
412
413
414

        self.assertEqual(current_url.clicks, 1)

415
    # qrclicks -----------------------------------------------------------------
416

David Haynes's avatar
David Haynes committed
417
418
    def test_qrclicks(self):
        """
David Haynes's avatar
David Haynes committed
419
        Test that cliqrclickscks incremention works
David Haynes's avatar
David Haynes committed
420
421
422
423
424
425
426
427
        """

        # Get the URL to test
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Increment
David Haynes's avatar
David Haynes committed
428
429
        current_url.qrclicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
430
431
432

        self.assertEqual(current_url.qrclicks, 1)

433
    # socialclicks -------------------------------------------------------------
434

David Haynes's avatar
David Haynes committed
435
436
    def test_socialclicks(self):
        """
David Haynes's avatar
David Haynes committed
437
        Test that socialclicks incremention works
David Haynes's avatar
David Haynes committed
438
439
440
441
442
443
444
445
        """

        # Get the URL to test
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Increment
David Haynes's avatar
David Haynes committed
446
447
        current_url.socialclicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
448
449
450

        self.assertEqual(current_url.socialclicks, 1)

451
    # expires ------------------------------------------------------------------
452

453
454
    def test_expires(self):
        """
David Haynes's avatar
David Haynes committed
455
        Test that the expires field functions as intended
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
        """
        
        tomorrow = timezone.now() + timezone.timedelta(days=1)

        # Get the URL to apply it to
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        # Apply the date
        current_url.date_created = tomorrow
        current_url.save()

        self.assertEqual(current_url.date_created, tomorrow)


472
    # __str__ ------------------------------------------------------------------
473

David Haynes's avatar
David Haynes committed
474
475
    def test_check_str(self):
        """
David Haynes's avatar
David Haynes committed
476
        check printing
David Haynes's avatar
David Haynes committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
        """

        # Get the URL to test
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        current_url = URL.objects.get(owner=get_registered_user)

        current_url.target = "https://dhaynes.xyz"
        current_url.save()

        expected = '<Owner: dhaynes - Target URL: https://dhaynes.xyz>'
        actual = str(current_url)
        self.assertEqual(expected, actual)

491
    # generate_valid_short -----------------------------------------------------
492

David Haynes's avatar
David Haynes committed
493
494
    # def test_generate_valid_short(self):
    #     """
David Haynes's avatar
David Haynes committed
495
    #     Test that we can generate a short url at will
David Haynes's avatar
David Haynes committed
496
    #     """
497

David Haynes's avatar
David Haynes committed
498
499
500
501
    #     # Get the URL to test
    #     get_user = User.objects.get(username='dhaynes')
    #     get_registered_user = RegisteredUser.objects.get(user=get_user)
    #     current_url = URL.objects.get(owner=get_registered_user)
502

David Haynes's avatar
David Haynes committed
503
    #     self.assertTrue(current_url.generate_valid_short())