예제 #1
0
def settings(request):
    """
    This fixture initializes a Django settings object that wraps our
    `awx.conf.settings.SettingsWrapper` and passes it as an argument into the
    test function.

    This mimics the work done by `awx.conf.settings.SettingsWrapper.initialize`
    on `django.conf.settings`.
    """
    cache = LocMemCache(str(uuid4()), {})  # make a new random cache each time
    settings = LazySettings()
    registry = SettingsRegistry(settings)
    defaults = {}

    # @pytest.mark.defined_in_file can be used to mark specific setting values
    # as "defined in a settings file".  This is analogous to manually
    # specifying a setting on the filesystem (e.g., in a local_settings.py in
    # development, or in /etc/tower/conf.d/<something>.py)
    for marker in request.node.own_markers:
        if marker.name == 'defined_in_file':
            defaults = marker.kwargs

    defaults['DEFAULTS_SNAPSHOT'] = {}
    settings.configure(**defaults)
    settings._wrapped = SettingsWrapper(settings._wrapped, cache, registry)
    return settings
예제 #2
0
파일: tests.py 프로젝트: guyi2020/blog
 def test_evaluated_lazysettings_repr(self):
     lazy_settings = LazySettings()
     module = os.environ.get(ENVIRONMENT_VARIABLE)
     expected = '<LazySettings "%s">' % module
     # Force evaluation of the lazy object.
     lazy_settings.APPEND_SLASH
     self.assertEqual(repr(lazy_settings), expected)
예제 #3
0
def test_base_logging_handler_from_django_settings(param, django_settings_name):
    settings = LazySettings()
    settings.configure(**{
        django_settings_name: 'EXAMPLE'
    })
    handler = BaseHandler.from_django_settings(settings)
    assert hasattr(handler, param) and getattr(handler, param) == 'EXAMPLE'
예제 #4
0
def test_https_logging_handler_connectivity_test(http_adapter, status, reason, exc):
    http_adapter.status = status
    http_adapter.reason = reason
    settings = LazySettings()
    settings.configure(**{
        'LOG_AGGREGATOR_HOST': 'example.org',
        'LOG_AGGREGATOR_PORT': 8080,
        'LOG_AGGREGATOR_TYPE': 'logstash',
        'LOG_AGGREGATOR_USERNAME': '******',
        'LOG_AGGREGATOR_PASSWORD': '******',
        'LOG_AGGREGATOR_LOGGERS': ['awx', 'activity_stream', 'job_events', 'system_tracking'],
        'CLUSTER_HOST_ID': '',
        'LOG_AGGREGATOR_TOWER_UUID': str(uuid4()),
        'LOG_AGGREGATOR_LEVEL': 'DEBUG',
    })

    class FakeHTTPSHandler(HTTPSHandler):

        def __init__(self, *args, **kwargs):
            super(FakeHTTPSHandler, self).__init__(*args, **kwargs)
            self.session.mount('http://', http_adapter)

        def emit(self, record):
            return super(FakeHTTPSHandler, self).emit(record)

    if exc:
        with pytest.raises(exc) as e:
            FakeHTTPSHandler.perform_test(settings)
        assert str(e).endswith('%s: %s' % (status, reason))
    else:
        assert FakeHTTPSHandler.perform_test(settings) is None
def test_protocol_not_specified():
    settings = LazySettings()
    settings.configure(
        **{
            'LOG_AGGREGATOR_HOST': 'https://server.invalid',
            'LOG_AGGREGATOR_PORT': 22222,
            'LOG_AGGREGATOR_PROTOCOL': None  # awx/settings/defaults.py
        })
    handler = AWXProxyHandler().get_handler(custom_settings=settings)
    assert isinstance(handler, logging.NullHandler)
