Пример #1
0
 def handle_action(self, action, data):
     api = Api(protocol)
     request = dict(data, action=action)
     action_name, data = api.handle_request(json.dumps(request))
     response = actions.handle_action(action_name, dict(data))
     api.handle_response(action_name, dict(response))
     return response
Пример #2
0
 def handle_action(self, action, data, req_info):
     api = Api(protocol)
     request = dict(data, action=action)
     action_name, data = api.handle_request(json.dumps(request))
     response = actions.handle_action(action_name, dict(data), req_info)
     api.handle_response(action_name, dict(response))
     return response
Пример #3
0
class Application(object):
    def __init__(self, action_handler, protocol, logger, tracking_api_calls=()):
        self.action_handler = action_handler
        self.logger = logger
        self.helix_api = HelixApi(protocol)
        self.tracking_api_calls = tracking_api_calls
        self.logger.info('WSGI application %s started', self.__class__.__name__)

    def track_api_call(self, s_req, s_resp, remote_addr,
        action_name, authorized_data):
        pass

    def _remote_addr(self, environ):
        remote_addr = environ.get('HTTP_X_FORWARDED_FOR')
        if remote_addr is None:
            remote_addr = environ.get('REMOTE_ADDR', 'undefined')
        return remote_addr

    def __call__(self, environ, start_response):
        raw_data = environ['wsgi.input'].read()
        remote_addr = self._remote_addr(environ)

        action_name = None
        processed_action_data = {}
        secured_request = {}
        secured_response = {}

        try:
            action_name, action_data = self.helix_api.handle_request(raw_data)
            secured_request = self._secured_request(action_name, action_data)
            self.logger.debug('Request from %s: %s' % (remote_addr, secured_request))

            processed_action_data = dict(action_data)
            req_info = RequestInfo(remote_addr=remote_addr)
            raw_response = self.action_handler(action_name, processed_action_data,
                req_info)

            secured_response = security.sanitize_credentials(raw_response)
            self.logger.log(logging.DEBUG, 'Response to %s: %s' % (remote_addr, secured_response))

            response = self.helix_api.handle_response(action_name, raw_response)
        except ValidationError, e:
            action_name, action_data = self.helix_api.handle_request(raw_data, validation=False)
            secured_request = self._secured_request(action_name, action_data)

            raw_response = response_error(e)
            response = self.helix_api.handle_response(action_name, raw_response, validation=False)
            self.logger.log(logging.ERROR, 'Request from %s: %s' % (remote_addr, secured_request))
            secured_response = security.sanitize_credentials(raw_response)
            self.logger.log(logging.ERROR, 'Response to %s: %s. Error: %s' % (remote_addr, secured_response,
                ';'.join(e.args)))
        except RequestProcessingError, e:
            raw_response = response_error(e)
            response = self.helix_api.handle_response(action_name, raw_response, validation=False)
            self.logger.log(logging.ERROR, 'Request from %s: %s' % (remote_addr, secured_request))
            secured_response = security.sanitize_credentials(raw_response)
            self.logger.log(logging.ERROR, 'Response to %s: %s. Error: %s' % (remote_addr, secured_response,
                ';'.join(e.args)))
Пример #4
0
    def test_request_ok(self):
        good_data = {
            'action': 'add_currency',
            'name': 'USD',
            'designation': '$',
            'cent_factor': 100,
        }

        raw_data = json.dumps(good_data)
        protocol = [
            ApiCall('add_currency_request', {'name': Text(), 'designation': Text(), 'cent_factor': int}),
            ApiCall('add_currency_response', {'status': Text()}),
        ]
        test_api = Api(protocol)
        action_name, data = test_api.handle_request(raw_data)
        self.assertEquals(action_name, good_data.pop('action'))
        self.assertEquals(data, good_data)

        good_response = {'status': 'ok'}
        actual_response = json.loads(test_api.handle_response(action_name, good_response))
        self.assertEquals(good_response, actual_response)
