def test_system_version(self): """ Check that the API version string is returned correctly """ client = trovebox.Trovebox(config_file=self.config_file) version = client.system.version() self.assertEqual(version["api"], "v%s" % trovebox.LATEST_API_VERSION)
def setUpClass(cls): """ Ensure there is nothing on the server before running any tests """ if cls.debug: if cls.api_version is None: print("\nTesting Latest %s" % cls.testcase_name) else: print("\nTesting %s v%d" % (cls.testcase_name, cls.api_version)) cls.client = trovebox.Trovebox(config_file=cls.config_file) cls.client.configure(api_version=cls.api_version) if cls.client.photos.list() != []: raise ValueError("The test server (%s) contains photos. " "Please delete them before running the tests" % cls.client.host) if cls.client.tags.list() != []: raise ValueError("The test server (%s) contains tags. " "Please delete them before running the tests" % cls.client.host) if cls.client.albums.list() != []: raise ValueError("The test server (%s) contains albums. " "Please delete them before running the tests" % cls.client.host)
def test_get_without_oauth(self): """Check that the get method works without OAuth parameters""" self.client = trovebox.Trovebox(host=self.test_host) self._register_uri(httpretty.GET) response = self.client.get(self.test_endpoint) self.assertNotIn("authorization", self._last_request().headers) self.assertEqual(response, self.test_data)
def test_system_diagnostics(self): """ Check that the system diagnostics can be performed """ client = trovebox.Trovebox(config_file=self.config_file) diagnostics = client.system.diagnostics() self.assertIn(diagnostics, "database")
def test_api_version(self, method): """Check that an API version can be specified""" self.client = trovebox.Trovebox(host=self.test_host, **self.test_oauth) self.client.configure(api_version=1) self._register_uri(method, uri="http://%s/v1/%s" % (self.test_host, self.test_endpoint)) GetOrPost(self.client, method).call(self.test_endpoint)
def setUp(self): self.client = trovebox.Trovebox(host=self.test_host) self.test_tags = [ trovebox.objects.tag.Tag(self.client, tag) for tag in self.test_tags_dict ] self.test_tag_unicode = trovebox.objects.tag.Tag( self.client, self.test_tag_unicode_dict)
def test_unspecified_api_version(self): """ If the API version is unspecified, we get a generic hello world message. """ client = trovebox.Trovebox(config_file=self.config_file) result = client.get("hello.json") self.assertEqual(result['message'], "Hello, world!") self.assertEqual(result['result']['__route__'], "/hello.json")
def test_future_api_version(self): """ If the API version is unsupported, we should get an error (ValueError, since the returned 404 HTML page is not valid JSON) """ version = trovebox.LATEST_API_VERSION + 1 client = trovebox.Trovebox(config_file=self.config_file) client.configure(api_version=version) with self.assertRaises(trovebox.Trovebox404Error): client.get("hello.json")
def setUp(self): self.client = trovebox.Trovebox(host=self.test_host) self.test_photos = [ trovebox.objects.photo.Photo(self.client, photo) for photo in self.test_photos_dict ] self.test_albums = [ trovebox.objects.album.Album(self.client, album) for album in self.test_albums_dict ]
def setUp(self): self.client = trovebox.Trovebox(host=self.test_host) self.test_photos = [ trovebox.objects.photo.Photo(self.client, photo) for photo in self.test_photos_dict ] self.test_activities = [ trovebox.objects.activity.Activity(self.client, activity) for activity in self.test_activities_dict ]
def test_api_version_zero(self): """ API v0 has a special hello world message """ client = trovebox.Trovebox(config_file=self.config_file) client.configure(api_version=0) result = client.get("hello.json") self.assertEqual(result['message'], "Hello, world! This is version zero of the API!") self.assertEqual(result['result']['__route__'], "/v0/hello.json")
def test_specified_api_version(self): """ For all API versions >0, we get a generic hello world message """ for api_version in range(1, test_base.get_test_server_api() + 1): client = trovebox.Trovebox(config_file=self.config_file) client.configure(api_version=api_version) result = client.get("hello.json") self.assertEqual(result['message'], "Hello, world!") self.assertEqual(result['result']['__route__'], "/v%d/hello.json" % api_version)
def test_ssl_verify_disabled(self, method, mock_session): """Check that SSL verification can be disabled for the get method""" session = mock_session.return_value.__enter__.return_value session.get.return_value.text = "response text" session.get.return_value.status_code = 200 session.get.return_value.json.return_value = self.test_data # Handle either post or get session.post = session.get self.client = trovebox.Trovebox(host=self.test_host, **self.test_oauth) self.client.configure(ssl_verify=False) GetOrPost(self.client, method).call(self.test_endpoint) self.assertEqual(session.verify, False)
def test_no_scheme(self, method): """Check that we can access hosts without a 'http://' prefix""" self._register_uri(method, uri="http://test.example.com/%s" % self.test_endpoint) self.client = trovebox.Trovebox(host="test.example.com", **self.test_oauth) response = GetOrPost(self.client, method).call(self.test_endpoint) self.assertIn("OAuth", self._last_request().headers["authorization"]) self.assertEqual(response, self.test_data) self.assertEqual(self.client.last_url, "http://test.example.com/%s" % self.test_endpoint) self.assertEqual(self.client.last_response.json(), self.test_data)
def test_endpoint_leading_slash(self, method): """Check that an endpoint with a leading slash is constructed correctly""" self._register_uri(method, uri="http://test.example.com/%s" % self.test_endpoint) self.client = trovebox.Trovebox(host="http://test.example.com", **self.test_oauth) response = GetOrPost(self.client, method).call("/" + self.test_endpoint) self.assertIn("OAuth", self._last_request().headers["authorization"]) self.assertEqual(response, self.test_data) self.assertEqual(self.client.last_url, "http://test.example.com/%s" % self.test_endpoint) self.assertEqual(self.client.last_response.json(), self.test_data)
def setUp(self): self.client = trovebox.Trovebox(host=self.test_host) self.test_photos = [trovebox.objects.photo.Photo(self.client, photo) for photo in self.test_photos_dict]
def main(): import argparse import getpass parser = argparse.ArgumentParser( description='Transfer photos from Trovebox to PicasaWeb (Google+)') parser.add_argument('--config', help="Trovebox configuration file to use") parser.add_argument( '--host', help="Hostname of the Trovebox server (overrides config_file)") parser.add_argument('--consumer-key') parser.add_argument('--consumer-secret') parser.add_argument('--token') parser.add_argument('--token-secret') parser.add_argument('--public-albums', action="store_true", help="Make newly created albums public") parser.add_argument( '--dry-run', action="store_true", help="Create albums, but don't actually transfer any photos") config = parser.parse_args() # Host option overrides config file settings if config.host: trovebox_client = trovebox.Trovebox( host=config.host, consumer_key=config.consumer_key, consumer_secret=config.consumer_secret, token=config.token, token_secret=config.token_secret) else: try: trovebox_client = trovebox.Trovebox(config_file=config.config) except IOError as error: print error print print "You must create a configuration file in ~/.config/trovebox/default" print "with the following contents:" print " host = your.host.com" print " consumerKey = your_consumer_key" print " consumerSecret = your_consumer_secret" print " token = your_access_token" print " tokenSecret = your_access_token_secret" print print "To get your credentials:" print " * Log into your Trovebox site" print " * Click the arrow on the top-right and select 'Settings'." print " * Click the 'Create a new app' button." print " * Click the 'View' link beside the newly created app." print print error sys.exit(1) print "Log in to your Google account:" gd_client = gdata.photos.service.PhotosService() gd_client.email = raw_input("Email address : ") gd_client.password = getpass.getpass("Password : ") gd_client.source = 'Trovebox-trovebox_to_picasaweb' gd_client.ProgrammaticLogin() try: TroveboxToPicasaweb(trovebox_client, gd_client, config.public_albums, config.dry_run).run() except KeyboardInterrupt: pass
def __init__(self, *args, **kwds): self._trovebox = trovebox.Trovebox() self._photo_count = None Provider.__init__(self, *args, **kwds)
def main(args=sys.argv[1:]): """Run the commandline script""" usage = "%prog --help" parser = OptionParser(usage, add_help_option=False) parser.add_option('-c', '--config', help="Configuration file to use", action='store', type='string', dest='config_file') parser.add_option('-h', '-H', '--host', help=("Hostname of the Trovebox server " "(overrides config_file)"), action='store', type='string', dest='host') parser.add_option('-X', help="Method to use (GET or POST)", action='store', type='choice', dest='method', choices=('GET', 'POST'), default="GET") parser.add_option('-F', help="Endpoint field", action='append', type='string', dest='fields') parser.add_option('-e', help="Endpoint to call", action='store', type='string', dest='endpoint', default='/photos/list.json') parser.add_option('-p', help="Pretty print the json", action="store_true", dest="pretty", default=False) parser.add_option('-v', help="Verbose output", action="store_true", dest="verbose", default=False) parser.add_option('--version', help="Display the current version", action="store_true") parser.add_option('--help', help='show this help message', action="store_true") options, args = parser.parse_args(args) if options.help: parser.print_help() return if options.version: print(trovebox.__version__) return if args: parser.error("Unknown argument: %s" % args) params = {} if options.fields: for field in options.fields: (key, value) = field.split('=') params[key] = value # Host option overrides config file settings if options.host: client = trovebox.Trovebox(host=options.host) else: try: client = trovebox.Trovebox(config_file=options.config_file) except IOError as error: print(error) print(CONFIG_ERROR) print(error) sys.exit(1) if options.method == "GET": result = client.get(options.endpoint, process_response=False, **params) else: params, files = extract_files(params) result = client.post(options.endpoint, process_response=False, files=files, **params) for file_ in files: files[file_].close() if options.verbose: print("==========\nMethod: %s\nHost: %s\nEndpoint: %s" % (options.method, client.host, options.endpoint)) if params: print("Fields:") for key, value in params.items(): print(" %s=%s" % (key, value)) print("==========\n") if options.pretty: print( json.dumps(json.loads(result), sort_keys=True, indent=4, separators=(',', ':'))) else: print(result)
def test_post_without_oauth(self): """Check that the post method fails without OAuth parameters""" self.client = trovebox.Trovebox(host=self.test_host) self._register_uri(httpretty.POST) with self.assertRaises(trovebox.TroveboxError): self.client.post(self.test_endpoint)
def setUp(self): self.client = trovebox.Trovebox(host=self.test_host) self.test_photos = [trovebox.objects.photo.Photo(self.client, photo) for photo in self.test_photos_dict] self.test_actions = [trovebox.objects.action.Action(self.client, action) for action in self.test_actions_dict]
def setUp(self): self.client = trovebox.Trovebox(host=self.test_host, **self.test_oauth)