Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
994d6137
Kaydet (Commit)
994d6137
authored
Kas 10, 2014
tarafından
Thomas Chaumeny
Kaydeden (comit)
Tim Graham
Ara 22, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #23792 -- Added test.utils.freeze_time() context manager.
üst
54085b0f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
21 deletions
+25
-21
utils.py
django/test/utils.py
+17
-0
tests.py
tests/signed_cookies_tests/tests.py
+3
-8
tests.py
tests/signing/tests.py
+5
-13
No files found.
django/test/utils.py
Dosyayı görüntüle @
994d6137
...
...
@@ -555,3 +555,20 @@ def reset_warning_registry():
for
mod
in
sys
.
modules
.
values
():
if
hasattr
(
mod
,
key
):
getattr
(
mod
,
key
)
.
clear
()
@contextmanager
def
freeze_time
(
t
):
"""
Context manager to temporarily freeze time.time(). This temporarily
modifies the time function of the time module. Modules which import the
time function directly (e.g. `from time import time`) won't be affected
This isn't meant as a public API, but helps reduce some repetitive code in
Django's test suite.
"""
_real_time
=
time
.
time
time
.
time
=
lambda
:
t
try
:
yield
finally
:
time
.
time
=
_real_time
tests/signed_cookies_tests/tests.py
Dosyayı görüntüle @
994d6137
from
__future__
import
unicode_literals
import
time
from
django.core
import
signing
from
django.http
import
HttpRequest
,
HttpResponse
from
django.test
import
TestCase
,
override_settings
from
django.test.utils
import
freeze_time
class
SignedCookieTest
(
TestCase
):
...
...
@@ -46,22 +45,18 @@ class SignedCookieTest(TestCase):
def
test_max_age_argument
(
self
):
value
=
'hello'
_time
=
time
.
time
time
.
time
=
lambda
:
123456789
try
:
with
freeze_time
(
123456789
):
response
=
HttpResponse
()
response
.
set_signed_cookie
(
'c'
,
value
)
request
=
HttpRequest
()
request
.
COOKIES
[
'c'
]
=
response
.
cookies
[
'c'
]
.
value
self
.
assertEqual
(
request
.
get_signed_cookie
(
'c'
),
value
)
time
.
time
=
lambda
:
123456800
with
freeze_time
(
123456800
):
self
.
assertEqual
(
request
.
get_signed_cookie
(
'c'
,
max_age
=
12
),
value
)
self
.
assertEqual
(
request
.
get_signed_cookie
(
'c'
,
max_age
=
11
),
value
)
self
.
assertRaises
(
signing
.
SignatureExpired
,
request
.
get_signed_cookie
,
'c'
,
max_age
=
10
)
finally
:
time
.
time
=
_time
@override_settings
(
SECRET_KEY
=
b
'
\xe7
'
)
def
test_signed_cookies_with_binary_key
(
self
):
...
...
tests/signing/tests.py
Dosyayı görüntüle @
994d6137
from
__future__
import
unicode_literals
import
datetime
import
time
from
django.core
import
signing
from
django.test
import
TestCase
from
django.test.utils
import
freeze_time
from
django.utils.encoding
import
force_str
from
django.utils
import
six
...
...
@@ -117,23 +117,15 @@ class TestTimestampSigner(TestCase):
def
test_timestamp_signer
(
self
):
value
=
'hello'
_time
=
time
.
time
time
.
time
=
lambda
:
123456789
try
:
with
freeze_time
(
123456789
):
signer
=
signing
.
TimestampSigner
(
'predictable-key'
)
ts
=
signer
.
sign
(
value
)
self
.
assertNotEqual
(
ts
,
signing
.
Signer
(
'predictable-key'
)
.
sign
(
value
))
self
.
assertEqual
(
signer
.
unsign
(
ts
),
value
)
time
.
time
=
lambda
:
123456800
self
.
assertEqual
(
signer
.
unsign
(
ts
,
max_age
=
13
),
value
)
with
freeze_time
(
123456800
):
self
.
assertEqual
(
signer
.
unsign
(
ts
,
max_age
=
12
),
value
)
# max_age parameter can also accept a datetime.timedelta object
self
.
assertEqual
(
signer
.
unsign
(
ts
,
max_age
=
datetime
.
timedelta
(
seconds
=
11
)),
value
)
self
.
assertRaises
(
signing
.
SignatureExpired
,
signer
.
unsign
,
ts
,
max_age
=
10
)
with
self
.
assertRaises
(
signing
.
SignatureExpired
):
self
.
assertEqual
(
signer
.
unsign
(
ts
,
max_age
=
datetime
.
timedelta
(
seconds
=
10
)),
value
)
finally
:
time
.
time
=
_time
self
.
assertRaises
(
signing
.
SignatureExpired
,
signer
.
unsign
,
ts
,
max_age
=
10
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment