• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python db_connection.env函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中spec.python.db_connection.env函数的典型用法代码示例。如果您正苦于以下问题:Python env函数的具体用法?Python env怎么用?Python env使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了env函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_empty_user

def test_empty_user():
    with db_connection.env(login_active=False):
        u = User({})

        assert u.email == 'SAM'
        assert u.name == 'SAM'
        assert u.logged_in is True
        assert u.plan_active is True
        assert u.plan == 'auto'
        assert u.subscription == sub_id
        assert u.viewing == sub_id
        assert u.groups.issuperset({'login', 'subscribed'})
        assert 'logout' not in u.groups
        assert 'unsubscribed' not in u.groups
        assert 'debug' not in u.groups

    with db_connection.env(login_active=True):
        u = User({})

        assert u.email is None
        assert u.name is None
        assert u.logged_in is False
        assert u.plan_active is False
        assert u.plan is None
        assert u.subscription is None
        assert u.viewing is None
        assert u.groups.issuperset({'logout', 'unsubscribed'})
        assert 'login' not in u.groups
        assert 'subscribed' not in u.groups
        assert 'debug' not in u.groups
开发者ID:riolet,项目名称:SAM,代码行数:30,代码来源:test_user.py


示例2: test_headlesspost

def test_headlesspost():
    decode_calls = []
    perform_calls = []
    encode_calls = []

    class headless_t(sam.pages.base.HeadlessPost):
        def decode_post_request(self, data):
            decode_calls.append(data.copy())
            return {'a': 1, 'b': 2}

        def perform_post_command(self, request):
            perform_calls.append(request.copy())
            return {'z': 9, 'x': 8}

        def encode_post_response(self, response):
            encode_calls.append(response.copy())
            return 'abc', 123

        def require_ownership(self):
            return True

    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        # web.input must be set before instantiating the class
        web.input = lambda: {'postinput': 'test'}
        web.ctx['headers'] = []
        p = headless_t()
        resp = p.POST()

        assert decode_calls == [{'postinput': 'test'}]
        assert perform_calls == [{'a': 1, 'b': 2}]
        assert encode_calls == [{'x': 8, 'z': 9}]
        assert resp == '["abc", 123]'
开发者ID:riolet,项目名称:SAM,代码行数:32,代码来源:test_base.py


示例3: test_post_upload

def test_post_upload():
    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        p = sam.pages.settings.Settings()
        data = {'command': 'upload', 'ds': 'ds12', 'format': 'paloalto', 'file': 'b64,YWJjMTIz'}
        request = p.decode_post_request(data)
        expected = {
            'command': 'upload',
            'ds': 12,
            'format': 'paloalto',
            'file': 'b64,YWJjMTIz'
        }
        assert request == expected
        with pytest.raises(errors.MalformedRequest):
            data = {'command': 'upload', 'format': 'paloalto', 'file': 'b64,YWJjMTIz'}
            p.decode_post_request(data)
        with pytest.raises(errors.MalformedRequest):
            data = {'command': 'upload', 'ds': 'ds12', 'file': 'b64,YWJjMTIz'}
            p.decode_post_request(data)
        with pytest.raises(errors.MalformedRequest):
            data = {'command': 'upload', 'ds': 'ds12', 'format': 'paloalto'}
            p.decode_post_request(data)

        old = sam.models.upload.Uploader
        try:
            sam.models.upload.Uploader = db_connection.Mocker
            p.perform_post_command(expected)
            calls = p.uploadModel.calls
            assert p.uploadModel.constructor == ((db, sub_id, 12, 'paloalto'), {})
            assert calls[0] == ('import_log', ('abc123',), {})
        finally:
            sam.models.upload.Uploader = old
开发者ID:riolet,项目名称:SAM,代码行数:31,代码来源:test_settings.py


示例4: test_headless

def test_headless():
    decode_calls = []
    perform_calls = []
    encode_calls = []

    class headless_t(sam.pages.base.Headless):
        def decode_get_request(self, data):
            decode_calls.append(data.copy())
            return {'a': 1, 'b': 2}

        def perform_get_command(self, request):
            perform_calls.append(request.copy())
            return {'z': 9, 'x': 8}

        def encode_get_response(self, response):
            encode_calls.append(response.copy())
            return 'abc', 123

    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        # web.input must be set before instantiating the class
        web.input = lambda: {'input': True}
        web.ctx['headers'] = []
        p = headless_t()
        resp = p.GET()

        assert decode_calls == [{'input': True}]
        assert perform_calls == [{'a': 1, 'b': 2}]
        assert encode_calls == [{'x': 8, 'z': 9}]
        assert resp == '["abc", 123]'

        with pytest.raises(web.nomethod):
            p.POST()
开发者ID:riolet,项目名称:SAM,代码行数:32,代码来源:test_base.py


示例5: test_alerts_get_decode

def test_alerts_get_decode():
    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        a_page = alerts.Alerts()

        data = {}
        request = a_page.decode_get_request(data)
        expected = {
            'subnet': None,
            'severity': 1,
            'time': 604800,
            'sort': 'id',
            'sort_dir': 'DESC',
            'page_size': 50,
            'page_num': 1,
        }
        assert request == expected

        data = {'subnet': '127.0.0.0/24', 'severity': '6', 'time': '1 day', 'sort': 'severity', 'sort_dir': 'aSC',
                'page_size': '10', 'page_num': '2'}
        request = a_page.decode_get_request(data)
        expected = {
            'subnet': '127.0.0.0/24',
            'severity': 6,
            'time': 86400,
            'sort': 'severity',
            'sort_dir': 'ASC',
            'page_size': 10,
            'page_num': 2,
        }
        assert request == expected
开发者ID:riolet,项目名称:SAM,代码行数:30,代码来源:test_alerts.py


示例6: test_download

def test_download():
    port_f = sam.models.filters.filterTypes.index(sam.models.filters.PortFilter)
    with db_connection.env(mock_input=True, mock_session=True, login_active=False, mock_render=True):
        web.input = lambda: {'download': '1', 'filters': 'ds{ds}|{port};1;2;80'.format(ds=ds_full, port=port_f)}
        web.ctx['headers'] = []
        p = sam.pages.table.Table()
        csv_data = p.GET()
        lines = csv_data.splitlines()
        headers = lines[0]
        lines = lines[1:]
        addresses = [line.partition(',')[0] for line in lines]
        expected = [u'10.0.0.0/8',
                    u'10.20.0.0/16',
                    u'10.20.32.0/24',
                    u'10.20.32.43/32',
                    u'10.24.0.0/16',
                    u'10.24.34.0/24',
                    u'10.24.34.44/32',
                    u'10.24.34.45/32',
                    u'50.0.0.0/8',
                    u'50.64.0.0/16',
                    u'50.64.76.0/24',
                    u'50.64.76.86/32',
                    u'101.0.0.0/8',
                    u'101.99.0.0/16',
                    u'101.99.86.0/24',
                    u'101.99.86.58/32']
        assert addresses == expected
        assert headers == 'Address,Hostname,"Role (0 = client, 1 = server)",' \
                          'Environment,Tags,Bytes handled,Packets handled'
开发者ID:riolet,项目名称:SAM,代码行数:30,代码来源:test_table.py


示例7: test_decode_post

def test_decode_post():
    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        p = sam.pages.nodes.Nodes()
        data = {'node': '10.20.30.40', 'alias': 'test_alias', 'tags': 'tag1,tag2,,', 'env': 'dev_2', 'extra': 'peanuts'}
        request = p.decode_post_request(data)
        expected = {'node': '10.20.30.40', 'alias': 'test_alias', 'tags': 'tag1,tag2,,', 'env': 'dev_2'}
        assert request == expected

        data = {'node': '10.20', 'alias': 'test_alias'}
        request = p.decode_post_request(data)
        expected = {'node': '10.20', 'alias': 'test_alias'}
        assert request == expected

        data = {'node': '10.20', 'tags': 'tag1,tag2,,'}
        request = p.decode_post_request(data)
        expected = {'node': '10.20', 'tags': 'tag1,tag2,,'}
        assert request == expected

        data = {'node': '10.20', 'env': 'smiles'}
        request = p.decode_post_request(data)
        expected = {'node': '10.20', 'env': 'smiles'}
        assert request == expected

        data = {'node': '10.20', 'env': None, 'tags': None, 'alias': None, 'extra': 'peanuts'}
        request = p.decode_post_request(data)
        expected = {'node': '10.20'}
        assert request == expected
开发者ID:riolet,项目名称:SAM,代码行数:27,代码来源:test_nodes.py


示例8: test_rulesedit_get_decode

