forked from auth0/jupiterone-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_list_questions.py
More file actions
1108 lines (994 loc) · 43.4 KB
/
Copy pathtest_list_questions.py
File metadata and controls
1108 lines (994 loc) · 43.4 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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Test list_questions method"""
import pytest
from unittest.mock import patch, Mock
from jupiterone.client import JupiterOneClient
from jupiterone.constants import QUESTIONS
from typing import List
from jupiterone.errors import JupiterOneApiError
class TestListQuestions:
"""Test list_questions method"""
def setup_method(self):
"""Set up test fixtures"""
self.client = JupiterOneClient(account="test-account", token="test-token")
@patch('requests.Session.post')
def test_list_questions_basic(self, mock_post):
"""Test basic questions listing with single page"""
# Mock response for single page
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "Test Question 1",
"description": "Test description 1",
"tags": ["test", "security"],
"queries": [{
"name": "Query1",
"query": "FIND Host",
"version": "v1",
"resultsAre": "INFORMATIVE"
}],
"compliance": {
"standard": "CIS",
"requirements": ["2.1", "2.2"],
"controls": ["Network Security"]
},
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
},
{
"id": "question-2",
"title": "Test Question 2",
"description": "Test description 2",
"tags": ["compliance", "audit"],
"queries": [{
"name": "Query2",
"query": "FIND User WITH mfaEnabled=false",
"version": "v1",
"resultsAre": "BAD"
}],
"compliance": None,
"variables": [
{
"name": "environment",
"required": True,
"default": "production"
}
],
"accountId": "test-account",
"showTrend": True,
"pollingInterval": "DISABLED",
"lastUpdatedTimestamp": "2024-01-02T00:00:00Z"
}
],
"totalHits": 2,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions
result = self.client.list_questions()
# Verify result
assert len(result) == 2
assert result[0]['id'] == "question-1"
assert result[0]['title'] == "Test Question 1"
assert result[0]['tags'] == ["test", "security"]
assert result[1]['id'] == "question-2"
assert result[1]['title'] == "Test Question 2"
assert result[1]['tags'] == ["compliance", "audit"]
# Verify API call
mock_post.assert_called_once()
call_args = mock_post.call_args
# Check the query was called with correct parameters
assert call_args[1]['json']['query'] == QUESTIONS
assert call_args[1]['json']['flags']['variableResultSize'] is True
@patch('requests.Session.post')
def test_list_questions_with_pagination(self, mock_post):
"""Test questions listing with multiple pages"""
# Mock first page response
first_response = Mock()
first_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "Test Question 1",
"tags": ["test"],
"queries": [{"name": "Query1", "query": "FIND Host"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 3,
"pageInfo": {
"endCursor": "cursor-1",
"hasNextPage": True
}
}
}
}
# Mock second page response
second_response = Mock()
second_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-2",
"title": "Test Question 2",
"tags": ["security"],
"queries": [{"name": "Query2", "query": "FIND User"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-02T00:00:00Z"
},
{
"id": "question-3",
"title": "Test Question 3",
"tags": ["compliance"],
"queries": [{"name": "Query3", "query": "FIND Finding"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-03T00:00:00Z"
}
],
"totalHits": 3,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
# Set up mock to return different responses for each call
mock_post.side_effect = [first_response, second_response]
# Call list_questions
result = self.client.list_questions()
# Verify result
assert len(result) == 3
assert result[0]['id'] == "question-1"
assert result[1]['id'] == "question-2"
assert result[2]['id'] == "question-3"
# Verify API calls (2 calls for 2 pages)
assert mock_post.call_count == 2
# Check first call
first_call = mock_post.call_args_list[0]
assert first_call[1]['json']['query'] == QUESTIONS
assert first_call[1]['json']['flags']['variableResultSize'] is True
# Check second call (with cursor)
second_call = mock_post.call_args_list[1]
assert second_call[1]['json']['query'] == QUESTIONS
assert second_call[1]['json']['variables']['cursor'] == "cursor-1"
assert second_call[1]['json']['flags']['variableResultSize'] is True
@patch('requests.Session.post')
def test_list_questions_empty_response(self, mock_post):
"""Test questions listing with empty response"""
# Mock empty response
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [],
"totalHits": 0,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions
result = self.client.list_questions()
# Verify result
assert len(result) == 0
assert result == []
# Verify API call
mock_post.assert_called_once()
@patch('requests.Session.post')
def test_list_questions_with_compliance_data(self, mock_post):
"""Test questions listing with compliance metadata"""
# Mock response with compliance data
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "CIS Compliance Check",
"tags": ["cis", "compliance"],
"queries": [{"name": "CISQuery", "query": "FIND Host WITH encrypted=false"}],
"compliance": {
"standard": "CIS AWS Foundations",
"requirements": ["2.1", "2.2", "2.3"],
"controls": ["Data Protection", "Network Security"]
},
"variables": [],
"accountId": "test-account",
"showTrend": True,
"pollingInterval": "ONE_HOUR",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 1,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions
result = self.client.list_questions()
# Verify result
assert len(result) == 1
question = result[0]
assert question['id'] == "question-1"
assert question['title'] == "CIS Compliance Check"
assert question['tags'] == ["cis", "compliance"]
# Verify compliance data
compliance = question['compliance']
assert compliance['standard'] == "CIS AWS Foundations"
assert compliance['requirements'] == ["2.1", "2.2", "2.3"]
assert compliance['controls'] == ["Data Protection", "Network Security"]
@patch('requests.Session.post')
def test_list_questions_with_variables(self, mock_post):
"""Test questions listing with variable definitions"""
# Mock response with variables
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "Environment-Specific Query",
"tags": ["environment", "variables"],
"queries": [{"name": "EnvQuery", "query": "FIND * WITH tag.Environment={{env}}"}],
"compliance": None,
"variables": [
{
"name": "env",
"required": True,
"default": "production"
},
{
"name": "region",
"required": False,
"default": "us-east-1"
}
],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "DISABLED",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 1,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions
result = self.client.list_questions()
# Verify result
assert len(result) == 1
question = result[0]
assert question['id'] == "question-1"
assert question['title'] == "Environment-Specific Query"
# Verify variables
variables = question['variables']
assert len(variables) == 2
assert variables[0]['name'] == "env"
assert variables[0]['required'] is True
assert variables[0]['default'] == "production"
assert variables[1]['name'] == "region"
assert variables[1]['required'] is False
assert variables[1]['default'] == "us-east-1"
@patch('requests.Session.post')
def test_list_questions_with_polling_intervals(self, mock_post):
"""Test questions listing with different polling intervals"""
# Mock response with various polling intervals
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "Daily Check",
"tags": ["daily"],
"queries": [{"name": "DailyQuery", "query": "FIND Finding"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
},
{
"id": "question-2",
"title": "Hourly Check",
"tags": ["hourly"],
"queries": [{"name": "HourlyQuery", "query": "FIND User"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": True,
"pollingInterval": "ONE_HOUR",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
},
{
"id": "question-3",
"title": "Disabled Check",
"tags": ["disabled"],
"queries": [{"name": "DisabledQuery", "query": "FIND Host"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "DISABLED",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 3,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions
result = self.client.list_questions()
# Verify result
assert len(result) == 3
# Verify polling intervals
assert result[0]['pollingInterval'] == "ONE_DAY"
assert result[1]['pollingInterval'] == "ONE_HOUR"
assert result[2]['pollingInterval'] == "DISABLED"
# Verify showTrend settings
assert result[0]['showTrend'] is False
assert result[1]['showTrend'] is True
assert result[2]['showTrend'] is False
@patch('requests.Session.post')
def test_list_questions_error_handling(self, mock_post):
"""Test questions listing with error handling"""
# Mock error response
mock_response = Mock()
mock_response.json.return_value = {
"errors": [
{
"message": "Unauthorized access",
"extensions": {"code": "UNAUTHORIZED"}
}
]
}
mock_post.return_value = mock_response
# Call list_questions and expect it to handle the error gracefully
# The method should return an empty list or raise an exception
with pytest.raises(Exception):
self.client.list_questions()
@patch('requests.Session.post')
def test_list_questions_malformed_response(self, mock_post):
"""Test questions listing with malformed response"""
# Mock malformed response
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
# Missing required fields
"questions": [
{
"id": "question-1",
# Missing title
"tags": ["test"]
# Missing other required fields
}
]
}
}
}
mock_post.return_value = mock_response
# Call list_questions
result = self.client.list_questions()
# Verify result (should still work with missing fields)
assert len(result) == 1
question = result[0]
assert question['id'] == "question-1"
assert question['tags'] == ["test"]
# Missing fields should be None or not present
assert 'title' not in question or question['title'] is None
@patch('requests.Session.post')
def test_list_questions_with_search_query(self, mock_post):
"""Test questions listing with search query parameter"""
# Mock response for search query
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "Security Compliance Check",
"description": "Check for security compliance issues",
"tags": ["security", "compliance"],
"queries": [{"name": "SecurityQuery", "query": "FIND Finding WITH severity='HIGH'"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 1,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions with search query
result = self.client.list_questions(search_query="security")
# Verify result
assert len(result) == 1
assert result[0]['title'] == "Security Compliance Check"
assert "security" in result[0]['tags']
# Verify API call with search query
mock_post.assert_called_once()
call_args = mock_post.call_args
assert call_args[1]['json']['variables']['searchQuery'] == "security"
@patch('requests.Session.post')
def test_list_questions_with_tags_filter(self, mock_post):
"""Test questions listing with tags filter parameter"""
# Mock response for tags filter
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "CIS AWS Compliance",
"description": "CIS AWS Foundations compliance checks",
"tags": ["cis", "aws", "compliance"],
"queries": [{"name": "CISQuery", "query": "FIND aws_instance WITH encrypted=false"}],
"compliance": {
"standard": "CIS AWS Foundations",
"requirements": ["2.1", "2.2"],
"controls": ["Data Protection"]
},
"variables": [],
"accountId": "test-account",
"showTrend": True,
"pollingInterval": "ONE_HOUR",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 1,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions with tags filter
result = self.client.list_questions(tags=["cis", "aws"])
# Verify result
assert len(result) == 1
assert result[0]['title'] == "CIS AWS Compliance"
assert "cis" in result[0]['tags']
assert "aws" in result[0]['tags']
# Verify API call with tags filter
mock_post.assert_called_once()
call_args = mock_post.call_args
assert call_args[1]['json']['variables']['tags'] == ["cis", "aws"]
@patch('requests.Session.post')
def test_list_questions_with_search_and_tags(self, mock_post):
"""Test questions listing with both search query and tags filter"""
# Mock response for combined search and tags
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "Encryption Security Check",
"description": "Check for encryption compliance in security context",
"tags": ["security", "compliance", "encryption"],
"queries": [{"name": "EncryptionQuery", "query": "FIND DataStore WITH encrypted=false"}],
"compliance": {
"standard": "PCI-DSS",
"requirements": ["3.4"],
"controls": ["Data Protection"]
},
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 1,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions with both search query and tags
result = self.client.list_questions(
search_query="encryption",
tags=["security", "compliance"]
)
# Verify result
assert len(result) == 1
assert result[0]['title'] == "Encryption Security Check"
assert "encryption" in result[0]['tags']
assert "security" in result[0]['tags']
assert "compliance" in result[0]['tags']
# Verify API call with both parameters
mock_post.assert_called_once()
call_args = mock_post.call_args
variables = call_args[1]['json']['variables']
assert variables['searchQuery'] == "encryption"
assert variables['tags'] == ["security", "compliance"]
@patch('requests.Session.post')
def test_list_questions_with_pagination_and_filters(self, mock_post):
"""Test questions listing with filters and pagination"""
# Mock first page response with filters
first_response = Mock()
first_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "Security Question 1",
"tags": ["security"],
"queries": [{"name": "SecurityQuery1", "query": "FIND Host"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 2,
"pageInfo": {
"endCursor": "cursor-1",
"hasNextPage": True
}
}
}
}
# Mock second page response with filters
second_response = Mock()
second_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-2",
"title": "Security Question 2",
"tags": ["security"],
"queries": [{"name": "SecurityQuery2", "query": "FIND User"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-02T00:00:00Z"
}
],
"totalHits": 2,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
# Set up mock to return different responses for each call
mock_post.side_effect = [first_response, second_response]
# Call list_questions with filters
result = self.client.list_questions(
search_query="security",
tags=["security"]
)
# Verify result
assert len(result) == 2
assert result[0]['title'] == "Security Question 1"
assert result[1]['title'] == "Security Question 2"
# Verify API calls (2 calls for 2 pages)
assert mock_post.call_count == 2
# Check first call with filters
first_call = mock_post.call_args_list[0]
first_variables = first_call[1]['json']['variables']
assert first_variables['searchQuery'] == "security"
assert first_variables['tags'] == ["security"]
# Check second call with filters and cursor
second_call = mock_post.call_args_list[1]
second_variables = second_call[1]['json']['variables']
assert second_variables['searchQuery'] == "security"
assert second_variables['tags'] == ["security"]
assert second_variables['cursor'] == "cursor-1"
@patch('requests.Session.post')
def test_list_questions_no_parameters(self, mock_post):
"""Test questions listing with no parameters (default behavior)"""
# Mock response for no parameters
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [
{
"id": "question-1",
"title": "Default Question",
"tags": ["default"],
"queries": [{"name": "DefaultQuery", "query": "FIND *"}],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "ONE_DAY",
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z"
}
],
"totalHits": 1,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions with no parameters
result = self.client.list_questions()
# Verify result
assert len(result) == 1
assert result[0]['title'] == "Default Question"
# Verify API call with no variables
mock_post.assert_called_once()
call_args = mock_post.call_args
assert call_args[1]['json']['variables'] == {}
@patch('requests.Session.post')
def test_list_questions_empty_search_results(self, mock_post):
"""Test questions listing with search that returns no results"""
# Mock empty response for search
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [],
"totalHits": 0,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions with search query
result = self.client.list_questions(search_query="nonexistent")
# Verify result
assert len(result) == 0
assert result == []
# Verify API call with search query
mock_post.assert_called_once()
call_args = mock_post.call_args
assert call_args[1]['json']['variables']['searchQuery'] == "nonexistent"
@patch('requests.Session.post')
def test_list_questions_empty_tags_results(self, mock_post):
"""Test questions listing with tags filter that returns no results"""
# Mock empty response for tags filter
mock_response = Mock()
mock_response.json.return_value = {
"data": {
"questions": {
"questions": [],
"totalHits": 0,
"pageInfo": {
"endCursor": None,
"hasNextPage": False
}
}
}
}
mock_post.return_value = mock_response
# Call list_questions with tags filter
result = self.client.list_questions(tags=["nonexistent_tag"])
# Verify result
assert len(result) == 0
assert result == []
# Verify API call with tags filter
mock_post.assert_called_once()
call_args = mock_post.call_args
assert call_args[1]['json']['variables']['tags'] == ["nonexistent_tag"]
def test_list_questions_method_exists(self):
"""Test that list_questions method exists and is callable"""
assert hasattr(self.client, 'list_questions')
assert callable(self.client.list_questions)
def test_list_questions_docstring(self):
"""Test that list_questions method has proper documentation"""
method = getattr(self.client, 'list_questions')
assert method.__doc__ is not None
assert "List all defined Questions" in method.__doc__
assert "J1 account Questions Library" in method.__doc__
def test_list_questions_parameter_validation(self):
"""Test that list_questions method accepts the correct parameter types"""
# Test that method exists with new signature
assert hasattr(self.client, 'list_questions')
method = getattr(self.client, 'list_questions')
# Test that method can be called with different parameter combinations
import inspect
sig = inspect.signature(method)
params = list(sig.parameters.keys())
# Should have self, search_query, and tags parameters
assert 'search_query' in params
assert 'tags' in params
# Check parameter types
search_query_param = sig.parameters['search_query']
tags_param = sig.parameters['tags']
# search_query should be optional string
assert search_query_param.default is None
assert search_query_param.annotation == str or search_query_param.annotation == 'str'
# tags should be optional List[str]
assert tags_param.default is None
assert tags_param.annotation == List[str] or 'List[str]' in str(tags_param.annotation)
def test_list_questions_docstring_updated(self):
"""Test that list_questions method documentation includes new parameters"""
method = getattr(self.client, 'list_questions')
docstring = method.__doc__
assert docstring is not None
assert "search_query" in docstring
assert "tags" in docstring
assert "searchQuery" in docstring or "search query" in docstring
assert "List[str]" in docstring or "List of tags" in docstring
class TestGetQuestionDetails:
"""Test get_question_details method"""
def setup_method(self):
"""Set up test fixtures"""
self.client = JupiterOneClient(account="test-account", token="test-token")
@patch('jupiterone.client.JupiterOneClient._execute_query')
def test_get_question_details_success(self, mock_execute):
"""Test successful retrieval of question details"""
# Mock response
mock_execute.return_value = {
"data": {
"question": {
"id": "question-123",
"sourceId": "source-123",
"title": "Test Question",
"description": "Test question description",
"tags": ["test", "security"],
"lastUpdatedTimestamp": "2024-01-01T00:00:00Z",
"queries": [
{
"name": "TestQuery",
"query": "FIND Host WITH open=true",
"version": "v1",
"resultsAre": "BAD"
}
],
"compliance": {
"standard": "CIS",
"requirements": ["2.1", "2.2"],
"controls": ["Network Security"]
},
"variables": [
{
"name": "environment",
"required": True,
"default": "production"
}
],
"accountId": "test-account",
"integrationDefinitionId": "integration-123",
"showTrend": True,
"pollingInterval": "ONE_HOUR"
}
}
}
# Call get_question_details
result = self.client.get_question_details(question_id="question-123")
# Verify result
assert result['id'] == "question-123"
assert result['title'] == "Test Question"
assert result['description'] == "Test question description"
assert result['tags'] == ["test", "security"]
assert result['sourceId'] == "source-123"
assert result['accountId'] == "test-account"
assert result['showTrend'] is True
assert result['pollingInterval'] == "ONE_HOUR"
# Verify queries
queries = result['queries']
assert len(queries) == 1
assert queries[0]['name'] == "TestQuery"
assert queries[0]['query'] == "FIND Host WITH open=true"
assert queries[0]['version'] == "v1"
assert queries[0]['resultsAre'] == "BAD"
# Verify compliance
compliance = result['compliance']
assert compliance['standard'] == "CIS"
assert compliance['requirements'] == ["2.1", "2.2"]
assert compliance['controls'] == ["Network Security"]
# Verify variables
variables = result['variables']
assert len(variables) == 1
assert variables[0]['name'] == "environment"
assert variables[0]['required'] is True
assert variables[0]['default'] == "production"
# Verify API call
mock_execute.assert_called_once()
call_args = mock_execute.call_args
assert call_args[1]['variables']['id'] == "question-123"
@patch('jupiterone.client.JupiterOneClient._execute_query')
def test_get_question_details_minimal_response(self, mock_execute):
"""Test question details with minimal response data"""
# Mock minimal response
mock_execute.return_value = {
"data": {
"question": {
"id": "question-456",
"title": "Minimal Question",
"tags": [],
"queries": [],
"compliance": None,
"variables": [],
"accountId": "test-account",
"showTrend": False,
"pollingInterval": "DISABLED"
}
}
}
# Call get_question_details
result = self.client.get_question_details(question_id="question-456")
# Verify result
assert result['id'] == "question-456"
assert result['title'] == "Minimal Question"
assert result['tags'] == []
assert result['queries'] == []
assert result['compliance'] is None
assert result['variables'] == []
assert result['showTrend'] is False
assert result['pollingInterval'] == "DISABLED"
@patch('jupiterone.client.JupiterOneClient._execute_query')
def test_get_question_details_with_compliance_list(self, mock_execute):
"""Test question details when compliance is returned as a list"""
# Mock response with compliance as list (edge case)
mock_execute.return_value = {
"data": {
"question": {
"id": "question-789",
"title": "List Compliance Question",
"tags": ["compliance"],
"queries": [{"name": "Query1", "query": "FIND Host"}],
"compliance": [