예제 #1
0
def append_middleware(path):
    if django.VERSION[:2] >= (1, 10):
        middleware_setting = 'MIDDLEWARE'
    else:
        middleware_setting = 'MIDDLEWARE_CLASSES'

    return modify_settings(**{middleware_setting: {'append': path}})
예제 #2
0
def app_with_scout(config=None):
    """
    Context manager that configures and installs the Scout plugin for Bottle.

    """
    # Enable Scout by default in tests.
    if config is None:
        config = {"SCOUT_MONITOR": True}

    # Disable running the agent.
    config["SCOUT_CORE_AGENT_LAUNCH"] = False

    # Setup according to http://help.apm.scoutapp.com/#django
    with override_settings(**config):
        # Prevent durable changes to MIDDLEWARE and MIDDLEWARE_CLASSES by
        # replacing them with a copy of their value.
        for name in ["MIDDLEWARE", "MIDDLEWARE_CLASSES"]:
            try:
                value = getattr(settings, name)
            except AttributeError:
                pass
            else:
                setattr(settings, name, value)
        # Scout settings must be overridden before inserting scout_apm.django
        # in INSTALLED_APPS because ScoutApmDjangoConfig.ready() accesses it.
        with modify_settings(INSTALLED_APPS={"prepend": "scout_apm.django"}):
            try:
                # Django initializes middleware when in creates the WSGI app.
                # Modifying MIDDLEWARE setting has no effect on the app.
                # Create a new WSGI app to account for the new middleware
                # that "scout_apm.django" injected.
                yield get_wsgi_application()
            finally:
                # Reset Scout configuration.
                Config.reset_all()
예제 #3
0
    def override_settings(self):

        if self.overridden_settings:
            overridden = override_settings(**self.overridden_settings)
            overridden.enable()

        if self.modified_settings:
            modified = modify_settings(self.modified_settings)
            modified.enable()
예제 #4
0
    def override_settings(self):

        if self.overridden_settings:
            overridden = override_settings(**self.overridden_settings)
            overridden.enable()

        if self.modified_settings:
            modified = modify_settings(self.modified_settings)
            modified.enable()
예제 #5
0
 def test_register_type_handlers_connection(self):
     from django.contrib.postgres.signals import register_type_handlers
     self.assertNotIn(register_type_handlers,
                      connection_created._live_receivers(None))
     with modify_settings(
             INSTALLED_APPS={'append': 'django.contrib.postgres'}):
         self.assertIn(register_type_handlers,
                       connection_created._live_receivers(None))
     self.assertNotIn(register_type_handlers,
                      connection_created._live_receivers(None))
예제 #6
0
def add_middleware(action, path):
    if django.VERSION[:2] >= (1, 10):
        middleware_setting = 'MIDDLEWARE'
    else:
        middleware_setting = 'MIDDLEWARE_CLASSES'

    return modify_settings(**{
        middleware_setting: {
            action: path,
        }
    })
예제 #7
0
    def __init__(self, addr):
        import django
        from django.db import connections
        from django.test.testcases import LiveServerThread
        from django.test.utils import modify_settings

        connections_override = {}
        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (conn.settings_dict['ENGINE'] == 'django.db.backends.sqlite3'
                    and conn.settings_dict['NAME'] == ':memory:'):
                # Explicitly enable thread-shareability for this connection
                conn.allow_thread_sharing = True
                connections_override[conn.alias] = conn

        liveserver_kwargs = {'connections_override': connections_override}
        from django.conf import settings
        if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
            from django.contrib.staticfiles.handlers import StaticFilesHandler
            liveserver_kwargs['static_handler'] = StaticFilesHandler
        else:
            from django.test.testcases import _StaticFilesHandler
            liveserver_kwargs['static_handler'] = _StaticFilesHandler

        if django.VERSION < (1, 11):
            host, possible_ports = parse_addr(addr)
            self.thread = LiveServerThread(host, possible_ports,
                                           **liveserver_kwargs)
        elif django.VERSION > (1, 11, 2):
            host, possible_ports = parse_addr(addr)
            self.thread = LiveServerThread(host,
                                           port=possible_ports[0],
                                           **liveserver_kwargs)
        else:
            try:
                host, port = addr.split(':')
            except ValueError:
                host = addr
            else:
                liveserver_kwargs['port'] = int(port)
            self.thread = LiveServerThread(host, **liveserver_kwargs)

        self._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={'append': host}, )
        self._live_server_modified_settings.enable()

        self.thread.daemon = True
        self.thread.start()
        self.thread.is_ready.wait()

        if self.thread.error:
            raise self.thread.error