Пример #5
0
class ProtocolTestCase(RootTestCase, ProtocolTester):
    api = Api(protocol)

    def test_login(self):
        a_name = 'login'
        self.api.validate_request(
            a_name, {
                'login': '******',
                'password': '******',
                'environment_name': 'e',
                'custom_actor_info': 'i'
            })
        self.api.validate_request(a_name, {
            'login': '******',
            'password': '******',
            'environment_name': 'n'
        })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'session_id': 'i',
            'user_id': 5,
            'environment_id': 7
        })
        self.validate_error_response(a_name)

    def test_logout(self):
        a_name = 'logout'
        self.api.validate_request(a_name, {'session_id': 'i'})
        self.validate_status_response(a_name)

    def test_get_currencies(self):
        a_name = 'get_currencies'
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'code': 'XXX'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {},
                'ordering_params': ['code']
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {},
                'ordering_params': ['-code']
            })

        self.api.validate_response(a_name, {'status': 'ok', 'currencies': []})
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'currencies': [{
                    'id': 1,
                    'code': 'YYY',
                    'name': 'y',
                    'location': 'y',
                    'cent_factor': 100
                }]
            })
        self.validate_error_response(a_name)

    def test_get_used_currencies(self):
        a_name = 'get_used_currencies'
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'code': 'XXX'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {},
                'ordering_params': ['code']
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {},
                'ordering_params': ['-code']
            })

        self.api.validate_response(a_name, {'status': 'ok', 'currencies': []})
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'currencies': [{
                    'id': 1,
                    'code': 'YYY',
                    'name': 'y',
                    'location': 'y',
                    'cent_factor': 100
                }]
            })
        self.validate_error_response(a_name)

    def test_modify_used_currencies(self):
        a_name = 'modify_used_currencies'
        self.api.validate_request(a_name, {'session_id': 's'})
        self.api.validate_request(a_name, {
            'session_id': 's',
            'new_currencies_codes': []
        })
        self.api.validate_request(a_name, {
            'session_id': 's',
            'new_currencies_codes': ['XXX', 'RUR', 'RUB']
        })

        self.validate_status_response(a_name)

    def test_get_action_logs(self):
        a_name = 'get_action_logs'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                },
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                },
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'from_request_date': '2011-02-21 00:00:00',
                    'to_request_date': '2011-02-21 23:59:59'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'action': 'a'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'user_id': 1
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'session_id': ''
                },
                'paging_params': {}
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'action_logs': []
        })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                4,
                'action_logs': [
                    {
                        'id': 42,
                        'session_id': 's_id',
                        'custom_actor_info': None,
                        'subject_users_ids': [3],
                        'actor_user_id': 1,
                        'action': 'a',
                        'request_date': '%s' % datetime.datetime.now(pytz.utc),
                        'remote_addr': '127.0.0.1',
                        'request': 'req',
                        'response': 'resp'
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_get_action_logs_self(self):
        a_name = 'get_action_logs_self'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                },
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                },
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'from_request_date': '2011-02-21 00:00:00',
                    'to_request_date': '2011-02-21 23:59:59'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'action': 'a'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'session_id': ''
                },
                'paging_params': {}
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'action_logs': []
        })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                4,
                'action_logs': [
                    {
                        'id': 42,
                        'session_id': 's_id',
                        'custom_actor_info': None,
                        'subject_users_ids': [3],
                        'actor_user_id': 1,
                        'action': 'a',
                        'request_date': '%s' % datetime.datetime.now(pytz.utc),
                        'remote_addr': '127.0.0.1',
                        'request': 'req',
                        'response': 'resp'
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_add_balance(self):
        a_name = 'add_balance'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'user_id': 23,
            'currency_code': 'YYY'
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'user_id': 23,
                'is_active': True,
                'currency_code': 'YYY',
                'overdraft_limit': '500.50'
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'user_id': 23,
                'is_active': True,
                'currency_code': 'YYY',
                'overdraft_limit': '500.50'
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'user_id': 23,
                'is_active': False,
                'currency_code': 'YYY',
                'overdraft_limit': '500.50'
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'user_id': 23,
                'is_active': False,
                'currency_code': 'YYY',
                'overdraft_limit': '500.50'
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'user_id': 23,
                'is_active': False,
                'currency_code': 'YYY',
                'overdraft_limit': '500.50',
                'check_user_exist': True
            })

        self.api.validate_response(a_name, {'status': 'ok', 'id': 1})
        self.validate_error_response(a_name)

    def test_modify_balances(self):
        a_name = 'modify_balances'
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'ids': [2],
                'new_is_active': True,
                'new_overdraft_limit': '500.50'
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'ids': [2, 3, 4],
                'new_is_active': True,
                'new_overdraft_limit': '500.50'
            })

        self.validate_status_response(a_name)

    def test_get_balances_self(self):
        a_name = 'get_balances_self'
        self.api.validate_request(a_name, {'session_id': 'i'})

        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'balances': [{
                    'id': 2,
                    'user_id': 3,
                    'is_active': True,
                    'currency_code': 'RUB',
                    'real_amount': '3.15',
                    'virtual_amount': '0.0',
                    'locked_amount': '14.09',
                    'overdraft_limit': '0.14',
                }],
                'total':
                1,
            })
        self.validate_error_response(a_name)

    def test_get_balances(self):
        a_name = 'get_balances'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                }
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'users_ids': [],
                    'is_active': True,
                    'currency_code': 'XXX',
                    'from_real_amount': '1.0',
                    'to_real_amount': '2.01',
                    'from_virtual_amount': '7.10',
                    'to_virtual_amount': '19.01',
                    'from_overdraft_limit': '600.17',
                    'to_overdraft_limit': '700',
                    'from_locked_amount': '41.24',
                    'to_locked_amount': '50',
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'ids': [2, 3]
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 2
                },
                'paging_params': {},
                'ordering_params': ['currency_id']
            })

        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'balances': [
                    {
                        'id': 22,
                        'user_id': 4,
                        'is_active': True,
                        'currency_code': 'RU',
                        'real_amount': '3.15',
                        'virtual_amount': '0.0',
                        'locked_amount': '14.09',
                        'overdraft_limit': '0.14',
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_add_receipt(self):
        a_name = 'add_receipt'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'balance_id': 23,
            'amount': '44'
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'balance_id': 23,
                'amount': '44.42',
                'info': {
                    'payment_system': 'YaD'
                }
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'transaction_id': 1
        })
        self.validate_error_response(a_name)

    def test_add_bonus(self):
        a_name = 'add_bonus'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'balance_id': 23,
            'amount': '44'
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'balance_id': 23,
                'amount': '44.42',
                'info': {
                    'reason': 'beauty eyes'
                }
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'transaction_id': 1
        })
        self.validate_error_response(a_name)

    def test_lock(self):
        a_name = 'lock'
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'balance_id': 23,
                'amount': '44',
                'order_id': 'o_44',
                'locking_order': ['real_amount', 'virtual_amount']
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'balance_id': 23,
                'amount': '44.42',
                'order_id': 'o_45',
                'locking_order': ['real_amount'],
                'info': {
                    'reason': 'beauty eyes'
                }
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'transaction_id': 1,
            'lock_id': 2
        })
        self.validate_error_response(a_name)

    def test_get_locks(self):
        a_name = 'get_locks'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                }
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 1,
                    'ids': [1, 2],
                    'user_id': 3,
                    'balance_id': 5,
                    'currency_code': 'XXX',
                    'order_id': 'o1',
                    'from_real_amount': '1.0',
                    'to_real_amount': '2.01',
                    'from_virtual_amount': '7.10',
                    'to_virtual_amount': '19.01',
                    'from_creation_date': '2011-02-21 00:00:00',
                    'to_creation_date': '2011-02-21 23:59:59',
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 2
                },
                'paging_params': {},
                'ordering_params': ['id', '-id']
            })

        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'locks': [
                    {
                        'id': 22,
                        'user_id': 4,
                        'balance_id': 5,
                        'currency_code': 'RUB',
                        'creation_date': '2011-02-21 00:00:00',
                        'real_amount': '3.15',
                        'virtual_amount': '0.0',
                        'info': {
                            'p0': 'v0',
                            'p1': 'v1'
                        },
                        'order_id': 'o4'
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_get_locks_self(self):
        a_name = 'get_locks_self'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                }
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 1,
                    'ids': [1, 2],
                    'balance_id': 5,
                    'currency_code': 'XXX',
                    'order_id': 'o1',
                    'from_real_amount': '1.0',
                    'to_real_amount': '2.01',
                    'from_virtual_amount': '7.10',
                    'to_virtual_amount': '19.01',
                    'from_creation_date': '2011-02-21 00:00:00',
                    'to_creation_date': '2011-02-21 23:59:59',
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 2
                },
                'paging_params': {},
                'ordering_params': ['id', '-id']
            })

        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'locks': [
                    {
                        'id': 22,
                        'user_id': 4,
                        'balance_id': 5,
                        'currency_code': 'RUB',
                        'creation_date': '2011-02-21 00:00:00',
                        'real_amount': '3.15',
                        'virtual_amount': '0.0',
                        'info': {
                            'p0': 'v0',
                            'p1': 'v1'
                        },
                        'order_id': '555'
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_unlock(self):
        a_name = 'unlock'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'balance_id': 1,
            'lock_id': 23
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'balance_id': 1,
                'lock_id': 23,
                'info': {
                    'reason': 'order canceled'
                }
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'transaction_id': 1
        })
        self.validate_error_response(a_name)

    def test_charge_off(self):
        a_name = 'charge_off'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'balance_id': 1,
            'lock_id': 23
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'balance_id': 1,
                'lock_id': 23,
                'info': {
                    'reason': 'order canceled'
                }
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'transaction_id': 1
        })
        self.validate_error_response(a_name)

    def test_get_transactions(self):
        a_name = 'get_transactions'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                }
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 1,
                    'ids': [1, 2],
                    'balance_id': 5,
                    'user_id': 3,
                    'from_creation_date': '2011-02-21 00:00:00',
                    'to_creation_date': '2011-02-21 23:59:59',
                    'type': 'charge_off'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 2
                },
                'paging_params': {},
                'ordering_params': ['id', '-id']
            })

        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'transactions': [
                    {
                        'id': 22,
                        'user_id': 4,
                        'balance_id': 5,
                        'currency_code': 'RUB',
                        'creation_date': '2011-02-21 00:00:00',
                        'real_amount': '3.15',
                        'virtual_amount': '0.0',
                        'type': 'bonus',
                        'order_id': '5',
                        'info': {
                            'p0': 'v0',
                            'p1': 'v1'
                        },
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_get_transactions_self(self):
        a_name = 'get_transactions_self'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                }
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 1,
                    'ids': [1, 2],
                    'balance_id': 5,
                    'from_creation_date': '2011-02-21 00:00:00',
                    'to_creation_date': '2011-02-21 23:59:59',
                    'type': 'charge_off'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 2
                },
                'paging_params': {},
                'ordering_params': ['id', '-id']
            })

        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'transactions': [
                    {
                        'id': 22,
                        'user_id': 4,
                        'balance_id': 5,
                        'currency_code': 'RUB',
                        'creation_date': '2011-02-21 00:00:00',
                        'real_amount': '3.15',
                        'virtual_amount': '0.0',
                        'type': 'bonus',
                        'order_id': '5',
                        'info': {
                            'p0': 'v0',
                            'p1': 'v1'
                        },
                    },
                ]
            })
        self.validate_error_response(a_name)