예제 #6
0
def test_invalid_kwarg_to_real_handler():
    settings = LazySettings()
    settings.configure(
        **{
            'LOG_AGGREGATOR_HOST': 'https://server.invalid',
            'LOG_AGGREGATOR_PORT': 22222,
            'LOG_AGGREGATOR_PROTOCOL': 'udp',
            'LOG_AGGREGATOR_VERIFY_CERT':
            False  # setting not valid for UDP handler
        })
    handler = AWXProxyHandler().get_handler(custom_settings=settings)
    assert not hasattr(handler, 'verify_cert')
예제 #7
0
def settings(request):
    """Adds django settings dict to context"""

    dict_settings = dict()
    settings = LazySettings()
    #Good hack to use pdb-friendly dir function
    for k in dir(settings):
        #Wrap settings as object
        attr = getattr(settings._wrapped, k)
        if not callable(attr) and k.isupper() and not k.startswith('_'):
            dict_settings[k] = attr
    return dict_settings
예제 #8
0
def test_real_handler_from_django_settings(params):
    settings = LazySettings()
    settings.configure(**params)
    handler = AWXProxyHandler().get_handler(custom_settings=settings)
    # need the _reverse_ dictionary from PARAM_NAMES
    attr_lookup = {}
    for attr_name, setting_name in PARAM_NAMES.items():
        attr_lookup[setting_name] = attr_name
    for setting_name, val in params.items():
        attr_name = attr_lookup[setting_name]
        if attr_name == 'protocol':
            continue
        assert hasattr(handler, attr_name)
def test_https_logging_handler_connectivity_test(https_adapter, status, reason,
                                                 exc, protocol):
    host = 'example.org'
    if protocol:
        host = '://'.join([protocol, host])
    https_adapter.status = status
    https_adapter.reason = reason
    settings = LazySettings()
    settings.configure(
        **{
            'LOG_AGGREGATOR_HOST':
            host,
            'LOG_AGGREGATOR_PORT':
            8080,
            'LOG_AGGREGATOR_TYPE':
            'logstash',
            'LOG_AGGREGATOR_USERNAME':
            '******',
            'LOG_AGGREGATOR_PASSWORD':
            '******',
            'LOG_AGGREGATOR_LOGGERS':
            ['awx', 'activity_stream', 'job_events', 'system_tracking'],
            'LOG_AGGREGATOR_PROTOCOL':
            'https',
            'CLUSTER_HOST_ID':
            '',
            'LOG_AGGREGATOR_TOWER_UUID':
            str(uuid4()),
            'LOG_AGGREGATOR_LEVEL':
            'DEBUG',
        })

    class FakeHTTPSHandler(HTTPSHandler):
        def __init__(self, *args, **kwargs):
            super(FakeHTTPSHandler, self).__init__(*args, **kwargs)
            self.session.mount('{}://'.format(protocol or 'https'),
                               https_adapter)

        def emit(self, record):
            return super(FakeHTTPSHandler, self).emit(record)

    with mock.patch.object(AWXProxyHandler,
                           'get_handler_class') as mock_get_class:
        mock_get_class.return_value = FakeHTTPSHandler
        if exc:
            with pytest.raises(exc) as e:
                AWXProxyHandler().perform_test(settings)
            assert str(e).endswith('%s: %s' % (status, reason))
        else:
            assert AWXProxyHandler().perform_test(settings) is None
예제 #10
0
    def test_use_deprecated_pytz_deprecation(self):
        settings_module = ModuleType('fake_settings_module')
        settings_module.USE_DEPRECATED_PYTZ = True
        settings_module.USE_TZ = True
        sys.modules['fake_settings_module'] = settings_module
        try:
            with self.assertRaisesMessage(RemovedInDjango50Warning, USE_DEPRECATED_PYTZ_DEPRECATED_MSG):
                Settings('fake_settings_module')
        finally:
            del sys.modules['fake_settings_module']

        holder = LazySettings()
        with self.assertRaisesMessage(RemovedInDjango50Warning, USE_DEPRECATED_PYTZ_DEPRECATED_MSG):
            holder.configure(USE_DEPRECATED_PYTZ=True)