예제 #8
0
파일: testcases.py 프로젝트: 912/M-new
    def _pre_setup(self):
        """Performs any pre-test setup. This includes:

        * Creating a test client.
        * If the class has a 'urls' attribute, replace ROOT_URLCONF with it.
        * Clearing the mail test outbox.
        """
        if self._overridden_settings:
            self._overridden_context = override_settings(**self._overridden_settings)
            self._overridden_context.enable()
        if self._modified_settings:
            self._modified_context = modify_settings(self._modified_settings)
            self._modified_context.enable()
        self.client = self.client_class()
        self._urlconf_setup()
        mail.outbox = []
예제 #9
0
파일: testcases.py 프로젝트: ikkebr/twitter
    def _pre_setup(self):
        """Performs any pre-test setup. This includes:

        * Creating a test client.
        * If the class has a 'urls' attribute, replace ROOT_URLCONF with it.
        * Clearing the mail test outbox.
        """
        if self._overridden_settings:
            self._overridden_context = override_settings(**self._overridden_settings)
            self._overridden_context.enable()
        if self._modified_settings:
            self._modified_context = modify_settings(self._modified_settings)
            self._modified_context.enable()
        self.client = self.client_class()
        self._urlconf_setup()
        mail.outbox = []
    def __init__(self, addr: str) -> None:
        from django.db import connections
        from django.test.testcases import LiveServerThread
        from django.test.utils import modify_settings

        liveserver_kwargs = {}  # type: Dict[str, Any]

        connections_override = {}
        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (conn.settings_dict["ENGINE"] == "django.db.backends.sqlite3"
                    and conn.settings_dict["NAME"] == ":memory:"):
                # Explicitly enable thread-shareability for this connection
                conn.allow_thread_sharing = True
                connections_override[conn.alias] = conn

        liveserver_kwargs["connections_override"] = connections_override
        from django.conf import settings

        if "django.contrib.staticfiles" in settings.INSTALLED_APPS:
            from django.contrib.staticfiles.handlers import StaticFilesHandler

            liveserver_kwargs["static_handler"] = StaticFilesHandler
        else:
            from django.test.testcases import _StaticFilesHandler

            liveserver_kwargs["static_handler"] = _StaticFilesHandler

        try:
            host, port = addr.split(":")
        except ValueError:
            host = addr
        else:
            liveserver_kwargs["port"] = int(port)
        self.thread = LiveServerThread(host, **liveserver_kwargs)

        self._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={"append": host})

        self.thread.daemon = True
        self.thread.start()
        self.thread.is_ready.wait()

        if self.thread.error:
            raise self.thread.error
예제 #11
0
파일: live.py 프로젝트: zst0717/channels
    def _pre_setup(self):
        for connection in connections.all():
            if self._is_in_memory_db(connection):
                raise ImproperlyConfigured(
                    "ChannelLiveServerTestCase can not be used with in memory databases"
                )

        super(ChannelsLiveServerTestCase, self)._pre_setup()

        self._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={"append": self.host})
        self._live_server_modified_settings.enable()
        self._server_process = self.ProtocolServerProcess(
            self.host, get_default_application())
        self._server_process.start()
        self._server_process.ready.wait()
        self._port = self._server_process.port.value
