def test_get_from_file(self): token = 'xyz' # Store in one instance cache = flickrapi.TokenCache(self.api_key) cache.token = token # Read from another instance cache = flickrapi.TokenCache(self.api_key) self.assertEquals(token, cache.token)
def test_create_dir(self): '''Tests that the token cache can automatically create its storage directory. ''' token_path = self.target_path() tokendir = os.path.dirname(token_path) # Move token dir to a temporary dir tempdir = None try: if os.path.exists(tokendir): tempdir = '%s-DO-NOT-EXIST' % tokendir if os.path.exists(tempdir): raise Exception("Tempdir %s exists, please remove" % tempdir) os.rename(tokendir, tempdir) self.assertFalse(os.path.exists(tokendir)) cache = flickrapi.TokenCache(self.api_key) cache.token = 'x' self.assertTrue(os.path.exists(tokendir)) os.unlink(os.path.join(tokendir, 'auth.token')) os.rmdir(tokendir) finally: if tempdir: os.rename(tempdir, tokendir)
def test_set_get(self): token = 'xyz' cache = flickrapi.TokenCache(self.api_key) cache.token = token self.assertTrue(os.path.exists(self.target_path())) contents = open(self.target_path()).read() self.assertEquals(token, contents.strip()) self.assertEquals(token, cache.token)
def test_store_token(self): '''Tests that store_token=False FlickrAPI uses SimpleTokenCache''' token_disk = '123-abc-disk' token_mem = '123-abc-mem' # Create a non-public-only instance, and set the on-disk token flickr = flickrapi.FlickrAPI(key, secret) flickr.token_cache.token = token_disk flickr = flickrapi.FlickrAPI(key, secret, store_token=False) # The token shouldn't be set self.assertEqual(None, flickr.token_cache.token) # Now set it flickr.token_cache.token = token_mem # It should not be in the on-disk token cache, only in memory self.assertEqual(token_disk, flickrapi.TokenCache(key).token) self.assertNotEqual(token_mem, flickrapi.TokenCache(key).token)
def test_multi_user(self): token = 'xyz' username = u'Sybren Stüvel' # Cache the auth token cache = flickrapi.TokenCache(self.api_key, username) cache.token = token # Ensure the token is stored in the right place self.assertTrue(os.path.exists(self.target_path(username))) # And that it contains the proper stuff contents = open(self.target_path(username)).read() self.assertEquals(token, contents.strip()) self.assertEquals(token, cache.token) # Ensure it can't be found by using another user cache = flickrapi.TokenCache(self.api_key, username + u'blah') self.assertEquals(None, cache.token) self.remove_token(username)
def test_token_constructor(self): '''Test passing a token to the constructor''' token = '123-abc-def' # Pass the token flickr = flickrapi.FlickrAPI(key, secret, token=token) # It should be in the in-memory token cache now self.assertEqual(token, flickr.token_cache.token) # But not in the on-disk token cache self.assertNotEqual(token, flickrapi.TokenCache(key))
def test_alternate_dir(self): '''Tests that the token cache has a configurable storage directory. ''' cache = flickrapi.TokenCache(self.api_key) cache.path = '/tmp/flicktokens' cache.token = 'x' tokendir = os.path.join(cache.path, self.api_key) tokenfile = os.path.join(tokendir, 'auth.token') self.assertTrue(os.path.exists(tokenfile)) # Clean up os.unlink(tokenfile) os.rmdir(tokendir) os.rmdir(cache.path)
def test_remove(self): token = 'xyz' # Make sure the token doesn't exist yet before we start self.assertFalse(os.path.exists(self.target_path())) cache = flickrapi.TokenCache(self.api_key) # Make sure we can forget a token that doesn't exist cache.forget() self.assertFalse(os.path.exists(self.target_path())) self.assertEquals(None, cache.token) # Make sure remembering the token works cache.token = token self.assertTrue(os.path.exists(self.target_path())) # Make sure forgetting really works cache.forget() self.assertFalse(os.path.exists(self.target_path())) self.assertEquals(None, cache.token)