def testbed(): tb = Testbed() tb.activate() tb.init_datastore_v3_stub() tb.init_memcache_stub() yield tb tb.deactivate()
def test_save(self): testbed = Testbed() testbed.activate() testbed.init_datastore_v3_stub() testbed.init_memcache_stub() s = mommy.save_one(Stub) self.assertIsNotNone(s.key) testbed.deactivate()
def testbed(): from google.appengine.ext.testbed import Testbed # create an instance of testbed class testbed = Testbed() # activate the testbed, which prepares the services stub for use testbed.activate() # declare which stubs want to use testbed.init_datastore_v3_stub() testbed.init_memcache_stub() return testbed
def setUp(self): tb = Testbed() tb.activate() tb.init_datastore_v3_stub() tb.init_memcache_stub() tb.init_user_stub() self.testbed = tb self.orig_http = set_http_mock() self.orig_user = set_user_mock() self.current_user = users.get_current_user() self.secret = 'xsrfsecret123' self.cache = CredentialsStore() self.clear_cache()
def testbed(): from google.appengine.ext.testbed import Testbed # create an instance of testbed class testbed = Testbed() # activate the testbed, which prepares the services stub for use testbed.activate() testbed.setup_env(app_id='debug', overwrite=True) # declare which stubs want to use testbed.init_search_stub() testbed.init_urlfetch_stub() return testbed
class _MyTest(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) try: import webtest except ImportError: import setuptools.command.easy_install as easy_install easy_install.main(["WebTest"]) exit() wsgi_application = webapp.WSGIApplication(paths) self.test_app = webtest.TestApp(wsgi_application) from google.appengine.ext.testbed import Testbed self.testbed = Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() def tearDown(self): self.testbed.deactivate() unittest.TestCase.tearDown(self) def test_Path(self): response = self.test_app.get("/_Path") self.assertEqual(response.status, "200 OK") self.assertEqual(response.headers["Content-Type"], 'text/html; charset=utf-8') self.assertEqual(response.body, '/_Path') def test_Params(self): response = self.test_app.request(b"/_Params;ppp?a=b&c=d#e", method=b"GET") self.assertEqual(response.headers["a"], "abc") self.assertEqual(response.status, "200 OK") self.assertEqual(response.headers["Content-Type"], 'text/html; charset=utf-8') self.assertEqual(response.body, "") def test_Fragment(self): response = self.test_app.request(b"/_Fragment?a=b&c=d#e", method=b"GET") self.assertEqual(response.status, "200 OK") self.assertEqual(response.headers["Content-Type"], 'text/html; charset=utf-8') self.assertEqual(response.body, "e") def test_Query(self): response = self.test_app.request(b"/_Query?a=b&c=d#e", method=b"GET") self.assertEqual(response.status, "200 OK") self.assertEqual(response.headers["Content-Type"], 'text/html; charset=utf-8') self.assertEqual(response.body, "a=b&c=d")
class BasicTestCase(unittest.TestCase): def setUp(self): # First, create an instance of the Testbed class. self.testbed = Testbed() # Then activate the testbed, which prepares the service stubs for use. self.testbed.activate() # Next, declare which service stubs you want to use. #self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.testapp = webtest.TestApp(application()) self.testapi = service def tearDown(self): self.testbed.deactivate()
class TestBase(TestCase): def setUp(self): from google.appengine.ext.testbed import Testbed self.testbed = Testbed() self.testbed.activate() # default setup with no param # you may re-setup in a test code by using this. self.setUpPyramid() def setUpPyramid(self, **kwargs): import netprintbox self.config = testing.setUp(**kwargs) self.config.include('netprintbox') def tearDown(self): self.testbed.deactivate() testing.tearDown()
def testbed(): testbed = Testbed() testbed.activate() # testbed.setup_env(app_id='_') os.environ['APPLICATION_ID'] = '_' # This is a hack to get things working; `testbed.setup_env` does # not seem to be doing the job. # See: http://einaregilsson.com/unit-testing-model-classes-in-google-app-engine/ # Will almost always need datastore for tests that use this fixture. testbed.init_datastore_v3_stub() # ndb uses memcache, so stub it as well. testbed.init_memcache_stub() # Clear in-context cache before test. ndb.get_context().clear_cache() yield testbed ndb.get_context().clear_cache() testbed.deactivate()
class _MyTest(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) try: import webtest except ImportError: import setuptools.command.easy_install as easy_install easy_install.main(["WebTest"]) exit() wsgi_application = webapp.WSGIApplication(post.paths) self.test_app = webtest.TestApp(wsgi_application) from google.appengine.ext.testbed import Testbed self.testbed = Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() def tearDown(self): self.testbed.deactivate() unittest.TestCase.tearDown(self) def testGet(self): response = self.test_app.get("/post") import webtest self.assertIsInstance(response, webtest.TestResponse) self.assertEqual(response.status, "200 OK") self.assertEqual(response.headers["Content-Type"], 'text/plain') def testGet2(self): TEST_QUERY = "a=b&c=d&e=f" response = self.test_app.get("/post?" + TEST_QUERY) import webtest self.assertIsInstance(response, webtest.TestResponse) self.assertEqual(response.status, "200 OK") self.assertEqual(response.headers["Content-Type"], 'text/plain') print(response.body)
def testbed(): testbed = Testbed() testbed.activate() # testbed.setup_env(app_id='_') os.environ['APPLICATION_ID'] = '_' # this is a hack to get things working; `testbed.setup_env` does # not seem to be doing the job # see: # http://einaregilsson.com/unit-testing-model-classes-in-google-app-engine/ # will almost always need datastore for tests that use this fixture testbed.init_datastore_v3_stub() # ndb uses memcache, so stub it as well testbed.init_memcache_stub() # clear in-context memcache before test ndb.get_context().clear_cache() yield testbed ndb.get_context().clear_cache() testbed.deactivate()
class BaseAppengineDatastoreTester(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self._testbed = Testbed() self._testbed.activate() self._testbed.init_datastore_v3_stub() self._testbed.init_memcache_stub() def __init__(self, methodName = None): """ Constructor override to allow creation of an instance to be used outside the test run process Useful for defining helper methods reusable in other test classes """ if methodName != None: unittest.TestCase.__init__(self, methodName) else: # Map types to custom assertEqual functions that will compare # instances of said type in more detail to generate a more useful # error message. # Copied from unittest.TestCase self._type_equality_funcs = {} self.addTypeEqualityFunc(dict, self.assertDictEqual) self.addTypeEqualityFunc(list, self.assertListEqual) self.addTypeEqualityFunc(tuple, self.assertTupleEqual) self.addTypeEqualityFunc(set, self.assertSetEqual) self.addTypeEqualityFunc(frozenset, self.assertSetEqual) self.addTypeEqualityFunc(unicode, self.assertMultiLineEqual) def verify_entity_instance(self, instance = None, type = None): """ verify that instance is not null, of the specified type, and having a not null key """ self.assertIsNotNone(instance) self.assertIsInstance(instance, type) self.assertIsInstance(instance.key(), db.Key)
class SimpleTest(unittest.TestCase): """ """ def setUp(self): """ """ self.testbed = Testbed() self.testbed.setup_env(app_id='dev~foobar') self.testbed.activate() self.testbed.init_memcache_stub() self.testbed.init_datastore_v3_stub(datastore_file='/tmp/appengine.foobar.kgreen/datastore.db', use_sqlite=True) def tearDown(self): self.testbed.deactivate() def test_index(self): model = DataModel(name='users', value=[PropertyModel(name='first', value='John'), PropertyModel(name='last', value='Dough')]) model.put()
class _TestRawDataNdb(TestCase): TEST_RAWDATA_ID = 12345 TEST_BODY = "bodybody" TEST_QUERY = "a=b&c=d" TEST_FRAGMENT = "fragmentfragment" TEST_PATH = "/a/b/c" TEST_PARAMETERS = "" def setUp(self): TestCase.setUp(self) from google.appengine.ext.testbed import Testbed self.testbed = Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() def tearDown(self): self.testbed.deactivate() TestCase.tearDown(self) def testGetLast(self): key = RawData.fetchByRawDataId(self.TEST_RAWDATA_ID) if key is not None: key.delete() key = RawData.fetchByRawDataId(self.TEST_RAWDATA_ID) self.assertTrue(key is None) raw_data = RawData() raw_data.rawDataId = self.TEST_RAWDATA_ID raw_data.path = self.TEST_PATH raw_data.parameters = self.TEST_PARAMETERS raw_data.query = self.TEST_QUERY raw_data.fragment = self.TEST_FRAGMENT raw_data.body = self.TEST_BODY raw_data.put() key = RawData.fetchByRawDataId(self.TEST_RAWDATA_ID) self.assertTrue(key is not None)
class TestCase(unittest.TestCase): """Utility methods for testing. This class should be used as the superclass of all other test classes. It sets up the necessary test stubs and provides various utility methods to use in tests. """ def setUp(self): """Initialize stubs for testing. This initializes the following fields: self.testbed: An App Engine Testbed used for mocking App Engine services. This is activated, and any necessary stubs are initialized. self.testapp: A webtest.TestApp wrapping the CherryPy application. Subclasses that define their own setUp method should be sure to call this one as well. """ self.__users = {} self.__admins = {} self.testbed = Testbed() self.testbed.activate() self.testbed.init_app_identity_stub() self.testbed.init_blobstore_stub() self.testbed.init_datastore_v3_stub() self.testbed.init_files_stub() self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub() self.testbed.init_user_stub() self.testapp = webtest.TestApp(Application()) # Pretend we are running on AppEngine so that things like the error # page that behave differently when run locally act like they do in # production. os.environ['SERVER_SOFTWARE'] = 'Google App Engine/TESTING' # This private key is not actually used for anything other than testing. PrivateKey.set('''-----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,CEB8C6541017AC8B q1SgqAfgHXK5cQvtdIF8rkSlbAS6a92T5tYMVKJIW24giF5cw+1++X7X6S5ECykC /iECExP7WfVlPDVoJMZpWGsYLMPhxncnKfUDICbVgwsO4KKeEv8lYTrvkY1sCZIx kSja/lGAWpyxBnmxwoLh0LbLJBGUxUgn2Uqv/8Iid+w0m3NlgrllV+kOo4gUYF9q bestH4nEQj6F0CeI8VPW0FxzMwn0vreHFBT5hul6xbNdB1vnRcz6ed4FiGXoAB5K /8/Q2qyy1UPe2Hr9IoC6wo4h2kXq7pmhy/rtzU9/gjsGPD33ByavbgHAen0ajY5p RmCx0AGidK9T6/SNoyDD9CMq7ypL+0JWoCeVoAEw2aZqN4rMJNbKMgPH+ajgiAXe AWuMVjWN6uTL5+QJmWQct6a6TF8GPKdTcOZfNIXU5U9drHB2hggLcy6XkCYGjVJv MvLascE4oAtOHnwz7WhrpcmX6F6Fww+TPnFzjk+IGJrnBUnAArEcM13IjiDUgg9B iLuKimwrqasdBBY3Ua3SRMoG8PJoNKdsk1hDGlpxLnqOVUCvPKZuz4QnFusnUJrR kiv+aqVBpZYymMh2Q1MWcogA7rL7LIowAkyLzS8dNwDhyk9jbO+1uoFSHKr5BTNv cMJfCVm8pqhXwCVx3uYnhUzvth7mcEseXh5Dcg1RHka5rCXUz4eVxTkj1u3FOy9o 9jgARPpnDYsXH1lESxmiNZucHa50qI/ezNvQx8CbNa1/+JWoZ77yqM9qnDlXUwDY 1Sxqx9+4kthG9oyCzzUwFvhf1kTEHd0RfIapgjJ16HBQmzLnEPtjPA== -----END RSA PRIVATE KEY-----''') self.dont_be_oauth_user() def tearDown(self): """Deactivates the stubs initialized in setUp. Subclasses that define their own tearDown method should be sure to call this one as well. """ self.testbed.deactivate() def normal_user(self, name = None): """Constructs a non-admin user object. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. Returns: The user object. """ if self.__users.has_key(name): return self.__users[name] email = '*****@*****.**' if name: email = '*****@*****.**' % name self.__users[name] = users.User(email = email) return self.__users[name] def admin_user(self, name = None): """Constructs an admin user object. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. Returns: The user object. """ if self.__admins.has_key(name): return self.__admins[name] email = '*****@*****.**' if name: email = '*****@*****.**' % name self.__admins[name] = users.User(email = email) return self.__admins[name] def be_normal_user(self, name = None): """Log in as a non-admin user. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. """ self.testbed.setup_env( user_email = self.normal_user(name).email(), overwrite = True) def be_admin_user(self, name = None): """Log in as an admin user. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. """ self.testbed.setup_env( user_email = self.admin_user(name).email(), user_is_admin = '1', overwrite = True) def dont_be_oauth_user(self): """Don't consider the user to be logged in via OAuth.""" self.testbed.setup_env( oauth_error_code=str(UserServiceError.OAUTH_INVALID_REQUEST), oauth_error_detail='', oauth_last_scope='https://www.googleapis.com/auth/userinfo.email') def be_normal_oauth_user(self, name=None): """Log in as a non-admin OAuth user. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. """ try: del os.environ['OAUTH_ERROR_CODE'] except KeyError: pass self.set_oauth_user(self.normal_user(name)) def be_admin_oauth_user(self, name=None): """Log in as an admin OAuth user. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. """ try: del os.environ['OAUTH_ERROR_CODE'] except KeyError: pass self.set_oauth_user(self.admin_user(name)) def set_oauth_user(self, user): """Set the user that is logged in via OAuth.""" self.testbed.get_stub(USER_SERVICE_NAME).SetOAuthUser( email=user.email(), user_id=user.user_id() or '0', is_admin=user.email().startswith('test-admin')) def create_package(self, name, version): """Create and save a package object with a version.""" Package.new(name=name, uploaders=[users.get_current_user()]).put() self.set_latest_version(name, version) def set_latest_version(self, package_name, version): """Set the latest version of the given package.""" package = Package.get_by_key_name(package_name) package.latest_version = self.package_version(package, version) package.latest_version.put() package.put() def package_version(self, package, version, **additional_pubspec_fields): """Create a package version object. This constructs the package archive based on the other information passed in. The archive contains only the pubspec. """ pubspec = Pubspec(name=package.name, version=version) pubspec.update(additional_pubspec_fields) return PackageVersion.new( version=version, package=package, pubspec=pubspec, uploader=package.uploaders[0]) def tar_package(self, pubspec, files={}): """Return a tarfile containing the given pubspec. The pubspec is dumped to YAML, then placed in a tarfile. The tarfile is then returned as a string. """ files['pubspec.yaml'] = yaml.dump(pubspec) files['README'] = "This is a README." return self.io_for_archive(files).getvalue() def io_for_archive(self, files): """Return a StringIO object containing a .tar.gz archive. Arugments: files: A dict of the names and contents of files in the archive. """ tarfile_io = StringIO() tar = tarfile.open(fileobj=tarfile_io, mode='w:gz') for name, contents in files.iteritems(): tarinfo = tarfile.TarInfo(name) io = StringIO(contents) tarinfo.size = len(contents) tar.addfile(tarinfo, io) tar.close() tarfile_io.seek(0) return tarfile_io def archive(self, files): """Creates a TarFile archive. Arugments: files: A dict of the names and contents of files in the archive. """ return tarfile.open(fileobj=self.io_for_archive(files)) def upload_archive(self, name, version, **additional_pubspec_fields): """Return a tuple representing a package archive upload.""" pubspec = {'name': name, 'version': version} pubspec.update(additional_pubspec_fields) contents = self.tar_package(pubspec) return ('file', '%s-%s.tar.gz' % (name, version), contents) def assert_requires_login(self, response): """Assert that the given response is requesting user authentication.""" self.assertEqual(response.status_int, 302) self.assertTrue( response.headers['Location'].startswith( 'https://www.google.com/accounts/Login?continue='), 'expected response to redirect to the Google login page') def assert_error_page(self, response): """Assert that the given response renders an HTTP error page. This only checks for HTTP errors; it will not detect flash error messages. Arguments: response: The webtest response object to check. """ error = self.html(response).find("p", "error-message") self.assertTrue(error is not None, "expected error page") def assert_json_error(self, response): """Assert that the given response is a JSON error.""" self.assertTrue("error" in json.loads(response.body)) def assert_json_success(self, response): """Assert that the given response is a JSON success.""" self.assertEqual(response.status_int, 200) self.assertTrue("success" in json.loads(response.body)) def assert_oauth_error(self, response): """Assert that the given response is an OAuth2 error.""" print(response.headers) self.assertTrue(response.headers['WWW-Authenticate'].startswith( 'Bearer error="')) def html(self, response): """Parse a webtest response with BeautifulSoup. WebTest includes built-in support for BeautifulSoup 3.x, but we want to be able to use the features in 4.x. Arguments: response: The webtest response object to parse. Returns: The BeautifulSoup parsed HTML. """ return BeautifulSoup(response.body) def assert_link(self, response, url): """Assert that the response contains a link to the given url. Arguments: response: The webtest response object to check. url: The link URL to look for. """ error_msg = "expected response body to contain a link to '%s'" % url self.assertTrue(self._link_exists(response, url), error_msg) def assert_no_link(self, response, url): """Assert that the response doesn't contain a link to the given url. Arguments: response: The webtest response object to check. url: The link URL to look for. """ error_msg = "expected response body not to contain a link to '%s'" % url self.assertFalse(self._link_exists(response, url), error_msg) def assert_list_in_html(self, url, selector, expected_items): """Assert that the selected elements' contents match the given list. There must be as many selected elements as strings in the list, and each element's text must contain the respective string in the list. Arguments: url: The URL to request. selector: The CSS selector to apply to the resulting HTML. expected_items: A list of strings to look for in the matched elements. """ response = self.testapp.get(url) for html in self.html(response).select(selector): if not expected_items: self.fail("more items were listed than expected: %s" % html) elif expected_items[0] in ''.join(html.strings): del expected_items[0] else: self.fail("expected '%s' in %s" % (expected_items[0], html)) self.assertEqual(expected_items, [], "Selector '%s' missing items: %s" % (selector, expected_items)) def run_deferred_tasks(self, queue='default'): """Run all tasks that have been deferred. Arguments: queue: The task queue in which the deferred tasks are queued. """ taskqueue_stub = self.testbed.get_stub(TASKQUEUE_SERVICE_NAME) for task in taskqueue_stub.GetTasks(queue): deferred.run(base64.b64decode(task['body'])) taskqueue_stub.FlushQueue(queue) def _link_exists(self, response, url): """Return whether or not the response contains a link to the given url. Arguments: response: The webtest response object to check. url: The link URL to look for. """ return any([link['href'] == url for link in self.html(response).find_all('a')])
import unittest import webapp2 from google.appengine.api import memcache from google.appengine.ext import ndb from google.appengine.ext.testbed import Testbed testbed = Testbed() testbed.activate() testbed.init_datastore_v3_stub() testbed.init_memcache_stub() ndb.get_context().clear_cache() import main class AppTest(unittest.TestCase): def setUp(self): ndb.get_context().clear_cache() def test_ok(self): request = webapp2.Request.blank('/ok') response = request.get_response(main.app) self.assertEqual(response.status_int, 200) self.assertEqual(response.body, 'ok')
class StubManager(object): def __init__(self): self.testbed = Testbed() self.active_stubs = None self.pre_test_stubs = None def setup_stubs(self, connection): if self.active_stubs is not None: return if not have_appserver: self.setup_local_stubs(connection) def activate_test_stubs(self): if self.active_stubs == 'test': return self.testbed.activate() self.pre_test_stubs = self.active_stubs self.active_stubs = 'test' self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub() self.testbed.init_urlfetch_stub() self.testbed.init_user_stub() self.testbed.init_xmpp_stub() self.testbed.init_channel_stub() def deactivate_test_stubs(self): if self.active_stubs == 'test': self.testbed.deactivate() self.active_stubs = self.pre_test_stubs def setup_local_stubs(self, connection): if self.active_stubs == 'local': return from .base import get_datastore_paths from google.appengine.tools import dev_appserver_main args = dev_appserver_main.DEFAULT_ARGS.copy() args.update(get_datastore_paths(connection.settings_dict)) args.update(connection.settings_dict.get('DEV_APPSERVER_OPTIONS', {})) log_level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARNING) from google.appengine.tools import dev_appserver dev_appserver.SetupStubs(appid, **args) logging.getLogger().setLevel(log_level) self.active_stubs = 'local' def setup_remote_stubs(self, connection): if self.active_stubs == 'remote': return if not connection.remote_api_path: from ..utils import appconfig for handler in appconfig.handlers: if handler.script == REMOTE_API_SCRIPT: connection.remote_api_path = handler.url.split('(', 1)[0] break server = '%s.%s' % (connection.remote_app_id, connection.domain) remote_url = 'https://%s%s' % (server, connection.remote_api_path) logging.info('Setting up remote_api for "%s" at %s' % (connection.remote_app_id, remote_url)) if not have_appserver: print('Connecting to remote_api handler.\n\n' 'IMPORTANT: Check your login method settings in the ' 'App Engine Dashboard if you have problems logging in. ' 'Login is only supported for Google Accounts.\n') from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApi(None, connection.remote_api_path, auth_func, servername=server, secure=connection.secure_remote_api, rpc_server_factory=rpc_server_factory) retry_delay = 1 while retry_delay <= 16: try: remote_api_stub.MaybeInvokeAuthentication() except HTTPError, e: if not have_appserver: print 'Retrying in %d seconds...' % retry_delay time.sleep(retry_delay) retry_delay *= 2 else: break else:
#!/usr/bin/env python # coding: utf-8 import os import logging from dev_appserver import fix_sys_path fix_sys_path() from google.appengine.ext.testbed import Testbed tb = Testbed() tb.activate() os.environ['APPLICATION_ID'] = 'rss-xmpp' tb.init_xmpp_stub() tb.init_datastore_v3_stub() tb.init_urlfetch_stub() from rss_xmpp.main import * from google.appengine.api import xmpp from rss_xmpp import feedcrawler import unittest url = 'http://feeds.feedburner.com/github' class XMPPTestMessage(xmpp.Message): def reply(self, body, message_type="chat", raw_xml=False, send_message=xmpp.send_message):
class StubManager(object): def __init__(self): self.testbed = Testbed() self.active_stubs = None self.pre_test_stubs = None def setup_stubs(self, connection): if self.active_stubs is not None: return if not have_appserver: self.setup_local_stubs(connection) def activate_test_stubs(self): if self.active_stubs == 'test': return self.testbed.activate() self.pre_test_stubs = self.active_stubs self.active_stubs = 'test' self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub(root_path=PROJECT_DIR) self.testbed.init_urlfetch_stub() self.testbed.init_user_stub() self.testbed.init_xmpp_stub() self.testbed.init_channel_stub() def deactivate_test_stubs(self): if self.active_stubs == 'test': self.testbed.deactivate() self.active_stubs = self.pre_test_stubs def setup_local_stubs(self, connection): if self.active_stubs == 'local': return from .base import get_datastore_paths from google.appengine.tools import dev_appserver_main args = dev_appserver_main.DEFAULT_ARGS.copy() args.update(get_datastore_paths(connection.settings_dict)) args.update(connection.settings_dict.get('DEV_APPSERVER_OPTIONS', {})) log_level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARNING) from google.appengine.tools import dev_appserver dev_appserver.SetupStubs('dev~' + appid, **args) logging.getLogger().setLevel(log_level) self.active_stubs = 'local' def setup_remote_stubs(self, connection): if self.active_stubs == 'remote': return if not connection.remote_api_path: from ..utils import appconfig for handler in appconfig.handlers: if handler.script in REMOTE_API_SCRIPTS: connection.remote_api_path = handler.url.split('(', 1)[0] break server = '%s.%s' % (connection.remote_app_id, connection.domain) remote_url = 'https://%s%s' % (server, connection.remote_api_path) logging.info("Setting up remote_api for '%s' at %s." % (connection.remote_app_id, remote_url)) if not have_appserver: logging.info( "Connecting to remote_api handler.\n\n" "IMPORTANT: Check your login method settings in the " "App Engine Dashboard if you have problems logging in. " "Login is only supported for Google Accounts.") from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApi( None, connection.remote_api_path, auth_func, servername=server, secure=connection.secure_remote_api, rpc_server_factory=rpc_server_factory) retry_delay = 1 while retry_delay <= 16: try: remote_api_stub.MaybeInvokeAuthentication() except HTTPError, e: if not have_appserver: logging.info("Retrying in %d seconds..." % retry_delay) time.sleep(retry_delay) retry_delay *= 2 else: break else:
def init_gae_api_stubs(): testbed = Testbed() testbed.activate() testbed.init_datastore_v3_stub() testbed.init_memcache_stub() return testbed
class StubManager(object): def __init__(self): self.testbed = None self.active_stubs = None self.pre_test_stubs = None def setup_stubs(self, connection): if self.active_stubs is not None: return if not have_appserver: self.activate_stubs(connection) def activate_stubs(self, connection): try: from google.appengine.tools import dev_appserver_main self.setup_local_stubs(connection) except ImportError: # patch: https://github.com/django-nonrel/djangoappengine/issues/97 import sys command_line_args = sys.argv is_local_shell = 'shell' in command_line_args and 'remote' not in command_line_args # datastore_path = connection.settings_dict.get('datastore_path', None) if is_local_shell else None # modification from ..boot import DATA_ROOT datastore_path = os.path.join(DATA_ROOT, 'datastore') # endpatch self.activate_test_stubs(connection, datastore_path) def reset_stubs(self, connection, datastore_path=None): if self.active_stubs == 'test': self.deactivate_test_stubs() self.activate_test_stubs(connection, datastore_path) elif self.active_stubs == 'local': self.setup_local_stubs(connection) elif self.active_stubs == 'remote': self.setup_remote_stubs(connection) def activate_test_stubs(self, connection, datastore_path=None): if self.active_stubs == 'test': return if self.testbed is None: from google.appengine.ext.testbed import Testbed self.testbed = Testbed() self.testbed.activate() self.pre_test_stubs = self.active_stubs self.active_stubs = 'test' os.environ['APPLICATION_ID'] = 'dev~' + appid os.environ['HTTP_HOST'] = "%s.appspot.com" % appid appserver_opts = connection.settings_dict.get('DEV_APPSERVER_OPTIONS', {}) high_replication = appserver_opts.get('high_replication', False) require_indexes = appserver_opts.get('require_indexes', False) use_sqlite = appserver_opts.get('use_sqlite', False) datastore_opts = { 'require_indexes': require_indexes, 'use_sqlite': use_sqlite } if high_replication: from google.appengine.datastore import datastore_stub_util datastore_opts[ 'consistency_policy'] = datastore_stub_util.PseudoRandomHRConsistencyPolicy( probability=1) self.testbed.init_datastore_v3_stub(datastore_file=datastore_path, **datastore_opts) self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub(auto_task_running=True, root_path=PROJECT_DIR) self.testbed.init_urlfetch_stub() self.testbed.init_user_stub() self.testbed.init_xmpp_stub() self.testbed.init_channel_stub() def deactivate_test_stubs(self): if self.active_stubs == 'test': self.testbed.deactivate() self.active_stubs = self.pre_test_stubs def setup_local_stubs(self, connection): if self.active_stubs == 'local': return from .base import get_datastore_paths from google.appengine.tools import dev_appserver_main args = dev_appserver_main.DEFAULT_ARGS.copy() args.update(get_datastore_paths(connection.settings_dict)) args.update(connection.settings_dict.get('DEV_APPSERVER_OPTIONS', {})) log_level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARNING) try: from google.appengine.tools import dev_appserver except ImportError: from google.appengine.tools import old_dev_appserver as dev_appserver dev_appserver.SetupStubs('dev~' + appid, **args) logging.getLogger().setLevel(log_level) self.active_stubs = 'local' def setup_remote_stubs(self, connection): if self.active_stubs == 'remote': return if not connection.remote_api_path: from djangoappengine.utils import appconfig from google.appengine.api import appinfo default_module = next(m for m in appconfig.modules if m.module_name == appinfo.DEFAULT_MODULE) for handler in default_module.handlers: if handler.script in REMOTE_API_SCRIPTS: connection.remote_api_path = handler.url.split('(', 1)[0] break server = '%s.%s' % (connection.remote_app_id, connection.domain) remote_url = 'https://%s%s' % (server, connection.remote_api_path) logging.info("Setting up remote_api for '%s' at %s." % (connection.remote_app_id, remote_url)) if not have_appserver: logging.info( "Connecting to remote_api handler.\n\n" "IMPORTANT: Check your login method settings in the " "App Engine Dashboard if you have problems logging in. " "Login is only supported for Google Accounts.") from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApi( None, connection.remote_api_path, auth_func, servername=server, secure=connection.secure_remote_api, rpc_server_factory=rpc_server_factory) retry_delay = 1 while retry_delay <= 16: try: remote_api_stub.MaybeInvokeAuthentication() except HTTPError, e: if not have_appserver: logging.info("Retrying in %d seconds..." % retry_delay) time.sleep(retry_delay) retry_delay *= 2 else: break else:
#!/usr/bin/env python # coding: utf-8 import os import logging from dev_appserver import fix_sys_path fix_sys_path() from google.appengine.ext.testbed import Testbed tb = Testbed() tb.activate() os.environ['APPLICATION_ID'] = 'rss-xmpp' tb.init_xmpp_stub() tb.init_datastore_v3_stub() tb.init_urlfetch_stub() from rss_xmpp.main import * from google.appengine.api import xmpp from rss_xmpp import feedcrawler import unittest url = 'http://feeds.feedburner.com/github' class XMPPTestMessage(xmpp.Message): def reply(self, body, message_type="chat", raw_xml=False, send_message=xmpp.send_message): super(XMPPTestMessage, self).reply(body, message_type, raw_xml, send_message) logging.debug('\nSent message:\n' + '='*80 + '\n' + body + '='*80 + '\n') class TestRSSXMPP(unittest.TestCase):
class StubManager(object): def __init__(self): self.testbed = None self.active_stubs = None self.pre_test_stubs = None def setup_stubs(self, connection): if self.active_stubs is not None: return if not have_appserver: self.activate_stubs(connection) def activate_stubs(self, connection): try: from google.appengine.tools import dev_appserver_main self.setup_local_stubs(connection) except ImportError: self.activate_test_stubs(connection) def reset_stubs(self, connection, datastore_path=None): if self.active_stubs == "test": self.deactivate_test_stubs() self.activate_test_stubs(connection, datastore_path) elif self.active_stubs == "local": self.setup_local_stubs(connection) elif self.active_stubs == "remote": self.setup_remote_stubs(connection) def activate_test_stubs(self, connection, datastore_path=None): if self.active_stubs == "test": return if self.testbed is None: from google.appengine.ext.testbed import Testbed self.testbed = Testbed() self.testbed.activate() self.pre_test_stubs = self.active_stubs self.active_stubs = "test" os.environ["APPLICATION_ID"] = "dev~" + appid os.environ["HTTP_HOST"] = "%s.appspot.com" % appid appserver_opts = connection.settings_dict.get("DEV_APPSERVER_OPTIONS", {}) high_replication = appserver_opts.get("high_replication", False) require_indexes = appserver_opts.get("require_indexes", False) use_sqlite = appserver_opts.get("use_sqlite", False) datastore_opts = {"require_indexes": require_indexes, "use_sqlite": use_sqlite} if high_replication: from google.appengine.datastore import datastore_stub_util datastore_opts["consistency_policy"] = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) self.testbed.init_datastore_v3_stub(datastore_file=datastore_path, **datastore_opts) self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub(auto_task_running=True, root_path=PROJECT_DIR) self.testbed.init_urlfetch_stub() self.testbed.init_user_stub() self.testbed.init_xmpp_stub() self.testbed.init_channel_stub() self.testbed.init_app_identity_stub() self.testbed.init_blobstore_stub() self.testbed.init_files_stub() self.testbed.init_images_stub() def deactivate_test_stubs(self): if self.active_stubs == "test": self.testbed.deactivate() self.active_stubs = self.pre_test_stubs def setup_local_stubs(self, connection): if self.active_stubs == "local": return from .base import get_datastore_paths from google.appengine.tools import dev_appserver_main args = dev_appserver_main.DEFAULT_ARGS.copy() args.update(get_datastore_paths(connection.settings_dict)) args.update(connection.settings_dict.get("DEV_APPSERVER_OPTIONS", {})) log_level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARNING) try: from google.appengine.tools import dev_appserver except ImportError: from google.appengine.tools import old_dev_appserver as dev_appserver dev_appserver.SetupStubs("dev~" + appid, **args) logging.getLogger().setLevel(log_level) self.active_stubs = "local" def setup_remote_stubs(self, connection): if self.active_stubs == "remote": return if not connection.remote_api_path: from djangoappengine.utils import appconfig from google.appengine.api import appinfo default_module = next(m for m in appconfig.modules if m.module_name == appinfo.DEFAULT_MODULE) for handler in default_module.handlers: if handler.script in REMOTE_API_SCRIPTS: connection.remote_api_path = handler.url.split("(", 1)[0] break server = "%s.%s" % (connection.remote_app_id, connection.domain) remote_url = "https://%s%s" % (server, connection.remote_api_path) logging.info("Setting up remote_api for '%s' at %s." % (connection.remote_app_id, remote_url)) if not have_appserver: logging.info( "Connecting to remote_api handler.\n\n" "IMPORTANT: Check your login method settings in the " "App Engine Dashboard if you have problems logging in. " "Login is only supported for Google Accounts." ) from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApi( None, connection.remote_api_path, auth_func, servername=server, secure=connection.secure_remote_api, rpc_server_factory=rpc_server_factory, ) retry_delay = 1 while retry_delay <= 16: try: remote_api_stub.MaybeInvokeAuthentication() except HTTPError, e: if not have_appserver: logging.info("Retrying in %d seconds..." % retry_delay) time.sleep(retry_delay) retry_delay *= 2 else: break else:
class FunctionalClientTest(TestCase): def setUp(self): from google.appengine.ext.testbed import Testbed self.testbed = Testbed() self.testbed.activate() self.testbed.init_urlfetch_stub() self.username = os.environ.get('NETPRINT_USERNAME') self.password = os.environ.get('NETPRINT_PASSWORD') if self.username is None or self.password is None: raise SkipTest("Need both " "NETPRINT_USERNAME and NETPRINT_PASSWORD") def tearDown(self): self.testbed.deactivate() def _getOUT(self): import httplib2 from netprint import Client httplib2.debuglevel = 1 return Client(httplib2.Http(), 'Mozilla/5.0 ' '(Macintosh; U;Intel Mac OS X 10_6_3; ja-jp) ' 'AppleWebKit/533.16 (KHTML, like Gecko) ' 'Version/5.0 Safari/533.16') @attr('functional', 'heavy') def test_login(self): client = self._getOUT() client.login(self.username, self.password, retry=1) self.assertIsNotNone(client.session_key) @attr('functional', 'heavy') def test_session_error(self): from netprint import UnexpectedContent client1 = self._getOUT() client2 = self._getOUT() client1.login(self.username, self.password, 1) client2.login(self.username, self.password, 1) self.assertNotEqual(client1.session_key, client2.session_key) self.assertRaises(UnexpectedContent, client1.reload) @attr('functional', 'heavy') def test_send_delete(self): client = self._getOUT() client.login(self.username, self.password, retry=1) client.send('tests/data/数独01.jpg') client.reload() self.assertIn(u'数独01', [item.name for item in client.list()]) client.delete(item) client.reload() self.assertNotIn(u'数独01', [item.name for item in client.list()]) @attr('functional', 'heavy') def test_send_delete_with_fileobj(self): client = self._getOUT() client.login(self.username, self.password, retry=1) client.send(file('tests/data/数独01.jpg')) client.reload() self.assertIn(u'数独01', [item.name for item in client.list()]) client.delete(item) client.reload() self.assertNotIn(u'数独01', [item.name for item in client.list()])
class StubManager(object): def __init__(self): self.active_stubs = None self.pre_test_stubs = None def setup_stubs(self, connection): if self.active_stubs is not None: return if not have_appserver: self.setup_local_stubs(connection) def activate_test_stubs(self, connection): from google.appengine.ext.testbed import Testbed self.testbed = Testbed() if self.active_stubs == 'test': return os.environ['HTTP_HOST'] = "%s.appspot.com" % appid appserver_opts = connection.settings_dict.get('DEV_APPSERVER_OPTIONS', {}) high_replication = appserver_opts.get('high_replication', False) datastore_opts = {} if high_replication: from google.appengine.datastore import datastore_stub_util datastore_opts['consistency_policy'] = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) self.testbed.activate() self.pre_test_stubs = self.active_stubs self.active_stubs = 'test' self.testbed.init_datastore_v3_stub(**datastore_opts) self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub(auto_task_running=True, root_path=PROJECT_DIR) self.testbed.init_urlfetch_stub() self.testbed.init_user_stub() self.testbed.init_xmpp_stub() self.testbed.init_channel_stub() def deactivate_test_stubs(self): if self.active_stubs == 'test': self.testbed.deactivate() self.active_stubs = self.pre_test_stubs def setup_local_stubs(self, connection): if self.active_stubs == 'local': return from .base import get_datastore_paths from google.appengine.tools import dev_appserver_main args = dev_appserver_main.DEFAULT_ARGS.copy() args.update(get_datastore_paths(connection.settings_dict)) args.update(connection.settings_dict.get('DEV_APPSERVER_OPTIONS', {})) log_level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARNING) from google.appengine.tools import dev_appserver dev_appserver.SetupStubs('dev~' + appid, **args) logging.getLogger().setLevel(log_level) self.active_stubs = 'local' def setup_remote_stubs(self, connection): if self.active_stubs == 'remote': return if not connection.remote_api_path: from ..utils import appconfig for handler in appconfig.handlers: if handler.script in REMOTE_API_SCRIPTS: connection.remote_api_path = handler.url.split('(', 1)[0] break server = '%s.%s' % (connection.remote_app_id, connection.domain) remote_url = 'https://%s%s' % (server, connection.remote_api_path) logging.info("Setting up remote_api for '%s' at %s." % (connection.remote_app_id, remote_url)) if not have_appserver: logging.info( "Connecting to remote_api handler.\n\n" "IMPORTANT: Check your login method settings in the " "App Engine Dashboard if you have problems logging in. " "Login is only supported for Google Accounts.") from google.appengine.ext.remote_api import remote_api_stub remote_api_stub.ConfigureRemoteApi(None, connection.remote_api_path, auth_func, servername=server, secure=connection.secure_remote_api, rpc_server_factory=rpc_server_factory) retry_delay = 1 while retry_delay <= 16: try: remote_api_stub.MaybeInvokeAuthentication() except HTTPError, e: if not have_appserver: logging.info("Retrying in %d seconds..." % retry_delay) time.sleep(retry_delay) retry_delay *= 2 else: break else:
class TestCase(unittest.TestCase): """Utility methods for testing. This class should be used as the superclass of all other test classes. It sets up the necessary test stubs and provides various utility methods to use in tests. """ def setUp(self): """Initialize stubs for testing. This initializes the following fields: self.testbed: An App Engine Testbed used for mocking App Engine services. This is activated, and any necessary stubs are initialized. self.testapp: A webtest.TestApp wrapping the CherryPy application. Subclasses that define their own setUp method should be sure to call this one as well. """ self.__users = {} self.__admins = {} self.testbed = Testbed() self.testbed.activate() self.testbed.init_app_identity_stub() self.testbed.init_blobstore_stub() self.testbed.init_datastore_v3_stub() self.testbed.init_files_stub() self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub() self.testbed.init_user_stub() self.testapp = webtest.TestApp(Application()) # These private keys are not used for anything other than testing. PrivateKey.set_oauth('''-----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,CEB8C6541017AC8B q1SgqAfgHXK5cQvtdIF8rkSlbAS6a92T5tYMVKJIW24giF5cw+1++X7X6S5ECykC /iECExP7WfVlPDVoJMZpWGsYLMPhxncnKfUDICbVgwsO4KKeEv8lYTrvkY1sCZIx kSja/lGAWpyxBnmxwoLh0LbLJBGUxUgn2Uqv/8Iid+w0m3NlgrllV+kOo4gUYF9q bestH4nEQj6F0CeI8VPW0FxzMwn0vreHFBT5hul6xbNdB1vnRcz6ed4FiGXoAB5K /8/Q2qyy1UPe2Hr9IoC6wo4h2kXq7pmhy/rtzU9/gjsGPD33ByavbgHAen0ajY5p RmCx0AGidK9T6/SNoyDD9CMq7ypL+0JWoCeVoAEw2aZqN4rMJNbKMgPH+ajgiAXe AWuMVjWN6uTL5+QJmWQct6a6TF8GPKdTcOZfNIXU5U9drHB2hggLcy6XkCYGjVJv MvLascE4oAtOHnwz7WhrpcmX6F6Fww+TPnFzjk+IGJrnBUnAArEcM13IjiDUgg9B iLuKimwrqasdBBY3Ua3SRMoG8PJoNKdsk1hDGlpxLnqOVUCvPKZuz4QnFusnUJrR kiv+aqVBpZYymMh2Q1MWcogA7rL7LIowAkyLzS8dNwDhyk9jbO+1uoFSHKr5BTNv cMJfCVm8pqhXwCVx3uYnhUzvth7mcEseXh5Dcg1RHka5rCXUz4eVxTkj1u3FOy9o 9jgARPpnDYsXH1lESxmiNZucHa50qI/ezNvQx8CbNa1/+JWoZ77yqM9qnDlXUwDY 1Sxqx9+4kthG9oyCzzUwFvhf1kTEHd0RfIapgjJ16HBQmzLnEPtjPA== -----END RSA PRIVATE KEY-----''') PrivateKey.set_api('not a real API key') self.dont_be_oauth_user() def tearDown(self): """Deactivates the stubs initialized in setUp. Subclasses that define their own tearDown method should be sure to call this one as well. """ self.testbed.deactivate() def normal_user(self, name=None): """Constructs a non-admin user object. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. Returns: The user object. """ if self.__users.has_key(name): return self.__users[name] email = '*****@*****.**' if name: email = '*****@*****.**' % name self.__users[name] = users.User(email=email) return self.__users[name] def admin_user(self, name=None): """Constructs an admin user object. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. Returns: The user object. """ if self.__admins.has_key(name): return self.__admins[name] email = '*****@*****.**' if name: email = '*****@*****.**' % name self.__admins[name] = users.User(email=email) return self.__admins[name] def be_normal_user(self, name=None): """Log in as a non-admin user. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. """ self.testbed.setup_env(user_email=self.normal_user(name).email(), overwrite=True) def be_admin_user(self, name=None): """Log in as an admin user. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. """ self.testbed.setup_env(user_email=self.admin_user(name).email(), user_is_admin='1', overwrite=True) def dont_be_oauth_user(self): """Don't consider the user to be logged in via OAuth.""" self.testbed.setup_env( oauth_error_code=str(UserServiceError.OAUTH_INVALID_REQUEST), oauth_error_detail='', oauth_last_scope='https://www.googleapis.com/auth/userinfo.email') def be_normal_oauth_user(self, name=None): """Log in as a non-admin OAuth user. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. """ try: del os.environ['OAUTH_ERROR_CODE'] except KeyError: pass self.set_oauth_user(self.normal_user(name)) def be_admin_oauth_user(self, name=None): """Log in as an admin OAuth user. Arguments: name: A string to distinguish this user from others. Users with different names will be distinct, and the same name will always refer to the same user. Admin users have a separate namespace from non-admin users. """ try: del os.environ['OAUTH_ERROR_CODE'] except KeyError: pass self.set_oauth_user(self.admin_user(name)) def set_oauth_user(self, user): """Set the user that is logged in via OAuth.""" self.testbed.get_stub(USER_SERVICE_NAME).SetOAuthUser( email=user.email(), user_id=user.user_id() or '0', is_admin=user.email().startswith('test-admin')) def create_package(self, name, version): """Create and save a package object with a version.""" Package.new(name=name, uploaders=[users.get_current_user()]).put() self.set_latest_version(name, version) def set_latest_version(self, package_name, version): """Set the latest version of the given package.""" package = Package.get_by_key_name(package_name) package.latest_version = self.package_version(package, version) package.latest_version.put() package.put() def package_version(self, package, version, **additional_pubspec_fields): """Create a package version object. This constructs the package archive based on the other information passed in. The archive contains only the pubspec. """ pubspec = Pubspec(name=package.name, version=version) pubspec.update(additional_pubspec_fields) return PackageVersion.new(version=version, package=package, pubspec=pubspec, uploader=package.uploaders[0]) def tar_package(self, pubspec, files={}): """Return a tarfile containing the given pubspec. The pubspec is dumped to YAML, then placed in a tarfile. The tarfile is then returned as a string. """ files['pubspec.yaml'] = yaml.dump(pubspec) files['README'] = "This is a README." return self.io_for_archive(files).getvalue() def io_for_archive(self, files): """Return a StringIO object containing a .tar.gz archive. Arugments: files: A dict of the names and contents of files in the archive. """ tarfile_io = StringIO() tar = tarfile.open(fileobj=tarfile_io, mode='w:gz') for name, contents in files.iteritems(): tarinfo = tarfile.TarInfo(name) io = StringIO(contents) tarinfo.size = len(contents) tar.addfile(tarinfo, io) tar.close() tarfile_io.seek(0) return tarfile_io def archive(self, files): """Creates a TarFile archive. Arugments: files: A dict of the names and contents of files in the archive. """ return tarfile.open(fileobj=self.io_for_archive(files)) def upload_archive(self, name, version, **additional_pubspec_fields): """Return a tuple representing a package archive upload.""" pubspec = {'name': name, 'version': version} pubspec.update(additional_pubspec_fields) contents = self.tar_package(pubspec) return ('file', '%s-%s.tar.gz' % (name, version), contents) def post_package_version(self, version, name='test-package'): response = self.upload_package(self.upload_archive(name, version)) self.assert_json_success(response) def upload_package(self, upload, status=None): get_response = self.testapp.get('/api/packages/versions/new') self.assertEqual(get_response.status_int, 200) content = json.loads(get_response.body) post_response = self.testapp.post(str(content['url']), content['fields'], upload_files=[upload]) self.assertEqual(post_response.status_int, 302) self.assertTrue( re.match( r'^http://localhost:80/api/packages/versions/[^/]+/create$', post_response.headers['Location'])) path = post_response.headers['Location'].replace( 'http://localhost:80', '') return self.testapp.get(path, status=status) def assert_requires_login(self, response): """Assert that the given response is requesting user authentication.""" self.assertEqual(response.status_int, 302) self.assertTrue( response.headers['Location'].startswith( 'https://www.google.com/accounts/Login?continue='), 'expected response to redirect to the Google login page') def assert_error_page(self, response): """Assert that the given response renders an HTTP error page. This only checks for HTTP errors; it will not detect flash error messages. Arguments: response: The webtest response object to check. """ error = self.html(response).find("p", "error-message") self.assertTrue(error is not None, "expected error page") def assert_json_error(self, response): """Assert that the given response is a JSON error.""" self.assertTrue("error" in json.loads(response.body)) def assert_json_success(self, response): """Assert that the given response is a JSON success.""" self.assertEqual(response.status_int, 200) self.assertTrue("success" in json.loads(response.body)) def assert_oauth_error(self, response): """Assert that the given response is an OAuth2 error.""" self.assertTrue( response.headers['WWW-Authenticate'].startswith('Bearer error="')) def html(self, response): """Parse a webtest response with BeautifulSoup. WebTest includes built-in support for BeautifulSoup 3.x, but we want to be able to use the features in 4.x. Arguments: response: The webtest response object to parse. Returns: The BeautifulSoup parsed HTML. """ return BeautifulSoup(response.body) def assert_link(self, response, url): """Assert that the response contains a link to the given url. Arguments: response: The webtest response object to check. url: The link URL to look for. """ error_msg = "expected response body to contain a link to '%s'" % url self.assertTrue(self._link_exists(response, url), error_msg) def assert_no_link(self, response, url): """Assert that the response doesn't contain a link to the given url. Arguments: response: The webtest response object to check. url: The link URL to look for. """ error_msg = "expected response body not to contain a link to '%s'" % url self.assertFalse(self._link_exists(response, url), error_msg) def assert_list_in_html(self, url, selector, expected_items): """Assert that the selected elements' contents match the given list. There must be as many selected elements as strings in the list, and each element's text must contain the respective string in the list. Arguments: url: The URL to request. selector: The CSS selector to apply to the resulting HTML. expected_items: A list of strings to look for in the matched elements. """ response = self.testapp.get(url) for html in self.html(response).select(selector): if not expected_items: self.fail("more items were listed than expected: %s" % html) elif expected_items[0] in ''.join(html.strings): del expected_items[0] else: self.fail("expected '%s' in %s" % (expected_items[0], html)) self.assertEqual( expected_items, [], "Selector '%s' missing items: %s" % (selector, expected_items)) def run_deferred_tasks(self, queue='default'): """Run all tasks that have been deferred. Arguments: queue: The task queue in which the deferred tasks are queued. """ taskqueue_stub = self.testbed.get_stub(TASKQUEUE_SERVICE_NAME) for task in taskqueue_stub.GetTasks(queue): deferred.run(base64.b64decode(task['body'])) taskqueue_stub.FlushQueue(queue) def _link_exists(self, response, url): """Return whether or not the response contains a link to the given url. Arguments: response: The webtest response object to check. url: The link URL to look for. """ return any([ link.get('href') == url for link in self.html(response).find_all('a') ]) def package_version_dict(self, name, version): """Returns the expected API dictionary for a package version.""" package_url = "http://localhost:80/api/packages/" + name version_url = package_url + "/versions/" + version return { "version": version, "url": version_url, "package_url": package_url, "new_dartdoc_url": version_url + "/new_dartdoc", "archive_url": "http://localhost:80/packages/" + name + "/versions/" + version + ".tar.gz", "pubspec": { "name": name, "version": version } }