aboutsummaryrefslogtreecommitdiff
path: root/tests/models/thl/test_user.py
blob: 4f10861da5041672106701bf0453c9426462bee8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import json
from datetime import datetime, timezone, timedelta
from decimal import Decimal
from random import randint, choice as rand_choice
from uuid import uuid4

import pytest
from pydantic import ValidationError


class TestUserUserID:

    def test_valid(self):
        from generalresearch.models.thl.user import User

        val = randint(1, 2**30)
        user = User(user_id=val)
        assert user.user_id == val

    def test_type(self):
        from generalresearch.models.thl.user import User

        # It will cast str to int
        assert User(user_id="1").user_id == 1

        # It will cast float to int
        assert User(user_id=1.0).user_id == 1

        # It will cast Decimal to int
        assert User(user_id=Decimal("1.0")).user_id == 1

        # pydantic Validation error is a ValueError, let's check both..
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=Decimal("1.00000001"))
        assert "1 validation error for User" in str(cm.value)
        assert "user_id" in str(cm.value)
        assert "Input should be a valid integer," in str(cm.value)

        with pytest.raises(expected_exception=ValidationError) as cm:
            User(user_id=Decimal("1.00000001"))
        assert "1 validation error for User" in str(cm.value)
        assert "user_id" in str(cm.value)
        assert "Input should be a valid integer," in str(cm.value)

    def test_zero(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(expected_exception=ValidationError) as cm:
            User(user_id=0)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be greater than 0" in str(cm.value)

    def test_negative(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(expected_exception=ValidationError) as cm:
            User(user_id=-1)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be greater than 0" in str(cm.value)

    def test_too_big(self):
        from generalresearch.models.thl.user import User

        val = 2**31
        with pytest.raises(expected_exception=ValidationError) as cm:
            User(user_id=val)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be less than 2147483648" in str(cm.value)

    def test_identifiable(self):
        from generalresearch.models.thl.user import User

        val = randint(1, 2**30)
        user = User(user_id=val)
        assert user.is_identifiable


class TestUserProductID:
    user_id = randint(1, 2**30)

    def test_valid(self):
        from generalresearch.models.thl.user import User

        product_id = uuid4().hex

        user = User(user_id=self.user_id, product_id=product_id)
        assert user.user_id == self.user_id
        assert user.product_id == product_id

    def test_type(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_id=0)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_id=0.0)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_id=Decimal("0"))
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

    def test_empty(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_id="")
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at least 32 characters" in str(cm.value)

    def test_invalid_len(self):
        from generalresearch.models.thl.user import User

        # Valid uuid4s are 32 char long
        product_id = uuid4().hex[:31]
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_id=product_id)
        assert "1 validation error for User", str(cm.value)
        assert "String should have at least 32 characters", str(cm.value)

        product_id = uuid4().hex * 2
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, product_id=product_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at most 32 characters" in str(cm.value)

        product_id = uuid4().hex
        product_id *= 2
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_id=product_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at most 32 characters" in str(cm.value)

    def test_invalid_uuid(self):
        from generalresearch.models.thl.user import User

        # Modify the UUID to break it
        product_id = uuid4().hex[:31] + "x"

        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_id=product_id)
        assert "1 validation error for User" in str(cm.value)
        assert "Invalid UUID" in str(cm.value)

    def test_invalid_hex_form(self):
        from generalresearch.models.thl.user import User

        # Sure not in hex form, but it'll get caught for being the
        # wrong length before anything else
        product_id = str(uuid4())  # '1a93447e-c77b-4cfa-b58e-ed4777d57110'
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_id=product_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at most 32 characters" in str(cm.value)

    def test_identifiable(self):
        """Can't create a User with only a product_id because it also
        needs to the product_user_id"""
        from generalresearch.models.thl.user import User

        product_id = uuid4().hex
        with pytest.raises(expected_exception=ValueError) as cm:
            User(product_id=product_id)
        assert "1 validation error for User" in str(cm.value)
        assert "Value error, User is not identifiable" in str(cm.value)


class TestUserProductUserID:
    user_id = randint(1, 2**30)

    def randomword(self, length: int = 50):
        # Raw so nothing is escaped to add additional backslashes
        _bpuid_allowed = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+,-.:;<=>?@[]^_{|}~"
        return "".join(rand_choice(_bpuid_allowed) for i in range(length))

    def test_valid(self):
        from generalresearch.models.thl.user import User

        product_user_id = uuid4().hex[:12]
        user = User(user_id=self.user_id, product_user_id=product_user_id)

        assert user.user_id == self.user_id
        assert user.product_user_id == product_user_id

    def test_type(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id=0)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id=0.0)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, product_user_id=Decimal("0"))
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

    def test_empty(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id="")
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at least 3 characters" in str(cm.value)

    def test_invalid_len(self):
        from generalresearch.models.thl.user import User

        product_user_id = self.randomword(251)
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id=product_user_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at most 128 characters" in str(cm.value)

        product_user_id = self.randomword(2)
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id=product_user_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at least 3 characters" in str(cm.value)

    def test_invalid_chars_space(self):
        from generalresearch.models.thl.user import User

        product_user_id = f"{self.randomword(50)} {self.randomword(50)}"
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id=product_user_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String cannot contain spaces" in str(cm.value)

    def test_invalid_chars_slash(self):
        from generalresearch.models.thl.user import User

        product_user_id = f"{self.randomword(50)}\{self.randomword(50)}"
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id=product_user_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String cannot contain backslash" in str(cm.value)

        product_user_id = f"{self.randomword(50)}/{self.randomword(50)}"
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id=product_user_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String cannot contain slash" in str(cm.value)

    def test_invalid_chars_backtick(self):
        """Yes I could keep doing these specific character checks. However,
        I wanted a test that made sure the regex was hit. I do not know
        how we want to provide with the level of specific String checks
        we do in here for specific error messages."""
        from generalresearch.models.thl.user import User

        product_user_id = f"{self.randomword(50)}`{self.randomword(50)}"
        with pytest.raises(expected_exception=ValueError) as cm:
            User(user_id=self.user_id, product_user_id=product_user_id)
        assert "1 validation error for User" in str(cm.value)
        assert "String is not valid regex" in str(cm.value)

    def test_unique_from_product_id(self):
        # We removed this filter b/c these users already exist. the manager checks for this
        # though and we can't create new users like this
        pass
        # product_id = uuid4().hex
        #
        # with pytest.raises(ValueError) as cm:
        #     User(product_id=product_id, product_user_id=product_id)
        # assert "1 validation error for User", str(cm.exception))
        # assert "product_user_id must not equal the product_id", str(cm.exception))

    def test_identifiable(self):
        """Can't create a User with only a product_user_id because it also
        needs to the product_id"""
        from generalresearch.models.thl.user import User

        product_user_id = uuid4().hex
        with pytest.raises(ValueError) as cm:
            User(product_user_id=product_user_id)
        assert "1 validation error for User" in str(cm.value)
        assert "Value error, User is not identifiable" in str(cm.value)


class TestUserUUID:
    user_id = randint(1, 2**30)

    def test_valid(self):
        from generalresearch.models.thl.user import User

        uuid_pk = uuid4().hex

        user = User(user_id=self.user_id, uuid=uuid_pk)
        assert user.user_id == self.user_id
        assert user.uuid == uuid_pk

    def test_type(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid=0)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid=0.0)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid=Decimal("0"))
        assert "1 validation error for User", str(cm.value)
        assert "Input should be a valid string" in str(cm.value)

    def test_empty(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid="")
        assert "1 validation error for User", str(cm.value)
        assert "String should have at least 32 characters", str(cm.value)

    def test_invalid_len(self):
        from generalresearch.models.thl.user import User

        # Valid uuid4s are 32 char long
        uuid_pk = uuid4().hex[:31]
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid=uuid_pk)
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at least 32 characters" in str(cm.value)

        # Valid uuid4s are 32 char long
        uuid_pk = uuid4().hex
        uuid_pk *= 2
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid=uuid_pk)
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at most 32 characters" in str(cm.value)

    def test_invalid_uuid(self):
        from generalresearch.models.thl.user import User

        # Modify the UUID to break it
        uuid_pk = uuid4().hex[:31] + "x"

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid=uuid_pk)
        assert "1 validation error for User" in str(cm.value)
        assert "Invalid UUID" in str(cm.value)

    def test_invalid_hex_form(self):
        from generalresearch.models.thl.user import User

        # Sure not in hex form, but it'll get caught for being the
        # wrong length before anything else
        uuid_pk = str(uuid4())  # '1a93447e-c77b-4cfa-b58e-ed4777d57110'
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid=uuid_pk)
        assert "1 validation error for User" in str(cm.value)
        assert "String should have at most 32 characters" in str(cm.value)

        uuid_pk = str(uuid4())[:32]  # '1a93447e-c77b-4cfa-b58e-ed4777d57110'
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, uuid=uuid_pk)
        assert "1 validation error for User" in str(cm.value)
        assert "Invalid UUID" in str(cm.value)

    def test_identifiable(self):
        from generalresearch.models.thl.user import User

        user_uuid = uuid4().hex
        user = User(uuid=user_uuid)
        assert user.is_identifiable


