Ejemplo n.º 1
0
    def test_rule(self):

        req_body = {'user_id': '111'}

        # 命中bool型策略
        control, weight = calculate_rule(id_=self.rule_id, req_body=req_body)
        self.assertEquals(control, 'log')
        self.assertEquals(weight, 90)

        # 命中名单型策略
        add_element_to_menu(event_code=self.event_code, menu_type='black', dimension='user_id', element='111')
        control, weight = calculate_rule(id_=self.rule_id, req_body=req_body)
        self.assertEquals(control, 'deny')
        self.assertEquals(weight, 100)
Ejemplo n.º 2
0
    def _test_destroy(self):

        # 参数不全
        resp = self.client.post(reverse(self.delete_uri))
        self.assertEquals(resp.status_code, 200)
        self.assertEquals(json.loads(resp.content)['error'], u"id不合法")

        # ID格式错误
        menu_element_id = get_sample_str(24)
        resp = self.client.post(reverse(self.delete_uri),
                                data={'ids': menu_element_id})
        self.assertEquals(resp.status_code, 200)
        self.assertEquals(json.loads(resp.content)['error'], u"id不合法")

        # 成功删除
        menu_element_id = add_element_to_menu(self.event_code, 'black',
                                              self.dimension, 'test_value')
        resp = self.client.post(reverse(self.delete_uri), data={'ids': menu_element_id})
        self.assertEquals(resp.status_code, 200)
        t = json.loads(resp.content)
        self.assertEquals(t['state'], True)

        # 删除不存在的记录
        resp = self.client.post(reverse(self.delete_uri),
                                data={'ids': menu_element_id})
        self.assertEquals(resp.status_code, 200)
        t = json.loads(resp.content)
        self.assertEquals(t['error'], u"记录均不存在")
Ejemplo n.º 3
0
    def test_hit_menu(self):
        event_code = create_menu_event()['event_code']
        sp_element = '111111'

        dimension = 'user_id'
        menu_type = 'black'
        add_element_to_menu(event_code=event_code,
                            menu_type=menu_type,
                            dimension=dimension,
                            element=sp_element)
        req_body = {'user_id': sp_element}
        self.assertEquals(
            hit_menu(req_body,
                     'is',
                     event_code,
                     dimension=dimension,
                     menu_type=menu_type), True)

        self.assertEquals(
            hit_menu(req_body,
                     'is_not',
                     event_code,
                     dimension=dimension,
                     menu_type=menu_type), False)

        self.assertEquals(
            hit_menu(req_body,
                     'is',
                     '',
                     dimension=dimension,
                     menu_type=menu_type), False)

        req_body['user_id'] = 'error_user_id'
        self.assertEquals(
            hit_menu(req_body,
                     'is',
                     event_code,
                     dimension=dimension,
                     menu_type=menu_type), False)

        req_body.clear()
        self.assertEquals(
            hit_menu(req_body,
                     'is',
                     event_code,
                     dimension=dimension,
                     menu_type=menu_type), False)
Ejemplo n.º 4
0
    def _test_test(self):
        super(TestMenuStrategyView, self)._test_test()

        sp_ip = '1.1.1.1'
        data = {
            'req_body': json.dumps({'ip': sp_ip}),
            'strategy': self.strategy_uuid
        }
        response = self.client.post(reverse(self.test_uri), data=data)
        self.assertEquals(response.status_code, 200)
        resp_dict = json.loads(response.content)
        self.assertIs(resp_dict['state'], True)
        self.assertEquals(resp_dict['data'], u'不命中')

        add_element_to_menu(event_code=self.event_code,
                            element=sp_ip,
                            menu_type='black',
                            dimension='ip')

        response = self.client.post(reverse(self.test_uri), data=data)
        self.assertEquals(response.status_code, 200)
        resp_dict = json.loads(response.content)
        self.assertIs(resp_dict['state'], True)
        self.assertEquals(resp_dict['data'], u'命中')
