12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
from typing import Any , Dict , List
15
- from unittest . mock import patch
15
+ from unittest import mock
16
16
17
17
import pytest
18
18
import yaml
@@ -36,7 +36,7 @@ def converted_extra_var(var: str) -> str:
36
36
37
37
38
38
@pytest .mark .django_db
39
- @patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
39
+ @mock . patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
40
40
def test_create_activation (
41
41
admin_awx_token : models .AwxToken ,
42
42
activation_payload : Dict [str , Any ],
@@ -74,7 +74,7 @@ def test_create_activation(
74
74
75
75
76
76
@pytest .mark .django_db
77
- @patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
77
+ @mock . patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
78
78
def test_create_activation_blank_text (
79
79
admin_awx_token : models .AwxToken ,
80
80
activation_payload_blank_text : Dict [str , Any ],
@@ -163,6 +163,35 @@ def test_create_activation_bad_entity(admin_client: APIClient):
163
163
assert response .status_code == status .HTTP_400_BAD_REQUEST
164
164
165
165
166
+ @pytest .mark .parametrize (
167
+ "enabled" ,
168
+ [True , False ],
169
+ )
170
+ @pytest .mark .django_db
171
+ @mock .patch ("aap_eda.core.tasking.is_redis_failed" , return_value = True )
172
+ def test_create_activation_redis_unavailable (
173
+ is_redis_failed : mock .Mock ,
174
+ activation_payload : Dict [str , Any ],
175
+ admin_awx_token : models .AwxToken ,
176
+ default_rulebook : models .Rulebook ,
177
+ admin_client : APIClient ,
178
+ enabled : bool ,
179
+ preseed_credential_types ,
180
+ ):
181
+ activation_payload ["is_enabled" ] = enabled
182
+ response = admin_client .post (
183
+ f"{ api_url_v1 } /activations/" , data = activation_payload
184
+ )
185
+
186
+ if not enabled :
187
+ assert response .status_code == status .HTTP_201_CREATED
188
+ else :
189
+ assert response .status_code == status .HTTP_409_CONFLICT
190
+ assert response .json () == {
191
+ "detail" : "Redis is required but unavailable."
192
+ }
193
+
194
+
166
195
@pytest .mark .parametrize (
167
196
"dependent_object" ,
168
197
[
@@ -442,6 +471,21 @@ def test_delete_activation(
442
471
assert response .status_code == expected_response
443
472
444
473
474
+ @pytest .mark .django_db
475
+ @mock .patch ("aap_eda.core.tasking.is_redis_failed" , return_value = True )
476
+ def test_delete_activation_redis_unavailable (
477
+ is_redis_failed : mock .Mock ,
478
+ default_activation : models .Activation ,
479
+ admin_client : APIClient ,
480
+ preseed_credential_types ,
481
+ ):
482
+ response = admin_client .delete (
483
+ f"{ api_url_v1 } /activations/{ default_activation .id } /"
484
+ )
485
+ assert response .status_code == status .HTTP_409_CONFLICT
486
+ assert response .json () == {"detail" : "Redis is required but unavailable." }
487
+
488
+
445
489
@pytest .mark .django_db
446
490
def test_restart_activation (
447
491
default_activation : models .Activation ,
@@ -455,6 +499,38 @@ def test_restart_activation(
455
499
assert response .status_code == status .HTTP_204_NO_CONTENT
456
500
457
501
502
+ @pytest .mark .parametrize (
503
+ "enabled" ,
504
+ [True , False ],
505
+ )
506
+ @pytest .mark .django_db
507
+ @mock .patch ("aap_eda.core.tasking.is_redis_failed" , return_value = True )
508
+ def test_restart_activation_redis_unavailable (
509
+ is_redis_failed : mock .Mock ,
510
+ default_activation : models .Activation ,
511
+ admin_client : APIClient ,
512
+ enabled : bool ,
513
+ preseed_credential_types ,
514
+ ):
515
+ default_activation .is_enabled = enabled
516
+ default_activation .save (update_fields = ["is_enabled" ])
517
+
518
+ response = admin_client .post (
519
+ f"{ api_url_v1 } /activations/{ default_activation .id } /restart/"
520
+ )
521
+
522
+ if not enabled :
523
+ assert response .status_code == status .HTTP_403_FORBIDDEN
524
+ assert response .json () == {
525
+ "detail" : "Activation is disabled and cannot be run."
526
+ }
527
+ else :
528
+ assert response .status_code == status .HTTP_409_CONFLICT
529
+ assert response .json () == {
530
+ "detail" : "Redis is required but unavailable."
531
+ }
532
+
533
+
458
534
@pytest .mark .django_db
459
535
@pytest .mark .parametrize ("action" , [enums .Action .RESTART , enums .Action .ENABLE ])
460
536
def test_restart_activation_without_de (
@@ -489,7 +565,7 @@ def test_restart_activation_without_de(
489
565
490
566
491
567
@pytest .mark .django_db
492
- @patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
568
+ @mock . patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
493
569
def test_enable_activation (
494
570
default_activation : models .Activation ,
495
571
admin_client : APIClient ,
@@ -537,6 +613,36 @@ def test_enable_activation(
537
613
)
538
614
539
615
616
+ @pytest .mark .parametrize (
617
+ "enabled" ,
618
+ [True , False ],
619
+ )
620
+ @pytest .mark .django_db
621
+ @mock .patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
622
+ @mock .patch ("aap_eda.core.tasking.is_redis_failed" , return_value = True )
623
+ def test_enable_activation_redis_unavailable (
624
+ is_redis_failed : mock .Mock ,
625
+ default_activation : models .Activation ,
626
+ admin_client : APIClient ,
627
+ enabled : bool ,
628
+ preseed_credential_types ,
629
+ ):
630
+ default_activation .is_enabled = enabled
631
+ default_activation .save (update_fields = ["is_enabled" ])
632
+
633
+ response = admin_client .post (
634
+ f"{ api_url_v1 } /activations/{ default_activation .id } /enable/"
635
+ )
636
+
637
+ if enabled :
638
+ assert response .status_code == status .HTTP_204_NO_CONTENT
639
+ else :
640
+ assert response .status_code == status .HTTP_409_CONFLICT
641
+ assert response .json () == {
642
+ "detail" : "Redis is required but unavailable."
643
+ }
644
+
645
+
540
646
@pytest .mark .django_db
541
647
def test_disable_activation (
542
648
default_activation : models .Activation ,
@@ -550,6 +656,35 @@ def test_disable_activation(
550
656
assert response .status_code == status .HTTP_204_NO_CONTENT
551
657
552
658
659
+ @pytest .mark .parametrize (
660
+ "enabled" ,
661
+ [True , False ],
662
+ )
663
+ @pytest .mark .django_db
664
+ @mock .patch ("aap_eda.core.tasking.is_redis_failed" , return_value = True )
665
+ def test_disable_activation_redis_unavailable (
666
+ is_redis_failed : mock .Mock ,
667
+ default_activation : models .Activation ,
668
+ admin_client : APIClient ,
669
+ enabled : bool ,
670
+ preseed_credential_types ,
671
+ ):
672
+ default_activation .is_enabled = enabled
673
+ default_activation .save (update_fields = ["is_enabled" ])
674
+
675
+ response = admin_client .post (
676
+ f"{ api_url_v1 } /activations/{ default_activation .id } /disable/"
677
+ )
678
+
679
+ if not enabled :
680
+ assert response .status_code == status .HTTP_204_NO_CONTENT
681
+ else :
682
+ assert response .status_code == status .HTTP_409_CONFLICT
683
+ assert response .json () == {
684
+ "detail" : "Redis is required but unavailable."
685
+ }
686
+
687
+
553
688
@pytest .mark .django_db
554
689
def test_list_activation_instances (
555
690
default_activation : models .Activation ,
@@ -812,7 +947,7 @@ def test_create_activation_with_awx_token(
812
947
813
948
814
949
@pytest .mark .django_db
815
- @patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
950
+ @mock . patch .object (settings , "RULEBOOK_WORKER_QUEUES" , [])
816
951
def test_create_activation_with_skip_audit_events (
817
952
admin_awx_token : models .AwxToken ,
818
953
activation_payload_skip_audit_events : Dict [str , Any ],
0 commit comments