class TestUserCreated:
    user_id = randint(1, 2**30)

    def test_valid(self):
        from generalresearch.models.thl.user import User

        user = User(user_id=self.user_id)
        dt = datetime.now(tz=timezone.utc)
        user.created = dt

        assert user.created == dt

    def test_tz_naive_throws_init(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, created=datetime.now(tz=None))
        assert "1 validation error for User" in str(cm.value)
        assert "Input should have timezone info" in str(cm.value)

    def test_tz_naive_throws_setter(self):
        from generalresearch.models.thl.user import User

        user = User(user_id=self.user_id)
        with pytest.raises(ValueError) as cm:
            user.created = datetime.now(tz=None)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should have timezone info" in str(cm.value)

    def test_tz_utc(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(ValueError) as cm:
            User(
                user_id=self.user_id,
                created=datetime.now(tz=timezone(-timedelta(hours=8))),
            )
        assert "1 validation error for User" in str(cm.value)
        assert "Timezone is not UTC" in str(cm.value)

    def test_not_in_future(self):
        from generalresearch.models.thl.user import User

        the_future = datetime.now(tz=timezone.utc) + timedelta(minutes=1)
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, created=the_future)
        assert "1 validation error for User" in str(cm.value)
        assert "Input is in the future" in str(cm.value)

    def test_after_anno_domini(self):
        from generalresearch.models.thl.user import User

        before_ad = datetime(
            year=2015, month=1, day=1, tzinfo=timezone.utc
        ) + timedelta(minutes=1)
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, created=before_ad)
        assert "1 validation error for User" in str(cm.value)
        assert "Input is before Anno Domini" in str(cm.value)


class TestUserLastSeen:
    user_id = randint(1, 2**30)

    def test_valid(self):
        from generalresearch.models.thl.user import User

        user = User(user_id=self.user_id)
        dt = datetime.now(tz=timezone.utc)
        user.last_seen = dt

        assert user.last_seen == dt

    def test_tz_naive_throws_init(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, last_seen=datetime.now(tz=None))
        assert "1 validation error for User" in str(cm.value)
        assert "Input should have timezone info" in str(cm.value)

    def test_tz_naive_throws_setter(self):
        from generalresearch.models.thl.user import User

        user = User(user_id=self.user_id)
        with pytest.raises(ValueError) as cm:
            user.last_seen = datetime.now(tz=None)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should have timezone info" in str(cm.value)

    def test_tz_utc(self):
        from generalresearch.models.thl.user import User

        with pytest.raises(ValueError) as cm:
            User(
                user_id=self.user_id,
                last_seen=datetime.now(tz=timezone(-timedelta(hours=8))),
            )
        assert "1 validation error for User" in str(cm.value)
        assert "Timezone is not UTC" in str(cm.value)

    def test_not_in_future(self):
        from generalresearch.models.thl.user import User

        the_future = datetime.now(tz=timezone.utc) + timedelta(minutes=1)
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, last_seen=the_future)
        assert "1 validation error for User" in str(cm.value)
        assert "Input is in the future" in str(cm.value)

    def test_after_anno_domini(self):
        from generalresearch.models.thl.user import User

        before_ad = datetime(
            year=2015, month=1, day=1, tzinfo=timezone.utc
        ) + timedelta(minutes=1)
        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, last_seen=before_ad)
        assert "1 validation error for User" in str(cm.value)
        assert "Input is before Anno Domini" in str(cm.value)


class TestUserBlocked:
    user_id = randint(1, 2**30)

    def test_valid(self):
        from generalresearch.models.thl.user import User

        user = User(user_id=self.user_id, blocked=True)
        assert user.blocked

    def test_str_casting(self):
        """We don't want any of these to work, and that's why
        we set strict=True on the column"""
        from generalresearch.models.thl.user import User

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, blocked="true")
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid boolean" in str(cm.value)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, blocked="True")
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid boolean" in str(cm.value)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, blocked="1")
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid boolean" in str(cm.value)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, blocked="yes")
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid boolean" in str(cm.value)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, blocked="no")
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid boolean" in str(cm.value)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, blocked=uuid4().hex)
        assert "1 validation error for User" in str(cm.value)
        assert "Input should be a valid boolean" in str(cm.value)