예제 #12
0
    def __init__(self, addr):
        import django
        from django.db import connections
        from django.test.testcases import LiveServerThread
        from django.test.utils import modify_settings

        connections_override = {}
        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (
                conn.settings_dict["ENGINE"] == "django.db.backends.sqlite3"
                and conn.settings_dict["NAME"] == ":memory:"
            ):
                # Explicitly enable thread-shareability for this connection
                conn.allow_thread_sharing = True
                connections_override[conn.alias] = conn

        liveserver_kwargs = {"connections_override": connections_override}
        from django.conf import settings

        if "django.contrib.staticfiles" in settings.INSTALLED_APPS:
            from django.contrib.staticfiles.handlers import StaticFilesHandler

            liveserver_kwargs["static_handler"] = StaticFilesHandler
        else:
            from django.test.testcases import _StaticFilesHandler

            liveserver_kwargs["static_handler"] = _StaticFilesHandler

        if django.VERSION < (1, 11):
            host, possible_ports = parse_addr(addr)
            self.thread = LiveServerThread(host, possible_ports, **liveserver_kwargs)
        else:
            host = addr
            self.thread = LiveServerThread(host, **liveserver_kwargs)

        self._live_server_modified_settings = modify_settings(ALLOWED_HOSTS={"append": host})
        self._live_server_modified_settings.enable()

        self.thread.daemon = True
        self.thread.start()
        self.thread.is_ready.wait()

        if self.thread.error:
            raise self.thread.error
예제 #13
0
    def test_permission_billing(self):
        # Permissions should apply without billing
        with modify_settings(INSTALLED_APPS={"remove": "weblate.billing"}):
            self.assertTrue(
                self.superuser.has_perm("billing:project.permissions",
                                        self.project))
            self.assertTrue(
                self.admin.has_perm("billing:project.permissions",
                                    self.project))
            self.assertFalse(
                self.user.has_perm("billing:project.permissions",
                                   self.project))

        # With billing enabled and no plan it should be disabled
        self.assertFalse(
            self.superuser.has_perm("billing:project.permissions",
                                    self.project))
        self.assertFalse(
            self.admin.has_perm("billing:project.permissions", self.project))
        self.assertFalse(
            self.user.has_perm("billing:project.permissions", self.project))

        project = Project.objects.get(pk=self.project.pk)
        billing = create_test_billing(self.admin)
        billing.projects.add(project)

        # The default plan allows
        self.assertTrue(
            self.superuser.has_perm("billing:project.permissions", project))
        self.assertTrue(
            self.admin.has_perm("billing:project.permissions", project))
        self.assertFalse(
            self.user.has_perm("billing:project.permissions", project))

        billing.plan.change_access_control = False
        billing.plan.save()
        project = Project.objects.get(pk=self.project.pk)

        # It should be restricted now
        self.assertFalse(
            self.superuser.has_perm("billing:project.permissions", project))
        self.assertFalse(
            self.admin.has_perm("billing:project.permissions", project))
        self.assertFalse(
            self.user.has_perm("billing:project.permissions", project))
예제 #14
0
    def setUpClass(cls):
        super().setUpClass()
        connections_override = {}

        cls._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={'append': cls.allowed_host},
        )
        cls._live_server_modified_settings.enable()
        cls.server_thread = cls._create_server_thread(connections_override)
        cls.server_thread.daemon = True
        cls.server_thread.start()

        # Wait for the live server to be ready
        cls.server_thread.is_ready.wait()
        if cls.server_thread.error:
            # Clean up behind ourselves, since tearDownClass won't get called
            # in case of errors.
            cls._tearDownClassInternal()
            raise cls.server_thread.error
예제 #15
0
파일: live.py 프로젝트: ostcar/channels
    def _pre_setup(self):
        for connection in connections.all():
            if self._is_in_memory_db(connection):
                raise ImproperlyConfigured(
                    "ChannelLiveServerTestCase can not be used with in memory databases"
                )

        super(ChannelsLiveServerTestCase, self)._pre_setup()

        self._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={"append": self.host},
        )
        self._live_server_modified_settings.enable()
        self._server_process = self.ProtocolServerProcess(
            self.host,
            get_default_application(),
        )
        self._server_process.start()
        self._server_process.ready.wait()
        self._port = self._server_process.port.value
