def setUpClass(cls): super().setUpClass() tenants = { 'items': [ { 'uuid': str(uuid.uuid4()), 'name': 'first' }, { 'uuid': str(uuid.uuid4()), 'name': 'second' }, ] } mock_auth_client = MockAuthClient('127.0.0.1', cls.service_port(9497, 'auth')) user_token_1 = MockUserToken.some_token( metadata={'tenant_uuid': tenants['items'][0]['uuid']}) user_token_2 = MockUserToken.some_token( metadata={'tenant_uuid': tenants['items'][1]['uuid']}) mock_auth_client.set_token(user_token_1) mock_auth_client.set_token(user_token_2) mock_auth_client.set_tenants(tenants) cls.token_1 = user_token_1.token_id cls.token_2 = user_token_2.token_id
class BaseOffice365TestCase(DirdAssetRunningTestCase): service = 'dird' MICROSOFT_EXTERNAL_AUTH = { "access_token": "an-access-token", "scope": "a-scope", "token_expiration": 42, } LOOKUP_ARGS = {'user_uuid': 'a-xivo-uuid', 'token': 'a-token'} FAVORITE_ARGS = {'user_uuid': 'a-xivo-uuid', 'token': 'a-token'} WARIO = {'givenName': 'Wario', 'surname': 'Bros', 'mobilePhone': ''} def setUp(self): super().setUp() port = self.service_port(9489, 'dird') dird_config = { 'host': '127.0.0.1', 'port': port, 'token': VALID_TOKEN_MAIN_TENANT, 'prefix': None, 'https': False, } self.client = DirdClient(**dird_config) self.source = self.client.backends.create_source(backend=self.BACKEND, body=self.config()) self.display = self.client.displays.create({ 'name': 'display', 'columns': [{ 'field': 'firstname' }] }) self.profile = self.client.profiles.create({ 'name': 'default', 'display': self.display, 'services': { 'lookup': { 'sources': [self.source] } }, }) self.auth_client_mock = AuthMock(host='0.0.0.0', port=self.service_port(9497, 'auth')) self.auth_client_mock.set_external_auth(self.MICROSOFT_EXTERNAL_AUTH) def tearDown(self): try: self.client.profiles.delete(self.profile['uuid']) self.client.displays.delete(self.display['uuid']) self.client.backends.delete_source(backend=self.BACKEND, source_uuid=self.source['uuid']) except requests.HTTPError: pass self.auth_client_mock.reset_external_auth() super().tearDown()
def test_that_a_token_with_no_user_uuid_returns_401(self): mock_auth_client = MockAuthClient('127.0.0.1', self.service_port(9497, 'auth')) tenant_uuid = MAIN_TENANT invalid_token_uuid = str(uuid.uuid4()) invalid_token = MockUserToken.some_token( token=invalid_token_uuid, metadata={'uuid': None, 'tenant_uuid': tenant_uuid}, ) mock_auth_client.set_token(invalid_token) result = self.get_personal_result('unknown-id', token=invalid_token_uuid) assert_that(result, has_properties(status_code=401))
def setUpClass(cls): super().setUpClass() mock_auth_client = MockAuthClient('127.0.0.1', cls.service_port(9497, 'auth')) tenant_uuid = MAIN_TENANT valid_token_1 = MockUserToken.some_token( token=VALID_TOKEN_1, metadata={'tenant_uuid': tenant_uuid} ) valid_token_2 = MockUserToken.some_token( token=VALID_TOKEN_2, metadata={'tenant_uuid': tenant_uuid} ) mock_auth_client.set_token(valid_token_1) mock_auth_client.set_token(valid_token_2) mock_auth_client.set_tenants({'items': [{'uuid': tenant_uuid}]})
class AuthClient: def __init__(self, port): self._client = Client('127.0.0.1', port) disable_warnings() # Proxy unknown method to client def __getattr__(self, attr): return getattr(self._client, attr) def make_token(self, *, token_uuid=None, user_uuid=None, tenant_uuid=TENANT1_UUID, session_uuid='my-session-uuid', acl=['websocketd']): metadata = {'tenant_uuid': str(tenant_uuid)} if token_uuid is None: token = MockUserToken.some_token(session_uuid=str(session_uuid), acl=acl, metadata=metadata) else: token = MockUserToken( str(token_uuid), str(user_uuid), session_uuid=str(session_uuid), metadata=metadata, acl=acl, ) self._client.set_token(token) return token.token_id @contextmanager def token(self, *, token_uuid=None, user_uuid=None, tenant_uuid=TENANT1_UUID, session_uuid='my-session-uuid', acl=['websocketd']): token = self.make_token( token_uuid=token_uuid, user_uuid=user_uuid, tenant_uuid=tenant_uuid, session_uuid=session_uuid, acl=acl, ) try: yield token finally: self.revoke_token(token)
def setUp(self): super().setUp() auth_port = self.service_port(9497, 'auth') source = self.client.backends.create_source( 'google', { 'name': 'google', 'auth': {'host': 'auth', 'port': 9497, 'prefix': None, 'https': False}, }, ) self.source_uuid = source['uuid'] auth_client_mock = AuthMock(host='127.0.0.1', port=auth_port) auth_client_mock.set_external_auth(self.GOOGLE_EXTERNAL_AUTH)
class BaseOffice365PluginTestCase(BaseOffice365TestCase): asset = 'dird_microsoft' def setUp(self): self.auth_mock = AuthMock(host='0.0.0.0', port=self.service_port(9497, 'auth')) self.backend = BackendWrapper('office365', { 'config': self.config(), 'api': Mock() }) def tearDown(self): self.backend.unload() self.auth_mock.reset_external_auth()
def setUp(self): super().setUp() auth_port = self.service_port(9497, 'auth') source = self.client.backends.create_source( 'office365', { 'name': 'office365', 'auth': {'host': 'auth', 'port': 9497, 'prefix': None, 'https': False}, 'endpoint': 'http://microsoft.com:443/v1.0/me/contacts', }, ) self.source_uuid = source['uuid'] auth_client_mock = AuthMock(host='127.0.0.1', port=auth_port) auth_client_mock.set_external_auth(self.OFFICE365_EXTERNAL_AUTH)
def setUp(self): self.auth_mock = AuthMock(host='0.0.0.0', port=self.service_port(9497, 'auth')) self.backend = BackendWrapper('office365', { 'config': self.config(), 'api': Mock() })
def make_auth(cls): try: port = cls.service_port(9497, 'auth') except (NoSuchService, NoSuchPort): return WrongClient(name='auth') return AuthClient('127.0.0.1', port)
def make_auth(cls): try: port = cls.service_port(9497, 'auth') except NoSuchService as e: logger.debug(e) return return AuthClient('127.0.0.1', port=port)
def setUp(self): super().setUp() port = self.service_port(9489, 'dird') dird_config = { 'host': '127.0.0.1', 'port': port, 'token': VALID_TOKEN_MAIN_TENANT, 'prefix': None, 'https': False, } self.client = DirdClient(**dird_config) self.source = self.client.backends.create_source(backend=self.BACKEND, body=self.config()) self.display = self.client.displays.create({ 'name': 'display', 'columns': [{ 'field': 'firstname' }] }) self.profile = self.client.profiles.create({ 'name': 'default', 'display': self.display, 'services': { 'lookup': { 'sources': [self.source] } }, }) self.auth_client_mock = AuthMock(host='0.0.0.0', port=self.service_port(9497, 'auth')) self.auth_client_mock.set_external_auth(self.MICROSOFT_EXTERNAL_AUTH)
def setUpClass(cls): super().setUpClass() mock_auth_client = MockAuthClient('127.0.0.1', cls.service_port(9497, 'auth')) tenant_uuid = MAIN_TENANT try: auth_port = cls.service_port(9497, 'auth') auth_client = AuthClient('127.0.0.1', auth_port, prefix=None, https=False) auth_client.users.new( uuid=VALID_UUID_1, tenant_uuid=MAIN_TENANT, username='******' ) except Exception: pass valid_token_1 = MockUserToken( token=VALID_TOKEN_1, user_uuid=VALID_UUID_1, metadata={'uuid': VALID_UUID_1, 'tenant_uuid': tenant_uuid}, ) mock_auth_client.set_token(valid_token_1)
def setUp(self): super().setUp() self.tenant_uuid = SUB_TENANT self.tenant_name = 'mytenant' bus_port = self.service_port(5672, 'rabbitmq') self.bus = BusClient.from_connection_fields(host='127.0.0.1', port=bus_port) self.bus.downstream_exchange_declare('wazo-headers', 'headers') self.mock_auth_client = MockAuthClient( '127.0.0.1', self.service_port(9497, 'auth') ) def wait_for_dird_bus_connection(): response = self.client.status.get() assert_that(response, has_entries(bus_consumer={'status': 'ok'})) until.assert_(wait_for_dird_bus_connection, timeout=6)
class TestDirdOffice365Plugin(BaseOffice365TestCase): asset = 'dird_microsoft' BACKEND = 'office365' display_body = { 'name': 'default', 'columns': [ { 'title': 'Firstname', 'field': 'firstname' }, { 'title': 'Lastname', 'field': 'lastname' }, { 'title': 'Number', 'field': 'number' }, ], } def config(self): return { 'auth': { 'host': 'auth', 'port': 9497, 'prefix': None, 'https': False }, 'endpoint': 'http://microsoft.com:443/v1.0/me/contacts', 'first_matched_columns': [], 'format_columns': { 'firstname': "{givenName}", 'lastname': "{surname}", 'number': "{businessPhones[0]}", }, 'name': 'office365', 'searched_columns': ["givenName", "surname", "businessPhones"], 'type': 'office365', } def setUp(self): super().setUp() self.auth_client_mock = AuthMock(host='0.0.0.0', port=self.service_port(9497, 'auth')) @fixtures.office365_result(OFFICE365_CONTACTS) def test_given_microsoft_when_lookup_then_contacts_fetched( self, office365_api): self.auth_client_mock.set_external_auth(self.MICROSOFT_EXTERNAL_AUTH) result = self.client.directories.lookup(term='war', profile='default') assert_that( result, has_entries( results=contains(has_entries( column_values=contains('Wario')))), ) @fixtures.office365_result(OFFICE365_CONTACTS) def test_given_no_microsoft_when_lookup_then_no_result( self, office365_api): self.auth_client_mock.reset_external_auth() result = self.client.directories.lookup(term='war', profile='default') result = result['results'] assert_that(result, is_(empty())) @fixtures.office365_result(OFFICE365_CONTACTS) def test_given_microsoft_source_when_get_all_contacts_then_contacts_fetched( self, office365_api): self.auth_client_mock.set_external_auth(self.MICROSOFT_EXTERNAL_AUTH) result = self.client.backends.list_contacts_from_source( backend=self.BACKEND, source_uuid=self.source['uuid']) assert_that( result, has_entries( total=1, filtered=1, items=contains( has_entries( displayName='Wario Bros', surname='Bros', businessPhones=['5555555555'], givenName='Wario', )), ), ) @fixtures.office365_result(OFFICE365_CONTACTS) def test_given_non_existing_microsoft_source_when_get_all_contacts_then_not_found( self, office365_api): self.auth_client_mock.set_external_auth(self.MICROSOFT_EXTERNAL_AUTH) assert_that( calling(self.client.backends.list_contacts_from_source).with_args( backend=self.BACKEND, source_uuid='a-non-existing-source-uuid'), raises(requests.HTTPError).matching( has_property('response', has_properties('status_code', 404))), ) @fixtures.office365_result(OFFICE365_CONTACTS) def test_given_source_and_non_existing_tenant_when_get_all_contacts_then_not_found( self, office365_api): self.auth_client_mock.set_external_auth(self.MICROSOFT_EXTERNAL_AUTH) assert_that( calling(self.client.backends.list_contacts_from_source).with_args( backend=self.BACKEND, source_uuid=self.source['uuid'], tenant_uuid=UNKNOWN_TENANT, ), raises(requests.HTTPError).matching( has_property('response', has_properties('status_code', 404))), )
def __init__(self, port): self._client = Client('127.0.0.1', port) disable_warnings()
class TestConfigAutoCreation(BaseDirdIntegrationTest): asset = 'all_routes' wait_strategy = RestApiOkWaitStrategy() def setUp(self): super().setUp() self.tenant_uuid = SUB_TENANT self.tenant_name = 'mytenant' bus_port = self.service_port(5672, 'rabbitmq') self.bus = BusClient.from_connection_fields(host='127.0.0.1', port=bus_port) self.bus.downstream_exchange_declare('wazo-headers', 'headers') self.mock_auth_client = MockAuthClient( '127.0.0.1', self.service_port(9497, 'auth') ) def wait_for_dird_bus_connection(): response = self.client.status.get() assert_that(response, has_entries(bus_consumer={'status': 'ok'})) until.assert_(wait_for_dird_bus_connection, timeout=6) def test_conference_source(self): self._publish_tenant_created_event() def check(): response = self.client.conference_source.list(tenant_uuid=self.tenant_uuid) key_file = '/var/lib/wazo-auth-keys/wazo-dird-conference-backend-key.yml' assert_that( response, has_entries( items=has_item( has_entries( uuid=uuid_(), tenant_uuid=self.tenant_uuid, name='auto_conference_mytenant', auth={ 'host': 'localhost', 'port': 9497, 'prefix': None, 'https': False, 'version': '0.1', 'key_file': key_file, }, confd={ 'host': 'localhost', 'port': 9486, 'prefix': None, 'https': False, 'version': '1.1', }, first_matched_columns=contains_inanyorder( 'extensions', 'incalls' ), searched_columns=contains_inanyorder( 'name', 'extensions', 'incalls' ), format_columns={ 'phone': '{extensions[0]}', 'reverse': '{name}', }, ) ) ), ) until.assert_(check, timeout=3) for source in self.client.conference_source.list(tenant_uuid=self.tenant_uuid)[ 'items' ]: if source['name'] == 'auto_conference_mytenant': conference_uuid = source['uuid'] def check(): response = self.client.profiles.list( name='default', tenant_uuid=self.tenant_uuid ) assert_that( response, has_entries( items=has_item( has_entries( services=has_entries( lookup=has_entries( sources=has_item(has_entries(uuid=conference_uuid)) ) ) ) ) ), ) until.assert_(check, timeout=3) def test_google_source(self): self._publish_tenant_created_event() def check(): response = self.client.backends.list_sources( 'google', tenant_uuid=self.tenant_uuid ) assert_that( response, has_entries( items=has_item( has_entries( uuid=uuid_(), tenant_uuid=self.tenant_uuid, name='auto_google_mytenant', auth={ 'host': 'localhost', 'port': 9497, 'prefix': None, 'https': False, 'version': '0.1', }, first_matched_columns=has_item('numbers'), searched_columns=contains_inanyorder( 'name', 'numbers', 'familyName', 'givenName' ), format_columns=has_entries( phone_mobile='{numbers_by_label[mobile]}', reverse='{name}', phone='{numbers_except_label[mobile][0]}', ), ) ) ), ) until.assert_(check, timeout=3) for source in self.client.backends.list_sources( 'google', tenant_uuid=self.tenant_uuid )['items']: if source['name'] == 'auto_google_mytenant': google_source_uuid = source['uuid'] def check(): response = self.client.profiles.list( name='default', tenant_uuid=self.tenant_uuid ) assert_that( response, has_entries( items=has_item( has_entries( services=has_entries( lookup=has_entries( sources=has_item( has_entries(uuid=google_source_uuid) ) ) ) ) ) ), ) until.assert_(check, timeout=3) def test_lookup(self): self._publish_tenant_created_event() token = self._create_user() def check(): result = self.lookup('alice', 'default', token=token) assert_that( result, has_entries( column_headers=contains( 'Nom', 'Numéro', 'Mobile', 'Boîte vocale', 'Favoris', 'E-mail' ), column_types=contains( 'name', 'number', 'number', 'voicemail', 'favorite', 'email' ), results=contains_inanyorder( has_entries( column_values=contains( 'Alice', '1234', None, None, False, None ) ) ), ), ) with self.personal({'firstname': 'Alice', 'number': '1234'}, token=token): until.assert_(check, timeout=3) def _create_user(self): self.mock_auth_client.set_tenants( { 'total': 1, 'filtered': 1, 'items': [{'uuid': self.tenant_uuid, 'name': self.tenant_name}], } ) token = MockUserToken.some_token(metadata={'tenant_uuid': self.tenant_uuid}) self.mock_auth_client.set_token(token) return token.token_id def _publish_tenant_created_event(self): event = TenantCreatedEvent(uuid=self.tenant_uuid, name=self.tenant_name) message = { 'data': event.marshal(), 'name': event.name, 'origin_uuid': 'the-xivo-uuid', 'timestamp': datetime.now().isoformat(), } headers = {'name': TenantCreatedEvent.name} self.bus.publish(message, headers=headers, routing_key=event.routing_key)
def setUp(self): super().setUp() self.auth_client_mock = AuthMock(host='0.0.0.0', port=self.service_port(9497, 'auth'))