def test_rulesedit_get_decode():
    reset_dummy_rules()
    r_model = rules.Rules(db, sub_id)
    all_rules = r_model.get_all_rules()
    ids = [rule.id for rule in all_rules]
    # valid requests require a rule id ('id') to be specified

    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        r = RulesEdit()

        # bad data input
        with pytest.raises(errors.RequiredKey):
            data = {}
            r.decode_get_request(data)
        with pytest.raises(errors.MalformedRequest):
            data = {'id': 'not a number'}
            r.decode_get_request(data)

        # good input data

        data = {'id': ids[0]}
        request = r.decode_get_request(data)
        expected = {
            'id': ids[0]
        }
        assert request == expected
开发者ID:riolet,项目名称:SAM,代码行数:26,代码来源:test_rules.py


示例9: test_alertdetails_post_perform

def test_alertdetails_post_perform():
    reset_mock_alerts()
    a_model = AlertsModel(db, sub_id)

    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        a_page = alerts.AlertDetails()

        all_alerts = a_model.get(AlertFilter())
        a_id1 = [a['id'] for a in all_alerts if a['label'] == 'LDAP Access'][0]
        a_id2 = [a['id'] for a in all_alerts if a['label'] != 'LDAP Access'][0]

        request = {
            'method': 'update_label',
            'id': a_id1,
            'label': 'new label'
        }
        a_page.perform_post_command(request)
        request = {
            'method': 'update_label',
            'id': a_id2,
            'label': 'meh label'
        }
        a_page.perform_post_command(request)

        labels = {row['label'] for row in db.select("s{}_Alerts".format(sub_id), what='label')}
        assert labels == {'new label', 'meh label'}
开发者ID:riolet,项目名称:SAM,代码行数:26,代码来源:test_alerts.py


示例10: test_decode_get

def test_decode_get():
    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        p = sam.pages.nodes.Nodes()
        data = {'address': '10.20.30,150.20,59', 'ds': 'ds_13'}
        request = p.decode_get_request(data)
        expected = {'addresses': ['10.20.30', '150.20', '59'], 'flat': False, 'ds': 13}
        assert request == expected

        data = {'address': '50.60.70.80', 'ds': 'ds_13'}
        request = p.decode_get_request(data)
        expected = {'addresses': ['50.60.70.80'], 'flat': False, 'ds': 13}
        assert request == expected

        data = {'ds': '53'}
        request = p.decode_get_request(data)
        expected = {'addresses': [], 'flat': False, 'ds': 53}
        assert request == expected

        data = {'horseradish': 'pickles', 'ds': 'ds_13'}
        request = p.decode_get_request(data)
        expected = {'addresses': [], 'flat': False, 'ds': 13}
        assert request == expected

        data = {'horseradish': 'pickles', 'ds': 'ds_13', 'flat': 'true'}
        request = p.decode_get_request(data)
        expected = {'addresses': [], 'flat': True, 'ds': 13}
        assert request == expected
开发者ID:riolet,项目名称:SAM,代码行数:27,代码来源:test_nodes.py


示例11: test_alerts_post_decode

def test_alerts_post_decode():
    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        a_page = alerts.Alerts()

        data = {}
        with pytest.raises(errors.MalformedRequest):
            a_page.decode_post_request(data)
        data = {'method': 'delete'}
        with pytest.raises(errors.RequiredKey):
            a_page.decode_post_request(data)
        data = {'method': 'delete', 'id': 'NaN'}
        with pytest.raises(errors.RequiredKey):
            a_page.decode_post_request(data)

        data = {'method': 'delete', 'id': 199}
        request = a_page.decode_post_request(data)
        expected = {'method': 'delete', 'id': 199}
        assert request == expected

        data = {'method': 'delete_all'}
        request = a_page.decode_post_request(data)
        expected = {'method': 'delete_all', 'id': None}
        assert request == expected

        data = {'method': 'delete_all', 'id': 201}
        request = a_page.decode_post_request(data)
        expected = {'method': 'delete_all', 'id': None}
        assert request == expected
开发者ID:riolet,项目名称:SAM,代码行数:28,代码来源:test_alerts.py


示例12: test_alertdetails_post_decode

def test_alertdetails_post_decode():
    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        a_page = alerts.AlertDetails()

        data = {}
        with pytest.raises(errors.RequiredKey):
            a_page.decode_post_request(data)
        data = {'method': 'bananas'}
        with pytest.raises(errors.RequiredKey):
            a_page.decode_post_request(data)
        data = {'method': 'update_label'}
        with pytest.raises(errors.RequiredKey):
            a_page.decode_post_request(data)
        data = {'method': 'update_label', 'id': 'non-number', 'label': 'new label'}
        with pytest.raises(errors.RequiredKey):
            a_page.decode_post_request(data)
        data = {'method': 'update_label', 'id': '55'}
        with pytest.raises(errors.RequiredKey):
            a_page.decode_post_request(data)
        data = {'method': 'update_label', 'id': '55', 'label': 'new label'}
        request = a_page.decode_post_request(data)
        expected = {
            'method': 'update_label',
            'id': 55,
            'label': 'new label'
        }
        assert request == expected