예제 #11
0
def reg(request):
    """
    This fixture initializes an awx settings registry object and passes it as
    an argument into the test function.
    """
    cache = LocMemCache(str(uuid4()), {})  # make a new random cache each time
    settings = LazySettings()
    registry = SettingsRegistry(settings)

    # @pytest.mark.defined_in_file can be used to mark specific setting values
    # as "defined in a settings file".  This is analogous to manually
    # specifying a setting on the filesystem (e.g., in a local_settings.py in
    # development, or in /etc/tower/conf.d/<something>.py)
    defaults = request.node.get_marker('defined_in_file')
    if defaults:
        settings.configure(**defaults.kwargs)
    settings._wrapped = SettingsWrapper(settings._wrapped, cache, registry)
    return registry
예제 #12
0
# -*- coding: utf-8 -*-

DJANGO_REQUEST_ENABLE_SETTING = 'DJANGO_REQUEST_ENABLE'
DJANGO_REQUEST_LOGFILE_SETTING = 'DJANGO_REQUEST_LOGFILE'
REQUEST_ID_HEADER_SETTING = 'DJANGO_REQUEST_LOG_REQUEST_ID_HEADER'
REQUEST_ID_RESPONSE_HEADER_SETTING = 'DJANGO_REQUEST_SET_REQUEST_ID_HEADER'
NO_REQUEST_ID = '-'

from django.conf import settings, LazySettings
from django_request_logger.log import LOGGING
import threading

__version__ = "0.1"

# ThreadLocal - dirty but does the job
local = threading.local()

if getattr(settings, DJANGO_REQUEST_ENABLE_SETTING, True):
    # override any existing logging configuration
    settings.LOGGING = LOGGING

    # create a new Settings object and pass in our settings,
    # we don't need/use the new Settings object, it is just to
    # get the private _configure_logging() method called
    unused_settings = LazySettings()
    unused_settings.configure(default_settings=settings)
예제 #13
0
파일: tests.py 프로젝트: guyi2020/blog
 def test_unevaluated_lazysettings_repr(self):
     lazy_settings = LazySettings()
     expected = '<LazySettings [Unevaluated]>'
     self.assertEqual(repr(lazy_settings), expected)
예제 #14
0
파일: tests.py 프로젝트: guyi2020/blog
    def test_configure(self):
        s = LazySettings()
        s.configure(SECRET_KEY='foo')

        self.assertTrue(s.is_overridden('SECRET_KEY'))
예제 #15
0
 def test_configure_initializes_logging(self):
     settings = LazySettings()
     settings.configure(LOGGING_CONFIG='logging_tests.tests.dictConfig')
     self.assertTrue(dictConfig.called)
예제 #16
0
 def test_nonupper_settings_ignored_in_default_settings(self):
     s = LazySettings()
     s.configure(SimpleNamespace(foo='bar'))
     with self.assertRaises(AttributeError):
         getattr(s, 'foo')
예제 #17
0
 def test_nonupper_settings_prohibited_in_configure(self):
     s = LazySettings()
     with self.assertRaisesMessage(TypeError,
                                   "Setting 'foo' must be uppercase."):
         s.configure(foo='bar')
