-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_wls_rest_python.py
More file actions
696 lines (606 loc) · 22.8 KB
/
test_wls_rest_python.py
File metadata and controls
696 lines (606 loc) · 22.8 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
try:
from json.decoder import JSONDecodeError
except ImportError:
# python2
JSONDecodeError = ValueError
try:
from unittest.mock import MagicMock
except ImportError:
# python2
from mock import MagicMock
import pytest
import requests_mock
import wls_rest_python
def test_wls_init():
collection = {
"version": "17.12.3.1",
"isLatest": True,
"lifecycle": "active",
"links": [
{"rel": "edit", "href": "https://edit-link"},
{"rel": "domainRuntime", "href": "https://domainruntime-link"},
],
}
with requests_mock.mock() as r:
r.get(
"https://wls.example.com:7001/management/weblogic/latest", json=collection
)
wls = wls_rest_python.WLS(
"https://wls.example.com:7001", "weblogic", "Welcome1"
)
assert wls.isLatest is True
assert wls.lifecycle == "active"
assert wls.version == "17.12.3.1"
assert wls.timeout == wls_rest_python.DEFAULT_TIMEOUT
assert wls.edit._url == "https://edit-link"
assert wls.session.verify is True
assert wls.session.auth == ("weblogic", "Welcome1")
assert "X-Requested-By" in wls.session.headers
def test_wls_init_nondefault_version():
collection = {
"version": "17.12.3.1",
"isLatest": False,
"lifecycle": "deprecated",
"links": [
{"rel": "edit", "href": "https://edit-link"},
{"rel": "domainRuntime", "href": "https://domainruntime-link"},
],
}
with requests_mock.mock() as r:
r.get(
"https://wls.example.com:7001/management/weblogic/17.12.3.1",
json=collection,
)
wls = wls_rest_python.WLS(
"https://wls.example.com:7001", "weblogic", "Welcome1", version="17.12.3.1"
)
assert wls.isLatest is False
assert wls.lifecycle == "deprecated"
assert wls.version == "17.12.3.1"
def test_wls_init_noverify():
collection = {
"version": "17.12.3.1",
"isLatest": True,
"lifecycle": "active",
"links": [
{"rel": "edit", "href": "https://edit-link"},
{"rel": "domainRuntime", "href": "https://domainruntime-link"},
],
}
with requests_mock.mock() as r:
r.get(
"https://wls.example.com:7001/management/weblogic/latest", json=collection
)
wls = wls_rest_python.WLS(
"https://wls.example.com:7001", "weblogic", "Welcome1", verify=False
)
assert wls.session.verify == False
def test_wls_init_nondefault_timeout():
collection = {
"version": "17.12.3.1",
"isLatest": True,
"lifecycle": "active",
"links": [
{"rel": "edit", "href": "https://edit-link"},
{"rel": "domainRuntime", "href": "https://domainruntime-link"},
],
}
with requests_mock.mock() as r:
r.get(
"https://wls.example.com:7001/management/weblogic/latest", json=collection
)
wls = wls_rest_python.WLS(
"https://wls.example.com:7001", "weblogic", "Welcome1", timeout=832
)
assert wls.timeout == 832
def test_wls_repr():
collection = {
"version": "12.2.1.3",
"isLatest": True,
"lifecycle": "active",
"links": [
{"rel": "edit", "href": "https://edit-link"},
{"rel": "domainRuntime", "href": "https://domainruntime-link"},
],
}
with requests_mock.mock() as r:
r.get(
"https://wls.example.com:7001/management/weblogic/latest", json=collection
)
wls = wls_rest_python.WLS(
"https://wls.example.com:7001", "weblogic", "Welcome1"
)
assert (
repr(wls)
== "<WLS url='https://wls.example.com:7001/management/weblogic/latest' username='weblogic' version='12.2.1.3'>"
)
def test_wls_get():
fake_wls = MagicMock(spec=wls_rest_python.WLS)
fake_wls.timeout = 372
fake_wls.session = MagicMock()
fake_wls.session.get = MagicMock()
wls_rest_python.WLS.get(fake_wls, "https://url", weird_requests_option="hei")
fake_wls.session.get.assert_called_once_with(
"https://url", timeout=372, weird_requests_option="hei"
)
def test_wls_post():
fake_wls = MagicMock(spec=wls_rest_python.WLS)
fake_wls.timeout = 372
fake_wls.session = MagicMock()
fake_wls.session.post = MagicMock()
wls_rest_python.WLS.post(fake_wls, "https://url", weird_requests_option="hei")
fake_wls.session.post.assert_called_once_with(
"https://url", headers=None, timeout=372, weird_requests_option="hei"
)
def test_wls_delete():
fake_wls = MagicMock(spec=wls_rest_python.WLS)
fake_wls.timeout = 372
fake_wls.session = MagicMock()
fake_wls.session.delete = MagicMock()
wls_rest_python.WLS.delete(fake_wls, "https://url", weird_requests_option="hei")
fake_wls.session.delete.assert_called_once_with(
"https://url", headers=None, timeout=372, weird_requests_option="hei"
)
def test_wls_handle_response_from_get():
# it is important that we get the raw json back from GET
# as otherwise it wil get messy (recursion)
collection = {
"name": "naaame",
"links": [{"rel": "self", "href": "https://self-link"}],
}
fake_wls = MagicMock(spec=wls_rest_python.WLS)
response = MagicMock()
response.ok = True
# 'get'.upper() to check == and not is
response.request.method = "get".upper()
response.json = MagicMock(return_value=collection)
returned = wls_rest_python.WLS._handle_response(fake_wls, response)
assert returned == collection
assert not isinstance(returned, wls_rest_python.WLSObject)
fake_wls._handle_error.assert_not_called()
def test_wls_handle_response_empty():
fake_wls = MagicMock(spec=wls_rest_python.WLS)
response = MagicMock()
response.ok = True
response.json = MagicMock(return_value={})
assert wls_rest_python.WLS._handle_response(fake_wls, response) is None
fake_wls._handle_error.assert_not_called()
def test_wls_handle_response_job():
job = {
"links": [{"rel": "job", "href": "https://joblink"}],
"name": "Very important job",
"state": "STATE_IN_PROGRESS",
"progress": "yes",
"completed": False,
}
fake_wls = MagicMock(spec=wls_rest_python.WLS)
response = MagicMock()
response.ok = True
response.json = MagicMock(return_value=job)
decoded_response = wls_rest_python.WLS._handle_response(fake_wls, response)
assert decoded_response._name == "Very important job"
assert decoded_response._url == "https://joblink"
fake_wls._handle_error.assert_not_called()
def test_wls_handle_response_object():
collection = {
"items": [
{
"links": [{"rel": "self", "href": "https://item-link"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item_1",
}
],
"name": "Random object",
"property": "yesyes",
"links": [
{"rel": "action", "title": "superAction", "href": "https://action-link"},
{"rel": "underCollection", "href": "https://undercollection-link"},
{"rel": "self", "href": "https://self-link"},
],
}
fake_wls = MagicMock(spec=wls_rest_python.WLS)
response = MagicMock()
response.ok = True
response.json = MagicMock(return_value=collection)
decoded_response = wls_rest_python.WLS._handle_response(fake_wls, response)
assert decoded_response._name == "Random object"
assert decoded_response._url == "https://self-link"
fake_wls._handle_error.assert_not_called()
def test_wls_handle_response_no_self_link():
what = {
"what": True,
"is": False,
"this": "true story",
"links": [
{"rel": "action", "title": "superAction", "href": "https://action-link"},
{"rel": "underCollection", "href": "https://undercollection-link"},
],
}
fake_wls = MagicMock(spec=wls_rest_python.WLS)
response = MagicMock()
response.ok = True
response.json = MagicMock(return_value=what)
decoded_response = wls_rest_python.WLS._handle_response(fake_wls, response)
assert decoded_response == what
fake_wls._handle_error.assert_not_called()
def test_wls_handle_response_unknown():
what = {"what": True, "is": False, "this": "true story"}
fake_wls = MagicMock(spec=wls_rest_python.WLS)
response = MagicMock()
response.ok = True
response.json = MagicMock(return_value=what)
decoded_response = wls_rest_python.WLS._handle_response(fake_wls, response)
assert decoded_response == what
fake_wls._handle_error.assert_not_called()
def test_wls_handle_response_with_error():
fake_wls = MagicMock(spec=wls_rest_python.WLS)
fake_wls._handle_error = MagicMock()
fake_wls._handle_error.side_effect = wls_rest_python.WLSException
response = MagicMock()
response.ok = False
with pytest.raises(wls_rest_python.WLSException):
wls_rest_python.WLS._handle_response(fake_wls, response)
fake_wls._handle_error.assert_called_once_with(response)
def test_wls_handle_error_400():
response = MagicMock()
response.status_code = 400
response.json = MagicMock(return_value={"detail": "insert detail here"})
with pytest.raises(wls_rest_python.BadRequestException, match="insert detail here"):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_400_with_alternative_json():
response = MagicMock()
response.status_code = 400
response.json = MagicMock(
return_value={
u"status": 400,
u"wls:errorsDetails": [
{
u"o:errorPath": u"machine",
u"type": u"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1",
u"detail": u"Type mismatch. Cannot convert hei to weblogic.management.configuration.MachineMBean.",
u"title": u"FAILURE",
}
],
u"type": u"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1",
u"title": u"ERRORS",
}
)
with pytest.raises(wls_rest_python.BadRequestException, match="Type mismatch"):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_401():
response = MagicMock()
response.status_code = 401
response.json = MagicMock()
response.json.side_effect = JSONDecodeError("No json here", '"', 1)
with pytest.raises(wls_rest_python.UnauthorizedException):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_403():
response = MagicMock()
response.status_code = 403
response.json = MagicMock(return_value={"detail": "insert detail here"})
with pytest.raises(wls_rest_python.ForbiddenException, match="insert detail here"):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_404():
response = MagicMock()
response.status_code = 404
response.json = MagicMock(return_value={"detail": "insert detail here"})
with pytest.raises(wls_rest_python.NotFoundException, match="insert detail here"):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_405():
response = MagicMock()
response.status_code = 405
response.json = MagicMock(return_value={"detail": "insert detail here"})
with pytest.raises(
wls_rest_python.MethodNotAllowedException, match="insert detail here"
):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_406():
response = MagicMock()
response.status_code = 406
response.json = MagicMock(return_value={"detail": "insert detail here"})
with pytest.raises(
wls_rest_python.NotAcceptableException, match="insert detail here"
):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_500_json():
response = MagicMock()
response.status_code = 500
response.json = MagicMock(return_value={"detail": "insert detail here"})
with pytest.raises(
wls_rest_python.ServerErrorException, match="insert detail here"
):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_500_html():
response = MagicMock()
response.status_code = 500
response.text = "text here"
response.json = MagicMock()
response.json.side_effect = JSONDecodeError("No json here", '"', 1)
with pytest.raises(wls_rest_python.ServerErrorException, match="text here"):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_error_503():
response = MagicMock()
response.status_code = 503
response.json = MagicMock(return_value={"detail": "insert detail here"})
with pytest.raises(
wls_rest_python.ServiceUnavailableException, match="insert detail here"
):
wls_rest_python.WLS._handle_error(response)
def test_wls_handle_unknown_error():
response = MagicMock()
response.status_code = 507
response.json = MagicMock(return_value={"detail": "Weird error"})
with pytest.raises(wls_rest_python.WLSException, match="Weird error"):
wls_rest_python.WLS._handle_error(response)
def test_wls_object_dir():
collection = {
"items": [
{
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item_1",
}
],
"name": "navn",
"property": "yesyes",
"links": [
{"rel": "action", "title": "superAction", "href": "https://action-link"},
{"rel": "underCollection", "href": "https://undercollection-link"},
],
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
for attr in ["property", "item_1", "superAction"]:
assert attr in dir(wls_obj)
def test_wls_object_getattr():
collection = {
"items": [
{
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item_1",
}
],
"name": "navn",
"property": "yesyes",
"links": [
{"rel": "action", "title": "superAction", "href": "https://action-link"},
{"rel": "underCollection", "href": "https://undercollection-link"},
],
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
assert wls_obj.property == collection["property"]
assert wls_obj.superAction._url == "https://action-link"
assert wls_obj.underCollection._url == "https://undercollection-link"
assert isinstance(wls_obj.item_1, wls_rest_python.WLSObject)
with pytest.raises(AttributeError):
wls_obj.what
def test_wls_object_getitem():
collection = {
"items": [
{
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item_1",
}
],
"name": "navn",
"property": "yesyes",
"links": [
{"rel": "action", "title": "superAction", "href": "https://action-link"},
{"rel": "underCollection", "href": "https://undercollection-link"},
],
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
assert wls_obj["property"] == collection["property"]
assert wls_obj["superAction"]._url == "https://action-link"
assert wls_obj["underCollection"]._url == "https://undercollection-link"
assert isinstance(wls_obj.item_1, wls_rest_python.WLSObject)
with pytest.raises(KeyError):
wls_obj["what"]
def test_wls_object_iter():
collection = {
"items": [
{
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item 1",
},
{
"links": [{"rel": "self", "href": "https://self-link2"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item 2",
},
{
"links": [{"rel": "self", "href": "https://self-link3"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item 3",
},
]
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
counter = 0
for item in wls_obj:
counter += 1
coll = [x for x in collection["items"] if x["name"] == item._name][0]
assert item._url == coll["links"][0]["href"]
assert counter == 3
def test_wls_object_non_iter():
collection = {
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item 1",
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
with pytest.raises(TypeError):
for item in wls_obj:
pass
def test_wls_object_with_len():
collection = {
"items": [
{
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item 1",
},
{
"links": [{"rel": "self", "href": "https://self-link2"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item 2",
},
{
"links": [{"rel": "self", "href": "https://self-link3"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item 3",
},
{
"links": [{"rel": "self", "href": "https://self-link4"}],
"attr1": False,
"attr2": 8,
"attr3": 3,
"name": "item 4",
},
]
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
assert len(wls_obj) == 4
def test_wls_object_with_zero_len():
# If there is an empty item array in the response,
# it means it has length, but it is zero
collection = {"items": []}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
assert len(wls_obj) == 0
def test_wls_object_without_len():
collection = {
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": False,
"attr2": 9,
"attr3": -7,
"name": "what",
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
with pytest.raises(TypeError):
len(wls_obj)
def test_wls_object_repr():
collection = {
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": False,
"attr2": 9,
"attr3": -7,
"name": "what",
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
assert repr(wls_obj) == "<WLSObject name='name' url='https://url'>"
def test_wls_object_empty_items():
# If there is an empty item array in the response,
# it means it is iterable, only withouth objects
# this should not throw an Exception
collection = {
"links": [{"rel": "self", "href": "https://self-link"}],
"attr1": True,
"attr2": 2,
"attr3": 3,
"name": "item 1",
"items": [],
}
fake_wls = MagicMock()
fake_wls.get = MagicMock(return_value=collection)
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
counter = 0
for item in wls_obj:
counter += 1
assert counter == 0
def test_wls_object_delete():
fake_wls = MagicMock()
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
wls_obj.delete(hei="sann")
fake_wls.delete.assert_called_once_with("https://url", False, hei="sann")
wls_obj.delete(prefer_async=True)
fake_wls.delete.assert_called_with("https://url", True)
def test_wls_object_create():
fake_wls = MagicMock()
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
wls_obj.create(hei="sann")
fake_wls.post.assert_called_once_with("https://url", False, hei="sann")
wls_obj.create(prefer_async=True, important="true")
fake_wls.post.assert_called_with("https://url", True, important="true")
def test_wls_object_update():
fake_wls = MagicMock()
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
wls_obj.update(hei="sann")
fake_wls.post.assert_called_once_with("https://url", False, json={"hei": "sann"})
wls_obj.update(prefer_async=True, ssl=True)
fake_wls.post.assert_called_with("https://url", True, json={"ssl": True})
def test_wls_item():
items = ["item1", "item2"]
wls_item = wls_rest_python.WLSItems(items)
assert wls_item.items == items
assert wls_item.counter == 0
wls_item.__next__()
assert wls_item.counter == 1
# python2
wls_item.next()
assert wls_item.counter == 2
with pytest.raises(StopIteration):
wls_item.__next__()
def test_wls_action_with_data():
fake_wls = MagicMock()
wls_action = wls_rest_python.WLSAction("name", "https://url", fake_wls)
action_answer = wls_action(a="b")
fake_wls.post.assert_called_once_with("https://url", False, json={"a": "b"})
assert isinstance(action_answer, MagicMock)
def test_wls_action_no_data():
fake_wls = MagicMock()
wls_action = wls_rest_python.WLSAction("name", "https://url", fake_wls)
action_answer = wls_action()
fake_wls.post.assert_called_once_with("https://url", False, json={})
assert isinstance(action_answer, MagicMock)
def test_wls_action_async():
fake_wls = MagicMock()
wls_action = wls_rest_python.WLSAction("name", "https://url", fake_wls)
action_answer = wls_action(prefer_async=True)
fake_wls.post.assert_called_once_with("https://url", True, json={})
assert isinstance(action_answer, MagicMock)
def test_wls_action_repr():
fake_wls = MagicMock()
wls_action = wls_rest_python.WLSAction("name", "https://url", fake_wls)
assert repr(wls_action) == "<WLSAction name='name' url='https://url'>"