Skip to content

Commit

Permalink
Unbreak unit tests of url_for.
Browse files Browse the repository at this point in the history
  • Loading branch information
paxan committed Oct 2, 2009
1 parent 17e8d33 commit f6ff987
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 8 additions & 1 deletion simples3.py
Expand Up @@ -537,6 +537,13 @@ def listdir(self, prefix=None, marker=None, limit=None, delimiter=None):
if not data:
break

@staticmethod
def _now():
"""
Wraps datetime.now() for testability.
"""
return datetime.datetime.now()

def url_for(self, key, authenticated=False,
expire=datetime.timedelta(minutes=5)):
"""Produce the URL for given S3 object key.
Expand Down Expand Up @@ -568,7 +575,7 @@ def url_for(self, key, authenticated=False,
expire = datetime.datetime.fromtimestamp(expire)
else:
# Assume timedelta.
expire = datetime.datetime.now() + expire
expire = self._now() + expire
expire_desc = str(long(time.mktime(expire.timetuple())))
auth_descriptor = "".join((
"GET\n",
Expand Down
14 changes: 8 additions & 6 deletions tests.py
Expand Up @@ -3,6 +3,7 @@
import time
import unittest
import simples3
import datetime

class S3BucketTests(unittest.TestCase):
def setUp(self):
Expand All @@ -26,19 +27,20 @@ def test_url_for_with_auth(self):
"Signature=rucSbH0yNEcP9oM2XNlouVI3BH4%3D")
self.assertEquals(x,
self.bucket.url_for('photos/puppy.jpg', authenticated=True,
expires=1175139620))
expire=1175139620))

def test_url_for_with_auth_default_expires(self):
def test_url_for_with_auth_default_expire(self):
# Poor man's dynamic scoping is used to
# stub out time.time() function.
_real_time_func = time.time
time.time = lambda: 1239800000.01234
# stub out S3Bucket._now() method.
t0 = 1239800000.01234
_orig_func = self.bucket._now
self.bucket._now = lambda: datetime.datetime.fromtimestamp(t0)
try:
# Note: expected expiration value is 300 seconds (5 min) greater.
self.failUnless('Expires=1239800300' in
self.bucket.url_for('file.txt', authenticated=True))
finally:
time.time = _real_time_func
self.bucket._now = _orig_func

if __name__ == "__main__":
import doctest
Expand Down

0 comments on commit f6ff987

Please sign in to comment.