예제 #18
0
def handle_django_settings(filename):
    '''Attempts to load a Django project and get package dependencies from
    settings.

    Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
    the other versions.
    '''
    old_sys_path = sys.path[:]
    dirpath = os.path.dirname(filename)
    project = os.path.basename(dirpath)
    cwd = os.getcwd()
    project_path = os.path.normpath(os.path.join(dirpath, '..'))
    if project_path not in sys.path:
        sys.path.insert(0, project_path)
    os.chdir(project_path)

    project_settings = '{}.settings'.format(project)
    os.environ['DJANGO_SETTINGS_MODULE'] = project_settings

    try:
        import django
        # Sanity
        django.setup = lambda: False
    except ImportError:
        log.error('Found Django settings, but Django is not installed.')
        return

    log.warn('Loading Django Settings (Using {}): {}'.format(
        django.get_version(), filename))

    from django.conf import LazySettings

    installed_apps = set()
    settings_imports = set()

    try:
        settings = LazySettings()
        settings._setup()
        for k, v in vars(settings._wrapped).items():
            if k not in _excluded_settings and re.match(r'^[A-Z_]+$', k):
                # log.debug('Scanning Django setting: %s', k)
                scan_django_settings(v, settings_imports)

        # Manually scan INSTALLED_APPS since the broad scan won't include
        # strings without a period in it .
        for app in getattr(settings, 'INSTALLED_APPS', []):
            if hasattr(app, '__file__') and getattr(app, '__file__'):
                imp, _ = utils.import_path_from_file(getattr(app, '__file__'))
                installed_apps.add(imp)
            else:
                installed_apps.add(app)
    except Exception as e:
        log.error('Could not load Django settings: %s', e)
        log.debug('', exc_info=True)
        return

    if not installed_apps or not settings_imports:
        log.error('Got empty settings values from Django settings.')

    try:
        from django.apps.registry import apps, Apps, AppRegistryNotReady
        # Django doesn't like it when the initial instance of `apps` is reused,
        # but it has to be populated before other instances can be created.
        if not apps.apps_ready:
            apps.populate(installed_apps)
        else:
            apps = Apps(installed_apps)

        start = time.time()
        while True:
            try:
                for app in apps.get_app_configs():
                    installed_apps.add(app.name)
            except AppRegistryNotReady:
                if time.time() - start > 10:
                    raise Exception('Bail out of waiting for Django')
                log.debug('Waiting for apps to load...')
                continue
            break
    except Exception as e:
        log.debug('Could not use AppConfig: {}'.format(e))

    # Restore before sub scans can occur
    sys.path[:] = old_sys_path
    os.chdir(cwd)

    for item in settings_imports:
        need_scan = item.startswith(_filescan_modules)
        yield ('django', item, project_path if need_scan else None)

    for app in installed_apps:
        need_scan = app.startswith(project)
        yield ('django', app, project_path if need_scan else None)
예제 #19
0
 def __reset_settings():
     os.environ['DJANGO_SETTINGS_MODULE'] = _DJANGO_SETTINGS_MODULE
     conf.settings = LazySettings()
예제 #20
0
파일: tests.py 프로젝트: pallav10/django
    def test_configure(self):
        s = LazySettings()
        s.configure(SECRET_KEY="foo")

        self.assertTrue(s.is_overridden("SECRET_KEY"))
예제 #21
0
파일: tests.py 프로젝트: guyi2020/blog
 def test_usersettingsholder_repr(self):
     lazy_settings = LazySettings()
     lazy_settings.configure(APPEND_SLASH=False)
     expected = '<UserSettingsHolder>'
     self.assertEqual(repr(lazy_settings._wrapped), expected)
예제 #22
0
import jwt, re, logging
import traceback
from channels.auth import AuthMiddlewareStack
from django.contrib.auth.models import AnonymousUser
from django.conf import LazySettings
from jwt import InvalidSignatureError, ExpiredSignatureError, DecodeError
from urllib import parse
from rbac.models import UserProfile
from django.db import close_old_connections

warn_logger = logging.getLogger('warn')
settings = LazySettings()


class TokenAuthMiddleware:
    def __init__(self, inner):
        self.inner = inner

    def __call__(self, scope):
        # Close old database connections to prevent usage of timed out connections
        close_old_connections()
        query = parse.parse_qs(scope['query_string'].decode("utf-8"))
        try:
            query = parse.parse_qs(
                scope['query_string'].decode("utf-8"))['token'][0]
            if query:
                try:
                    user_jwt = jwt.decode(
                        query,
                        settings.SECRET_KEY,
                    )
예제 #23
0
 def _show_toolbar(self, request):
     return bool(LazySettings().DEBUG)