Пример #6
0
class ProtocolTestCase(RootTestCase, ProtocolTester):
    api = Api(protocol)

    def test_login(self):
        a_name = 'login'
        self.api.validate_request(
            a_name, {
                'login': '******',
                'password': '******',
                'environment_name': 'e',
                'custom_actor_info': 'i'
            })
        self.api.validate_request(a_name, {
            'login': '******',
            'password': '******',
            'environment_name': 'n'
        })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'session_id': 'i',
            'user_id': 5,
            'environment_id': 7
        })
        self.validate_error_response(a_name)

    def test_logout(self):
        a_name = 'logout'
        self.api.validate_request(a_name, {'session_id': 'i'})
        self.validate_status_response(a_name)

    def test_add_tariffication_object(self):
        a_name = 'add_tariffication_object'
        self.api.validate_request(a_name, {'session_id': 's', 'name': 'one'})
        self.api.validate_request(a_name, {
            'session_id': 's',
            'name': u'лунный свет'
        })
        self.api.validate_response(a_name, {'status': 'ok', 'id': 1})
        self.validate_error_response(a_name)

    def test_get_tariffication_objects(self):
        a_name = 'get_tariffication_objects'
        self.api.validate_request(a_name, {
            'session_id': 'i',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 'i',
                'filter_params': {
                    'id': 1,
                    'ids': [1, 2],
                    'name': 'lala'
                },
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                }
            })

        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'tariffication_objects': [
                    {
                        'id': 1,
                        'name': 'one'
                    },
                    {
                        'id': 2,
                        'name': 'two'
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_modify_tariffication_object(self):
        a_name = 'modify_tariffication_object'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'id': 1,
            'new_name': 'one'
        })
        self.validate_status_response(a_name)

    def test_delete_tariffication_object(self):
        a_name = 'delete_tariffication_object'
        self.api.validate_request(a_name, {'session_id': 's', 'id': 1})
        self.validate_status_response(a_name)

    def test_get_action_logs(self):
        a_name = 'get_action_logs'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                },
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                },
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'from_request_date': '2011-02-21 00:00:00',
                    'to_request_date': '2011-02-21 23:59:59'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'action': 'a'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'user_id': 1
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'session_id': ''
                },
                'paging_params': {}
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'action_logs': []
        })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                4,
                'action_logs': [
                    {
                        'id': 42,
                        'session_id': 's_id',
                        'custom_actor_info': None,
                        'subject_users_ids': [3],
                        'actor_user_id': 1,
                        'action': 'a',
                        'request_date': '%s' % datetime.datetime.now(pytz.utc),
                        'remote_addr': '127.0.0.1',
                        'request': 'req',
                        'response': 'resp'
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_get_action_logs_self(self):
        a_name = 'get_action_logs_self'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                },
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {
                    'limit': 0,
                    'offset': 0,
                },
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'from_request_date': '2011-02-21 00:00:00',
                    'to_request_date': '2011-02-21 23:59:59'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'action': 'a'
                },
                'paging_params': {}
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'session_id': ''
                },
                'paging_params': {}
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'action_logs': []
        })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                4,
                'action_logs': [
                    {
                        'id': 42,
                        'session_id': 's_id',
                        'custom_actor_info': None,
                        'subject_users_ids': [3],
                        'actor_user_id': 1,
                        'action': 'a',
                        'request_date': '%s' % datetime.datetime.now(pytz.utc),
                        'remote_addr': '127.0.0.1',
                        'request': 'req',
                        'response': 'resp'
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_add_tariff(self):
        a_name = 'add_tariff'
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'name': 'one',
                'parent_tariff_id': None,
                'type': Tariff.TYPE_PERSONAL,
                'status': Tariff.STATUS_ACTIVE
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'name': 'one',
                'parent_tariff_id': 1,
                'currency': 'RUB',
                'type': Tariff.TYPE_PUBLIC,
                'status': Tariff.STATUS_INACTIVE
            })

        self.api.validate_response(a_name, {'status': 'ok', 'id': 1})
        self.validate_error_response(a_name)

    def test_get_tariffs(self):
        a_name = 'get_tariffs'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'id': 1,
                    'ids': [1, 2],
                    'name': 't',
                    'type': Tariff.TYPE_PERSONAL,
                    'status': Tariff.STATUS_ARCHIVE
                },
                'paging_params': {
                    'limit': 0
                }
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'tariffs': []
        })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'tariffs': [{
                    'id':
                    1,
                    'name':
                    't0',
                    'parent_tariffs': [{
                        'id': 1,
                        'name': 'pt0',
                        'status': Tariff.STATUS_ACTIVE,
                        'currency': 'RUB'
                    }],
                    'type':
                    Tariff.TYPE_PUBLIC,
                    'status':
                    Tariff.STATUS_ACTIVE,
                    'currency':
                    'RUB'
                }]
            })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'tariffs': [
                    {
                        'id':
                        1,
                        'name':
                        't0',
                        'parent_tariffs': [{
                            'id': 2,
                            'name': 'pt2',
                            'status': Tariff.STATUS_ACTIVE,
                            'currency': None
                        }, {
                            'id': 3,
                            'name': 'pt3',
                            'status': Tariff.STATUS_ARCHIVE,
                            'currency': 'RUB'
                        }],
                        'type':
                        Tariff.TYPE_PUBLIC,
                        'status':
                        Tariff.STATUS_ACTIVE,
                        'currency':
                        'RUB'
                    },
                    {
                        'id':
                        1,
                        'name':
                        't0',
                        'parent_tariffs': [{
                            'id': 1,
                            'name': 'pt0',
                            'status': Tariff.STATUS_ACTIVE,
                            'currency': None
                        }],
                        'type':
                        Tariff.TYPE_PUBLIC,
                        'status':
                        Tariff.STATUS_ACTIVE,
                        'currency':
                        None
                    },
                ]
            })
        self.validate_error_response(a_name)

    def test_modify_tariff(self):
        a_name = 'modify_tariff'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'id': 1,
            'new_name': 'n'
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'id': 1,
                'new_name': 'n',
                'new_status': Tariff.STATUS_ARCHIVE,
                'new_type': Tariff.TYPE_PERSONAL,
                'new_parent_tariff_id': 1,
                'new_currency': 'RUB'
            })
        self.api.validate_request(a_name, {
            'session_id': 's',
            'id': 1,
            'new_currency': None
        })
        self.validate_status_response(a_name)

    def test_delete_tariff(self):
        a_name = 'delete_tariff'
        self.api.validate_request(a_name, {'session_id': 's', 'id': 1})
        self.validate_status_response(a_name)

    def test_add_viewing_tariff_context(self):
        a_name = 'add_tariff_viewing_context'
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'name': None,
                'tariff_id': 1,
                'view_order': 2,
                'context': []
            })
        self.api.validate_request(
            a_name, {
                'session_id':
                's',
                'name':
                None,
                'tariff_id':
                1,
                'view_order':
                2,
                'context': [
                    {
                        'name': 'num',
                        'value': 2
                    },
                    {
                        'name': 'num',
                        'value': 3
                    },
                    {
                        'name': 'name',
                        'value': 'n'
                    },
                ]
            })

        self.api.validate_response(a_name, {'status': 'ok', 'id': 1})
        self.validate_error_response(a_name)

    def test_get_tariff_viewing_contexts(self):
        a_name = 'get_tariff_viewing_contexts'
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'tariff_id': 4
                },
                'paging_params': {},
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'id': 1,
                    'ids': [1, 2],
                    'tariff_id': 2
                },
                'paging_params': {
                    'limit': 0
                },
                'ordering_params': ['view_order']
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'tariff_contexts': []
        })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'tariff_contexts': [{
                    'id':
                    1,
                    'name':
                    't0',
                    'tariff_id':
                    3,
                    'view_order':
                    3,
                    'context': [
                        {
                            'name': 'n',
                            'value': 'v'
                        },
                        {
                            'name': 'm',
                            'value': 1
                        },
                    ]
                }]
            })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'tariff_contexts': [{
                    'id':
                    1,
                    'name':
                    't0',
                    'tariff_id':
                    3,
                    'view_order':
                    3,
                    'context': [
                        {
                            'name': 'n',
                            'value': 'v'
                        },
                        {
                            'name': 'm',
                            'value': 1
                        },
                    ]
                }, {
                    'id':
                    1,
                    'name':
                    't0',
                    'tariff_id':
                    3,
                    'view_order':
                    4,
                    'context': [
                        {
                            'name': 'n',
                            'value': 'v'
                        },
                        {
                            'name': 'm',
                            'value': 1
                        },
                    ]
                }]
            })
        self.validate_error_response(a_name)

    def test_modify_viewing_tariff_context(self):
        a_name = 'modify_tariff_viewing_context'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'id': 1,
            'new_name': 'n'
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'id': 1,
                'new_name': 'n',
                'new_tariff_id': 4,
                'new_view_order': 3,
                'new_context': [
                    {
                        'name': 'num',
                        'value': 2
                    },
                ]
            })

        self.validate_status_response(a_name)

    def test_delete_viewing_tariff_context(self):
        a_name = 'delete_tariff_viewing_context'
        self.api.validate_request(a_name, {'session_id': 's', 'id': 1})
        self.validate_status_response(a_name)

    def test_save_rules(self):
        a_name = 'save_rule'
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'tariff_id': 1,
                'tariffication_object_id': 3,
                'draft_rule': 'p',
                'status': Rule.STATUS_ACTIVE
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'tariff_id': 1,
                'tariffication_object_id': 3,
                'draft_rule': 'p',
                'status': Rule.STATUS_ACTIVE,
                'view_order': 0
            })

        self.api.validate_response(a_name, {'status': 'ok', 'id': 1})
        self.validate_error_response(a_name)

    def test_get_rules(self):
        a_name = 'get_rules'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'id': 1,
                    'ids': [1, 2],
                    'tariff_id': 1,
                    'tariffication_object_id': 1,
                    'status': Rule.STATUS_ACTIVE
                },
                'paging_params': {
                    'limit': 0
                }
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'rules': []
        })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'rules': [{
                    'id': 1,
                    'tariff_id': 1,
                    'tariff_name': 't0',
                    'tariffication_object_id': 2,
                    'tariffication_object_name': 'to0',
                    'rule': None,
                    'draft_rule': 'rule',
                    'status': Rule.STATUS_ACTIVE,
                    'view_order': 0
                }, {
                    'id': 1,
                    'tariff_id': 1,
                    'tariff_name': 't0',
                    'tariffication_object_id': 2,
                    'tariffication_object_name': 'to0',
                    'rule': 'tt',
                    'draft_rule': None,
                    'status': Rule.STATUS_ACTIVE,
                    'view_order': 1
                }]
            })
        self.validate_error_response(a_name)

    def test_delete_rule(self):
        a_name = 'delete_rule'
        self.api.validate_request(a_name, {'session_id': 's', 'id': 1})
        self.validate_status_response(a_name)

    def test_apply_rules(self):
        a_name = 'apply_draft_rules'
        self.api.validate_request(a_name, {'session_id': 's', 'tariff_id': 1})
        self.validate_status_response(a_name)

    def test_get_tariffs_prices(self):
        a_name = 'get_tariffs_prices'
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {},
                'paging_params': {},
                'calculation_contexts': []
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'user_id': 1,
                    'ids': [1, 2]
                },
                'paging_params': {
                    'limit': 0
                },
                'calculation_contexts': []
            })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'user_id': 1,
                    'ids': [1, 2]
                },
                'paging_params': {
                    'limit': 0
                },
                'calculation_contexts': [{
                    'objects_num': 3
                }]
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'tariffs': []
        })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'tariffs': [{
                    'tariff_id':
                    1,
                    'tariff_name':
                    't0',
                    'tariff_status':
                    Tariff.STATUS_ACTIVE,
                    'tariffication_objects': [{
                        'tariffication_object_id':
                        2,
                        'tariffication_object_name':
                        'to0',
                        'view_order':
                        1,
                        'prices': [{
                            'calculation_context': {},
                            'rule': {
                                'rule_id': 1,
                                'rule_from_tariff_id': 1,
                                'rule_from_tariff_name': 't0',
                                'price': '10.1'
                            }
                        }],
                    }]
                }]
            })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'tariffs': [{
                    'tariff_id':
                    1,
                    'tariff_name':
                    't0',
                    'tariff_status':
                    Tariff.STATUS_ACTIVE,
                    'tariffication_objects': [{
                        'tariffication_object_id':
                        2,
                        'tariffication_object_name':
                        'to0',
                        'view_order':
                        1,
                        'prices': [{
                            'calculation_context': {},
                            'draft_rule': {
                                'rule_id': 1,
                                'rule_from_tariff_id': 1,
                                'rule_from_tariff_name': 't0',
                                'price': '10.1'
                            },
                            'rule': {
                                'rule_id': 1,
                                'rule_from_tariff_id': 1,
                                'rule_from_tariff_name': 't0',
                                'price': '10.1'
                            },
                        }],
                    }]
                }]
            })
        self.api.validate_response(
            a_name, {
                'status':
                'ok',
                'total':
                2,
                'tariffs': [
                    {
                        'tariff_id':
                        1,
                        'tariff_name':
                        't0',
                        'tariff_status':
                        Tariff.STATUS_ACTIVE,
                        'tariffication_objects': [{
                            'tariffication_object_id':
                            2,
                            'tariffication_object_name':
                            'to0',
                            'view_order':
                            1,
                            'prices': [{
                                'calculation_context': {},
                                'draft_rule': {
                                    'rule_id': 1,
                                    'rule_from_tariff_id': 1,
                                    'rule_from_tariff_name': 't0',
                                    'price': '10.1'
                                },
                                'rule': {
                                    'rule_id': 1,
                                    'rule_from_tariff_id': 1,
                                    'rule_from_tariff_name': 't0',
                                    'price': '10.2'
                                },
                            }],
                        }]
                    },
                    {
                        'tariff_id':
                        12,
                        'tariff_name':
                        't02',
                        'tariff_status':
                        Tariff.STATUS_ARCHIVE,
                        'tariffication_objects': [{
                            'tariffication_object_id':
                            2,
                            'tariffication_object_name':
                            'to0',
                            'view_order':
                            2,
                            'prices': [{
                                'calculation_context': {},
                                'draft_rule': {
                                    'rule_id': 1,
                                    'rule_from_tariff_id': 1,
                                    'rule_from_tariff_name': 't0',
                                    'price': '10.1'
                                },
                                'rule': {
                                    'rule_id': 1,
                                    'rule_from_tariff_id': 1,
                                    'rule_from_tariff_name': 't0',
                                    'price': '10.1'
                                },
                            }, {
                                'calculation_context': {
                                    'objects_num': 100
                                },
                                'draft_rule': {
                                    'rule_id': 1,
                                    'rule_from_tariff_id': 1,
                                    'rule_from_tariff_name': 't0',
                                    'price': '8.1'
                                },
                                'rule': {
                                    'rule_id': 1,
                                    'rule_from_tariff_id': 1,
                                    'rule_from_tariff_name': 't0',
                                    'price': '7.1'
                                },
                            }],
                        }]
                    },
                ]
            })

    def test_get_price(self):
        a_name = 'get_price'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'tariff_id': 1,
            'tariffication_object_id': 3
        })

        self.api.validate_response(
            a_name, {
                'status': 'ok',
                'price': '10.1',
                'rule_id': 1,
                'tariffication_object_id': 2,
                'tariffication_object_name': 'to0',
                'rule_from_tariff_id': 1,
                'rule_from_tariff_name': 't0',
            })
        self.api.validate_response(
            a_name, {
                'status': 'ok',
                'price': '10.1',
                'rule_id': 1,
                'tariffication_object_id': 2,
                'tariffication_object_name': 'to0',
                'rule_from_tariff_id': 1,
                'rule_from_tariff_name': 't0',
                'calculation_context': {
                    'objects_num': 3
                }
            })
        self.validate_error_response(a_name)

    def test_get_draft_price(self):
        a_name = 'get_draft_price'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'tariff_id': 1,
            'tariffication_object_id': 3
        })

        self.api.validate_response(
            a_name, {
                'status': 'ok',
                'price': '10.1',
                'rule_id': 1,
                'tariffication_object_id': 2,
                'tariffication_object_name': 'to0',
                'rule_from_tariff_id': 1,
                'rule_from_tariff_name': 't0',
            })
        self.api.validate_response(
            a_name, {
                'status': 'ok',
                'price': '10.1',
                'rule_id': 1,
                'tariffication_object_id': 2,
                'tariffication_object_name': 'to0',
                'rule_from_tariff_id': 1,
                'rule_from_tariff_name': 't0',
                'calculation_context': {
                    'objects_num': 3
                }
            })
        self.validate_error_response(a_name)

    def test_add_user_tariff(self):
        a_name = 'add_user_tariff'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'tariff_id': 1,
            'user_id': 2
        })

        self.api.validate_response(a_name, {'status': 'ok', 'id': 1})
        self.validate_error_response(a_name)

    def test_delete_user_tariffs(self):
        a_name = 'delete_user_tariffs'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'user_id': 1,
            'tariff_ids': [1, 2]
        })
        self.validate_status_response(a_name)

    def test_get_users_tariffs(self):
        a_name = 'get_user_tariffs'
        self.api.validate_request(a_name, {
            'session_id': 's',
            'filter_params': {},
            'paging_params': {},
        })
        self.api.validate_request(
            a_name, {
                'session_id': 's',
                'filter_params': {
                    'user_ids': [1],
                    'tariff_ids': [1, 2]
                },
                'paging_params': {
                    'limit': 0
                }
            })

        self.api.validate_response(a_name, {
            'status': 'ok',
            'total': 2,
            'user_tariffs': []
        })
        self.api.validate_response(
            a_name, {
                'status': 'ok',
                'total': 2,
                'user_tariffs': [{
                    'user_id': 1,
                    'tariff_ids': [1, 2]
                }]
            })
        self.validate_error_response(a_name)
Пример #7
0
 def __init__(self, action_handler, protocol, logger, tracking_api_calls=()):
     self.action_handler = action_handler
     self.logger = logger
     self.helix_api = HelixApi(protocol)
     self.tracking_api_calls = tracking_api_calls
     self.logger.info('WSGI application %s started', self.__class__.__name__)