-
Notifications
You must be signed in to change notification settings - Fork 26
Make channelData subfields optional to avoid throw #564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
| # pyright: basic | ||
|
|
||
| from typing import Any, Dict | ||
|
|
||
| import pytest | ||
| from microsoft_teams.api.activities import ActivityTypeAdapter | ||
|
|
||
|
|
||
| def _activity(**overrides: Any) -> Dict[str, Any]: | ||
| """A minimal inbound message activity, with room for per-case overrides.""" | ||
| activity: Dict[str, Any] = { | ||
| "type": "message", | ||
| "id": "activity-id", | ||
| "text": "hello", | ||
| "channelId": "msteams", | ||
| "serviceUrl": "https://smba.trafficmanager.net/emea/tenant/", | ||
| "from": {"id": "user-id"}, | ||
| "conversation": {"id": "conversation-id"}, | ||
| "recipient": {"id": "bot-id"}, | ||
| } | ||
| activity.update(overrides) | ||
| return activity | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "overrides", | ||
| [ | ||
| pytest.param({"channelData": {"app": {}}}, id="channel_data_app"), | ||
| pytest.param({"channelData": {"channel": {}}}, id="channel_data_channel"), | ||
| pytest.param({"channelData": {"team": {}}}, id="channel_data_team"), | ||
| pytest.param({"channelData": {"tenant": {}}}, id="channel_data_tenant"), | ||
| pytest.param({"channelData": {"settings": {}}}, id="channel_data_settings"), | ||
| pytest.param({"attachments": [{}]}, id="attachment"), | ||
| pytest.param({"entities": [{}]}, id="entity"), | ||
| ], | ||
| ) | ||
| def test_activity_parses_when_nested_object_is_empty(overrides: Dict[str, Any]) -> None: | ||
| """ | ||
| The service can send nested objects as empty objects. These are inbound-only models, so a | ||
| missing field must not reject the whole activity. | ||
|
|
||
| Regression test for https://github.com/microsoft/teams.py/issues/563, where a | ||
| ``channelData.app`` of ``{}`` raised a ValidationError and the activity was dropped. | ||
| """ | ||
| assert ActivityTypeAdapter.validate_python(_activity(**overrides)) is not None | ||
|
|
||
|
|
||
| def test_activity_parses_unrecognized_entity_type() -> None: | ||
| """An entity type this SDK version predates must not reject the activity.""" | ||
| activity = ActivityTypeAdapter.validate_python(_activity(entities=[{"type": "someFutureEntity"}])) | ||
|
|
||
| assert activity.entities is not None | ||
| assert activity.entities[0].type == "someFutureEntity" | ||
|
|
||
|
|
||
| def test_populated_channel_data_is_preserved() -> None: | ||
| """Relaxing the required fields must not stop populated values from being parsed.""" | ||
| activity = ActivityTypeAdapter.validate_python( | ||
| _activity( | ||
| channelData={ | ||
| "app": {"id": "app-id", "version": "1.2.3"}, | ||
| "channel": {"id": "channel-id"}, | ||
| "team": {"id": "team-id"}, | ||
| "tenant": {"id": "tenant-id"}, | ||
| "settings": {"selectedChannel": {"id": "selected-channel-id"}}, | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| channel_data = activity.channel_data | ||
| assert channel_data is not None | ||
| assert channel_data.app is not None | ||
| assert channel_data.app.id == "app-id" | ||
| assert channel_data.app.version == "1.2.3" | ||
| assert channel_data.channel is not None | ||
| assert channel_data.channel.id == "channel-id" | ||
| assert channel_data.team is not None | ||
| assert channel_data.team.id == "team-id" | ||
| assert channel_data.tenant is not None | ||
| assert channel_data.tenant.id == "tenant-id" | ||
| assert channel_data.settings is not None | ||
| assert channel_data.settings.selected_channel is not None | ||
| assert channel_data.settings.selected_channel.id == "selected-channel-id" | ||
|
|
||
|
|
||
| def test_absent_channel_data_still_parses() -> None: | ||
| """The pre-existing behaviour for a wholly absent channelData must be unchanged.""" | ||
| assert ActivityTypeAdapter.validate_python(_activity()).channel_data is None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.