diff --git a/.pylintrc b/.pylintrc index 6b7f0358e..e120e7dd0 100644 --- a/.pylintrc +++ b/.pylintrc @@ -154,7 +154,8 @@ disable=print-statement, too-many-public-methods, cyclic-import, too-many-locals, - too-many-function-args + too-many-function-args, + too-many-return-statements # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option diff --git a/libraries/botframework-connector/botframework/connector/aio/_connector_client_async.py b/libraries/botframework-connector/botframework/connector/aio/_connector_client_async.py index 7ee8bdbd6..ff6b9b314 100644 --- a/libraries/botframework-connector/botframework/connector/aio/_connector_client_async.py +++ b/libraries/botframework-connector/botframework/connector/aio/_connector_client_async.py @@ -13,7 +13,6 @@ from msrest import Serializer, Deserializer from .._configuration import ConnectorClientConfiguration -from msrest.exceptions import HttpOperationError from .operations_async import AttachmentsOperations from .operations_async import ConversationsOperations from .. import models @@ -24,14 +23,16 @@ class ConnectorClient(SDKClientAsync): [Bot Framework Developer Portal](https://dev.botframework.com). The Connector service uses industry-standard REST and JSON over HTTPS. Client libraries for this REST API are available. See below for a list. - Many bots will use both the Bot Connector REST API and the associated [Bot State REST API](/en-us/restapi/state). The - Bot State REST API allows a bot to store and retrieve state associated with users and conversations. + Many bots will use both the Bot Connector REST API and the associated [Bot State REST API](/en-us/restapi/state). + The Bot State REST API allows a bot to store and retrieve state associated with users and conversations. Authentication for both the Bot Connector and Bot State REST APIs is accomplished with JWT Bearer tokens, and is described in detail in the [Connector Authentication](/en-us/restapi/authentication) document. # Client Libraries for the Bot Connector REST API * [Bot Builder for C#](/en-us/csharp/builder/sdkreference/) * [Bot Builder for Node.js](/en-us/node/builder/overview/) - * Generate your own from the [Connector API Swagger file](https://raw.githubusercontent.com/Microsoft/BotBuilder/master/CSharp/Library/Microsoft.Bot.Connector.Shared/Swagger/ConnectorAPI.json) + * Generate your own from the + [Connector API Swagger file](https://raw.githubusercontent.com/Microsoft/BotBuilder/master/CSharp/Library/ + Microsoft.Bot.Connector.Shared/Swagger/ConnectorAPI.json) © 2016 Microsoft :ivar config: Configuration for client. diff --git a/libraries/botframework-connector/botframework/connector/aio/operations_async/_attachments_operations_async.py b/libraries/botframework-connector/botframework/connector/aio/operations_async/_attachments_operations_async.py index 151febed8..a46fa7da5 100644 --- a/libraries/botframework-connector/botframework/connector/aio/operations_async/_attachments_operations_async.py +++ b/libraries/botframework-connector/botframework/connector/aio/operations_async/_attachments_operations_async.py @@ -17,7 +17,8 @@ class AttachmentsOperations: """AttachmentsOperations async operations. - You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute. + You should not instantiate directly this class, but create a Client instance that will create it for you and attach + it as attribute. :param client: Client for service requests. :param config: Configuration of service client. diff --git a/libraries/botframework-connector/botframework/connector/aio/operations_async/_conversations_operations_async.py b/libraries/botframework-connector/botframework/connector/aio/operations_async/_conversations_operations_async.py index 04cf05c69..6afdf82c4 100644 --- a/libraries/botframework-connector/botframework/connector/aio/operations_async/_conversations_operations_async.py +++ b/libraries/botframework-connector/botframework/connector/aio/operations_async/_conversations_operations_async.py @@ -18,7 +18,8 @@ class ConversationsOperations: """ConversationsOperations async operations. - You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute. + You should not instantiate directly this class, but create a Client instance that will create it for you and attach + it as attribute. :param client: Client for service requests. :param config: Configuration of service client. diff --git a/libraries/botframework-connector/botframework/connector/auth/endorsements_validator.py b/libraries/botframework-connector/botframework/connector/auth/endorsements_validator.py index a62942fa0..a9c234972 100644 --- a/libraries/botframework-connector/botframework/connector/auth/endorsements_validator.py +++ b/libraries/botframework-connector/botframework/connector/auth/endorsements_validator.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + from typing import List @@ -9,7 +12,7 @@ def validate(channel_id: str, endorsements: List[str]): if not channel_id: return True - if endorsements == None: + if endorsements is None: raise ValueError("Argument endorsements is null.") # The Call path to get here is: @@ -28,5 +31,5 @@ def validate(channel_id: str, endorsements: List[str]): # of scope, tokens from WebChat have about 10 endorsements, and # tokens coming from Teams have about 20. - endorsementPresent = channel_id in endorsements - return endorsementPresent + endorsement_present = channel_id in endorsements + return endorsement_present diff --git a/libraries/botframework-connector/botframework/connector/auth/government_constants.py b/libraries/botframework-connector/botframework/connector/auth/government_constants.py index 15dabcc96..cba241196 100644 --- a/libraries/botframework-connector/botframework/connector/auth/government_constants.py +++ b/libraries/botframework-connector/botframework/connector/auth/government_constants.py @@ -14,7 +14,8 @@ class GovernmentConstants(ABC): """ TO CHANNEL FROM BOT: Login URL """ - TO_CHANNEL_FROM_BOT_LOGIN_URL = "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/oauth2/v2.0/token" + TO_CHANNEL_FROM_BOT_LOGIN_URL = \ + "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/oauth2/v2.0/token" """ TO CHANNEL FROM BOT: OAuth scope to request @@ -36,4 +37,5 @@ class GovernmentConstants(ABC): """ TO BOT FROM GOV EMULATOR: OpenID metadata document for tokens coming from MSA """ - TO_BOT_FROM_EMULATOR_OPEN_ID_METADATA_URL = "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/v2.0/.well-known/openid-configuration" + TO_BOT_FROM_EMULATOR_OPEN_ID_METADATA_URL = \ + "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/v2.0/.well-known/openid-configuration" diff --git a/libraries/botframework-connector/botframework/connector/auth/jwt_token_extractor.py b/libraries/botframework-connector/botframework/connector/auth/jwt_token_extractor.py index 56f71066d..4c58509a1 100644 --- a/libraries/botframework-connector/botframework/connector/auth/jwt_token_extractor.py +++ b/libraries/botframework-connector/botframework/connector/auth/jwt_token_extractor.py @@ -1,4 +1,6 @@ -import asyncio +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import json from datetime import datetime, timedelta import requests diff --git a/libraries/botframework-connector/botframework/connector/auth/jwt_token_validation.py b/libraries/botframework-connector/botframework/connector/auth/jwt_token_validation.py index 8f3b9a290..1a6813494 100644 --- a/libraries/botframework-connector/botframework/connector/auth/jwt_token_validation.py +++ b/libraries/botframework-connector/botframework/connector/auth/jwt_token_validation.py @@ -19,12 +19,12 @@ async def authenticate_request( channel_service: str = "", ) -> ClaimsIdentity: """Authenticates the request and sets the service url in the set of trusted urls. - :param activity: The incoming Activity from the Bot Framework or the Emulator :type activity: ~botframework.connector.models.Activity :param auth_header: The Bearer token included as part of the request :type auth_header: str :param credentials: The set of valid credentials, such as the Bot Application ID + :param channel_service: String for the channel service :type credentials: CredentialProvider :raises Exception: diff --git a/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py b/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py index 0567627de..b89e4b226 100644 --- a/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py +++ b/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py @@ -2,7 +2,6 @@ from urllib.parse import urlparse from msrest.authentication import BasicTokenAuthentication, Authentication import requests -import aiohttp from .constants import Constants # TODO: Decide to move this to Constants or viceversa (when porting OAuth) @@ -11,7 +10,8 @@ "refreshScope": "https://api.botframework.com/.default", "botConnectorOpenIdMetadata": "https://login.botframework.com/v1/.well-known/openidconfiguration", "botConnectorIssuer": "https://api.botframework.com", - "emulatorOpenIdMetadata": "https://login.microsoftonline.com/botframework.com/v2.0/.well-known/openid-configuration", + "emulatorOpenIdMetadata": + "https://login.microsoftonline.com/botframework.com/v2.0/.well-known/openid-configuration", "emulatorAuthV31IssuerV1": "https://sts.windows.net/d6d49420-f39b-4df7-a1dc-d59a935871db/", "emulatorAuthV31IssuerV2": "https://login.microsoftonline.com/d6d49420-f39b-4df7-a1dc-d59a935871db/v2.0", "emulatorAuthV32IssuerV1": "https://sts.windows.net/f8cdef31-a31e-4b4a-93e4-5f571e91255a/", @@ -63,7 +63,7 @@ def __init__(self, app_id: str, password: str, channel_auth_tenant: str = None): self.microsoft_app_password = password tenant = ( channel_auth_tenant - if channel_auth_tenant is not None and len(channel_auth_tenant) > 0 + if channel_auth_tenant else Constants.DEFAULT_CHANNEL_AUTH_TENANT ) self.oauth_endpoint = ( @@ -73,7 +73,7 @@ def __init__(self, app_id: str, password: str, channel_auth_tenant: str = None): ) self.token_cache_key = app_id + "-cache" - def signed_session(self) -> requests.Session: + def signed_session(self) -> requests.Session: # pylint: disable=arguments-differ """ Gets the signed session. :returns: Signed requests.Session object @@ -113,8 +113,7 @@ def get_access_token(self, force_refresh: bool = False) -> str: oauth_token = self.refresh_token() MicrosoftAppCredentials.cache.setdefault(self.token_cache_key, oauth_token) return oauth_token.access_token - else: - return "" + return "" def refresh_token(self) -> _OAuthResponse: """ diff --git a/libraries/botframework-connector/botframework/connector/connector_client.py b/libraries/botframework-connector/botframework/connector/connector_client.py index 9d3dd08ed..ab88ac9ae 100644 --- a/libraries/botframework-connector/botframework/connector/connector_client.py +++ b/libraries/botframework-connector/botframework/connector/connector_client.py @@ -13,7 +13,6 @@ from msrest import Serializer, Deserializer from ._configuration import ConnectorClientConfiguration -from msrest.exceptions import HttpOperationError from .operations import AttachmentsOperations from .operations import ConversationsOperations from . import models @@ -24,14 +23,16 @@ class ConnectorClient(SDKClient): [Bot Framework Developer Portal](https://dev.botframework.com). The Connector service uses industry-standard REST and JSON over HTTPS. Client libraries for this REST API are available. See below for a list. - Many bots will use both the Bot Connector REST API and the associated [Bot State REST API](/en-us/restapi/state). The - Bot State REST API allows a bot to store and retrieve state associated with users and conversations. + Many bots will use both the Bot Connector REST API and the associated [Bot State REST API](/en-us/restapi/state). + The Bot State REST API allows a bot to store and retrieve state associated with users and conversations. Authentication for both the Bot Connector and Bot State REST APIs is accomplished with JWT Bearer tokens, and is described in detail in the [Connector Authentication](/en-us/restapi/authentication) document. # Client Libraries for the Bot Connector REST API * [Bot Builder for C#](/en-us/csharp/builder/sdkreference/) * [Bot Builder for Node.js](/en-us/node/builder/overview/) - * Generate your own from the [Connector API Swagger file](https://raw.githubusercontent.com/Microsoft/BotBuilder/master/CSharp/Library/Microsoft.Bot.Connector.Shared/Swagger/ConnectorAPI.json) + * Generate your own from the + [Connector API Swagger file](https://raw.githubusercontent.com/Microsoft/BotBuilder/master/CSharp/Library + /Microsoft.Bot.Connector.Shared/Swagger/ConnectorAPI.json) © 2016 Microsoft :ivar config: Configuration for client. diff --git a/libraries/botframework-connector/botframework/connector/emulator_api_client.py b/libraries/botframework-connector/botframework/connector/emulator_api_client.py index efb686807..6012456ca 100644 --- a/libraries/botframework-connector/botframework/connector/emulator_api_client.py +++ b/libraries/botframework-connector/botframework/connector/emulator_api_client.py @@ -21,7 +21,7 @@ async def emulate_oauth_cards( if res.status_code == 200: return True - else: - raise Exception( - f"EmulateOAuthCards failed with status code: { res.status_code }" - ) + + raise Exception( + f"EmulateOAuthCards failed with status code: { res.status_code }" + ) diff --git a/libraries/botframework-connector/botframework/connector/operations/_attachments_operations.py b/libraries/botframework-connector/botframework/connector/operations/_attachments_operations.py index 7d2738394..03cce075d 100644 --- a/libraries/botframework-connector/botframework/connector/operations/_attachments_operations.py +++ b/libraries/botframework-connector/botframework/connector/operations/_attachments_operations.py @@ -14,10 +14,11 @@ from .. import models -class AttachmentsOperations(object): +class AttachmentsOperations: """AttachmentsOperations operations. - You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute. + You should not instantiate directly this class, but create a Client instance that will create it for you and attach + it as attribute. :param client: Client for service requests. :param config: Configuration of service client. diff --git a/libraries/botframework-connector/botframework/connector/operations/_conversations_operations.py b/libraries/botframework-connector/botframework/connector/operations/_conversations_operations.py index 532ae0dfb..c5a8bb68c 100644 --- a/libraries/botframework-connector/botframework/connector/operations/_conversations_operations.py +++ b/libraries/botframework-connector/botframework/connector/operations/_conversations_operations.py @@ -15,10 +15,11 @@ from .. import models -class ConversationsOperations(object): +class ConversationsOperations: """ConversationsOperations operations. - You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute. + You should not instantiate directly this class, but create a Client instance that will create it for you and attach + it as attribute. :param client: Client for service requests. :param config: Configuration of service client. @@ -532,7 +533,7 @@ def reply_to_activity( "url": "/v3/conversations/{conversationId}/activities/{activityId}" } - def delete_activity( + def delete_activity( # pylint: disable=inconsistent-return-statements self, conversation_id, activity_id, @@ -744,7 +745,7 @@ def get_conversation_paged_members( "url": "/v3/conversations/{conversationId}/pagedmembers" } - def delete_conversation_member( + def delete_conversation_member( # pylint: disable=inconsistent-return-statements self, conversation_id, member_id, diff --git a/libraries/botframework-connector/botframework/connector/token_api/_token_api_client.py b/libraries/botframework-connector/botframework/connector/token_api/_token_api_client.py index 62814ee68..863dcb2e5 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/_token_api_client.py +++ b/libraries/botframework-connector/botframework/connector/token_api/_token_api_client.py @@ -13,7 +13,6 @@ from msrest import Serializer, Deserializer from ._configuration import TokenApiClientConfiguration -from msrest.exceptions import HttpOperationError from .operations import BotSignInOperations from .operations import UserTokenOperations from . import models diff --git a/libraries/botframework-connector/botframework/connector/token_api/aio/_token_api_client_async.py b/libraries/botframework-connector/botframework/connector/token_api/aio/_token_api_client_async.py index 2878d1bdc..a72fed429 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/aio/_token_api_client_async.py +++ b/libraries/botframework-connector/botframework/connector/token_api/aio/_token_api_client_async.py @@ -13,7 +13,6 @@ from msrest import Serializer, Deserializer from .._configuration import TokenApiClientConfiguration -from msrest.exceptions import HttpOperationError from .operations_async import BotSignInOperations from .operations_async import UserTokenOperations from .. import models diff --git a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_bot_sign_in_operations_async.py b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_bot_sign_in_operations_async.py index 8373db18d..1ec07e04c 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_bot_sign_in_operations_async.py +++ b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_bot_sign_in_operations_async.py @@ -18,7 +18,8 @@ class BotSignInOperations: """BotSignInOperations async operations. - You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute. + You should not instantiate directly this class, but create a Client instance that will create it for you and attach + it as attribute. :param client: Client for service requests. :param config: Configuration of service client. diff --git a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_user_token_operations_async.py b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_user_token_operations_async.py index f0230afa0..53fc2947a 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_user_token_operations_async.py +++ b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_user_token_operations_async.py @@ -17,7 +17,8 @@ class UserTokenOperations: """UserTokenOperations async operations. - You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute. + You should not instantiate directly this class, but create a Client instance that will create it for you and attach + it as attribute. :param client: Client for service requests. :param config: Configuration of service client. diff --git a/libraries/botframework-connector/botframework/connector/token_api/operations/_bot_sign_in_operations.py b/libraries/botframework-connector/botframework/connector/token_api/operations/_bot_sign_in_operations.py index 104bba69a..f4c45037d 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/operations/_bot_sign_in_operations.py +++ b/libraries/botframework-connector/botframework/connector/token_api/operations/_bot_sign_in_operations.py @@ -15,10 +15,11 @@ from .. import models -class BotSignInOperations(object): +class BotSignInOperations: """BotSignInOperations operations. - You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute. + You should not instantiate directly this class, but create a Client instance that will create it for you and attach + it as attribute. :param client: Client for service requests. :param config: Configuration of service client. diff --git a/libraries/botframework-connector/botframework/connector/token_api/operations/_user_token_operations.py b/libraries/botframework-connector/botframework/connector/token_api/operations/_user_token_operations.py index a0b11e975..f154c7cd2 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/operations/_user_token_operations.py +++ b/libraries/botframework-connector/botframework/connector/token_api/operations/_user_token_operations.py @@ -14,10 +14,11 @@ from .. import models -class UserTokenOperations(object): +class UserTokenOperations: """UserTokenOperations operations. - You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute. + You should not instantiate directly this class, but create a Client instance that will create it for you and attach + it as attribute. :param client: Client for service requests. :param config: Configuration of service client. diff --git a/libraries/botframework-connector/tests/authentication_stub.py b/libraries/botframework-connector/tests/authentication_stub.py index 6b5612646..4ae198659 100644 --- a/libraries/botframework-connector/tests/authentication_stub.py +++ b/libraries/botframework-connector/tests/authentication_stub.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + from msrest.authentication import BasicTokenAuthentication, Authentication @@ -5,8 +8,8 @@ class MicrosoftTokenAuthenticationStub(Authentication): def __init__(self, access_token): self.access_token = access_token - def signed_session(self): - basicAuthentication = BasicTokenAuthentication( + def signed_session(self, session=None): + basic_authentication = BasicTokenAuthentication( {"access_token": self.access_token} ) - return basicAuthentication.signed_session() + return session or basic_authentication.signed_session() diff --git a/libraries/botframework-connector/tests/test_attachments.py b/libraries/botframework-connector/tests/test_attachments.py index bd8cbcb45..690e34bc6 100644 --- a/libraries/botframework-connector/tests/test_attachments.py +++ b/libraries/botframework-connector/tests/test_attachments.py @@ -1,11 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import os import base64 -import pytest import asyncio +import pytest from azure_devtools.scenario_tests import ReplayableTest import msrest -from botbuilder.schema import * +from botbuilder.schema import AttachmentData, ErrorResponseException from botframework.connector import ConnectorClient from botframework.connector.auth import MicrosoftAppCredentials @@ -43,8 +46,8 @@ def read_base64(path_to_file): return encoded_string -loop = asyncio.get_event_loop() -auth_token = loop.run_until_complete(get_auth_token()) +LOOP = asyncio.get_event_loop() +AUTH_TOKEN = LOOP.run_until_complete(get_auth_token()) class AttachmentsTest(ReplayableTest): @@ -53,7 +56,7 @@ def __init__(self, method_name): @property def credentials(self): - return MicrosoftTokenAuthenticationStub(auth_token) + return MicrosoftTokenAuthenticationStub(AUTH_TOKEN) def test_attachments_upload_and_get_attachment(self): attachment = AttachmentData( @@ -124,7 +127,7 @@ def test_attachments_get_attachment_view_with_invalid_view_id_fails(self): CONVERSATION_ID, attachment ) attachment_id = response.id - attachment_view = connector.attachments.get_attachment( + connector.attachments.get_attachment( attachment_id, "invalid" ) diff --git a/libraries/botframework-connector/tests/test_attachments_async.py b/libraries/botframework-connector/tests/test_attachments_async.py index 33c7af665..a44eadad9 100644 --- a/libraries/botframework-connector/tests/test_attachments_async.py +++ b/libraries/botframework-connector/tests/test_attachments_async.py @@ -1,11 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import os import base64 -import pytest import asyncio +import pytest from azure_devtools.scenario_tests import ReplayableTest import msrest -from botbuilder.schema import * +from botbuilder.schema import AttachmentData, ErrorResponseException from botframework.connector.aio import ConnectorClient from botframework.connector.auth import MicrosoftAppCredentials @@ -50,8 +53,8 @@ async def return_sum(attachment_stream): return counter -loop = asyncio.get_event_loop() -auth_token = loop.run_until_complete(get_auth_token()) +LOOP = asyncio.get_event_loop() +AUTH_TOKEN = LOOP.run_until_complete(get_auth_token()) class AttachmentsTest(ReplayableTest): @@ -61,7 +64,7 @@ def __init__(self, method_name): @property def credentials(self): - return MicrosoftTokenAuthenticationStub(auth_token) + return MicrosoftTokenAuthenticationStub(AUTH_TOKEN) def test_attachments_upload_and_get_attachment(self): attachment = AttachmentData( @@ -140,7 +143,7 @@ def test_attachments_get_attachment_view_with_invalid_view_id_fails(self): connector.conversations.upload_attachment(CONVERSATION_ID, attachment) ) attachment_id = response.id - attachment_view = self.loop.run_until_complete( + self.loop.run_until_complete( connector.attachments.get_attachment(attachment_id, "invalid") ) diff --git a/libraries/botframework-connector/tests/test_auth.py b/libraries/botframework-connector/tests/test_auth.py index 8a0343cf6..02016715f 100644 --- a/libraries/botframework-connector/tests/test_auth.py +++ b/libraries/botframework-connector/tests/test_auth.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import pytest from botbuilder.schema import Activity @@ -152,7 +155,8 @@ async def test_channel_msa_header_from_user_specified_tenant(self): assert claims.get_claim_value("tid") == "72f988bf-86f1-41af-91ab-2d7cd011db47" @pytest.mark.asyncio - # Tests with a valid Token and invalid service url; and ensures that Service url is NOT added to Trusted service url list. + # Tests with a valid Token and invalid service url and ensures that Service url is NOT added to + # Trusted service url list. async def test_channel_msa_header_invalid_service_url_should_not_be_trusted(self): activity = Activity(service_url="https://webchat.botframework.com/") header = ( @@ -180,12 +184,12 @@ async def test_channel_authentication_disabled_should_be_anonymous(self): header = "" credentials = SimpleCredentialProvider("", "") - claimsPrincipal = await JwtTokenValidation.authenticate_request( + claims_principal = await JwtTokenValidation.authenticate_request( activity, header, credentials ) - assert claimsPrincipal.is_authenticated - assert len(claimsPrincipal.claims) == 0 + assert claims_principal.is_authenticated + assert not claims_principal.claims @pytest.mark.asyncio # Tests with no authentication header and makes sure the service URL is not added to the trusted list. diff --git a/libraries/botframework-connector/tests/test_conversations.py b/libraries/botframework-connector/tests/test_conversations.py index ffc401fc2..763be24ac 100644 --- a/libraries/botframework-connector/tests/test_conversations.py +++ b/libraries/botframework-connector/tests/test_conversations.py @@ -1,8 +1,12 @@ -import pytest +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import asyncio +import pytest from azure_devtools.scenario_tests import ReplayableTest -from botbuilder.schema import * +from botbuilder.schema import (Activity, ActivityTypes, Attachment, AttachmentLayoutTypes, CardImage, + ChannelAccount, ConversationParameters, ErrorResponseException, HeroCard) from botframework.connector import ConnectorClient from botframework.connector.auth import MicrosoftAppCredentials @@ -30,8 +34,8 @@ async def get_auth_token(): return "STUB_ACCESS_TOKEN" -loop = asyncio.get_event_loop() -auth_token = loop.run_until_complete(get_auth_token()) +LOOP = asyncio.get_event_loop() +AUTH_TOKEN = LOOP.run_until_complete(get_auth_token()) class ConversationTest(ReplayableTest): @@ -40,18 +44,18 @@ def __init__(self, method_name): @property def credentials(self): - return MicrosoftTokenAuthenticationStub(auth_token) + return MicrosoftTokenAuthenticationStub(AUTH_TOKEN) def test_conversations_create_conversation(self): - to = ChannelAccount(id=RECIPIENT_ID) + test_object = ChannelAccount(id=RECIPIENT_ID) create_conversation = ConversationParameters( bot=ChannelAccount(id=BOT_ID), - members=[to], + members=[test_object], activity=Activity( type=ActivityTypes.message, channel_id=CHANNEL_ID, from_property=ChannelAccount(id=BOT_ID), - recipient=to, + recipient=test_object, text="Hi there!", ), ) @@ -62,15 +66,15 @@ def test_conversations_create_conversation(self): assert conversation.id is not None def test_conversations_create_conversation_with_invalid_bot_id_fails(self): - to = ChannelAccount(id=RECIPIENT_ID) + test_object = ChannelAccount(id=RECIPIENT_ID) create_conversation = ConversationParameters( bot=ChannelAccount(id="INVALID"), - members=[to], + members=[test_object], activity=Activity( type=ActivityTypes.message, channel_id=CHANNEL_ID, from_property=ChannelAccount(id="INVALID"), - recipient=to, + recipient=test_object, text="Hi there!", ), ) @@ -102,16 +106,16 @@ def test_conversations_create_conversation_without_members_fails(self): assert "Conversations" in str(excinfo.value.error.error.message) def test_conversations_create_conversation_with_bot_as_only_member_fails(self): - to = ChannelAccount(id=BOT_ID) + test_object = ChannelAccount(id=BOT_ID) sender = ChannelAccount(id=BOT_ID) create_conversation = ConversationParameters( bot=sender, - members=[to], + members=[test_object], activity=Activity( type=ActivityTypes.message, channel_id=CHANNEL_ID, from_property=sender, - recipient=to, + recipient=test_object, text="Hi there!", ), ) @@ -206,7 +210,7 @@ def test_conversations_get_conversation_members(self): def test_conversations_get_conversation_members_invalid_id_fails(self): with pytest.raises(ErrorResponseException) as excinfo: connector = ConnectorClient(self.credentials, base_url=SERVICE_URL) - members = connector.conversations.get_conversation_members("INVALID_ID") + connector.conversations.get_conversation_members("INVALID_ID") assert excinfo.value.error.error.code == "ServiceError" assert "cannot send messages to this id" in str( diff --git a/libraries/botframework-connector/tests/test_conversations_async.py b/libraries/botframework-connector/tests/test_conversations_async.py index cc5c7fbdb..59ff4f4b0 100644 --- a/libraries/botframework-connector/tests/test_conversations_async.py +++ b/libraries/botframework-connector/tests/test_conversations_async.py @@ -1,8 +1,12 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import asyncio import pytest from azure_devtools.scenario_tests import ReplayableTest -from botbuilder.schema import * +from botbuilder.schema import (Activity, ActivityTypes, Attachment, AttachmentLayoutTypes, CardImage, + ChannelAccount, ConversationParameters, ErrorResponseException, HeroCard) from botframework.connector.aio import ConnectorClient from botframework.connector.auth import MicrosoftAppCredentials @@ -30,26 +34,26 @@ async def get_auth_token(): return "STUB_ACCESS_TOKEN" -loop = asyncio.get_event_loop() -auth_token = loop.run_until_complete(get_auth_token()) +LOOP = asyncio.get_event_loop() +AUTH_TOKEN = LOOP.run_until_complete(get_auth_token()) class TestAsyncConversation(ReplayableTest): def __init__(self, method_name): super(TestAsyncConversation, self).__init__(method_name) self.loop = asyncio.get_event_loop() - self.credentials = MicrosoftTokenAuthenticationStub(auth_token) + self.credentials = MicrosoftTokenAuthenticationStub(AUTH_TOKEN) def test_conversations_create_conversation(self): - to = ChannelAccount(id=RECIPIENT_ID) + test_object = ChannelAccount(id=RECIPIENT_ID) create_conversation = ConversationParameters( bot=ChannelAccount(id=BOT_ID), - members=[to], + members=[test_object], activity=Activity( type=ActivityTypes.message, channel_id=CHANNEL_ID, from_property=ChannelAccount(id=BOT_ID), - recipient=to, + recipient=test_object, text="Hi there!", ), ) @@ -61,21 +65,21 @@ def test_conversations_create_conversation(self): conversation = self.loop.run_until_complete( connector.conversations.create_conversation(create_conversation) ) - except Exception as e: - raise e + except Exception as error: + raise error else: assert conversation.id is not None def test_conversations_create_conversation_with_invalid_bot_id_fails(self): - to = ChannelAccount(id=RECIPIENT_ID) + test_object = ChannelAccount(id=RECIPIENT_ID) create_conversation = ConversationParameters( bot=ChannelAccount(id="INVALID"), - members=[to], + members=[test_object], activity=Activity( type=ActivityTypes.message, channel_id=CHANNEL_ID, from_property=ChannelAccount(id="INVALID"), - recipient=to, + recipient=test_object, text="Hi there!", ), ) @@ -111,16 +115,16 @@ def test_conversations_create_conversation_without_members_fails(self): assert "Conversations" in str(excinfo.value.error.error.message) def test_conversations_create_conversation_with_bot_as_only_member_fails(self): - to = ChannelAccount(id=BOT_ID) + test_object = ChannelAccount(id=BOT_ID) sender = ChannelAccount(id=BOT_ID) create_conversation = ConversationParameters( bot=sender, - members=[to], + members=[test_object], activity=Activity( type=ActivityTypes.message, channel_id=CHANNEL_ID, from_property=sender, - recipient=to, + recipient=test_object, text="Hi there!", ), ) @@ -147,8 +151,8 @@ def test_conversations_send_to_conversation(self): response = self.loop.run_until_complete( connector.conversations.send_to_conversation(CONVERSATION_ID, activity) ) - except Exception as e: - raise e + except Exception as error: + raise error else: assert response is not None @@ -222,7 +226,7 @@ def test_conversations_get_conversation_members(self): def test_conversations_get_conversation_members_invalid_id_fails(self): with pytest.raises(ErrorResponseException) as excinfo: connector = ConnectorClient(self.credentials, base_url=SERVICE_URL) - members = self.loop.run_until_complete( + self.loop.run_until_complete( connector.conversations.get_conversation_members("INVALID_ID") )