开发者ID:riolet,项目名称:SAM,代码行数:27,代码来源:test_alerts.py


示例13: test_decode_connection_string

def test_decode_connection_string():
    with db_connection.env(mock_input=True, mock_session=True):
        p = sam.pages.login.Login_LDAP()
        cs = 'ldaps://ipa.demo1.freeipa.org/CN=users,CN=accounts,DC=demo1,DC=freeipa,DC=org'
        address, ns = p.decode_connection_string(cs)
        assert address == 'ldaps://ipa.demo1.freeipa.org'
        assert ns == 'CN=users,CN=accounts,DC=demo1,DC=freeipa,DC=org'
开发者ID:riolet,项目名称:SAM,代码行数:7,代码来源:test_login.py


示例14: test_connect

def test_connect():
    # TODO: I don't control this test server. This could break.
    with db_connection.env(mock_input=True, mock_session=True):
        p = sam.pages.login.Login_LDAP()
        p.server_address = 'ldaps://ipa.demo1.freeipa.org'
        p.namespace = 'CN=users,CN=accounts,DC=demo1,DC=freeipa,DC=org'

        # wrong password
        request = {
            'user': 'bob',
            'password': 'bobpass'
        }
        with pytest.raises(errors.AuthenticationError):
            p.perform_post_command(request)

        # correct details
        #request = {
        #    'user': 'admin',
        #    'password': 'Secret123',
        #}
        #assert p.perform_post_command(request)

        # wrong url
        p.server_address = 'ldaps://ipa.demo1.garbage.org'
        with pytest.raises(errors.AuthenticationError):
            p.perform_post_command(request)
开发者ID:riolet,项目名称:SAM,代码行数:26,代码来源:test_login.py


示例15: test_perform_post_command

def test_perform_post_command():
    # request can be one of:
    #   {'method': 'enable' | 'disable' | 'reset_all'}
    #   {'method': accept|reject|ignore, 'warning_id': ###}
    #   {'method': reset, 'host': 'a.b.c.d'}
    test_warnings.populate_db_warnings()

    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        ad = ADPlugin()
        ap = model_ap.ADPlugin(db, sub_id)
        ws = ap.get_warnings(show_all=True)
        i = ws[7]['id']

        assert ad.perform_post_command({'method': 'disable'}) == 'success'
        assert ad.perform_post_command({'method': 'enable'}) == 'success'
        assert ad.perform_post_command({'method': 'accept', 'warning_id': i}) == 'success'
        assert ad.perform_post_command({'method': 'reject', 'warning_id': i}) == 'success'
        assert ad.perform_post_command({'method': 'ignore', 'warning_id': i}) == 'success'

        with pytest.raises(errors.MalformedRequest):
            ad.perform_post_command({'method': 'accept', 'warning_id': -200})
        with pytest.raises(errors.MalformedRequest):
            ad.perform_post_command({'method': 'reject', 'warning_id': -200})
        with pytest.raises(errors.MalformedRequest):
            ad.perform_post_command({'method': 'ignore', 'warning_id': -200})
        with pytest.raises(errors.MalformedRequest):
            ad.perform_post_command({'method': 'reset_all'})
        with pytest.raises(errors.MalformedRequest):
            ad.perform_post_command({'method': 'reset', 'host': '10.20.30.40'})
        with pytest.raises(errors.MalformedRequest):
            ad.perform_post_command({'method': 'bogus'})
开发者ID:riolet,项目名称:SAM,代码行数:31,代码来源:test_anomaly_plugin.py


示例16: test_rulesapply_post_perform

