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

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

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

David Haynes's avatar
David Haynes committed
15
class RegisteredUserTest(TestCase):
16
    def setUp(self):
17
        """
David Haynes's avatar
David Haynes committed
18
        Set up any variables such as dummy objects that will be utilized in
David Haynes's avatar
David Haynes committed
19
        testing methods
20
        """
21
        User.objects.create(username='dhaynes', password='password')
22

David Haynes's avatar
David Haynes committed
23
    # user --------------------------------------------------------------------
24 25
    def test_registereduser_creation(self):
        """
David Haynes's avatar
David Haynes committed
26
        check if RegisteredUsers are actually made
27
        """
28 29 30
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        self.assertTrue(get_registered_user)
31

David Haynes's avatar
David Haynes committed
32
    # full_name ---------------------------------------------------------------
33 34
    def test_full_name(self):
        """
David Haynes's avatar
David Haynes committed
35
        check if full_name char field functions as intentioned
36 37 38 39 40 41 42 43 44 45
        """
        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
46
        check if full_name char field functions as intentioned
47 48 49 50
        """
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        get_registered_user.full_name = """
David Haynes's avatar
David Haynes committed
51 52 53 54 55 56
        ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
57
        """
58 59 60
        try:
            get_registered_user.save()
        except DataError as ex:
David Haynes's avatar
David Haynes committed
61
            self.assertTrue(ex)
62

David Haynes's avatar
David Haynes committed
63
    # organization ------------------------------------------------------------
64 65
    def test_organization(self):
        """
David Haynes's avatar
David Haynes committed
66
        check if organization char field functions as intentioned
67 68 69 70 71 72 73 74 75 76
        """
        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
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 = """
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
        """
David Haynes's avatar
David Haynes committed
89 90 91 92

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

David Haynes's avatar
David Haynes committed
95
    # description -------------------------------------------------------------
96 97
    def test_description_blank(self):
        """
David Haynes's avatar
David Haynes committed
98
        add in description (blank)
99
        """
100 101 102
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        self.assertEqual(get_registered_user.description, "")
103 104 105

    def test_description_text(self):
        """
David Haynes's avatar
David Haynes committed
106
        add in description (text)
107
        """
108 109 110 111 112 113 114 115 116
        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"
        )
117

David Haynes's avatar
David Haynes committed
118
    # registered --------------------------------------------------------------
119 120
    def test_registered(self):
        """
David Haynes's avatar
David Haynes committed
121
        test the registered bool
122 123 124 125 126 127 128 129 130 131
        """
        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
132
        test the registered bool
133 134 135 136 137 138
        """
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)

        self.assertFalse(get_registered_user.registered)

David Haynes's avatar
David Haynes committed
139
    # approved ----------------------------------------------------------------
140 141
    def test_approved(self):
        """
David Haynes's avatar
David Haynes committed
142
        test the approved bool
143 144 145 146 147 148 149 150 151 152
        """
        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
153
        test the approved bool default
154 155 156 157 158 159
        """
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)

        self.assertFalse(get_registered_user.approved)

David Haynes's avatar
David Haynes committed
160
    # blocked -----------------------------------------------------------------
161 162
    def test_blocked(self):
        """
David Haynes's avatar
David Haynes committed
163
        test the blocked bool
164 165 166 167 168 169 170 171
        """
        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)

David Haynes's avatar
David Haynes committed
172
    def test_blocked_default(self):
173
        """
David Haynes's avatar
David Haynes committed
174
        test the blocked bool default
175 176 177 178 179 180
        """
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)

        self.assertFalse(get_registered_user.blocked)

David Haynes's avatar
David Haynes committed
181
    # __str__ -----------------------------------------------------------------
182 183
    def test_check_str(self):
        """
David Haynes's avatar
David Haynes committed
184
        check printing
185
        """
186 187
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
188
        expected = '<Registered User: dhaynes - Approval Status: False>'
189
        actual = str(get_registered_user)
190 191 192
        self.assertEqual(expected, actual)

class URLTest(TestCase):
193 194
    def setUp(self):
        """
David Haynes's avatar
David Haynes committed
195
        Set up any variables such as dummy objects that will be utilized in
David Haynes's avatar
David Haynes committed
196
        testing methods
197
        """
David Haynes's avatar
David Haynes committed
198
        # Setup a blank URL object with an owner
199 200 201 202
        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
203 204

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

David Haynes's avatar
David Haynes committed
207
    # owner -------------------------------------------------------------------
208 209
    def test_change_owner(self):
        """
David Haynes's avatar
David Haynes committed
210
        Test the ability to change the owner of a URL
211 212 213 214 215 216 217 218 219 220 221 222
        """
        # 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
223
        current_url.save()
224 225 226

        self.assertEqual(current_url.owner, get_registered_user)

David Haynes's avatar
David Haynes committed
227
    # date_created ------------------------------------------------------------
David Haynes's avatar
David Haynes committed
228 229
    def test_date_created(self):
        """
David Haynes's avatar
David Haynes committed
230
        Test that the timedate is set properly on URL creation
