2
0

test_ticket_view.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import json
  2. import mock
  3. from django.test import TestCase
  4. from django.test.client import Client
  5. from tests.base import LoonflowApiCall
  6. from service.common.constant_service import constant_service_ins
  7. class TestTicketView(TestCase):
  8. fixtures = ['accounts.json', 'workflows.json', 'tickets.json']
  9. def test_get_ticket_list_without_arg_and_header(self):
  10. """
  11. 获取工单列表,不传参数及header
  12. :return:
  13. """
  14. c = Client()
  15. response_content = c.get('/api/v1.0/tickets').content
  16. response_content_dict = json.loads(str(response_content, encoding='utf-8'))
  17. self.assertEqual(response_content_dict.get('code'), -1)
  18. def test_get_ticket_list_without_arg(self):
  19. """
  20. 获取工单列表,不传参数
  21. :return:
  22. """
  23. url = '/api/v1.0/tickets'
  24. response_content_dict = LoonflowApiCall().api_call('get', url)
  25. self.assertEqual(response_content_dict.get('code'), -1)
  26. def test_get_ticket_list_with_arg(self):
  27. """
  28. 获取工单列表,正确的参数及header
  29. :return:
  30. """
  31. url = '/api/v1.0/tickets'
  32. response_content_dict = LoonflowApiCall().api_call('get', url, {'category':'all', 'username':'wangfei'})
  33. self.assertEqual(response_content_dict.get('code'), 0)
  34. def test_get_ticket_list_category_duty(self):
  35. """
  36. 获取待办工单列表
  37. :return:
  38. """
  39. result = self.get_ticket_list_by_params(dict(category='duty', username='lilei'))
  40. self.assertEqual(result.get('code'), 0)
  41. def test_get_ticket_list_category_owner(self):
  42. """
  43. 获取用户创建的工单列表
  44. :return:
  45. """
  46. result = self.get_ticket_list_by_params(dict(category='owner', username='lilei'))
  47. self.assertEqual(result.get('code'), 0)
  48. def test_get_ticket_list_category_relation(self):
  49. """
  50. 获取用户关联的工单列表
  51. :return:
  52. """
  53. result = self.get_ticket_list_by_params(dict(category='relation', username='lilei'))
  54. self.assertEqual(result.get('code'), 0)
  55. @mock.patch('tasks.flow_hook_task.apply_async')
  56. @mock.patch('tasks.send_ticket_notice.apply_async')
  57. @mock.patch('service.ticket.ticket_base_service.TicketBaseService.gen_ticket_sn')
  58. def test_new_ticket(self, gen_ticket_sn, notice_apply_async, hook_apply_async):
  59. """
  60. 新建工单
  61. :return:
  62. """
  63. gen_ticket_sn.return_value = (True, dict(ticket_sn='ops_202005240001'))
  64. notice_apply_async.return_value = True
  65. hook_apply_async.return_value = True
  66. params = dict(title='测试工单0001', leave_start='2018-10-14 09:00:00', leave_end='2018-10-15 09:00:00',
  67. leave_proxy='lisi', leave_type='2', leave_reason='请假原因', workflow_id=1,
  68. username='guiji', transition_id=1)
  69. url = '/api/v1.0/tickets'
  70. result = LoonflowApiCall().api_call('post', url, params)
  71. self.assertEqual(result.get('code'), 0)
  72. def test_get_ticket_detail(self):
  73. """
  74. 获取工单详情
  75. :return:
  76. """
  77. url = '/api/v1.0/tickets/{}'.format(39)
  78. result = LoonflowApiCall().api_call('get', url, dict(username='admin'))
  79. self.assertEqual(result.get('code'), 0)
  80. def test_get_ticket_transition(self):
  81. """
  82. 测试获取工单可以执行的操作
  83. :return:
  84. """
  85. from apps.ticket.models import TicketRecord
  86. last_ticket_obj = TicketRecord.objects.filter(is_deleted=0).order_by('-id').first()
  87. last_ticket_id = last_ticket_obj.id
  88. ticket_participant_type_id = last_ticket_obj.participant_type_id
  89. ticket_participant = last_ticket_obj.participant
  90. if ticket_participant_type_id == constant_service_ins.PARTICIPANT_TYPE_PERSONAL:
  91. username= ticket_participant
  92. elif ticket_participant_type_id == constant_service_ins.PARTICIPANT_TYPE_MULTI:
  93. username = ticket_participant.split(',')[0]
  94. url = '/api/v1.0/tickets/{}/transitions'.format(last_ticket_id)
  95. result = LoonflowApiCall().api_call('get', url, dict(username=username))
  96. self.assertEqual(result.get('code'), 0)
  97. def test_get_ticket_flow_logs(self):
  98. """
  99. 测试获取工单流转记录
  100. :return:
  101. """
  102. from apps.ticket.models import TicketRecord
  103. last_ticket_obj = TicketRecord.objects.filter(is_deleted=0).order_by('-id').first()
  104. last_ticket_id = last_ticket_obj.id
  105. url = '/api/v1.0/tickets/{}/flowlogs'.format(last_ticket_id)
  106. result = LoonflowApiCall().api_call('get', url, dict(username='lilie'))
  107. self.assertEqual(result.get('code'), 0)
  108. def test_get_ticket_flow_steps(self):
  109. """
  110. 测试获取工单step记录
  111. :return:
  112. """
  113. from apps.ticket.models import TicketRecord
  114. last_ticket_obj = TicketRecord.objects.filter(is_deleted=0).order_by('-id').first()
  115. last_ticket_id = last_ticket_obj.id
  116. url = '/api/v1.0/tickets/{}/flowsteps'.format(last_ticket_id)
  117. result = LoonflowApiCall().api_call('get', url, dict(username='admin'))
  118. self.assertEqual(result.get('code'), 0)
  119. @mock.patch('tasks.send_ticket_notice.apply_async')
  120. def test_alter_ticket_state(self, notice_apply_async):
  121. """
  122. 修改工单状态
  123. :return:
  124. """
  125. ticket_id = 39
  126. new_state_id = 4
  127. url = '/api/v1.0/tickets/{}/state'.format(ticket_id)
  128. notice_apply_async.return_value = True
  129. result = LoonflowApiCall().api_call('put', url, dict(state_id=new_state_id))
  130. self.assertEqual(result.get('code'), 0)
  131. def test_alter_ticket_field_value(self):
  132. """
  133. 修改工单字段的值
  134. :return:
  135. """
  136. from apps.ticket.models import TicketRecord
  137. ticket_id = 39
  138. ticket_obj = TicketRecord.objects.get(id=ticket_id)
  139. ticket_title = ticket_obj.title
  140. url = '/api/v1.0/tickets/{}/fields'.format(ticket_id)
  141. result = LoonflowApiCall().api_call('patch', url, dict(username='lilie', title='_test'.format(ticket_title)))
  142. self.assertEqual(result.get('code'), 0)
  143. def test_add_ticket_comment(self):
  144. """
  145. 测试新增评论
  146. :return:
  147. """
  148. from apps.ticket.models import TicketRecord
  149. ticket_id = 39
  150. last_ticket_obj = TicketRecord.objects.get(id=ticket_id)
  151. last_ticket_id = last_ticket_obj.id
  152. url = '/api/v1.0/tickets/{}/comments'.format(last_ticket_id)
  153. result = LoonflowApiCall().api_call('post', url, dict(suggestion='test for commnet'))
  154. self.assertEqual(result.get('code'), 0)
  155. def get_ticket_list_by_params(self, params):
  156. """
  157. 根据参数获取用户工单列表
  158. :param category:
  159. :return:
  160. """
  161. url = '/api/v1.0/tickets'
  162. response_content_dict = LoonflowApiCall().api_call('get', url, params)
  163. return response_content_dict