Commit 2d9b18c
chore: Use mock time for consistent token generation and verification tests (#881)
* Fix(tests): Use mock time for consistent token generation and verification tests
Patches time.time and google.auth.jwt._helpers.utcnow to use a fixed
timestamp (MOCK_CURRENT_TIME) throughout tests/test_token_gen.py.
This addresses test flakiness and inconsistencies by ensuring that:
1. Tokens and cookies are generated with predictable `iat` and `exp` claims
based on MOCK_CURRENT_TIME.
2. The verification logic within the Firebase Admin SDK and the underlying
google-auth library also uses MOCK_CURRENT_TIME.
Helper functions _get_id_token and _get_session_cookie were updated to
default to using MOCK_CURRENT_TIME for their internal time calculations,
simplifying test code.
Relevant fixtures and token definitions were updated to rely on these
new defaults and the fixed timestamp.
The setup_method in TestVerifyIdToken, TestVerifySessionCookie,
TestCertificateCaching, and TestCertificateFetchTimeout now mock
time.time and google.auth.jwt._helpers.utcnow to ensure that all
time-sensitive operations during testing use the MOCK_CURRENT_TIME.
* Fix(tests): Apply time mocking to test_tenant_mgt.py
Extends the time mocking strategy (using a fixed MOCK_CURRENT_TIME)
to tests in `tests/test_tenant_mgt.py` to ensure consistency with
changes previously made in `tests/test_token_gen.py`.
Specifically:
- Imported `MOCK_CURRENT_TIME` from `tests.test_token_gen`.
- Added `setup_method` (and `teardown_method`) to the
`TestVerifyIdToken` and `TestCreateCustomToken` classes.
- These setup methods patch `time.time` and
`google.auth.jwt._helpers.utcnow` to return `MOCK_CURRENT_TIME`
(or its datetime equivalent).
This ensures that token generation (for custom tokens) and token
verification within `test_tenant_mgt.py` align with the mocked
timeline, preventing potential flakiness or failures due to
time inconsistencies. All tests in `test_tenant_mgt.py` pass
with these changes.
* fix lint and refactor
---------
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>1 parent 70013c8 commit 2d9b18c
2 files changed
+92
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| |||
964 | 966 | | |
965 | 967 | | |
966 | 968 | | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
967 | 980 | | |
968 | 981 | | |
969 | 982 | | |
| |||
997 | 1010 | | |
998 | 1011 | | |
999 | 1012 | | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
1000 | 1024 | | |
1001 | 1025 | | |
1002 | 1026 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| |||
105 | 109 | | |
106 | 110 | | |
107 | 111 | | |
108 | | - | |
| 112 | + | |
109 | 113 | | |
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
| 117 | + | |
113 | 118 | | |
114 | 119 | | |
115 | 120 | | |
116 | | - | |
117 | | - | |
| 121 | + | |
| 122 | + | |
118 | 123 | | |
119 | 124 | | |
120 | 125 | | |
| |||
127 | 132 | | |
128 | 133 | | |
129 | 134 | | |
130 | | - | |
| 135 | + | |
| 136 | + | |
131 | 137 | | |
132 | 138 | | |
133 | 139 | | |
134 | 140 | | |
135 | | - | |
| 141 | + | |
136 | 142 | | |
137 | 143 | | |
138 | 144 | | |
| |||
205 | 211 | | |
206 | 212 | | |
207 | 213 | | |
208 | | - | |
| 214 | + | |
209 | 215 | | |
210 | 216 | | |
211 | 217 | | |
| |||
218 | 224 | | |
219 | 225 | | |
220 | 226 | | |
221 | | - | |
| 227 | + | |
222 | 228 | | |
223 | 229 | | |
224 | 230 | | |
| |||
420 | 426 | | |
421 | 427 | | |
422 | 428 | | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
423 | 440 | | |
424 | 441 | | |
425 | 442 | | |
| |||
435 | 452 | | |
436 | 453 | | |
437 | 454 | | |
438 | | - | |
| 455 | + | |
439 | 456 | | |
440 | | - | |
441 | | - | |
| 457 | + | |
| 458 | + | |
442 | 459 | | |
443 | 460 | | |
444 | | - | |
445 | | - | |
| 461 | + | |
| 462 | + | |
446 | 463 | | |
447 | 464 | | |
448 | 465 | | |
| |||
618 | 635 | | |
619 | 636 | | |
620 | 637 | | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
621 | 649 | | |
622 | 650 | | |
623 | 651 | | |
| |||
633 | 661 | | |
634 | 662 | | |
635 | 663 | | |
636 | | - | |
| 664 | + | |
637 | 665 | | |
638 | | - | |
639 | | - | |
| 666 | + | |
| 667 | + | |
640 | 668 | | |
641 | 669 | | |
642 | | - | |
643 | | - | |
| 670 | + | |
| 671 | + | |
644 | 672 | | |
645 | 673 | | |
646 | 674 | | |
| |||
792 | 820 | | |
793 | 821 | | |
794 | 822 | | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
795 | 834 | | |
796 | 835 | | |
797 | 836 | | |
| |||
810 | 849 | | |
811 | 850 | | |
812 | 851 | | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
813 | 864 | | |
814 | 865 | | |
815 | 866 | | |
| |||
852 | 903 | | |
853 | 904 | | |
854 | 905 | | |
855 | | - | |
856 | | - | |
857 | | - | |
| |||
0 commit comments