def setUp(self): super(ChatClientTest, self).setUp() self.recording_processors.extend([ BodyReplacerProcessor(keys=[ "id", "token", "createdBy", "members", "multipleStatus", "value" ]), URIIdentityReplacer(), ChatURIReplacer() ]) self.identity_client = CommunicationIdentityClient.from_connection_string( self.connection_str) endpoint, _ = parse_connection_str(self.connection_str) self.endpoint = endpoint # create user and issue token self.user = self.identity_client.create_user() tokenresponse = self.identity_client.issue_token(self.user, scopes=["chat"]) self.token = tokenresponse.token # create ChatClient refresh_options = CommunicationTokenRefreshOptions(self.token) self.chat_client = ChatClient( self.endpoint, CommunicationTokenCredential(refresh_options))
def test_get_relay_configuration(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) user = identity_client.create_user() relay_client = CommunicationRelayClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) print('Getting relay config:\n') config = relay_client.get_relay_configuration(user) print('Ice Servers: \n') for iceServer in config.ice_servers: assert iceServer.username is not None print('Username: '******'Credential: ' + iceServer.credential) assert iceServer.urls is not None for url in iceServer.urls: print('Url: ' + url) assert config is not None
def setUp(self): super(ChatThreadClientTest, self).setUp() self.recording_processors.extend([ BodyReplacerProcessor(keys=["id", "token", "senderId", "chatMessageId", "nextLink", "participants", "multipleStatus", "value"]), URIIdentityReplacer(), ChatURIReplacer()]) self.identity_client = CommunicationIdentityClient.from_connection_string( self.connection_str) endpoint, _ = parse_connection_str(self.connection_str) self.endpoint = endpoint # create user and issue token self.user = self.identity_client.create_user() tokenresponse = self.identity_client.get_token(self.user, scopes=["chat"]) self.token = tokenresponse.token # create another user self.new_user = self.identity_client.create_user() tokenresponse = self.identity_client.get_token(self.new_user, scopes=["chat"]) self.token_new_user = tokenresponse.token # create ChatClient self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token)) self.chat_client_new_user = ChatClient(self.endpoint, CommunicationTokenCredential(self.token_new_user))
def test_get_token_with_no_scopes(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string) user = identity_client.create_user() with pytest.raises(Exception) as ex: token_response = identity_client.get_token(user, scopes=None)
def get_test_identity_id(is_live, in_recording, connection_str): if not is_live and not in_recording: return TEST_RESOURCE_IDENTIFIER else: identity_client = CommunicationIdentityClient.from_connection_string(connection_str) user = identity_client.create_user() return user.properties['id']
def get_relay_config(self): identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string) relay_client = CommunicationRelayClient.from_connection_string(self.connection_string) print("Creating new user") user = identity_client.create_user() print("User created with id:" + user.properties.get('id')) print("Getting relay configuration") relay_configuration = relay_client.get_relay_configuration(user) for iceServer in relay_configuration.ice_servers: print("Ice server:") print(iceServer) # You can now setup the RTCPeerConnection iceServersList = [] # Create the list of RTCIceServers for iceServer in relay_configuration.ice_servers: iceServersList.append(RTCIceServer(username = iceServer.username, credential=iceServer.credential, urls = iceServer.urls)) # Initialize the RTCConfiguration config = RTCConfiguration(iceServersList) # Initialize the RTCPeerConnection pc = RTCPeerConnection(config)
def get_token_for_teams_user(self): if (os.getenv("SKIP_INT_IDENTITY_EXCHANGE_TOKEN_TEST") == "true"): print("Skipping the Get Access Token for Teams User sample") return from azure.communication.identity import CommunicationIdentityClient if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None: from azure.identity import DefaultAzureCredential endpoint, _ = parse_connection_str(self.connection_string) identity_client = CommunicationIdentityClient( endpoint, DefaultAzureCredential()) else: identity_client = CommunicationIdentityClient.from_connection_string( self.connection_string) msal_app = PublicClientApplication(client_id=self.m365_app_id, authority="{}/{}".format( self.m365_aad_authority, self.m365_aad_tenant)) result = msal_app.acquire_token_by_username_password( username=self.msal_username, password=self.msal_password, scopes=[self.m365_scope]) add_token = result["access_token"] print("AAD access token of a Teams User: "******"Token issued with value: " + tokenresponse.token)
def setUp(self): super(ChatThreadClientTestAsync, self).setUp() self.recording_processors.extend([ BodyReplacerProcessor(keys=["id", "token", "senderId", "chatMessageId", "nextLink", "members", "multipleStatus", "value"]), URIIdentityReplacer(), ResponseReplacerProcessor(keys=[self._resource_name]), ChatURIReplacer()]) endpoint, _ = parse_connection_str(self.connection_str) self.endpoint = endpoint self.identity_client = CommunicationIdentityClient.from_connection_string(self.connection_str) # create user self.user = self.identity_client.create_user() token_response = self.identity_client.issue_token(self.user, scopes=["chat"]) self.token = token_response.token # create another user self.new_user = self.identity_client.create_user() # create ChatClient refresh_option = CommunicationTokenRefreshOptions(self.token) self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_option))
def test_get_relay_configuration_with_route_type_nearest(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) user = identity_client.create_user() relay_client = CommunicationRelayClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) print('Getting relay config with route type nearest:\n') config = relay_client.get_relay_configuration(user, RouteType.NEAREST) for iceServer in config.ice_servers: assert iceServer.username is not None print('Username: '******'Credential: ' + iceServer.credential) assert iceServer.urls is not None for url in iceServer.urls: print('Url: ' + url) print(iceServer.route_type) assert iceServer.route_type == RouteType.NEAREST assert config is not None
def test_revoke_tokens_with_no_user(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string, http_logging_policy=get_http_logging_policy()) with pytest.raises(Exception) as ex: identity_client.revoke_tokens(user=None)
def setUp(self): super(ChatClientTestAsync, self).setUp() self.recording_processors.extend([ BodyReplacerProcessor(keys=[ "id", "token", "createdBy", "participants", "multipleStatus", "value" ]), URIIdentityReplacer(), ResponseReplacerProcessor(keys=[self._resource_name]), ChatURIReplacer() ]) endpoint, _ = parse_connection_str(self.connection_str) self.endpoint = endpoint self.identity_client = CommunicationIdentityClient.from_connection_string( self.connection_str) # create user self.user = self.identity_client.create_user() token_response = self.identity_client.get_token(self.user, scopes=["chat"]) self.token = token_response.token # create ChatClient self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token))
def test_get_token_with_no_user(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string) with pytest.raises(Exception) as ex: token_response = identity_client.get_token( user=None, scopes=[CommunicationTokenScope.CHAT])
def get_relay_config(self): from azure.communication.networktraversal import ( CommunicationRelayClient ) from azure.communication.identity import ( CommunicationIdentityClient ) if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None: from azure.identity import DefaultAzureCredential endpoint, _ = parse_connection_str(self.connection_string) identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential()) relay_client = CommunicationRelayClient(endpoint, DefaultAzureCredential()) else: identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string) relay_client = CommunicationRelayClient.from_connection_string(self.connection_string) print("Creating new user") user = identity_client.create_user() print("User created with id:" + user.properties.get('id')) print("Getting relay configuration") relay_configuration = relay_client.get_relay_configuration(user=user) for iceServer in relay_configuration.ice_servers: print("Icer server:") print(iceServer)
def test_create_user_with_token(self, connection_string): identity_client = CommunicationIdentityClient.from_connection_string( connection_string) user, token_response = identity_client.create_user_with_token( scopes=[CommunicationTokenScope.CHAT]) assert user.identifier is not None assert token_response.token is not None
def test_create_user_and_token_with_no_scopes(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) with pytest.raises(Exception) as ex: user, token_response = identity_client.create_user_and_token(scopes=None)
def test_delete_user(self, connection_string): identity_client = CommunicationIdentityClient.from_connection_string( connection_string) user = identity_client.create_user() identity_client.delete_user(user) assert user.identifier is not None
def test_create_user_and_token(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string) user, token_response = identity_client.create_user_and_token( scopes=[CommunicationTokenScope.CHAT]) assert user.properties.get('id') is not None assert token_response.token is not None
def test_delete_user(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string) user = identity_client.create_user() identity_client.delete_user(user) assert user.properties.get('id') is not None
def test_get_token_with_no_user(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) with pytest.raises(Exception) as ex: token_response = identity_client.get_token(user=None, scopes=[CommunicationTokenScope.CHAT])
def test_delete_user_with_no_user(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) with pytest.raises(Exception) as ex: identity_client.delete_user(user=None)
def test_issue_token(self, connection_string): identity_client = CommunicationIdentityClient.from_connection_string( connection_string) user = identity_client.create_user() token_response = identity_client.issue_token(user, scopes=["chat"]) assert user.identifier is not None assert token_response.token is not None
def test_create_user_and_token(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) user, token_response = identity_client.create_user_and_token(scopes=[CommunicationTokenScope.CHAT]) assert user.properties.get('id') is not None assert token_response.token is not None
def test_revoke_tokens(self, connection_string): identity_client = CommunicationIdentityClient.from_connection_string( connection_string) user = identity_client.create_user() token_response = identity_client.issue_token(user, scopes=[CommunicationTokenScope.CHAT]) identity_client.revoke_tokens(user) assert user.identifier is not None assert token_response.token is not None
def test_get_token_for_teams_user_with_valid_token( self, communication_livetest_dynamic_connection_string): if (self.skip_get_token_for_teams_user_test()): return identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy()) add_token = self.generate_teams_user_aad_token() token_response = identity_client.get_token_for_teams_user(add_token) assert token_response.token is not None
def test_delete_user(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) user = identity_client.create_user() identity_client.delete_user(user) assert user.properties.get('id') is not None
def cf_communication_identity(cli_ctx, kwargs): from azure.communication.identity import CommunicationIdentityClient connection_string = kwargs.pop('connection_string', None) if connection_string is None: error_msg = 'Please specify --connection-string, or set AZURE_COMMUNICATION_CONNECTION_STRING.' raise RequiredArgumentMissingError(error_msg) client = CommunicationIdentityClient.from_connection_string( connection_string) return client
def create_user(self): from azure.communication.identity import CommunicationIdentityClient if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None: from azure.identity import DefaultAzureCredential identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential()) else: identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string) print("Creating new user") user = identity_client.create_user() print("User created with id:" + user.identifier)
def test_revoke_tokens(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string, http_logging_policy=get_http_logging_policy()) user = identity_client.create_user() token_response = identity_client.get_token( user, scopes=[CommunicationTokenScope.CHAT]) identity_client.revoke_tokens(user) assert user.properties.get('id') is not None assert token_response.token is not None
def delete_user(self): from azure.communication.identity import CommunicationIdentityClient if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None: from azure.identity import DefaultAzureCredential identity_client = CommunicationIdentityClient( self.endpoint, DefaultAzureCredential()) else: identity_client = CommunicationIdentityClient.from_connection_string( self.connection_string) user = identity_client.create_user() identity_client.delete_user(user)
def test_get_token_for_teams_user_with_empty_token( self, communication_livetest_dynamic_connection_string): if (self.skip_get_token_for_teams_user_test()): return identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy()) with pytest.raises(Exception) as ex: token_response = identity_client.get_token_for_teams_user("") assert str(ex.value.status_code) == "401" assert ex.value.message is not None