class TestUserTiming:
    user_id = randint(1, 2**30)

    def test_valid(self):
        from generalresearch.models.thl.user import User

        created = datetime.now(tz=timezone.utc) - timedelta(minutes=60)
        last_seen = datetime.now(tz=timezone.utc) - timedelta(minutes=59)

        user = User(user_id=self.user_id, created=created, last_seen=last_seen)
        assert user.created == created
        assert user.last_seen == last_seen

    def test_created_first(self):
        from generalresearch.models.thl.user import User

        created = datetime.now(tz=timezone.utc) - timedelta(minutes=60)
        last_seen = datetime.now(tz=timezone.utc) - timedelta(minutes=59)

        with pytest.raises(ValueError) as cm:
            User(user_id=self.user_id, created=last_seen, last_seen=created)
        assert "1 validation error for User" in str(cm.value)
        assert "User created time invalid" in str(cm.value)


class TestUserModelVerification:
    """Tests that may be dependent on more than 1 attribute"""

    def test_identifiable(self):
        from generalresearch.models.thl.user import User

        product_id = uuid4().hex
        product_user_id = uuid4().hex
        user = User(product_id=product_id, product_user_id=product_user_id)
        assert user.is_identifiable

    def test_valid_helper(self):
        from generalresearch.models.thl.user import User

        user_bool = User.is_valid_ubp(
            product_id=uuid4().hex, product_user_id=uuid4().hex
        )
        assert user_bool

        user_bool = User.is_valid_ubp(product_id=uuid4().hex, product_user_id=" - - - ")
        assert not user_bool


class TestUserSerialization:

    def test_basic_json(self):
        from generalresearch.models.thl.user import User

        product_id = uuid4().hex
        product_user_id = uuid4().hex

        user = User(
            product_id=product_id,
            product_user_id=product_user_id,
            created=datetime.now(tz=timezone.utc),
            blocked=False,
        )

        d = json.loads(user.to_json())
        assert d.get("product_id") == product_id
        assert d.get("product_user_id") == product_user_id
        assert not d.get("blocked")

        assert d.get("product") is None
        assert d.get("created").endswith("Z")

    def test_basic_dict(self):
        from generalresearch.models.thl.user import User

        product_id = uuid4().hex
        product_user_id = uuid4().hex

        user = User(
            product_id=product_id,
            product_user_id=product_user_id,
            created=datetime.now(tz=timezone.utc),
            blocked=False,
        )

        d = user.to_dict()
        assert d.get("product_id") == product_id
        assert d.get("product_user_id") == product_user_id
        assert not d.get("blocked")

        assert d.get("product") is None
        assert d.get("created").tzinfo == timezone.utc

    def test_from_json(self):
        from generalresearch.models.thl.user import User

        product_id = uuid4().hex
        product_user_id = uuid4().hex

        user = User(
            product_id=product_id,
            product_user_id=product_user_id,
            created=datetime.now(tz=timezone.utc),
            blocked=False,
        )

        u = User.model_validate_json(user.to_json())
        assert u.product_id == product_id
        assert u.product is None
        assert u.created.tzinfo == timezone.utc


class TestUserMethods:

    def test_audit_log(self, user, audit_log_manager):
        assert user.audit_log is None
        user.prefetch_audit_log(audit_log_manager=audit_log_manager)
        assert user.audit_log == []

        audit_log_manager.create_dummy(user_id=user.user_id)
        user.prefetch_audit_log(audit_log_manager=audit_log_manager)
        assert len(user.audit_log) == 1

    def test_transactions(
        self, user_factory, thl_lm, session_with_tx_factory, product_user_wallet_yes
    ):
        u1 = user_factory(product=product_user_wallet_yes)

        assert u1.transactions is None
        u1.prefetch_transactions(thl_lm=thl_lm)
        assert u1.transactions == []

        session_with_tx_factory(user=u1)

        u1.prefetch_transactions(thl_lm=thl_lm)
        assert len(u1.transactions) == 1

    @pytest.mark.skip(reason="TODO")
    def test_location_history(self, user):
        assert user.location_history is None