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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
"""Tests for python-rt / REST2 - Python interface to Request Tracker :term:`API`."""
# ruff: noqa: S101, S105, S311
__license__ = """ Copyright (C) 2013 CZ.NIC, z.s.p.o.
Copyright (c) 2021 CERT Gouvernemental (GOVCERT.LU)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__docformat__ = 'reStructuredText en'
__authors__ = [
'"Jiri Machalek" <jiri.machalek@nirt_connection.cz>',
'"Georges Toth" <georges.toth@govcert.etat.lu>',
]
import base64
import random
import string
import typing
import httpx
import pytest
import rt.exceptions
import rt.rest2
from . import random_string
RT_URL = 'http://localhost:8080/REST/2.0/'
RT_USER = 'root'
RT_PASSWORD = 'password'
RT_QUEUE = 'General'
def test_get_user(rt_connection: rt.rest2.Rt):
user = rt_connection.get_user(RT_USER)
assert user['Name'] == RT_USER
assert '@' in user['EmailAddress']
assert user['Privileged'] == 1
def test_invalid_api_url():
with pytest.raises(ValueError):
rt.rest2.Rt(url='https://example.com', http_auth=httpx.BasicAuth('dummy', 'dummy'))
def test_ticket_operations(rt_connection: rt.rest2.Rt):
ticket_subject = f'Testing issue {random_string()}'
ticket_text = (
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
)
# empty search result
search_result = list(rt_connection.search(Subject=ticket_subject))
assert not len(search_result)
# create
ticket_id = rt_connection.create_ticket(subject=ticket_subject, content=ticket_text, queue=RT_QUEUE)
assert ticket_id > -1
# search
search_result = list(rt_connection.search(Subject=ticket_subject))
assert len(search_result) == 1
assert search_result[0]['id'] == str(ticket_id)
assert search_result[0]['Status'] == 'new'
# search with query_format field list
search_result = list(rt_connection.search(Subject=ticket_subject, query_format=['Status','Subject']))
assert len(search_result) == 1
assert search_result[0]['Subject'] == str(ticket_subject)
assert search_result[0]['Status'] == 'new'
assert 'Requestor' not in search_result[0].keys()
# raw search
search_result = list(rt_connection.search(raw_query=f'Subject="{ticket_subject}"'))
assert len(search_result) == 1
assert search_result[0]['id'] == str(ticket_id)
assert search_result[0]['Status'] == 'new'
# get ticket
ticket = rt_connection.get_ticket(ticket_id)
search_result[0]['id'] = int(search_result[0]['id'])
for k in search_result[0]:
if k.startswith('_') or k in ('type', 'CustomFields'):
continue
assert k in ticket
assert ticket[k] == search_result[0][k]
# edit ticket
requestors = ['tester1@example.com', 'tester2@example.com']
rt_connection.edit_ticket(ticket_id, Status='open', Requestor=requestors)
# get ticket (edited)
ticket = rt_connection.get_ticket(ticket_id)
assert ticket['Status'] == 'open'
for requestor in ticket['Requestor']:
assert requestor['id'] in requestors
for requestor in requestors:
found = False
for _requestor in ticket['Requestor']:
if _requestor['id'] == requestor:
found = True
break
assert found
# get history
hist = rt_connection.get_ticket_history(ticket_id)
assert len(hist) > 0
transaction = rt_connection.get_transaction(hist[0]['id'])
found = False
for hyperlink in transaction['_hyperlinks']:
if hyperlink['ref'] == 'attachment':
attachment_id = hyperlink['_url'].rsplit('/', 1)[1]
attachment = base64.b64decode(rt_connection.get_attachment(attachment_id)['Content']).decode('utf-8')
found = True
assert attachment == ticket_text
break
assert found
# get_short_history
short_hist = rt_connection.get_ticket_history(ticket_id)
assert len(short_hist) > 0
assert short_hist[0]['Type'] == 'Create'
assert short_hist[0]['Creator']['Name'] == RT_USER
# create 2nd ticket
ticket2_subject = 'Testing issue ' + ''.join([random.choice(string.ascii_letters) for i in range(15)])
ticket2_id = rt_connection.create_ticket(queue=RT_QUEUE, subject=ticket2_subject)
assert ticket2_id > -1
# edit link
assert rt_connection.edit_link(ticket_id, 'DependsOn', ticket2_id)
# get links
links1 = rt_connection.get_links(ticket_id)
found = False
for link in links1:
if link['ref'] == 'depends-on' and link['id'] == str(ticket2_id):
found = True
assert found
links2 = rt_connection.get_links(ticket2_id)
found = False
for link in links2:
if link['ref'] == 'depended-on-by' and link['id'] == str(ticket_id):
found = True
assert found
# reply with attachment
attachment_content = b'Content of attachment.'
attachment_name = 'attachment-name.txt'
reply_text = 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
attachment = rt.rest2.Attachment(attachment_name, 'text/plain', attachment_content)
assert rt_connection.reply(ticket_id, content=reply_text, attachments=[attachment])
# reply with a comment
reply_text = 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
assert rt_connection.comment(ticket_id, content=reply_text)
# reply with a html content
reply_text = '<em>Ut enim ad minim veniam</em>, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
assert rt_connection.reply(ticket_id, content=reply_text, content_type='text/html')
# attachments list
at_list = rt_connection.get_attachments(ticket_id)
assert at_list
at_names = [at['Filename'] for at in at_list]
assert attachment_name in at_names
# get the attachment and compare it's content
at_id = at_list[at_names.index(attachment_name)]['id']
at_content = base64.b64decode(rt_connection.get_attachment(at_id)['Content'])
assert at_content == attachment_content
# set invalid user
with pytest.raises(rt.exceptions.NotFoundError):
rt_connection.edit_ticket(ticket_id, Owner='invalid_user')
# set invalid queue
with pytest.raises(rt.exceptions.NotFoundError):
rt_connection.edit_ticket(ticket_id, Queue='invalid_queue')
# edit invalid ticket
with pytest.raises(rt.exceptions.NotFoundError):
rt_connection.edit_ticket(999999999, Owner='Nobody')
# merge tickets
assert rt_connection.merge_ticket(ticket2_id, ticket_id)
# delete ticket
assert rt_connection.delete_ticket(ticket_id) is None
# delete invalid ticket
with pytest.raises(rt.exceptions.NotFoundError):
assert rt_connection.delete_ticket(999999999)
def test_attachments_create(rt_connection: rt.rest2.Rt):
"""Create a ticket with a random (>= 2) number of attachments and verify that they have been successfully added to the ticket."""
ticket_subject = f'Testing issue {random_string()}'
ticket_text = (
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
)
attachment_count = random.randint(2, 10)
attachments = []
for _i in range(attachment_count):
attachment_content = random_string(length=100).encode()
attachment_name = f'attachment-{random_string(length=10)}.txt'
attachments.append(rt.rest2.Attachment(attachment_name, 'text/plain', attachment_content))
# create
ticket_id = rt_connection.create_ticket(subject=ticket_subject, content=ticket_text, queue=RT_QUEUE, attachments=attachments)
assert ticket_id > -1
# get ticket
ticket = rt_connection.get_ticket(ticket_id)
assert int(ticket['id']) == ticket_id
# attachments list
at_list = rt_connection.get_attachments(ticket_id)
assert at_list
assert len(at_list) == len(attachments)
at_names = [at['Filename'] for at in at_list]
for k in attachments:
assert k.file_name in at_names
# get the attachment and compare it's content
at_id = at_list[at_names.index(k.file_name)]['id']
at_content = base64.b64decode(rt_connection.get_attachment(at_id)['Content'])
assert at_content == k.file_content
def test_attachments_comment(rt_connection: rt.rest2.Rt):
"""Create a ticket and comment to it with a random (>= 2) number of attachments and verify that they have been successfully added to the ticket."""
ticket_subject = f'Testing issue {random_string()}'
ticket_text = (
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
)
# create
ticket_id = rt_connection.create_ticket(subject=ticket_subject, content=ticket_text, queue=RT_QUEUE)
assert ticket_id > -1
attachment_count = random.randint(2, 10)
attachments = []
for _i in range(attachment_count):
attachment_content = random_string(length=100).encode()
attachment_name = f'attachment-{random_string(length=10)}.txt'
attachments.append(rt.rest2.Attachment(attachment_name, 'text/plain', attachment_content))
# comment with attachments
ticket_text = (
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
)
comment_success = rt_connection.comment(ticket_id=ticket_id, content=ticket_text, attachments=attachments)
assert comment_success
# attachments list
at_list = rt_connection.get_attachments(ticket_id)
assert at_list
assert len(at_list) == len(attachments)
at_names = [at['Filename'] for at in at_list]
for k in attachments:
assert k.file_name in at_names
# get the attachment and compare it's content
at_id = at_list[at_names.index(k.file_name)]['id']
at_content = base64.b64decode(rt_connection.get_attachment(at_id)['Content'])
assert at_content == k.file_content
def test_attachments_reply(rt_connection: rt.rest2.Rt):
"""Create a ticket and reply to it with a random (>= 2) number of attachments and verify that they have been successfully added to the ticket."""
ticket_subject = f'Testing issue {random_string()}'
ticket_text = (
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
)
# create
ticket_id = rt_connection.create_ticket(subject=ticket_subject, content=ticket_text, queue=RT_QUEUE)
assert ticket_id > -1
attachment_count = random.randint(2, 10)
attachments = []
for _i in range(attachment_count):
attachment_content = random_string(length=100).encode()
attachment_name = f'attachment-{random_string(length=10)}.txt'
attachments.append(rt.rest2.Attachment(attachment_name, 'text/plain', attachment_content))
# comment with attachments
ticket_text = (
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
)
comment_success = rt_connection.reply(ticket_id=ticket_id, content=ticket_text, attachments=attachments)
assert comment_success
# attachments list
at_list = rt_connection.get_attachments(ticket_id)
assert at_list
assert len(at_list) == len(attachments)
at_names = [at['Filename'] for at in at_list]
for k in attachments:
assert k.file_name in at_names
# get the attachment and compare it's content
at_id = at_list[at_names.index(k.file_name)]['id']
at_content = base64.b64decode(rt_connection.get_attachment(at_id)['Content'])
assert at_content == k.file_content
def test_ticket_operations_admincc_cc(rt_connection: rt.rest2.Rt):
ticket_subject = f'Testing issue {random_string()}'
ticket_text = (
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
)
def compare_list(from_list: typing.List[str], ticket_list: typing.List[dict]) -> bool:
"""Lists (Requestor, AdminCc, Cc) returned from REST2 contain a list of dicts with additional user information.
Thus a simple compare of both lists is not enough.
"""
if len(from_list) != len(ticket_list):
return False
to_list = [entry['id'] for entry in ticket_list]
diff = set(from_list) ^ set(to_list)
return not diff
ticket_id = rt_connection.create_ticket(subject=ticket_subject, content=ticket_text, queue=RT_QUEUE)
assert ticket_id > -1
# make sure Requestor, AdminCc and Cc are present and an empty list, as would be expected
ticket = rt_connection.get_ticket(ticket_id)
assert len(ticket['Requestor']) >= 0
assert len(ticket['AdminCc']) >= 0
assert len(ticket['Cc']) >= 0
# set requestors
requestors = ['tester1@example.com', 'tester2@example.com']
rt_connection.edit_ticket(ticket_id, Status='open', Requestor=requestors)
# verify
ticket = rt_connection.get_ticket(ticket_id)
assert compare_list(requestors, ticket['Requestor'])
# set admincc
admincc = ['tester2@example.com', 'tester3@example.com']
rt_connection.edit_ticket(ticket_id, Status='open', AdminCc=admincc)
# verify
ticket = rt_connection.get_ticket(ticket_id)
assert compare_list(requestors, ticket['Requestor'])
assert compare_list(admincc, ticket['AdminCc'])
# update admincc
admincc = ['tester2@example.com', 'tester3@example.com', 'tester4@example.com']
rt_connection.edit_ticket(ticket_id, Status='open', AdminCc=admincc)
# verify
ticket = rt_connection.get_ticket(ticket_id)
assert compare_list(requestors, ticket['Requestor'])
assert compare_list(admincc, ticket['AdminCc'])
# unset requestors and admincc
requestors = []
admincc = []
rt_connection.edit_ticket(ticket_id, Status='open', Requestor=requestors, AdminCc=admincc)
# verify
ticket = rt_connection.get_ticket(ticket_id)
assert compare_list(requestors, ticket['Requestor'])
assert compare_list(admincc, ticket['AdminCc'])
def test_users(rt_connection: rt.rest2.Rt):
assert rt_connection.get_user('tester1@example.com') is not None
assert rt_connection.user_exists('root', privileged=True) is True
assert rt_connection.user_exists('tester1@example.com', privileged=False) is True
assert rt_connection.user_exists('tester1@example.com', privileged=True) is False
assert rt_connection.user_exists('does-not-exist@example.com') is False
random_user_name = f'username_{random_string()}'
random_user_email = f'user-{random_string()}@example.com'
random_user_password = f'user_password-{random_string()}'
assert rt_connection.create_user(random_user_name, random_user_email, Password=random_user_password) == random_user_name
rt_connection.delete_user(random_user_name)
with pytest.raises(rt.exceptions.NotFoundError):
rt_connection.delete_user(f'username_{random_string()}')
assert rt_connection.edit_user(user_id=random_user_name, RealName=random_user_name)
def test_queues(rt_connection: rt.rest2.Rt):
queue = rt_connection.get_queue(RT_QUEUE)
assert queue['Name'] == RT_QUEUE
queues = rt_connection.get_all_queues()
assert len(queues) >= 1
found = False
for q in queues:
if q['Name'] == RT_QUEUE:
found = True
assert found
random_queue_name = f'Queue {random_string()}'
random_queue_email = f'q-{random_string()}@example.com'
rt_connection.create_queue(random_queue_name, CorrespondAddress=random_queue_email, Description='test queue')
queues = rt_connection.get_all_queues()
found = False
for q in queues:
if q['Name'] == random_queue_name:
found = True
assert found
rt_connection.edit_queue(random_queue_name, Disabled='1')
assert rt_connection.get_queue(random_queue_name)['Disabled'] == '1'
rt_connection.edit_queue(random_queue_name, Disabled='0')
assert rt_connection.get_queue(random_queue_name)['Disabled'] == '0'
with pytest.raises(rt.exceptions.NotFoundError):
rt_connection.get_queue('InvalidName')
rt_connection.delete_queue(random_queue_name)
with pytest.raises(rt.exceptions.NotFoundError):
rt_connection.delete_queue(f'Queue {random_string()}')
|