Skip to content

Commit 02e4224

Browse files
authored
Validate date string (Azure#25967)
# Description Please add an informative description that covers that changes made by the pull request and link all relevant issues. If an SDK is being regenerated based on a new swagger spec, a link to the pull request containing these swagger spec changes has been included above. # All SDK Contribution checklist: - [ ] **The pull request does not introduce [breaking changes]** - [ X] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [ ] **I have read the [contribution guidelines](https://github.com/Azure/azure-sdk-for-python/blob/main/CONTRIBUTING.md).** ## General Guidelines and Best Practices - [ ] Title of the pull request is clear and informative. - [ ] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### [Testing Guidelines](https://github.com/Azure/azure-sdk-for-python/blob/main/CONTRIBUTING.md##building-and-testing) - [ X] Pull request includes test coverage for the included changes.
1 parent 92d121c commit 02e4224

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

sdk/communication/azure-communication-rooms/CHANGELOG.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
# Release History
22

3-
## 1.0.0b2 (Unreleased)
4-
5-
### Features Added
6-
7-
### Breaking Changes
3+
## 1.0.0b2 (2022-08-30)
84

95
### Bugs Fixed
6+
- Invalid datestrings for `valid_from` and `valid_until` raises exception
107

118
### Other Changes
129
Python 3.6 is no longer supported. Please use Python version 3.7 or later. For more details, please read our page on [Azure SDK for Python version support policy](https://github.com/Azure/azure-sdk-for-python/wiki/Azure-SDKs-Python-version-support-policy).

sdk/communication/azure-communication-rooms/azure/communication/rooms/_rooms_client.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
RoomJoinPolicy
2626
)
2727

28-
from ._shared.utils import parse_connection_str
28+
from ._shared.utils import parse_connection_str, verify_datetime_format
2929
from ._version import SDK_MONIKER
3030
from ._api_versions import DEFAULT_VERSION
3131

@@ -117,23 +117,24 @@ def create_room(
117117
:rtype: ~azure.communication.rooms.CommunicationRoom
118118
:raises: ~azure.core.exceptions.HttpResponseError
119119
"""
120-
create_room_request = CreateRoomRequest(
121-
valid_from=valid_from,
122-
valid_until=valid_until,
123-
room_join_policy=room_join_policy,
124-
# pylint: disable=protected-access
125-
participants=[p._to_room_participant_internal() for p in participants] if participants else None
126-
)
127-
128-
repeatability_request_id = uuid.uuid1()
129-
repeatability_first_sent = datetime.utcnow()
130-
131-
create_room_response = self._rooms_service_client.rooms.create_room(
132-
create_room_request=create_room_request,
133-
repeatability_request_id=repeatability_request_id,
134-
repeatability_first_sent=repeatability_first_sent,
135-
**kwargs)
136-
return CommunicationRoom._from_room_response(create_room_response) # pylint: disable=protected-access
120+
if verify_datetime_format(valid_from) and verify_datetime_format(valid_until):
121+
create_room_request = CreateRoomRequest(
122+
valid_from=valid_from,
123+
valid_until=valid_until,
124+
room_join_policy=room_join_policy,
125+
# pylint: disable=protected-access
126+
participants=[p._to_room_participant_internal() for p in participants] if participants else None
127+
)
128+
129+
repeatability_request_id = uuid.uuid1()
130+
repeatability_first_sent = datetime.utcnow()
131+
132+
create_room_response = self._rooms_service_client.rooms.create_room(
133+
create_room_request=create_room_request,
134+
repeatability_request_id=repeatability_request_id,
135+
repeatability_first_sent=repeatability_first_sent,
136+
**kwargs)
137+
return CommunicationRoom._from_room_response(create_room_response) # pylint: disable=protected-access
137138

138139
@distributed_trace
139140
def delete_room(
@@ -182,18 +183,18 @@ def update_room(
182183
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
183184
184185
"""
185-
186-
update_room_request = UpdateRoomRequest(
187-
valid_from=valid_from,
188-
valid_until=valid_until,
189-
room_join_policy=room_join_policy,
186+
if verify_datetime_format(valid_from) and verify_datetime_format(valid_until):
187+
update_room_request = UpdateRoomRequest(
188+
valid_from=valid_from,
189+
valid_until=valid_until,
190+
room_join_policy=room_join_policy,
191+
# pylint: disable=protected-access
192+
participants=[p._to_room_participant_internal() for p in participants] if participants else None
193+
)
194+
update_room_response = self._rooms_service_client.rooms.update_room(
195+
room_id=room_id, patch_room_request=update_room_request, **kwargs)
190196
# pylint: disable=protected-access
191-
participants=[p._to_room_participant_internal() for p in participants] if participants else None
192-
)
193-
update_room_response = self._rooms_service_client.rooms.update_room(
194-
room_id=room_id, patch_room_request=update_room_request, **kwargs)
195-
# pylint: disable=protected-access
196-
return CommunicationRoom._from_room_response(update_room_response)
197+
return CommunicationRoom._from_room_response(update_room_response)
197198

198199
@distributed_trace
199200
def get_room(

sdk/communication/azure-communication-rooms/azure/communication/rooms/_shared/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Tuple,
1212
)
1313
from datetime import datetime
14+
import isodate
1415
from msrest.serialization import TZ_UTC
1516
from azure.core.credentials import AccessToken
1617

@@ -66,6 +67,20 @@ def get_current_utc_as_int():
6667
return _convert_datetime_to_utc_int(current_utc_datetime)
6768

6869

70+
def verify_datetime_format(input_datetime):
71+
#type: (datetime) -> bool
72+
if input_datetime is None:
73+
return True
74+
try:
75+
if isinstance(input_datetime, str):
76+
input_datetime = isodate.parse_datetime(input_datetime)
77+
if isinstance(input_datetime, datetime):
78+
return True
79+
except:
80+
raise ValueError("{} is not a valid ISO-8601 datetime format".format(input_datetime)) from None
81+
return True
82+
83+
6984
def create_access_token(token):
7085
# type: (str) -> azure.core.credentials.AccessToken
7186
"""Creates an instance of azure.core.credentials.AccessToken from a

sdk/communication/azure-communication-rooms/tests/test_rooms_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,16 @@ def mock_send(*_, **__):
249249

250250
self.assertFalse(raised, 'Expected is no excpetion raised')
251251
self.assertListEqual(response.participants, [self.room_participant])
252+
253+
def test_invalid_datetime_raises_valueError(self):
254+
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=None)
255+
256+
# verify value error on create room call
257+
self.assertRaises(ValueError, rooms_client.create_room, valid_from="dummy", valid_until=self.valid_until)
258+
self.assertRaises(ValueError, rooms_client.create_room, valid_from=self.valid_from, valid_until="dummy")
259+
self.assertRaises(ValueError, rooms_client.create_room, valid_from="dummy", valid_until="dummy")
260+
261+
# verify ValueError on update_room calls
262+
self.assertRaises(ValueError, rooms_client.update_room, room_id=self.room_id, valid_from="dummy", valid_until=self.valid_until)
263+
self.assertRaises(ValueError, rooms_client.update_room, room_id=self.room_id, valid_from=self.valid_from, valid_until="dummy")
264+
self.assertRaises(ValueError, rooms_client.update_room, room_id=self.room_id, valid_from="dummy", valid_until="dummy")

0 commit comments

Comments
 (0)