예제 #16
0
def virtual_static(args):
    """
    Collect the static in the directory given in argument
    """
    tempdir, = args
    args = ['collectstatic', '--verbosity', '0', '--noinput', '--link']

    from django.conf import settings
    from django.test.utils import override_settings, modify_settings

    with override_settings(STATIC_ROOT=tempdir):
        with modify_settings(STATICFILES_DIRS={
            'append': getattr(settings, 'STATICFILES_DIRS_DEV', []),
            'remove': [
                # Remove with and without trailing /
                ROOT.joinpath(defaults.RJS_BUILD_DIR).normpath().parent,
                ROOT.joinpath(defaults.RJS_BUILD_DIR).normpath().parent.joinpath(''),
            ]
        }):
            call_task('django', args=args)
예제 #17
0
 def test_profile_password_warning(self):
     with mock.patch.object(User, "has_usable_password",
                            return_value=False):
         response = self.client.get(reverse("profile"))
         self.assertContains(response,
                             "Please enable the password authentication")
         with modify_settings(
                 AUTHENTICATION_BACKENDS={
                     "remove": "social_core.backends.email.EmailAuth"
                 }):
             load_backends(settings.AUTHENTICATION_BACKENDS,
                           force_load=True)
             response = self.client.get(reverse("profile"))
             self.assertNotContains(
                 response, "Please enable the password authentication")
     self.assertEqual(self.user.has_usable_password(), True)
     response = self.client.get(reverse("profile"))
     self.assertNotContains(response,
                            "Please enable the password authentication")
     load_backends(settings.AUTHENTICATION_BACKENDS, force_load=True)
예제 #18
0
    def start_live_django_server(self):
        connections_override = {}

        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if conn.vendor == "sqlite" and conn.is_in_memory_db():
                # Explicitly enable thread-shareability for this connection
                conn.inc_thread_sharing()
                connections_override[conn.alias] = conn

        liveserver_kwargs = {
            "port": self.port,
            "connections_override": connections_override,
        }

        if "django.contrib.staticfiles" in settings.INSTALLED_APPS:
            from django.contrib.staticfiles.handlers import StaticFilesHandler

            liveserver_kwargs["static_handler"] = StaticFilesHandler
        else:
            liveserver_kwargs["static_handler"] = self.static_handler

        self._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={"append": self.host}
        )

        self.server_thread = self.server_thread_class(self.host, **liveserver_kwargs)

        self.server_thread.daemon = True
        self.server_thread.start()
        self.server_thread.is_ready.wait()

        if self.server_thread.error:
            raise self.server_thread.error

        logger.console(
            f"Start Django on {self.server_thread.host}:{self.server_thread.port}"
        )

        return self.server_thread
예제 #19
0
    def __init__(self, host='localhost'):
        from django.db import connections
        from django.test.utils import modify_settings

        for connection in connections.all():
            if self._is_in_memory_db(connection):
                raise ImproperlyConfigured(
                    "ChannelLiveServerTestCase can not be used with in memory databases"
                )

        self._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={"append": host})

        self._server_process = self.ProtocolServerProcess(
            host, self.get_application())
        self._server_process.start()
        self._server_process.ready.wait()
        self._host = host
        self._port = self._server_process.port.value

        if not self._server_process.errors.empty():
            raise self._server_process.errors.get()
예제 #20
0
파일: testcases.py 프로젝트: ikkebr/twitter
 def modify_settings(self, **kwargs):
     """
     A context manager that temporarily applies changes a list setting and
     reverts back to the original value when exiting the context.
     """
     return modify_settings(**kwargs)
예제 #21
0
 def test_register_type_handlers_connection(self):
     from django.contrib.postgres.signals import register_type_handlers
     self.assertNotIn(register_type_handlers, connection_created._live_receivers(None))
     with modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'}):
         self.assertIn(register_type_handlers, connection_created._live_receivers(None))
     self.assertNotIn(register_type_handlers, connection_created._live_receivers(None))
