Skip to content

Commit

Permalink
update and refactor test_tickets.py
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Oct 23, 2023
1 parent 58de91e commit 9db171c
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 6 deletions.
112 changes: 111 additions & 1 deletion tests/test_tickets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from typing import Generator, Tuple

import pytest

from zammadoo.tickets import Ticket


def test_ticket_create_article_sender_attribute(client):
Expand Down Expand Up @@ -51,7 +56,7 @@ def test_ticket_state_attribute(client):
assert ticket.state == client.ticket_states(456)


def test_tickets_create(rclient, temporary_resources):
def test_tickets_create_body_only(rclient, temporary_resources):
with temporary_resources("tickets") as infos:
ticket = rclient.tickets.create(
"__pytest__",
Expand All @@ -66,7 +71,112 @@ def test_tickets_create(rclient, temporary_resources):
assert ticket.organization is None
assert ticket.owner.id == 1
assert ticket.article_count == 1
assert ticket.tags() == []
assert ticket.links() == {"parent": [], "child": [], "normal": []}

article = ticket.articles[0]
assert article.body == "article body"
assert article.internal is False


def test_tickets_create_with_article_params(rclient, temporary_resources):
with temporary_resources("tickets") as infos:
ticket = rclient.tickets.create(
"__pytest__",
"Users",
"guess:pytest@localhost.local",
{"body": "article body", "internal": True},
)
infos.append(ticket.view())
article = ticket.articles[0]
assert article.body == "article body"
assert article.internal is True


@pytest.fixture(scope="function")
def ticket_pair(
rclient, temporary_resources
) -> Generator[Tuple[Ticket, Ticket], None, None]:
tickets = [
{
"title": "__pytest__",
"customer_id": "guess:pytest@localhost.local",
"group": "Users",
"article": {"body": "..."},
},
{
"title": "__pytest__",
"customer_id": "guess:pytest@localhost.local",
"group": "Users",
"article": {"body": "..."},
},
]

with temporary_resources("tickets", *tickets) as infos:
yield tuple(rclient.tickets(info["id"], info=info) for info in infos)


def test_ticket_link_with_and_unlink__normal(ticket_pair):
ticket_a, ticket_b = ticket_pair
ticket_a.link_with(ticket_b)
assert ticket_a.links() == {"parent": [], "child": [], "normal": [ticket_b]}
assert ticket_b.links() == {"parent": [], "child": [], "normal": [ticket_a]}

ticket_a.unlink_from(ticket_b, "normal")
assert ticket_a.links() == {"parent": [], "child": [], "normal": []}
assert ticket_b.links() == {"parent": [], "child": [], "normal": []}


def test_ticket_link_with_and_unlink_from__parent_child(ticket_pair):
ticket_a, ticket_b = ticket_pair
ticket_a.link_with(ticket_b, link_type="child")
assert ticket_a.links() == {"parent": [], "child": [ticket_b], "normal": []}
assert ticket_b.links() == {"parent": [ticket_a], "child": [], "normal": []}

ticket_a.unlink_from(ticket_b)
assert ticket_a.links() == {"parent": [], "child": [], "normal": []}
assert ticket_b.links() == {"parent": [], "child": [], "normal": []}


def test_ticket_merge(ticket_pair):
ticket_a, ticket_b = ticket_pair
b_origin_article = ticket_b.articles[0]

merged_ticket = ticket_b.merge_into(ticket_a.id)
ticket_b.reload()

assert merged_ticket == ticket_a
assert merged_ticket.links() == {"child": [ticket_b], "parent": [], "normal": []}
merged_articles = merged_ticket.articles
assert len(merged_articles) == 2
assert b_origin_article in merged_articles

assert ticket_b.state.name == "merged"
assert ticket_b.links() == {"child": [], "parent": [ticket_a], "normal": []}
b_articles = ticket_b.articles
assert len(b_articles) == 1
assert b_articles[0].body == "merged"


def test_ticket_update(ticket_pair):
ticket, _ = ticket_pair
assert ticket.article_count == 1
updated_ticket = ticket.update(body="new article")
assert updated_ticket.article_count == 2


def test_ticket_create_article(ticket_pair):
ticket, _ = ticket_pair
assert ticket.article_count == 1
new_article = ticket.create_article(body="new article")
assert len(ticket.articles) == 2
assert new_article.body == "new article"


def test_ticket_history(ticket_pair):
ticket, _ = ticket_pair
history = ticket.history()
assert len(history) == 2

assert history[0].items() > {"object": "Ticket", "type": "created"}.items()
assert history[1].items() > {"object": "Ticket::Article", "type": "created"}.items()
12 changes: 12 additions & 0 deletions tests/test_tickets/test_ticket_create_article.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{"method": "POST", "url": "https://localhost/api/v1/tickets#fixture", "status_code": 201, "reason": "Created", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "904"}, "encoding": "utf-8", "content_size": 904}
{"id":76,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67066","title":"__pytest__","owner_id":1,"customer_id":10,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":1,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T20:43:57.192Z","updated_at":"2023-10-23T20:43:57.430Z","article_ids":[128],"ticket_time_accounting_ids":[]}
{"method": "POST", "url": "https://localhost/api/v1/tickets#fixture", "status_code": 201, "reason": "Created", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "904"}, "encoding": "utf-8", "content_size": 904}
{"id":77,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67067","title":"__pytest__","owner_id":1,"customer_id":10,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":1,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T20:43:57.554Z","updated_at":"2023-10-23T20:43:57.809Z","article_ids":[129],"ticket_time_accounting_ids":[]}
{"method": "POST", "url": "https://localhost/api/v1/ticket_articles", "status_code": 201, "reason": "Created", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "569"}, "encoding": "utf-8", "content_size": 569}
{"id":130,"ticket_id":76,"type_id":10,"sender_id":1,"from":"zammadoo admin","to":null,"cc":null,"subject":null,"reply_to":null,"message_id":null,"message_id_md5":null,"in_reply_to":null,"content_type":"text/plain","references":null,"body":"new article","internal":true,"preferences":{},"updated_by_id":3,"created_by_id":3,"origin_by_id":null,"created_at":"2023-10-23T20:43:58.001Z","updated_at":"2023-10-23T20:43:58.001Z","attachments":[],"created_by":"zammadoo@hephaestus.local","updated_by":"zammadoo@hephaestus.local","type":"note","sender":"Agent","time_unit":null}
{"method": "GET", "url": "https://localhost/api/v1/ticket_articles/by_ticket/76", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "1134"}, "encoding": "utf-8", "content_size": 1134}
[{"id":128,"ticket_id":76,"type_id":10,"sender_id":1,"from":"zammadoo admin","to":null,"cc":null,"subject":null,"reply_to":null,"message_id":null,"message_id_md5":null,"in_reply_to":null,"content_type":"text/plain","references":null,"body":"...","internal":false,"preferences":{},"updated_by_id":3,"created_by_id":3,"origin_by_id":null,"created_at":"2023-10-23T20:43:57.309Z","updated_at":"2023-10-23T20:43:57.309Z","attachments":[],"created_by":"zammadoo@hephaestus.local","updated_by":"zammadoo@hephaestus.local","type":"note","sender":"Agent","time_unit":null},{"id":130,"ticket_id":76,"type_id":10,"sender_id":1,"from":"zammadoo admin","to":null,"cc":null,"subject":null,"reply_to":null,"message_id":null,"message_id_md5":null,"in_reply_to":null,"content_type":"text/plain","references":null,"body":"new article","internal":true,"preferences":{},"updated_by_id":3,"created_by_id":3,"origin_by_id":null,"created_at":"2023-10-23T20:43:58.001Z","updated_at":"2023-10-23T20:43:58.001Z","attachments":[],"created_by":"zammadoo@hephaestus.local","updated_by":"zammadoo@hephaestus.local","type":"note","sender":"Agent","time_unit":null}]
{"method": "DELETE", "url": "https://localhost/api/v1/tickets/76#fixture", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "text/html", "Content-Length": "0"}, "encoding": "ISO-8859-1", "content_size": 0}