David Haynes's avatar
David Haynes committed
231
        """
David Haynes's avatar
David Haynes committed
232
        # Get a date
233
        now = timezone.now()
David Haynes's avatar
David Haynes committed
234

David Haynes's avatar
David Haynes committed
235
        # Get the URL to apply it to
David Haynes's avatar
David Haynes committed
236 237 238 239
        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
240
        # Apply the date
David Haynes's avatar
David Haynes committed
241
        current_url.date_created = now
242
        current_url.save()
David Haynes's avatar
David Haynes committed
243 244 245

        self.assertEqual(current_url.date_created, now)

David Haynes's avatar
David Haynes committed
246 247
    # destination ------------------------------------------------------------------
    def test_destination(self):
David Haynes's avatar
David Haynes committed
248
        """
David Haynes's avatar
David Haynes committed
249
        Test that the destination field properly accepts a URL
David Haynes's avatar
David Haynes committed
250 251 252 253 254 255 256 257 258 259
        """
        # 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
David Haynes's avatar
David Haynes committed
260
        current_url.destination = test_url
261
        current_url.save()
David Haynes's avatar
David Haynes committed
262

David Haynes's avatar
David Haynes committed
263
        self.assertEqual(current_url.destination, test_url)
David Haynes's avatar
David Haynes committed
264

David Haynes's avatar
David Haynes committed
265
    def test_destination_length(self):
David Haynes's avatar
David Haynes committed
266
        """
David Haynes's avatar
David Haynes committed
267
        Test that we can't input a URL longer than 1000 chars
David Haynes's avatar
David Haynes committed
268 269 270 271 272 273 274 275 276 277
        """
        # 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
David Haynes's avatar
David Haynes committed
278
        current_url.destination = test_url
David Haynes's avatar
David Haynes committed
279 280 281 282 283 284

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

David Haynes's avatar
David Haynes committed
285
    # short -------------------------------------------------------------------
286 287
    def test_short(self):
        """
David Haynes's avatar
David Haynes committed
288
        Test that the short field functions as intended
289
        """
David Haynes's avatar
David Haynes committed
290
        # Get a short
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        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
306
        Test that the short field primary key functions as intended
307
        """
David Haynes's avatar
David Haynes committed
308
        # Get a short
309 310 311 312 313 314 315 316 317 318 319 320
        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:
David Haynes's avatar
David Haynes committed
321 322
            new_url = URL.objects.create(owner=get_registered_user,
                                         short="dhaynes")
323 324 325 326 327 328
            new_url.save()
        except IntegrityError as ex:
            self.assertTrue(ex)

    def test_short_length(self):
        """
David Haynes's avatar
David Haynes committed
329
        Test that a short field can be no longer than 20 characters
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        """
        # 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)

David Haynes's avatar
David Haynes committed
347
    # clicks ------------------------------------------------------------------
David Haynes's avatar
David Haynes committed
348 349
    def test_clicks(self):
        """
David Haynes's avatar
David Haynes committed
350
        Test that clicks incremention works
David Haynes's avatar
David Haynes committed
351 352 353 354 355 356 357
        """
        # 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
358 359
        current_url.clicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
360 361 362

        self.assertEqual(current_url.clicks, 1)

David Haynes's avatar
David Haynes committed
363
    # qrclicks ----------------------------------------------------------------
David Haynes's avatar
David Haynes committed
364 365
    def test_qrclicks(self):
        """
David Haynes's avatar
David Haynes committed
366
        Test that cliqrclickscks incremention works
David Haynes's avatar
David Haynes committed
367 368 369 370 371 372 373
        """
        # 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
374 375
        current_url.qrclicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
376 377 378

        self.assertEqual(current_url.qrclicks, 1)

David Haynes's avatar
David Haynes committed
379
    # socialclicks ------------------------------------------------------------
David Haynes's avatar
David Haynes committed
380 381
    def test_socialclicks(self):
        """
David Haynes's avatar
David Haynes committed
382
        Test that socialclicks incremention works
David Haynes's avatar
David Haynes committed
383 384 385 386 387 388 389
        """
        # 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
390 391
        current_url.socialclicks += 1
        current_url.save()
David Haynes's avatar
David Haynes committed
392 393 394

        self.assertEqual(current_url.socialclicks, 1)

David Haynes's avatar
David Haynes committed
395
    # expires -----------------------------------------------------------------
396 397
    def test_expires(self):
        """
David Haynes's avatar
David Haynes committed
398
        Test that the expires field functions as intended
399 400 401 402 403 404 405 406 407 408 409 410 411 412
        """
        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)

David Haynes's avatar
David Haynes committed
413
    # __str__ -----------------------------------------------------------------
414 415 416 417 418 419 420 421 422
    # def test_check_str(self):
    #     """
    #     check printing
    #     """
    #     # 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)

David Haynes's avatar
David Haynes committed
423
    #     current_url.destination = "https://dhaynes.xyz"
424 425
    #     current_url.save()

David Haynes's avatar
David Haynes committed
426
    #     expected = '<Owner: dhaynes - destination URL: https://dhaynes.xyz>'
427 428
    #     actual = str(current_url)
    #     self.assertEqual(expected, actual) TODO