-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathtest_messages.py
386 lines (290 loc) · 10.9 KB
/
test_messages.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""WebexAPI Messages API fixtures and tests.
Copyright (c) 2016-2024 Cisco and/or its affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import itertools
import json
import os
import pytest
import webexpythonsdk
from tests.environment import WEBEX_TEST_FILE_URL
from tests.utils import create_string
# Module Variables
adaptive_card_path = os.path.abspath(
os.path.join(__file__, os.pardir, "adaptive_card.json")
)
# Helper Functions
def is_valid_message(obj):
return isinstance(obj, webexpythonsdk.Message) and obj.id is not None
def are_valid_messages(iterable):
return all([is_valid_message(obj) for obj in iterable])
# Fixtures
@pytest.fixture(scope="session")
def adaptive_card():
with open(adaptive_card_path) as file:
card = json.load(file)
return card
@pytest.fixture(scope="session")
def direct_message_by_person_email(api, test_people):
person = test_people["member_added_by_email"]
message = api.messages.create(
toPersonEmail=person.emails[0],
text=create_string("Message"),
)
yield message
api.messages.delete(message.id)
@pytest.fixture(scope="session")
def direct_message_reply_by_person_email(api, direct_message_by_person_email):
text = create_string("Reply Message")
return api.messages.create(
toPersonEmail=direct_message_by_person_email.toPersonEmail,
parentId=direct_message_by_person_email.id,
text=text,
)
@pytest.fixture(scope="session")
def direct_message_by_email_with_card(api, test_people, adaptive_card):
person = test_people["member_added_by_email"]
message = api.messages.create(
toPersonEmail=person.emails[0],
text=create_string("Message"),
attachments=[
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": adaptive_card,
}
],
)
yield message
api.messages.delete(message.id)
@pytest.fixture(scope="session")
def direct_message_by_person_id(api, test_people):
person = test_people["member_added_by_id"]
message = api.messages.create(
toPersonId=person.id,
text=create_string("Message"),
)
yield message
api.messages.delete(message.id)
@pytest.fixture(scope="session")
def direct_message_reply_by_person_id(api, direct_message_by_person_id):
text = create_string("Reply Message")
return api.messages.create(
toPersonId=direct_message_by_person_id.toPersonId,
parentId=direct_message_by_person_id.id,
text=text,
)
@pytest.fixture(scope="session")
def send_group_room_message(api):
messages = []
def inner_function(room_id, **message_attributes):
new_message = api.messages.create(roomId=room_id, **message_attributes)
messages.append(new_message)
return new_message
yield inner_function
for message in messages:
try:
api.messages.delete(message.id)
except webexpythonsdk.ApiError:
pass
@pytest.fixture(scope="session")
def group_room_text_message(group_room, send_group_room_message):
text = create_string("Message")
return send_group_room_message(group_room.id, text=text)
@pytest.fixture(scope="session")
def group_room_message_reply_by_id(api, group_room, group_room_text_message):
text = create_string("Reply Message")
return api.messages.create(
# roomId=group_room.id,
parentId=group_room_text_message.id,
text=text,
)
@pytest.fixture(scope="session")
def group_room_markdown_message(
me,
group_room,
send_group_room_message,
group_room_text_message,
):
# Uses / depends-on group_room_text_message to ensure this message is
# created after group_room_text_message, so that we can be sure that a
# message exists 'before' this one - used to test 'before' list filters.
markdown = create_string(
"<@personId:{id}|{name}>, This is **markdown** with a mention."
"".format(id=me.id, name=me.displayName)
)
return send_group_room_message(group_room.id, markdown=markdown)
@pytest.fixture(scope="session")
def group_room_message_with_file_by_url(group_room, send_group_room_message):
text = "File posted via URL"
return send_group_room_message(
room_id=group_room.id, text=text, files=[WEBEX_TEST_FILE_URL]
)
@pytest.fixture(scope="session")
def group_room_message_with_file_by_local_upload(
api,
group_room,
local_file,
send_group_room_message,
):
text = "File posted via URL"
return send_group_room_message(
room_id=group_room.id,
text=text,
files=[local_file],
)
@pytest.fixture(scope="session")
def group_room_message_with_card(
api,
group_room,
adaptive_card,
send_group_room_message,
):
return send_group_room_message(
room_id=group_room.id,
text=create_string("Message"),
attachments=[
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": adaptive_card,
}
],
)
@pytest.fixture(scope="session")
def group_room_messages(
api,
group_room,
group_room_text_message,
group_room_markdown_message,
group_room_message_with_file_by_url,
group_room_message_with_file_by_local_upload,
group_room_message_with_card,
):
return list(api.messages.list(group_room.id))
@pytest.fixture(scope="session")
def direct_messages(
direct_message_by_person_email,
direct_message_by_person_id,
direct_message_by_email_with_card,
):
return [
direct_message_by_person_email,
direct_message_by_person_id,
direct_message_by_email_with_card,
]
# Tests
def test_list_all_messages(group_room_messages):
assert len(group_room_messages) >= 1
assert are_valid_messages(group_room_messages)
def test_list_messages_with_paging(api, group_room, group_room_messages):
page_size = 1
pages = 3
num_messages = pages * page_size
assert len(group_room_messages) >= num_messages
messages = api.messages.list(group_room.id, max=page_size)
messages_list = list(itertools.islice(messages, num_messages))
assert len(messages_list) == num_messages
assert are_valid_messages(messages_list)
def test_list_messages_before_datetime(
api, group_room, group_room_markdown_message
):
message_list = list(
api.messages.list(
roomId=group_room.id,
before=str(group_room_markdown_message.created),
)
)
assert len(message_list) >= 1
assert are_valid_messages(message_list)
def test_list_messages_before_message_with_id(
api, group_room, group_room_markdown_message
):
message_list = list(
api.messages.list(
roomId=group_room.id,
beforeMessage=group_room_markdown_message.id,
)
)
assert len(message_list) >= 1
assert are_valid_messages(message_list)
@pytest.mark.xfail(
reason="API Error: The API is not returning the expected results"
)
def test_list_messages_mentioning_me(
api, group_room, group_room_markdown_message
):
messages = api.messages.list(group_room.id, mentionedPeople="me")
messages_list = list(messages)
assert len(messages_list) >= 1
assert are_valid_messages(messages_list)
def test_create_direct_messages_by_email(direct_message_by_person_email):
assert is_valid_message(direct_message_by_person_email)
def test_reply_to_direct_message_by_person_email(
direct_message_reply_by_person_email,
):
assert is_valid_message(direct_message_reply_by_person_email)
def test_create_direct_messages_by_email_with_card(
direct_message_by_email_with_card,
):
assert is_valid_message(direct_message_by_email_with_card)
def test_create_direct_messages_by_id(direct_message_by_person_id):
assert is_valid_message(direct_message_by_person_id)
def test_reply_to_direct_message_by_person_id(
direct_message_reply_by_person_id,
):
assert is_valid_message(direct_message_reply_by_person_id)
def test_list_direct_messages_by_person_id(api, direct_message_by_person_id):
messages = api.messages.list_direct(
personId=direct_message_by_person_id.toPersonId,
)
assert are_valid_messages(messages)
def test_list_direct_messages_by_person_email(
api, direct_message_by_person_email
):
messages = api.messages.list_direct(
personEmail=direct_message_by_person_email.toPersonEmail,
)
assert are_valid_messages(messages)
def test_create_text_message(group_room_text_message):
assert is_valid_message(group_room_text_message)
def test_create_reply_message(group_room_text_message):
assert is_valid_message(group_room_text_message)
def test_create_markdown_message(group_room_markdown_message):
assert is_valid_message(group_room_markdown_message)
def test_post_file_by_url(group_room_message_with_file_by_url):
assert is_valid_message(group_room_message_with_file_by_url)
def test_post_file_by_local_upload(
group_room_message_with_file_by_local_upload,
):
assert is_valid_message(group_room_message_with_file_by_local_upload)
def test_get_message_by_id(api, group_room_text_message):
message = api.messages.get(group_room_text_message.id)
assert is_valid_message(message)
def test_delete_message(api, group_room, send_group_room_message):
text = create_string("Message")
message = api.messages.create(group_room.id, text=text)
assert is_valid_message(message)
api.messages.delete(message.id)
def test_edit_message(api, group_room):
text = create_string("Edit this Message")
message = api.messages.create(group_room.id, text=text)
text = create_string("Message Edited")
assert text == api.messages.edit(message.id, group_room.id, text).text
def test_update_message(api, group_room):
text = create_string("Update this Message")
message = api.messages.create(group_room.id, text=text)
text = create_string("Message Updated")
assert text == api.messages.edit(message.id, group_room.id, text).text