1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
# cSpell:ignore aiounittest
import aiounittest
import datetime
from azure.core.credentials import AzureKeyCredential
from azure.communication.rooms.aio import RoomsClient
from azure.communication.rooms import (
RoomParticipant,
RoomJoinPolicy,
RoleType
)
from azure.communication.rooms._shared.models import CommunicationUserIdentifier, UnknownIdentifier
from unittest_helpers import mock_response
from unittest.mock import Mock
class TestRoomsClient(aiounittest.AsyncTestCase):
room_id = "999126454"
valid_from = datetime.datetime(2022, 2, 25, 4, 34, 0)
valid_until = datetime.datetime(2022, 4, 25, 4, 34, 0)
room_join_policy = RoomJoinPolicy.INVITE_ONLY
raw_id = "8:acs:abcd"
room_participant = RoomParticipant(
communication_identifier=CommunicationUserIdentifier(
id=raw_id
),
role=RoleType.ATTENDEE
)
json_participant = {
"communicationIdentifier": {
"rawId": raw_id,
"communicationUser": {"id": raw_id}
},
"role": "Attendee"
}
async def test_create_room(self):
raised = False
async def mock_send(*_, **__):
return mock_response(status_code=201, json_payload={
"id": self.room_id,
"createdDateTime": "2022-08-28T01:38:19.0359921+00:00",
"validFrom": self.valid_from.strftime("%Y-%m-%dT%H:%M:%S.%f"),
"validUntil": self.valid_until.strftime("%Y-%m-%dT%H:%M:%S.%f"),
"roomJoinPolicy": self.room_join_policy,
"participants": [self.json_participant]
})
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
response = None
try:
response = await rooms_client.create_room(valid_from=self.valid_from, valid_until=self.valid_until, participants=[self.room_participant])
except:
raised = True
raise
self.assertFalse(raised, 'Expected is no excpetion raised')
self.assertEqual(self.room_id, response.id)
self.assertEqual(self.valid_from, response.valid_from)
self.assertEqual(self.valid_until, response.valid_until)
self.assertEqual(self.room_join_policy, response.room_join_policy)
self.assertListEqual([self.room_participant], response.participants)
async def test_update_room(self):
raised = False
async def mock_send(*_, **__):
return mock_response(status_code=200, json_payload={
"id": self.room_id,
"createdDateTime": "2022-08-28T01:38:19.0359921+00:00",
"validFrom": self.valid_from.strftime("%Y-%m-%dT%H:%M:%S.%f"),
"validUntil": self.valid_until.strftime("%Y-%m-%dT%H:%M:%S.%f"),
"roomJoinPolicy": self.room_join_policy,
"participants": []
})
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
response = None
try:
response = await rooms_client.update_room(
room_id=self.room_id,
valid_from=self.valid_from,
valid_until=self.valid_until,
room_join_policy=self.room_join_policy)
except:
raised = True
raise
self.assertFalse(raised, 'Expected is no excpetion raised')
self.assertEqual(self.room_id, response.id)
self.assertEqual(self.valid_from, response.valid_from)
self.assertEqual(self.valid_until, response.valid_until)
self.assertEqual(self.room_join_policy, response.room_join_policy)
self.assertListEqual(response.participants, [])
async def test_delete_room_raises_error(self):
raised = False
async def mock_send(*_, **__):
return mock_response(status_code=404, json_payload={"msg": "some error"})
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
try:
await rooms_client.delete_room(room_id=self.room_id)
except:
raised = True
assert raised == True
async def test_get_room(self):
raised = False
async def mock_send(*_, **__):
return mock_response(status_code=200, json_payload={
"id": self.room_id,
"createdDateTime": "2022-08-28T01:38:19.0359921+00:00",
"validFrom": self.valid_from.strftime("%Y-%m-%dT%H:%M:%S.%f"),
"validUntil": self.valid_until.strftime("%Y-%m-%dT%H:%M:%S.%f"),
"roomJoinPolicy": self.room_join_policy,
"participants": []
})
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
response = None
try:
response = await rooms_client.get_room(room_id=self.room_id)
except:
raised = True
raise
self.assertFalse(raised, 'Expected is no excpetion raised')
self.assertEqual(self.room_id, response.id)
self.assertEqual(self.valid_from, response.valid_from)
self.assertEqual(self.valid_until, response.valid_until)
self.assertEqual(self.room_join_policy, response.room_join_policy)
self.assertListEqual(response.participants, [])
async def test_get_room_raises_error(self):
raised = False
async def mock_send(*_, **__):
return mock_response(status_code=404, json_payload={"msg": "some error"})
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
try:
await rooms_client.get_room(room_id=self.room_id)
except:
raised = True
assert raised == True
async def test_add_participants(self):
raised = False
additional_id = "8:acs:abcde"
additional_participant_json = {
"communicationIdentifier": {
"rawId": additional_id
},
"role": ""
}
additional_participant = RoomParticipant(
communication_identifier=UnknownIdentifier(additional_id),
role=''
)
async def mock_send(*_, **__):
return mock_response(status_code=200, json_payload={
"participants": [self.json_participant, additional_participant_json]
})
response = None
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
try:
response = await rooms_client.add_participants(room_id=self.room_id, participants=[additional_participant])
except:
raised = True
raise
self.assertFalse(raised, 'Expected is no excpetion raised')
self.assertEqual(None, response)
async def test_update_participants(self):
raised = False
updated_participant = RoomParticipant(
communication_identifier=CommunicationUserIdentifier(
id=self.raw_id
),
role=''
)
async def mock_send(*_, **__):
return mock_response(status_code=200, json_payload={
"participants": [self.json_participant]
})
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
response = None
try:
response = await rooms_client.update_participants(room_id=self.room_id, participants=[updated_participant])
except:
raised = True
raise
self.assertFalse(raised, 'Expected is no excpetion raised')
self.assertEqual(None, response)
async def test_remove_participants(self):
raised = False
user_to_remove = CommunicationUserIdentifier(self.raw_id)
async def mock_send(*_, **__):
return mock_response(status_code=200, json_payload={
"participants": []
})
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
response = None
try:
response = await rooms_client.remove_participants(room_id=self.room_id, communication_identifiers=[user_to_remove])
except:
raised = True
raise
self.assertFalse(raised, 'Expected is no excpetion raised')
self.assertEqual(None, response)
async def test_get_participants(self):
raised = False
async def mock_send(*_, **__):
return mock_response(status_code=200, json_payload={
"participants": [self.json_participant]
})
rooms_client = RoomsClient("https://endpoint", AzureKeyCredential("fakeCredential=="), transport=Mock(send=mock_send))
response = None
try:
response = await rooms_client.get_participants(room_id=self.room_id)
except:
raised = True
raise
self.assertFalse(raised, 'Expected is no excpetion raised')
self.assertListEqual(response.participants, [self.room_participant])
|