def test_rulesapply_post_perform():
    reset_dummy_rules()
    old_submit_job = ruling_process.submit_job
    try:
        ruling_process.submit_job = db_connection.Mocker()
        ruling_process.submit_job._retval = 32768
        with db_connection.env(mock_input=True, login_active=False, mock_session=True):
            r = RulesApply()
            request = {
                'ds': ds_full,
                'start': datetime.fromtimestamp(0),
                'end': datetime.fromtimestamp(2**31 - 1)
            }
            response, job_id = r.perform_post_command(request)
            assert response == 'success'
            calls = ruling_process.submit_job.calls
            assert calls[0][0] == 'self'
            args = calls[0][1]
            rj = args[0]
            assert isinstance(rj, ruling_process.RuleJob)
            # assert rj.id == job_id  # this can't be tested here because I'm overriding the function that
            # assigns the id with a generic mocker that won't
            assert rj.ds_id == ds_full
            assert rj.time_end == request['end']
            assert rj.time_start == request['start']
    finally:
        ruling_process.submit_job = old_submit_job
开发者ID:riolet,项目名称:SAM,代码行数:27,代码来源:test_rules.py


示例17: test_rules_perform

def test_rules_perform():
    reset_dummy_rules()
    r_model = rules.Rules(db, sub_id)
    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        r = Rules()
        response = r.perform_get_command(None)
        assert response == r_model.get_all_rules()
开发者ID:riolet,项目名称:SAM,代码行数:7,代码来源:test_rules.py


示例18: test_logout

def test_logout():
    with db_connection.env(mock_input=True, login_active=True, mock_session=True):
        p = sam.pages.logout.Logout()
        assert p.page.user.logged_in is False
        p.page.user.login_simple('phony', sub)
        assert p.page.user.logged_in is True
        p.perform_get_command({})
        assert p.page.user.logged_in is False
开发者ID:riolet,项目名称:SAM,代码行数:8,代码来源:test_logout.py


示例19: test_filter_protocol_blank

def test_filter_protocol_blank():
    qstring = '/links?address=10,50,110,150,59&filter=&protocol=&tstart=1521498000&tend=1521498300&ds=ds1'
    with db_connection.env(mock_session=True):
        app = web.application(constants.urls)
        req = app.request(qstring)
        assert req.status == "200 OK"
        data = json.loads(req.data)
        assert set(data.keys()) == {u'150', u'59', u'110', u'10', u'50'}
开发者ID:riolet,项目名称:SAM,代码行数:8,代码来源:test_links.py


示例20: test_decode_get_request

def test_decode_get_request():
    # request can be:
    #   (opt) method: "status"(default) | "warnings" | "warning"
    #   if warnings:
    #      (opt) all: "true" | "false"(default)
    #   if warning:
    #      warning_id: r"\d+"
    with db_connection.env(mock_input=True, login_active=False, mock_session=True):
        ad = ADPlugin()
        data = {}
        expected = {'method': 'status'}
        request = ad.decode_get_request(data)
        assert request == expected

        data = {'method': 'status'}
        expected = {'method': 'status'}
        request = ad.decode_get_request(data)
        assert request == expected

        data = {'method': 'warning'}
        with pytest.raises(errors.RequiredKey):
            request = ad.decode_get_request(data)
        data = {'method': 'warning', 'warning_id': ''}
        with pytest.raises(errors.MalformedRequest):
            request = ad.decode_get_request(data)
        data = {'method': 'warning', 'warning_id': 'huh'}
        with pytest.raises(errors.MalformedRequest):
            request = ad.decode_get_request(data)
        
        data = {'method': 'warning', 'warning_id': '12'}
        expected = {'method': 'warning', 'warning_id': 12}
        request = ad.decode_get_request(data)
        assert request == expected

        data = {'method': 'warnings'}
        expected = {'method': 'warnings', 'show_all': False}
        request = ad.decode_get_request(data)
        assert request == expected

        data = {'method': 'warnings', 'all': 'false'}
        expected = {'method': 'warnings', 'show_all': False}
        request = ad.decode_get_request(data)
        assert request == expected

        data = {'method': 'warnings', 'all': 'other'}
        expected = {'method': 'warnings', 'show_all': False}
        request = ad.decode_get_request(data)
        assert request == expected

        data = {'method': 'warnings', 'all': 'true'}
        expected = {'method': 'warnings', 'show_all': True}
        request = ad.decode_get_request(data)
        assert request == expected

        data = {'method': 'warnings', 'all': 'TRUE'}
        expected = {'method': 'warnings', 'show_all': True}
        request = ad.decode_get_request(data)
        assert request == expected
开发者ID:riolet,项目名称:SAM,代码行数:58,代码来源:test_anomaly_plugin.py



注:本文中的spec.python.db_connection.env函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python specfun.klvnzo函数代码示例发布时间:2022-05-27
下一篇:
Python helpers.failure函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap