File: ticket_tests.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (390 lines) | stat: -rw-r--r-- 15,445 bytes parent folder | download
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
"""
    SoftLayer.tests.CLI.modules.ticket_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    :license: MIT, see LICENSE for more details.
"""
import json
from unittest import mock as mock

from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting
from SoftLayer.CLI import ticket
from SoftLayer.managers import TicketManager
from SoftLayer import testing


class FakeTTY():
    """A fake object to fake STD input"""

    def __init__(self, isatty=False, read="Default Output"):
        """Sets isatty and read"""
        self._isatty = isatty
        self._read = read

    def isatty(self):
        """returns self.isatty"""
        return self._isatty

    def read(self):
        """returns self.read"""
        return self._read


class TicketTests(testing.TestCase):

    def test_list(self):
        result = self.run_command(['ticket', 'list'])

        expected = [{
            'assigned_user': 'John Smith',
            'Case_Number': 'CS123456',
            'id': 102,
            'last_edited': '2013-08-01',
            'priority': 0,
            'status': 'Open',
            'title': 'Cloud Instance Cancellation - 08/01/13',
            'updates': 0}]
        self.assert_no_fail(result)
        self.assertEqual(expected, json.loads(result.output))

    def test_detail(self):
        result = self.run_command(['ticket', 'detail', '1'])

        expected = {
            'Case_Number': 'CS123456',
            'created': '2013-08-01T14:14:04-07:00',
            'edited': '2013-08-01T14:16:47-07:00',
            'id': 100,
            'priority': 'No Priority',
            'status': 'Closed',
            'title': 'Cloud Instance Cancellation - 08/01/13',
            'update 1': 'a bot says something',
            'update 2': 'By John Smith user says something',
            'update 3': 'By emp1 (Employee) employee says something',
        }
        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output), expected)

    def test_create(self):
        result = self.run_command(['ticket', 'create', '--title=Test',
                                   '--subject-id=1000',
                                   '--body=ticket body'])

        self.assert_no_fail(result)

        args = ({'subjectId': 1000,
                 'assignedUserId': 12345,
                 'title': 'Test'}, 'ticket body')

        self.assert_called_with('SoftLayer_Ticket', 'createStandardTicket', args=args)

    def test_create_with_priority(self):
        result = self.run_command(['ticket', 'create', '--title=Test',
                                   '--subject-id=1000',
                                   '--body=ticket body',
                                   '--priority=1'])

        self.assert_no_fail(result)

        args = ({'subjectId': 1000,
                 'assignedUserId': 12345,
                 'title': 'Test',
                 'priority': 1}, 'ticket body')

        self.assert_called_with('SoftLayer_Ticket', 'createStandardTicket', args=args)

    def test_create_and_attach(self):
        result = self.run_command(['ticket', 'create', '--title=Test',
                                   '--subject-id=1000',
                                   '--body=ticket body',
                                   '--hardware=234',
                                   '--virtual=567'])

        self.assert_no_fail(result)

        args = ({'subjectId': 1000,
                 'assignedUserId': 12345,
                 'title': 'Test'}, 'ticket body')

        self.assert_called_with('SoftLayer_Ticket', 'createStandardTicket',
                                args=args)
        self.assert_called_with('SoftLayer_Ticket', 'addAttachedHardware',
                                args=(234,),
                                identifier=100)
        self.assert_called_with('SoftLayer_Ticket', 'addAttachedVirtualGuest',
                                args=(567,),
                                identifier=100)

    @mock.patch('click.edit')
    @mock.patch('click.get_text_stream')
    def test_create_no_body(self, isatty_mock, edit_mock):
        fake_tty = FakeTTY(True, "TEST")
        isatty_mock.return_value = fake_tty
        edit_mock.return_value = 'ticket body'
        result = self.run_command(['ticket', 'create', '--title=Test', '--subject-id=1000'])
        self.assert_no_fail(result)

        args = ({'subjectId': 1000,
                 'assignedUserId': 12345,
                 'title': 'Test'}, 'ticket body')

        self.assert_called_with('SoftLayer_Ticket', 'createStandardTicket', args=args)

    @mock.patch('click.get_text_stream')
    def test_create_no_body_stdin(self, isatty_mock):
        fake_tty = FakeTTY(False, "TEST TICKET BODY")
        isatty_mock.return_value = fake_tty
        result = self.run_command(['ticket', 'create', '--title=Test', '--subject-id=1000'])
        print(result.output)
        self.assert_no_fail(result)

        args = ({'subjectId': 1000,
                 'assignedUserId': 12345,
                 'title': 'Test'}, 'TEST TICKET BODY')

        self.assert_called_with('SoftLayer_Ticket', 'createStandardTicket', args=args)

    def test_subjects(self):
        list_expected_ids = [1001, 1002, 1003, 1004, 1005]
        result = self.run_command(['ticket', 'subjects'])

        self.assert_no_fail(result)
        results = json.loads(result.output)
        for result in results:
            self.assertIn(result['id'], list_expected_ids)

    def test_attach_no_identifier(self):
        result = self.run_command(['ticket', 'attach', '1'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_attach_two_identifiers(self):
        result = self.run_command(['ticket',
                                   'attach',
                                   '1',
                                   '--hardware=100',
                                   '--virtual=100'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_ticket_attach_hardware(self):
        result = self.run_command(['ticket', 'attach', '1', '--hardware=100'])

        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket', 'addAttachedHardware',
                                args=(100,),
                                identifier=1)

    def test_ticket_attach_virtual_server(self):
        result = self.run_command(['ticket', 'attach', '1', '--virtual=100'])

        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket', 'addAttachedVirtualGuest',
                                args=(100,),
                                identifier=1)

    def test_detach_no_identifier(self):
        result = self.run_command(['ticket', 'detach', '1'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_detach_two_identifiers(self):
        result = self.run_command(['ticket',
                                   'detach',
                                   '1',
                                   '--hardware=100',
                                   '--virtual=100'])
        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_ticket_detach_hardware(self):
        result = self.run_command(['ticket', 'detach', '1', '--hardware=100'])

        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket',
                                'removeAttachedHardware',
                                args=(100,),
                                identifier=1)

    def test_ticket_detach_virtual_server(self):
        result = self.run_command(['ticket', 'detach', '1', '--virtual=100'])

        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket',
                                'removeAttachedVirtualGuest',
                                args=(100,),
                                identifier=1)

    def test_ticket_upload_no_path(self):
        result = self.run_command(['ticket', 'upload', '1'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_ticket_upload_invalid_path(self):
        result = self.run_command(['ticket', 'upload', '1',
                                   '--path=tests/resources/nonexistent_file',
                                   '--name=a_file_name'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_ticket_upload_no_name(self):
        result = self.run_command(['ticket', 'upload', '1',
                                   '--path=tests/resources/attachment_upload'])

        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket',
                                'addAttachedFile',
                                args=({"filename": "attachment_upload",
                                       "data": b"ticket attached data"},),
                                identifier=1)

    def test_ticket_upload(self):
        result = self.run_command(['ticket', 'upload', '1',
                                   '--path=tests/resources/attachment_upload',
                                   '--name=a_file_name'])

        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket',
                                'addAttachedFile',
                                args=({"filename": "a_file_name",
                                       "data": b"ticket attached data"},),
                                identifier=1)

    def test_init_ticket_results(self):
        ticket_mgr = TicketManager(self.client)
        ticket_table = ticket.get_ticket_results(ticket_mgr, 100)
        self.assert_called_with('SoftLayer_Ticket', 'getObject', identifier=100)
        self.assertIsInstance(ticket_table, formatting.KeyValueTable)

        ticket_object = ticket_table.to_python()
        self.assertEqual('No Priority', ticket_object['priority'])
        self.assertEqual(100, ticket_object['id'])

    def test_init_ticket_results_asigned_user(self):
        mock = self.set_mock('SoftLayer_Ticket', 'getObject')
        mock.return_value = {
            "serviceProviderResourceId": "CS12345",
            "id": 100,
            "title": "Simple Title",
            "priority": 1,
            "assignedUser": {
                "firstName": "Test",
                "lastName": "User"
            },
            "status": {
                "name": "Closed"
            },
            "createDate": "2013-08-01T14:14:04-07:00",
            "lastEditDate": "2013-08-01T14:16:47-07:00",
            "updates": [{'entry': 'a bot says something'}]
        }

        ticket_mgr = TicketManager(self.client)
        ticket_table = ticket.get_ticket_results(ticket_mgr, 100)
        self.assert_called_with('SoftLayer_Ticket', 'getObject', identifier=100)
        self.assertIsInstance(ticket_table, formatting.KeyValueTable)

        ticket_object = ticket_table.to_python()
        self.assertEqual('Severity 1 - Critical Impact / Service Down', ticket_object['priority'])
        self.assertEqual('Test User', ticket_object['user'])

    def test_ticket_summary(self):
        mock = self.set_mock('SoftLayer_Account', 'getObject')
        mock.return_value = {
            'openTicketCount': 1,
            'closedTicketCount': 2,
            'openBillingTicketCount': 3,
            'openOtherTicketCount': 4,
            'openSalesTicketCount': 5,
            'openSupportTicketCount': 6,
            'openAccountingTicketCount': 7
        }
        expected = [
            {'Status': 'Open',
             'count': [
                 {'Type': 'Accounting', 'count': 7},
                 {'Type': 'Billing', 'count': 3},
                 {'Type': 'Sales', 'count': 5},
                 {'Type': 'Support', 'count': 6},
                 {'Type': 'Other', 'count': 4},
                 {'Type': 'Total', 'count': 1}]},
            {'Status': 'Closed', 'count': 2}
        ]
        result = self.run_command(['ticket', 'summary'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Account', 'getObject')
        self.assertEqual(expected, json.loads(result.output))

    def test_ticket_update(self):
        result = self.run_command(['ticket', 'update', '100', '--body=Testing'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket', 'addUpdate', args=({'entry': 'Testing'},), identifier=100)

    @mock.patch('click.edit')
    @mock.patch('click.get_text_stream')
    def test_ticket_update_no_body(self, isatty_mock, edit_mock):
        fake_tty = FakeTTY(True, "TEST TICKET BODY")
        isatty_mock.return_value = fake_tty
        edit_mock.return_value = 'Testing1'
        result = self.run_command(['ticket', 'update', '100'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket', 'addUpdate', args=({'entry': 'Testing1'},), identifier=100)

    @mock.patch('click.get_text_stream')
    def test_ticket_update_no_body_stdin(self, isatty_mock):
        fake_tty = FakeTTY(False, "TEST TICKET BODY")
        isatty_mock.return_value = fake_tty
        result = self.run_command(['ticket', 'update', '100'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Ticket', 'addUpdate',
                                args=({'entry': 'TEST TICKET BODY'},), identifier=100)

    def test_ticket_json(self):
        result = self.run_command(['--format=json', 'ticket', 'detail', '1'])
        expected = {'Case_Number': 'CS123456',
                    'created': '2013-08-01T14:14:04-07:00',
                    'edited': '2013-08-01T14:16:47-07:00',
                    'id': 100,
                    'priority': 'No Priority',
                    'status': 'Closed',
                    'title': 'Cloud Instance Cancellation - 08/01/13',
                    'update 1': 'a bot says something',
                    'update 2': 'By John Smith user says something',
                    'update 3': 'By emp1 (Employee) employee says something'}
        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output), expected)

    def test_list_limit(self):
        result = self.run_command(['ticket', 'list', '--limit', '1'])

        expected = [{
            'assigned_user': 'John Smith',
            'Case_Number': 'CS123456',
            'id': 102,
            'last_edited': '2013-08-01',
            'priority': 0,
            'status': 'Open',
            'title': 'Cloud Instance Cancellation - 08/01/13',
            'updates': 0}]
        self.assert_no_fail(result)
        self.assertEqual(expected, json.loads(result.output))

    def test_list_all_open(self):
        result = self.run_command(['ticket', 'list', '--all'])

        expected = [{
            'assigned_user': 'John Smith',
            'Case_Number': 'CS123456',
            'id': 102,
            'last_edited': '2013-08-01',
            'priority': 0,
            'status': 'Open',
            'title': 'Cloud Instance Cancellation - 08/01/13',
            'updates': 0}]
        self.assert_no_fail(result)
        self.assertEqual(expected, json.loads(result.output))