Ejemplo n.º 5
0
    def handle(self, *args, **options):
        # 创建普通用户
        create_user(email='*****@*****.**',
                    username='******',
                    password='******',
                    is_superuser=False)

        # 创建名单
        event_code = 'init_event'
        create_menu_event(event_code=event_code, event_name='初始项目')
        add_element_to_menu(event_code,
                            menu_type='black',
                            dimension='user_id',
                            element='111111')
        add_element_to_menu(event_code,
                            menu_type='white',
                            dimension='uid',
                            element=hashlib.md5(
                                'white_uid'.encode()).hexdigest())
        add_element_to_menu(event_code,
                            menu_type='gray',
                            dimension='ip',
                            element='1.1.1.1')
        add_element_to_menu(event_code,
                            menu_type='black',
                            dimension='phone',
                            element=hashlib.md5(
                                '12345678901'.encode()).hexdigest())
        add_element_to_menu(event_code,
                            menu_type='black',
                            dimension='pay',
                            element=hashlib.md5(
                                'pay_account'.encode()).hexdigest())

        # 创建策略
        # 名单型策略
        menu_strategy_name_1 = '用户在初始项目的用户黑名单中'
        menu_uuid_1 = create_menu_strategy(event_code,
                                           dimension='user_id',
                                           menu_type='black',
                                           menu_op='is',
                                           strategy_name=menu_strategy_name_1,
                                           strategy_desc='初始黑名单策略')
        menu_strategy_name_2 = 'uid在初始项目的设备白名单中'
        menu_uuid_2 = create_menu_strategy(event_code,
                                           dimension='uid',
                                           menu_type='white',
                                           menu_op='is',
                                           strategy_name=menu_strategy_name_2,
                                           strategy_desc='初始白名单策略')
        menu_strategy_name_3 = 'IP在初始项目的IP灰名单中'
        menu_uuid_3 = create_menu_strategy(event_code,
                                           dimension='ip',
                                           menu_type='gray',
                                           menu_op='is',
                                           strategy_name=menu_strategy_name_3,
                                           strategy_desc='初始灰名单策略')

        # Bool型策略
        bool_strategy_name_1 = '用户是异常用户'
        bool_uuid_1 = create_bool_strategy(strategy_var='user_id',
                                           strategy_op='is',
                                           strategy_func='is_abnormal',
                                           strategy_threshold='',
                                           strategy_name=bool_strategy_name_1,
                                           strategy_desc=bool_strategy_name_1)
        bool_strategy_name_2 = '用户登录次数大于50次'
        bool_uuid_2 = create_bool_strategy(strategy_var='user_id',
                                           strategy_op='gt',
                                           strategy_func='user_login_count',
                                           strategy_threshold='50',
                                           strategy_name=bool_strategy_name_2,
                                           strategy_desc=bool_strategy_name_2)
        # 数据源相关策略
        # 创建数据源
        source_key = 'init_source_key'
        create_data_source(source_key=source_key,
                           source_name='初始样例数据源',
                           fields=['user_id', 'uid', 'ip', 'phone'])

        # 时段频控型策略
        freq_strategy_name = '相同uid,24小时内限10次(初始样例数据源)'
        freq_uuid = create_freq_strategy(strategy_source=source_key,
                                         strategy_body='uid',
                                         strategy_time=24 * 3600,
                                         strategy_limit=10,
                                         strategy_name=freq_strategy_name,
                                         strategy_desc='初始时段频控型策略')
        # 限用户数型策略
        user_strategy_name = '同一设备当天内限10个用户(初始样例数据源)'
        user_uuid = create_user_strategy(strategy_source=source_key,
                                         strategy_body='uid',
                                         strategy_day=1,
                                         strategy_limit=10,
                                         strategy_name=user_strategy_name,
                                         strategy_desc='初始时段频控型策略')

        # 规则相关
        strategy_confs = [
            [
                ';'.join((menu_strategy_name_1, menu_strategy_name_2,
                          menu_strategy_name_3)), ';'.join(
                              (menu_uuid_1, menu_uuid_2, menu_uuid_3)), 'deny',
                '此用户命中了名单型策略', '100'
            ],
            [
                ';'.join((bool_strategy_name_1, bool_strategy_name_2)),
                ';'.join(
                    (bool_uuid_1, bool_uuid_2)), 'log', '此用户命中了布尔型策略', '90'
            ],
            [freq_strategy_name, freq_uuid, 'number', '此用户命中了时段频控型策略', '80'],
            [user_strategy_name, user_uuid, 'verify', '此用户命中了限用户数型策略', '80'],
        ]
        create_rule(strategy_confs=strategy_confs,
                    title='初始规则',
                    describe='初始样例规则',
                    status='on',
                    creator_name='超级管理员')
Ejemplo n.º 6
0
    def _test_create(self):
        create_url = reverse(self.create_url)

        # 初始页面
        response = self.client.get(create_url)
        self.assertEquals(response.status_code, 200)

        # 创建规则

        # 无参数
        response = self.client.post(create_url)
        self.assertEquals(response.status_code, 200)
        resp_json = json.loads(response.content)
        self.assertEquals(resp_json['state'], False)

        # 正常创建
        event_code = create_menu_event()['event_code']
        add_element_to_menu(event_code=event_code,
                            menu_type='black',
                            dimension='user_id',
                            element='111111')

        # user_id 在 event_code 所代表的黑名单中
        self.menu_strategy_uuid = create_menu_strategy(event_code=event_code,
                                                       dimension='user_id',
                                                       menu_type='black',
                                                       menu_op='is')

        # 相同uid,当天限1个用户
        data_source_key = create_data_source()
        self.user_strategy_uuid = create_user_strategy(
            strategy_source=data_source_key,
            strategy_body='uid',
            strategy_day=1,
            strategy_limit=1)

        # 用户是异常用户
        self.bool_strategy_uuid = create_bool_strategy(
            strategy_var='user_id',
            strategy_op='is',
            strategy_func='is_abnormal',
            strategy_threshold='')

        # 相同uid,在最近86400s内,限1次
        self.freq_strategy_uuid = create_freq_strategy(
            strategy_source=data_source_key,
            strategy_body='uid',
            strategy_time=24 * 60 * 60,
            strategy_limit=1)

        self.valid_post_data = {
            'title':
            get_sample_str(10),
            'describe':
            get_sample_str(8),
            'status':
            'on',
            'end_time': (datetime.today() +
                         timedelta(days=100)).strftime('%Y-%m-%d %H:%M:%S'),
            'strategys':
            ','.join([
                self.menu_strategy_uuid, self.user_strategy_uuid,
                self.bool_strategy_uuid, self.freq_strategy_uuid
            ]),
            'controls':
            ','.join(['deny', 'log', 'number', 'pass']),
            'customs':
            ':::'.join([get_sample_str()] * 4),
            'names':
            ':::'.join([get_sample_str()] * 4),
            'weights':
            ','.join(['100', '90', '80', '70'])
        }
        response = self.client.post(create_url, data=self.valid_post_data)
        self.assertEquals(response.status_code, 200)
        resp_json = json.loads(response.content)
        self.assertEquals(resp_json['state'], True)
        self.rule_uuid = resp_json['uuid']