66import stackify
77import os
88import stackify .rum
9- import base64
109import json
1110
1211from .bases import ClearEnvTest
1312from stackify .transport .application import ApiConfiguration
1413from stackify .utils import arg_or_env
1514from stackify .constants import DEFAULT_RUM_SCRIPT_URL
16- from stackifyapm .base import Client
1715from unittest import TestCase
1816
1917APM_CONFIG = {
@@ -40,43 +38,13 @@ def setUp(self):
4038 def shutDown (self ):
4139 pass
4240
43- def test_default_insert_rum_script_from_apm_with_transaction (self ):
44- self .update_apm_installed (True )
45- client = Client (APM_CONFIG )
46-
47- transaction = client .begin_transaction ("transaction_test" , client = client )
48- rum_data = stackify .rum .insert_rum_script ()
49- assert rum_data
50-
51- rum_settings = {
52- "ID" : transaction .get_trace_parent ().trace_id ,
53- "Name" : base64 .b64encode (APM_CONFIG ["APPLICATION_NAME" ].encode ("utf-8" )).decode ("utf-8" ),
54- "Env" : base64 .b64encode (APM_CONFIG ["ENVIRONMENT" ].encode ("utf-8" )).decode ("utf-8" ),
55- "Trans" : base64 .b64encode ('/' .encode ("utf-8" )).decode ("utf-8" )
56- }
57-
58- result_string = '<script type="text/javascript">(window.StackifySettings || (window.StackifySettings = {}))</script><script src="{}" data-key="{}" async></script>' .format (
59- json .dumps (rum_settings ),
60- APM_CONFIG ["RUM_SCRIPT_URL" ],
61- APM_CONFIG ["RUM_KEY" ]
62- )
63-
64- assert rum_data == result_string
65- client .end_transaction ("transaction_test" )
66- self .restore_apm_installed ()
67-
68- def test_default_insert_rum_script_from_apm_without_transaction (self ):
69- self .update_apm_installed (True )
70- rum_data = stackify .rum .insert_rum_script ()
71- assert not rum_data
72- self .restore_apm_installed ()
73-
41+ @mock .patch ('stackify.rum.is_apm_installed' )
7442 @mock .patch ('stackify.rum.get_reporting_url' )
7543 @mock .patch ('stackify.rum.get_transaction_id' )
76- def test_default_insert_rum_script (self , func , func_reporting_url ):
44+ def test_default_insert_rum_script (self , func , func_reporting_url , func_apm ):
7745 func .return_value = '123'
7846 func_reporting_url .return_value = 'test reporting url'
79- self . update_apm_installed ( False )
47+ func_apm . return_value = False
8048 self .update_common_config (
8149 rum_key = 'asd' ,
8250 application = 'app' ,
@@ -92,12 +60,12 @@ def test_default_insert_rum_script(self, func, func_reporting_url):
9260
9361 result = stackify .rum .insert_rum_script ()
9462 self .reset_common_config ()
95- self .restore_apm_installed ()
9663
9764 assert result == '<script type="text/javascript">(window.StackifySettings || (window.StackifySettings = {}))</script><script src="https://stckjs.stackify.com/stckjs.js" data-key="asd" async></script>' .format (json .dumps (rum_settings ))
9865
99- def test_default_insert_rum_script_no_transaction_id (self ):
100- self .update_apm_installed (False )
66+ @mock .patch ('stackify.rum.is_apm_installed' )
67+ def test_default_insert_rum_script_no_transaction_id (self , func_apm ):
68+ func_apm .return_value = False
10169 self .update_common_config (
10270 rum_key = 'asd' ,
10371 application = 'app' ,
@@ -106,12 +74,12 @@ def test_default_insert_rum_script_no_transaction_id(self):
10674
10775 result = stackify .rum .insert_rum_script ()
10876 self .reset_common_config ()
109- self .restore_apm_installed ()
11077
11178 assert result is None
11279
113- def test_default_insert_rum_script_no_key (self ):
114- self .update_apm_installed (False )
80+ @mock .patch ('stackify.rum.is_apm_installed' )
81+ def test_default_insert_rum_script_no_key (self , func_apm ):
82+ func_apm .return_value = False
11583 self .update_common_config (
11684 rum_key = '' ,
11785 application = 'app' ,
@@ -122,24 +90,24 @@ def test_default_insert_rum_script_no_key(self):
12290 assert not result
12391
12492 self .reset_common_config ()
125- self .restore_apm_installed ()
12693
127- def test_default_insert_rum_script_no_details (self ):
128- self .update_apm_installed (False )
94+ @mock .patch ('stackify.rum.is_apm_installed' )
95+ def test_default_insert_rum_script_no_details (self , func_apm ):
96+ func_apm .return_value = False
12997 self .update_common_config ()
13098
13199 result = stackify .rum .insert_rum_script ()
132100 assert not result
133101
134102 self .reset_common_config ()
135- self .restore_apm_installed ()
136103
104+ @mock .patch ('stackify.rum.is_apm_installed' )
137105 @mock .patch ('stackify.rum.get_reporting_url' )
138106 @mock .patch ('stackify.rum.get_transaction_id' )
139- def test_default_insert_rum_script_from_api (self , func , func_reporting_url ):
107+ def test_default_insert_rum_script_from_api (self , func , func_reporting_url , func_apm ):
140108 func .return_value = '123'
109+ func_apm .return_value = False
141110 func_reporting_url .return_value = 'test reporting url'
142- self .update_apm_installed (False )
143111 self .create_config (
144112 rum_key = 'asd1' ,
145113 application = 'app1' ,
@@ -153,11 +121,11 @@ def test_default_insert_rum_script_from_api(self, func, func_reporting_url):
153121 }
154122 result = stackify .rum .insert_rum_script ()
155123 self .reset_common_config ()
156- self .restore_apm_installed ()
157124 assert result == '<script type="text/javascript">(window.StackifySettings || (window.StackifySettings = {}))</script><script src="https://stckjs.stackify.com/stckjs.js" data-key="asd1" async></script>' .format (json .dumps (rum_settings ))
158125
159- def test_default_insert_rum_script_no_key_from_api (self ):
160- self .update_apm_installed (False )
126+ @mock .patch ('stackify.rum.is_apm_installed' )
127+ def test_default_insert_rum_script_no_key_from_api (self , func_apm ):
128+ func_apm .return_value = False
161129 self .create_config (
162130 rum_key = None ,
163131 application = 'app2' ,
@@ -166,12 +134,12 @@ def test_default_insert_rum_script_no_key_from_api(self):
166134
167135 result = stackify .rum .insert_rum_script ()
168136 self .reset_common_config ()
169- self .restore_apm_installed ()
170137
171138 assert not result
172139
173- def test_default_insert_rum_script_no_details_from_api (self ):
174- self .update_apm_installed (False )
140+ @mock .patch ('stackify.rum.is_apm_installed' )
141+ def test_default_insert_rum_script_no_details_from_api (self , func_apm ):
142+ func_apm .return_value = False
175143 self .create_config (
176144 application = None ,
177145 environment = None ,
@@ -180,17 +148,9 @@ def test_default_insert_rum_script_no_details_from_api(self):
180148
181149 result = stackify .rum .insert_rum_script ()
182150 self .reset_common_config ()
183- self .restore_apm_installed ()
184151
185152 assert not result
186153
187- def update_apm_installed (self , installed ):
188- self .apm_installed = stackify .rum .apm_installed
189- stackify .rum .apm_installed = installed
190-
191- def restore_apm_installed (self ):
192- stackify .rum .apm_installed = self .apm_installed
193-
194154 def update_common_config (self , rum_key = None , rum_script_url = None , application = None , environment = None ):
195155 self .config_rum_key = stackify .config .rum_key
196156 self .config_rum_script_url = stackify .config .rum_script_url
0 commit comments