{"method": "DELETE", "url": "https://localhost/api/v1/tickets/77#fixture", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "text/html", "Content-Length": "0"}, "encoding": "ISO-8859-1", "content_size": 0}

10 changes: 10 additions & 0 deletions tests/test_tickets/test_ticket_history.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{"method": "POST", "url": "https://localhost/api/v1/tickets#fixture", "status_code": 201, "reason": "Created", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "904"}, "encoding": "utf-8", "content_size": 904}
{"id":78,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67068","title":"__pytest__","owner_id":1,"customer_id":10,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":1,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T20:46:50.733Z","updated_at":"2023-10-23T20:46:51.004Z","article_ids":[131],"ticket_time_accounting_ids":[]}
{"method": "POST", "url": "https://localhost/api/v1/tickets#fixture", "status_code": 201, "reason": "Created", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "904"}, "encoding": "utf-8", "content_size": 904}
{"id":79,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67069","title":"__pytest__","owner_id":1,"customer_id":10,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":1,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T20:46:51.135Z","updated_at":"2023-10-23T20:46:51.366Z","article_ids":[132],"ticket_time_accounting_ids":[]}
{"method": "GET", "url": "https://localhost/api/v1/ticket_history/78", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "6697"}, "encoding": "utf-8", "content_size": 6697}
{"history":[{"id":410,"history_attribute_id":null,"sourceable_type":null,"sourceable_id":null,"sourceable_name":null,"o_id":78,"related_history_object_id":null,"created_by_id":3,"created_at":"2023-10-23T20:46:50.733Z","object":"Ticket","type":"created"},{"id":411,"history_attribute_id":null,"sourceable_type":null,"sourceable_id":null,"sourceable_name":null,"o_id":131,"related_o_id":78,"created_by_id":3,"created_at":"2023-10-23T20:46:50.848Z","object":"Ticket::Article","type":"created","related_object":"Ticket"}],"assets":{"User":{"3":{"id":3,"organization_id":3,"login":"zammadoo@hephaestus.local","firstname":"zammadoo","lastname":"admin","email":"zammadoo@hephaestus.local","image":null,"image_source":null,"web":"","phone":"","fax":"","mobile":"","department":"","street":"","zip":"","city":"","country":"","address":"","vip":false,"verified":false,"active":true,"note":"","last_login":"2023-10-19T04:17:31.698Z","source":null,"login_failed":0,"out_of_office":false,"out_of_office_start_at":null,"out_of_office_end_at":null,"out_of_office_replacement_id":null,"preferences":{"notification_config":{"matrix":{"create":{"criteria":{"owned_by_me":true,"owned_by_nobody":true,"subscribed":true,"no":false},"channel":{"email":true,"online":true}},"update":{"criteria":{"owned_by_me":true,"owned_by_nobody":true,"subscribed":true,"no":false},"channel":{"email":true,"online":true}},"reminder_reached":{"criteria":{"owned_by_me":true,"owned_by_nobody":false,"subscribed":false,"no":false},"channel":{"email":true,"online":true}},"escalation":{"criteria":{"owned_by_me":true,"owned_by_nobody":false,"subscribed":false,"no":false},"channel":{"email":true,"online":true}}}},"locale":"de-de","intro":true,"tickets_closed":0,"tickets_open":2,"theme":"dark","notification_sound":{"file":"Xylo.mp3","enabled":false}},"updated_by_id":3,"created_by_id":1,"created_at":"2023-09-22T20:25:51.668Z","updated_at":"2023-10-22T19:33:20.883Z","role_ids":[1,2],"two_factor_preference_ids":[],"organization_ids":[],"authorization_ids":[],"overview_sorting_ids":[],"group_ids":{"1":["full"]}},"1":{"id":1,"organization_id":null,"login":"-","firstname":"-","lastname":"","email":"","image":null,"image_source":null,"web":"","phone":"","fax":"","mobile":"","department":"","street":"","zip":"","city":"","country":"","address":"","vip":false,"verified":false,"active":false,"note":"","last_login":null,"source":null,"login_failed":0,"out_of_office":false,"out_of_office_start_at":null,"out_of_office_end_at":null,"out_of_office_replacement_id":null,"preferences":{"tickets_closed":0,"tickets_open":0},"updated_by_id":3,"created_by_id":1,"created_at":"2023-09-22T20:15:52.579Z","updated_at":"2023-10-23T17:41:54.206Z","role_ids":[3],"two_factor_preference_ids":[],"organization_ids":[],"authorization_ids":[],"overview_sorting_ids":[],"group_ids":{}},"10":{"id":10,"organization_id":null,"login":"pytest@localhost.local","firstname":"","lastname":"","email":"pytest@localhost.local","image":null,"image_source":null,"web":"","phone":"","fax":"","mobile":"","department":null,"street":"","zip":"","city":"","country":"","address":null,"vip":false,"verified":false,"active":true,"note":"","last_login":null,"source":null,"login_failed":0,"out_of_office":false,"out_of_office_start_at":null,"out_of_office_end_at":null,"out_of_office_replacement_id":null,"preferences":{"locale":"de-de","tickets_closed":0,"tickets_open":0},"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T19:42:04.240Z","updated_at":"2023-10-23T20:46:51.234Z","role_ids":[3],"two_factor_preference_ids":[],"organization_ids":[],"authorization_ids":[],"overview_sorting_ids":[],"group_ids":{}}},"Role":{"1":{"id":1,"name":"Admin","preferences":{},"default_at_signup":false,"active":true,"note":"To configure your system.","updated_by_id":3,"created_by_id":1,"created_at":"2023-09-22T20:15:52.702Z","updated_at":"2023-10-19T02:03:18.114Z","permission_ids":[1,43,55,65],"knowledge_base_permission_ids":[],"group_ids":{}},"2":{"id":2,"name":"Agent","preferences":{},"default_at_signup":false,"active":true,"note":"To work on Tickets.","updated_by_id":3,"created_by_id":1,"created_at":"2023-09-22T20:15:52.728Z","updated_at":"2023-10-19T02:03:18.117Z","permission_ids":[43,57,60,62,66],"knowledge_base_permission_ids":[],"group_ids":{}},"3":{"id":3,"name":"Customer","preferences":{},"default_at_signup":true,"active":true,"note":"People who create Tickets ask for help.","updated_by_id":3,"created_by_id":1,"created_at":"2023-09-22T20:15:52.744Z","updated_at":"2023-09-23T03:23:14.777Z","permission_ids":[44,47,48,50,54,58],"knowledge_base_permission_ids":[],"group_ids":{}}},"Group":{"1":{"id":1,"signature_id":1,"email_address_id":null,"name":"Users","assignment_timeout":null,"follow_up_possible":"yes","reopen_time_in_days":null,"follow_up_assignment":true,"active":true,"shared_drafts":true,"note":"Standard Group/Pool for Tickets.","updated_by_id":3,"created_by_id":1,"created_at":"2023-09-22T20:15:53.823Z","updated_at":"2023-10-19T04:18:40.446Z","user_ids":[3]}},"Organization":{"3":{"id":3,"name":"zammadoo","shared":true,"domain":"","domain_assignment":false,"active":true,"vip":false,"note":"","updated_by_id":3,"created_by_id":3,"created_at":"2023-10-19T01:57:58.784Z","updated_at":"2023-10-19T04:18:40.412Z","member_ids":[3],"secondary_member_ids":[]}},"Ticket":{"78":{"id":78,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67068","title":"__pytest__","owner_id":1,"customer_id":10,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":1,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T20:46:50.733Z","updated_at":"2023-10-23T20:46:51.004Z","article_ids":[131],"ticket_time_accounting_ids":[]}},"TicketArticle":{"131":{"id":131,"ticket_id":78,"type_id":10,"sender_id":1,"from":"zammadoo admin","to":null,"cc":null,"subject":null,"reply_to":null,"message_id":null,"message_id_md5":null,"in_reply_to":null,"content_type":"text/plain","references":null,"body":"...","internal":false,"preferences":{},"updated_by_id":3,"created_by_id":3,"origin_by_id":null,"created_at":"2023-10-23T20:46:50.848Z","updated_at":"2023-10-23T20:46:50.848Z","attachments":[]}}}}
{"method": "DELETE", "url": "https://localhost/api/v1/tickets/78#fixture", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "text/html", "Content-Length": "0"}, "encoding": "ISO-8859-1", "content_size": 0}

{"method": "DELETE", "url": "https://localhost/api/v1/tickets/79#fixture", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "text/html", "Content-Length": "0"}, "encoding": "ISO-8859-1", "content_size": 0}

Loading

0 comments on commit 9db171c

Please sign in to comment.