test_models.py 16.5 KB
Newer Older
David Haynes's avatar
David Haynes committed
1 2 3 4
"""
go/test_models.py
"""

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

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

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

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

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

    # user ---------------------------------------------------------------------
28 29 30

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

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

38
    # full_name ----------------------------------------------------------------
39

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

        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
54
        check if full_name char field functions as intentioned
55 56 57 58 59 60 61 62 63 64 65 66
        """

        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
        """
67 68 69
        try:
            get_registered_user.save()
        except DataError as ex:
David Haynes's avatar
David Haynes committed
70
            self.assertTrue(ex)
71 72 73

    # blank=False is purely form validation related

74
    # organization -------------------------------------------------------------
75

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

        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
90
        check if organization char field functions as intentioned
91 92 93 94 95 96 97 98 99 100 101 102
        """

        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
103 104 105 106

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

109 110 111

    # blank=False is purely form validation related

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

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

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

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

128 129 130 131 132 133 134 135 136
        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"
        )
137 138


139
    # registered ---------------------------------------------------------------
140

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

        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
155
        test the registered bool
156 157 158 159 160
        """

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

Zac Wood's avatar
Zac Wood committed
161
        self.assertTrue(get_registered_user.registered)
162

163
    # approved -----------------------------------------------------------------
164

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

        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
179
        test the approved bool default
180 181 182 183 184 185 186
        """

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

        self.assertFalse(get_registered_user.approved)

187
    # blocked ------------------------------------------------------------------
188

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

        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)

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

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

        self.assertFalse(get_registered_user.blocked)


212
    # __str__ ------------------------------------------------------------------
213 214 215

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

219 220
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
Zac Wood's avatar
Zac Wood committed
221
        expected = '<Registered User: dhaynes - Approval Status: True>'
222
        actual = str(get_registered_user)
223 224 225 226
        self.assertEqual(expected, actual)


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

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

David Haynes's avatar
David Haynes committed
237
        # Setup a blank URL object with an owner
238 239 240 241
        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
242 243

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

246
    # owner --------------------------------------------------------------------
247

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

        # 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
264
        current_url.save()
265 266 267

        self.assertEqual(current_url.owner, get_registered_user)

268
    # date_created -------------------------------------------------------------
269

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

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

David Haynes's avatar
David Haynes committed
278
        # Get the URL to apply it to
David Haynes's avatar
David Haynes committed
279 280 281 282
        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
283
        # Apply the date
David Haynes's avatar
David Haynes committed
284
        current_url.date_created = now
285
        current_url.save()
David Haynes's avatar
David Haynes committed
286 287 288 289

        self.assertEqual(current_url.date_created, now)


290
    # target -------------------------------------------------------------------
291

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

        # 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
307
        current_url.save()
David Haynes's avatar
David Haynes committed
308 309 310 311 312

        self.assertEqual(current_url.target, test_url)

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

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


333
    # short --------------------------------------------------------------------
334

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

        # 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
356
        Test that the short field primary key functions as intended
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
        """

        # 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
379
        Test that a short field can be no longer than 20 characters
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        """

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

398
    # clicks -------------------------------------------------------------------
399

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

        # 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
411 412
        current_url.clicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
413 414 415

        self.assertEqual(current_url.clicks, 1)

416
    # qrclicks -----------------------------------------------------------------
417

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

        # 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
429 430
        current_url.qrclicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
431 432 433

        self.assertEqual(current_url.qrclicks, 1)

434
    # socialclicks -------------------------------------------------------------
435

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

        # 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
447 448
        current_url.socialclicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
449 450 451

        self.assertEqual(current_url.socialclicks, 1)

452
    # expires ------------------------------------------------------------------
453

454 455
    def test_expires(self):
        """
David Haynes's avatar
David Haynes committed
456
        Test that the expires field functions as intended
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
        """
        
        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)


473
    # __str__ ------------------------------------------------------------------
474

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

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

492
    # generate_valid_short -----------------------------------------------------
493

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

David Haynes's avatar
David Haynes committed
499 500 501 502
    #     # 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)
503

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