def test_get_normalization_profiles(app_auth, client_auth_with_token): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.normalization import NormalizationProfile from wazo_router_confd.models.tenant import Tenant session = SessionLocal(bind=app_auth.engine) tenant = Tenant(name='fabio', uuid="ffffffff-ffff-4c1c-ad1c-ffffffffffff") normalization_profile = NormalizationProfile(name='profile 1', tenant=tenant) session.add_all([normalization_profile, tenant]) session.commit() # response = client_auth_with_token.get("/1.0/normalization-profiles") assert response.status_code == 200 assert response.json() == { "items": [{ "id": normalization_profile.id, "name": "profile 1", "tenant_uuid": str(tenant.uuid), "country_code": None, "area_code": None, "intl_prefix": None, "ld_prefix": None, "always_ld": False, "always_intl_prefix_plus": False, }] }
def test_get_normalization_rule(app, client): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.normalization import NormalizationProfile from wazo_router_confd.models.normalization import NormalizationRule from wazo_router_confd.models.tenant import Tenant session = SessionLocal(bind=app.engine) tenant = Tenant(name='fabio', uuid='5a6c0c40-b481-41bb-a41a-75d1cc25ff34') normalization_profile = NormalizationProfile(name='profile 1', tenant=tenant) normalization_rule = NormalizationRule( match_regex='^11', match_prefix='11', replace_regex='', profile=normalization_profile, ) session.add_all([tenant, normalization_profile, normalization_rule]) session.commit() # response = client.get("/1.0/normalization-rules/%s" % normalization_rule.id) assert response.status_code == 200 assert response.json() == { "id": normalization_rule.id, "rule_type": 1, "priority": 0, "profile_id": normalization_profile.id, "match_regex": "^11", "replace_regex": '', }
def test_create_normalization_rule(app, client): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.normalization import NormalizationProfile from wazo_router_confd.models.tenant import Tenant session = SessionLocal(bind=app.engine) tenant = Tenant(name='fabio', uuid='5a6c0c40-b481-41bb-a41a-75d1cc25ff34') normalization_profile = NormalizationProfile(name='profile 1', tenant=tenant) session.add_all([normalization_profile, tenant]) session.commit() # response = client.post( "/1.0/normalization-rules", json={ "profile_id": normalization_profile.id, "rule_type": 1, "priority": 1, "match_regex": "^11", "replace_regex": "", }, ) assert response.status_code == 200 assert response.json() == { "id": mock.ANY, "profile_id": normalization_profile.id, "rule_type": 1, "priority": 1, "match_regex": "^11", "replace_regex": "", }
def test_create_duplicated_normalization_rule(app, client): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.normalization import NormalizationProfile from wazo_router_confd.models.normalization import NormalizationRule from wazo_router_confd.models.tenant import Tenant session = SessionLocal(bind=app.engine) tenant = Tenant(name='fabio', uuid='5a6c0c40-b481-41bb-a41a-75d1cc25ff34') normalization_profile = NormalizationProfile(name='profile 1', tenant=tenant) normalization_rule = NormalizationRule( match_regex='^11', match_prefix='11', replace_regex='', profile=normalization_profile, ) session.add_all([tenant, normalization_profile, normalization_rule]) session.commit() # response = client.post( "/1.0/normalization-rules", json={ "profile_id": normalization_profile.id, "match_regex": "^11", "replace_regex": "", }, ) assert response.status_code == 409
def test_get_normalization_profile(app, client): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.normalization import NormalizationProfile from wazo_router_confd.models.tenant import Tenant session = SessionLocal(bind=app.engine) tenant = Tenant(name='fabio', uuid='5a6c0c40-b481-41bb-a41a-75d1cc25ff34') normalization_profile = NormalizationProfile(name='profile 1', tenant=tenant) session.add_all([normalization_profile, tenant]) session.commit() # response = client.get("/1.0/normalization-profiles/%s" % normalization_profile.id) assert response.status_code == 200 assert response.json() == { "id": normalization_profile.id, "name": "profile 1", "tenant_uuid": str(tenant.uuid), "country_code": None, "area_code": None, "intl_prefix": None, "ld_prefix": None, "always_ld": False, "always_intl_prefix_plus": False, }
def test_delete_normalization_rule(app_auth, client_auth_with_token): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.normalization import NormalizationProfile from wazo_router_confd.models.normalization import NormalizationRule from wazo_router_confd.models.tenant import Tenant session = SessionLocal(bind=app_auth.engine) tenant = Tenant(name='fabio', uuid="ffffffff-ffff-4c1c-ad1c-ffffffffffff") normalization_profile = NormalizationProfile(name='profile 1', tenant=tenant) normalization_rule = NormalizationRule( match_regex='^11', match_prefix='11', replace_regex='', profile=normalization_profile, ) session.add_all([tenant, normalization_profile, normalization_rule]) session.commit() # response = client_auth_with_token.delete( "/1.0/normalization-rules/%s" % normalization_rule.id ) assert response.status_code == 200 assert response.json() == { "id": normalization_rule.id, "rule_type": 1, "priority": 0, "profile_id": normalization_profile.id, "match_regex": "^11", "replace_regex": '', }
def test_create_duplicated_normalization_profile(app, client): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.normalization import NormalizationProfile from wazo_router_confd.models.tenant import Tenant session = SessionLocal(bind=app.engine) tenant = Tenant(name='fabio', uuid='5a6c0c40-b481-41bb-a41a-75d1cc25ff34') normalization_profile = NormalizationProfile(name='profile 1', tenant=tenant) session.add_all([normalization_profile, tenant]) session.commit() # response = client.post( "/1.0/normalization-profiles", json={ "name": "profile 1", "tenant_uuid": str(tenant.uuid) }, ) assert response.status_code == 409
async def get_normalization_profile_by_id( conn: Any, redis: Redis, normalization_profile_id: int ) -> Optional[NormalizationProfile]: redis_key = 'normalization_profiles:%s' % normalization_profile_id async def callback() -> dict: async with conn.cursor(cursor_factory=DictCursor) as cur: await cur.execute( "SELECT * from normalization_profiles WHERE id = %s;", [normalization_profile_id], ) row = await cur.fetchone() return ( dict( name=row['name'], country_code=row['country_code'], area_code=row['area_code'], intl_prefix=row['intl_prefix'], ld_prefix=row['ld_prefix'], always_intl_prefix_plus=row['always_intl_prefix_plus'], always_ld=row['always_ld'], ) if row is not None else {} ) profile = await get_cached_dict_from_redis(redis, redis_key, callback) normalization_profile = ( NormalizationProfile( name=profile['name'], country_code=profile['country_code'], area_code=profile['area_code'], intl_prefix=profile['intl_prefix'], ld_prefix=profile['ld_prefix'], always_intl_prefix_plus=profile['always_intl_prefix_plus'], always_ld=profile['always_ld'], ) if profile is not None and profile.get('country_code') else None ) return normalization_profile
def create_normalization_profile( db: Session, principal: Principal, normalization_profile: schema.NormalizationProfileCreate, ) -> NormalizationProfile: normalization_profile.tenant_uuid = tenant_service.get_uuid( principal, db, normalization_profile.tenant_uuid) db_normalization_profile = NormalizationProfile( name=normalization_profile.name, tenant_uuid=normalization_profile.tenant_uuid, country_code=normalization_profile.country_code, area_code=normalization_profile.area_code, intl_prefix=normalization_profile.intl_prefix, ld_prefix=normalization_profile.ld_prefix, always_ld=normalization_profile.always_ld, always_intl_prefix_plus=normalization_profile.always_intl_prefix_plus, ) db.add(db_normalization_profile) db.commit() db.refresh(db_normalization_profile) return db_normalization_profile
def test_create_duplicated_normalization_profile(app_auth, client_auth_with_token): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.normalization import NormalizationProfile from wazo_router_confd.models.tenant import Tenant session = SessionLocal(bind=app_auth.engine) tenant = Tenant(name='fabio', uuid="ffffffff-ffff-4c1c-ad1c-ffffffffffff") normalization_profile = NormalizationProfile(name='profile 1', tenant=tenant) session.add_all([normalization_profile, tenant]) session.commit() # response = client_auth_with_token.post( "/1.0/normalization-profiles", json={ "name": "profile 1", "tenant_uuid": str(tenant.uuid) }, ) assert response.status_code == 409
def test_kamailio_routing_outbound_with_single_ipbx(app, client): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.tenant import Tenant from wazo_router_confd.models.domain import Domain from wazo_router_confd.models.carrier import Carrier from wazo_router_confd.models.carrier_trunk import CarrierTrunk from wazo_router_confd.models.ipbx import IPBX from wazo_router_confd.models.normalization import NormalizationProfile session = SessionLocal(bind=app.engine) tenant = Tenant(name='fabio', uuid='5a6c0c40-b481-41bb-a41a-75d1cc25ff34') domain = Domain(domain='testdomain.com', tenant=tenant) normalization_profile = NormalizationProfile( tenant=tenant, name='Profile', country_code='39', area_code='040', intl_prefix='00', ld_prefix='', always_intl_prefix_plus=False, always_ld=False, ) carrier = Carrier(name='carrier1', tenant=tenant) carrier_trunk = CarrierTrunk( name='trunk1', carrier=carrier, sip_proxy='192.168.1.1', normalization_profile=normalization_profile, ) ipbx = IPBX( customer=1, ip_fqdn='10.0.0.1', domain=domain, registered=True, username='******', password='******', tenant=tenant, normalization_profile=normalization_profile, ) session.add_all( [tenant, domain, normalization_profile, ipbx, carrier, carrier_trunk]) session.commit() # request_from_name = "From name" request_from_uri = "sip:[email protected]" request_from_tag = "from_tag" request_to_name = "to name" request_to_uri = "sip:[email protected]" request_to_tag = "to_tag" # response = client.post( "/1.0/kamailio/routing", json={ "event": "sip-routing", "source_ip": "10.0.0.1", "source_port": 5060, "call_id": "call-id", "from_name": request_from_name, "from_uri": request_from_uri, "from_tag": request_from_tag, "to_uri": request_to_uri, "to_name": request_to_name, "to_tag": request_to_tag, }, ) assert response.status_code == 200 assert response.json() == { "auth": None, "rtjson": { "success": True, "version": "1.0", "routing": "serial", "routes": [{ "dst_uri": "sip:%s:%s" % (carrier_trunk.sip_proxy, carrier_trunk.sip_proxy_port), "path": "", "socket": "", "headers": { "from": { "display": request_from_name, "uri": request_from_uri }, "to": { "display": request_to_name, "uri": request_to_uri }, "extra": "", }, "branch_flags": 8, "fr_timer": 5000, "fr_inv_timer": 30000, }], }, }
def test_kamailio_routing_domain_with_single_ipbx_and_auth( app_auth, client_auth_with_token): from wazo_router_confd.database import SessionLocal from wazo_router_confd.models.tenant import Tenant from wazo_router_confd.models.domain import Domain from wazo_router_confd.models.carrier_trunk import CarrierTrunk from wazo_router_confd.models.carrier import Carrier from wazo_router_confd.models.ipbx import IPBX from wazo_router_confd.models.normalization import NormalizationProfile session = SessionLocal(bind=app_auth.engine) tenant = Tenant(name='fabio', uuid='0839cb47-5d31-4b5e-8c5b-a2481f9e212a') domain = Domain(domain='testdomain.com', tenant=tenant) normalization_profile = NormalizationProfile( tenant=tenant, name='Profile', country_code='39', area_code='040', intl_prefix='00', ld_prefix='', always_intl_prefix_plus=False, always_ld=False, ) ipbx = IPBX( customer=1, ip_fqdn='mypbx.com', ip_address='192.168.0.1', domain=domain, registered=True, username='******', password='******', normalization_profile=normalization_profile, tenant=tenant, ) carrier = Carrier(name="carrier", tenant=tenant) carrier_trunk = CarrierTrunk( name='carrier_trunk1', tenant=tenant, carrier=carrier, normalization_profile=normalization_profile, sip_proxy='proxy.somedomain.com', ip_address="10.0.0.1", ) session = SessionLocal(bind=app_auth.engine) session.add_all( [tenant, domain, normalization_profile, ipbx, carrier, carrier_trunk]) session.commit() # request_from_name = "From name" request_from_uri = "sip:[email protected]" request_from_tag = "from_tag" request_to_name = "to name" request_to_uri = "sip:[email protected]" request_to_tag = "to_tag" # response = client_auth_with_token.post( "/1.0/kamailio/routing", json={ "event": "sip-routing", "source_ip": "10.0.0.1", "source_port": 5060, "call_id": "call-id", "from_name": request_from_name, "from_uri": request_from_uri, "from_tag": request_from_tag, "to_uri": request_to_uri, "to_name": request_to_name, "to_tag": request_to_tag, "auth": True, }, ) assert response.status_code == 200 assert response.json() == { "auth": { 'tenant_uuid': str(tenant.uuid), 'carrier_trunk_id': carrier_trunk.id, 'ipbx_id': None, 'domain': None, 'username': None, 'password_ha1': None, 'success': True, }, "rtjson": { "success": True, "version": "1.0", "routing": "serial", "routes": [{ "dst_uri": "sip:%s:5060" % (ipbx.ip_fqdn), "path": "", "socket": "", "headers": { "from": { "display": request_from_name, "uri": request_from_uri }, "to": { "display": request_to_name, "uri": request_to_uri }, "extra": "P-Asserted-Identity: <sip:" + request_from_name + "@" + request_from_uri + ">\r\n", }, "branch_flags": 8, "fr_timer": 5000, "fr_inv_timer": 30000, }], }, }