33# Copyright (c) 2024, Ingram Micro
44# All rights reserved.
55#
6+ from datetime import datetime , timedelta
7+
68from connect .client import ClientError
79from connect .eaas .core .inject .models import Context
810
1113
1214def test_handle_installation_status_installed_create_task (
1315 connect_client ,
14- client_mocker_factory ,
1516 logger ,
1617 installation ,
1718 eaas_schedule_task ,
19+ mocker ,
1820):
1921 config = {}
20- client_mocker = client_mocker_factory (base_url = connect_client .endpoint )
22+ mocker .patch (
23+ 'connect_bi_reporter.events.get_extension_owner_client' ,
24+ return_value = connect_client ,
25+ )
26+ start = datetime .utcnow ()
27+ string_start_date = (
28+ start + timedelta (days = 1 )
29+ ).replace (hour = 0 , minute = 0 , second = 0 , microsecond = 0 ).isoformat ()
30+ p_datetime = mocker .patch (
31+ 'connect_bi_reporter.scheduler.datetime' ,
32+ )
33+ p_datetime .utcnow .return_value = start
34+ p_get_connect_task = mocker .patch (
35+ 'connect_bi_reporter.scheduler.get_schedule_tasks' ,
36+ return_value = [],
37+ )
38+ p_create_connect_task = mocker .patch (
39+ 'connect_bi_reporter.scheduler.create_schedule_task' ,
40+ return_value = eaas_schedule_task ,
41+ )
2142 ctx = Context (
2243 installation_id = installation ['id' ],
2344 environment_id = installation ['environment' ]['id' ],
2445 extension_id = installation ['environment' ]['extension' ]['id' ],
2546 account_id = installation ['environment' ]['extension' ]['owner' ]['id' ],
2647 )
27- (
28- client_mocker .ns ('devops' )
29- .services [ctx .extension_id ]
30- .environments [ctx .environment_id ]
31- .schedules .all ().mock (
32- return_value = [],
33- )
34- )
35- (
36- client_mocker .ns ('devops' )
37- .services [ctx .extension_id ]
38- .environments [ctx .environment_id ]
39- .schedules .create (return_value = eaas_schedule_task )
40- )
48+ task_payload = {
49+ 'description' : 'Create Uploads for recurrent processing.' ,
50+ 'method' : 'create_uploads' ,
51+ 'name' : f'Create Uploads - { ctx .account_id } ' ,
52+ 'parameter' : {
53+ 'installation_id' : ctx .installation_id ,
54+ 'account_id' : ctx .account_id ,
55+ },
56+ 'trigger' : {
57+ 'amount' : 1 ,
58+ 'start' : string_start_date ,
59+ 'type' : 'recurring' ,
60+ 'unit' : 'days' ,
61+ },
62+ }
4163 ext = ConnectBiReporterEventsApplication (
4264 connect_client ,
4365 logger ,
@@ -46,6 +68,9 @@ def test_handle_installation_status_installed_create_task(
4668 installation_client = connect_client ,
4769 )
4870 result = ext .handle_installation_status_change (installation )
71+ p_get_connect_task .assert_called_once_with (connect_client , ctx )
72+ p_create_connect_task .assert_called_once_with (connect_client , ctx , task_payload )
73+
4974 assert result .status == 'success'
5075 assert logger .method_calls [0 ].args [0 ] == (
5176 f'This extension has been installed by Provider '
@@ -59,27 +84,30 @@ def test_handle_installation_status_installed_create_task(
5984
6085def test_handle_installation_status_installed_task_already_exists (
6186 connect_client ,
62- client_mocker_factory ,
6387 logger ,
6488 installation ,
6589 eaas_schedule_task ,
90+ mocker ,
6691):
6792 config = {}
68- client_mocker = client_mocker_factory (base_url = connect_client .endpoint )
93+ mocker .patch (
94+ 'connect_bi_reporter.events.get_extension_owner_client' ,
95+ return_value = connect_client ,
96+ )
97+ p_get_connect_task = mocker .patch (
98+ 'connect_bi_reporter.scheduler.get_schedule_tasks' ,
99+ return_value = [eaas_schedule_task ],
100+ )
101+ p_create_connect_task = mocker .patch (
102+ 'connect_bi_reporter.scheduler.create_schedule_task' ,
103+ )
69104 ctx = Context (
70105 installation_id = installation ['id' ],
71106 environment_id = installation ['environment' ]['id' ],
72107 extension_id = installation ['environment' ]['extension' ]['id' ],
73108 account_id = installation ['environment' ]['extension' ]['owner' ]['id' ],
74109 )
75- (
76- client_mocker .ns ('devops' )
77- .services [ctx .extension_id ]
78- .environments [ctx .environment_id ]
79- .schedules .all ().mock (
80- return_value = [eaas_schedule_task ],
81- )
82- )
110+
83111 ext = ConnectBiReporterEventsApplication (
84112 connect_client ,
85113 logger ,
@@ -88,6 +116,8 @@ def test_handle_installation_status_installed_task_already_exists(
88116 installation_client = connect_client ,
89117 )
90118 result = ext .handle_installation_status_change (installation )
119+ p_get_connect_task .assert_called_once_with (connect_client , ctx )
120+ p_create_connect_task .assert_not_called ()
91121 assert result .status == 'success'
92122 assert logger .method_calls [0 ].args [0 ] == (
93123 f'This extension has been installed by Provider '
0 commit comments