예제 #22
0
    def _check_internal(self, tested_url):

        from linkcheck.utils import LinkCheckHandler

        if not (tested_url):
            self.message = 'Empty link'

        elif tested_url.startswith('mailto:'):
            self.status = None
            self.message = 'Email link (not automatically checked)'

        elif tested_url.startswith('tel:'):
            self.status = None
            self.message = 'Phone number (not automatically checked)'

        elif tested_url.startswith('#'):
            self.status = None
            self.message = 'Link to within the same page (not automatically checked)'

        elif tested_url.startswith(MEDIA_PREFIX):
            # TODO Assumes a direct mapping from media url to local filesystem path. This will break quite easily for alternate setups
            path = settings.MEDIA_ROOT + unquote(
                tested_url)[len(MEDIA_PREFIX) - 1:]
            decoded_path = html_decode(path)
            if os.path.exists(path) or os.path.exists(decoded_path):
                self.message = 'Working file link'
                self.status = True
            else:
                self.message = 'Missing Document'

        elif getattr(self, '_internal_hash', False) and getattr(
                self, '_instance', None):
            # This is a hash link pointing to itself
            from linkcheck import parse_anchors

            hash = self._internal_hash
            instance = self._instance
            if hash == '#':  # special case, point to #
                self.message = 'Working internal hash anchor'
                self.status = True
            else:
                hash = hash[1:]  #'#something' => 'something'
                html_content = ''
                for field in instance._linklist.html_fields:
                    html_content += getattr(instance, field, '')
                try:
                    names = parse_anchors(html_content)
                    if hash in names:
                        self.message = 'Working internal hash anchor'
                        self.status = True
                    else:
                        self.message = 'Broken internal hash anchor'
                except UnicodeDecodeError:
                    self.message = 'Failed to parse HTML for anchor'

        elif tested_url.startswith('/'):
            old_prepend_setting = settings.PREPEND_WWW
            settings.PREPEND_WWW = False
            c = Client()
            c.handler = LinkCheckHandler()
            with modify_settings(ALLOWED_HOSTS={'append': 'testserver'}):
                response = c.get(tested_url)
            if response.status_code == 200:
                self.message = 'Working internal link'
                self.status = True
                # see if the internal link points an anchor
                if tested_url[-1] == '#':  # special case, point to #
                    self.message = 'Working internal hash anchor'
                elif tested_url.count('#'):
                    anchor = tested_url.split('#')[1]
                    from linkcheck import parse_anchors
                    try:
                        names = parse_anchors(response.content)
                        if anchor in names:
                            self.message = 'Working internal hash anchor'
                            self.status = True
                        else:
                            self.message = 'Broken internal hash anchor'
                            self.status = False
                    except UnicodeDecodeError:
                        self.message = 'Failed to parse HTML for anchor'

            elif response.status_code == 302 or response.status_code == 301:
                with modify_settings(ALLOWED_HOSTS={'append': 'testserver'}):
                    redir_response = c.get(tested_url, follow=True)
                if redir_response.status_code == 200:
                    redir_state = 'Working redirect'
                    self.status = True
                else:
                    redir_state = 'Broken redirect'
                    self.status = False
                self.message = 'This link redirects: code %d (%s)' % (
                    response.status_code, redir_state)
            else:
                self.message = 'Broken internal link'
            settings.PREPEND_WWW = old_prepend_setting
        else:
            self.message = 'Invalid URL'

        if USE_REVERSION:
            # using test client will clear the RevisionContextManager stack.
            revision_context_manager.start()

        self.last_checked = now()
        self.save()
예제 #23
0
def add_middleware(action, path):
    return modify_settings(**{"MIDDLEWARE": {action: path}})
예제 #24
0
파일: testcases.py 프로젝트: 912/M-new
 def modify_settings(self, **kwargs):
     """
     A context manager that temporarily applies changes a list setting and
     reverts back to the original value when exiting the context.
     """
     return modify_settings(**kwargs)
예제 #25
0
def add_middleware(action, path):
    return modify_settings(**{
        'MIDDLEWARE': {
            action: path,
        }
    })