25
25
'C8Y_BASEURL' : 'http://baseurl' ,
26
26
'C8Y_TENANT' : 'tenant_id' ,
27
27
'C8Y_USER' : 'tenant_user' ,
28
- 'C8Y_PASSWORD' : 'tenant_password'
28
+ 'C8Y_PASSWORD' : 'tenant_password' ,
29
+ 'APPLICATION_KEY' : 'application_key' ,
29
30
}
30
31
31
32
env_multi_tenant = {
32
33
'C8Y_BASEURL' : 'http://baseurl' ,
33
34
'C8Y_BOOTSTRAP_TENANT' : 'tenant_id' ,
34
35
'C8Y_BOOTSTRAP_USER' : 'tenant_user' ,
35
- 'C8Y_BOOTSTRAP_PASSWORD' : 'tenant_password'
36
-
36
+ 'C8Y_BOOTSTRAP_PASSWORD' : 'tenant_password' ,
37
+ 'APPLICATION_KEY' : 'application_key' ,
37
38
}
38
39
39
40
@@ -55,6 +56,7 @@ def test_per_tenant():
55
56
assert c8y .username == env_per_tenant ['C8Y_USER' ]
56
57
assert isinstance (c8y .auth , HTTPBasicAuth )
57
58
assert c8y .auth .password == env_per_tenant ['C8Y_PASSWORD' ]
59
+ assert c8y .application_key == env_per_tenant ['APPLICATION_KEY' ]
58
60
59
61
# -> requests will be prepended with the base url
60
62
with responses .RequestsMock () as rsps :
@@ -71,7 +73,6 @@ def test_per_tenant__user_instances():
71
73
for single-tenant apps."""
72
74
73
75
c8y = SimpleCumulocityApp (
74
- application_key = 'app-key' ,
75
76
processing_mode = 'proc-mode' ,
76
77
cache_ttl = 2 ,
77
78
)
@@ -106,13 +107,29 @@ def test_per_tenant__user_instances():
106
107
assert c8y_3 is not c8y_1
107
108
108
109
110
+ @mock .patch .dict (os .environ , env_per_tenant , clear = True )
111
+ def test_per_tenant_application_key ():
112
+ """Verify that the application key from environment can be overwritten."""
113
+
114
+ # manually setting application
115
+ c8y = SimpleCumulocityApp (application_key = 'app_key' )
116
+ # -> application key from environment overridden
117
+ assert c8y .application_key == 'app_key'
118
+
119
+ # create user instance from HTTP headers
120
+ headers = build_basic_auth ('user' , 'pass' )
121
+ c8y_user = c8y .get_user_instance (headers )
122
+ # -> application key inherited
123
+ assert c8y_user .application_key == c8y .application_key
124
+
125
+
109
126
@mock .patch .dict (os .environ , env_multi_tenant , clear = True )
110
127
def test_multi_tenant__bootstrap_instance ():
111
128
"""Verify that the bootstrap instance will be created properly within a
112
129
multi-tenant environment."""
113
130
114
131
c8y = MultiTenantCumulocityApp (
115
- application_key = 'app-key' ,
132
+ # application_key='app-key',
116
133
processing_mode = 'proc-mode' ,
117
134
).bootstrap_instance
118
135
@@ -121,8 +138,8 @@ def test_multi_tenant__bootstrap_instance():
121
138
assert c8y .username == env_multi_tenant ['C8Y_BOOTSTRAP_USER' ]
122
139
assert isinstance (c8y .auth , HTTPBasicAuth )
123
140
assert c8y .auth .password == env_multi_tenant ['C8Y_BOOTSTRAP_PASSWORD' ]
141
+ assert c8y .application_key == env_multi_tenant ['APPLICATION_KEY' ]
124
142
# -> properties are inherited
125
- assert c8y .application_key == 'app-key'
126
143
assert c8y .processing_mode == 'proc-mode'
127
144
128
145
# -> requests will be prepended with the base url
@@ -248,6 +265,7 @@ def test_multi_tenant__build_from_subscriptions():
248
265
assert c8y .username == 'username'
249
266
assert isinstance (c8y .auth , HTTPBasicAuth )
250
267
assert c8y .auth .password == 'password'
268
+ assert c8y .application_key == c8y_factory .application_key
251
269
252
270
# using the same tenant ID again
253
271
read_subscriptions .reset_mock ()
@@ -266,6 +284,30 @@ def test_multi_tenant__build_from_subscriptions():
266
284
read_subscriptions .assert_not_called ()
267
285
268
286
287
+ @mock .patch .dict (os .environ , env_multi_tenant , clear = True )
288
+ def test_multi_tenant__override_application_key ():
289
+ """Verify that the application key from environment can be overwritten."""
290
+
291
+ # manually setting application
292
+ c8y = MultiTenantCumulocityApp (application_key = 'app_key' )
293
+ # -> application key from environment overridden
294
+ assert c8y .application_key == 'app_key'
295
+
296
+ # create user instance from HTTP headers
297
+ headers = build_basic_auth ('t12345/user' , 'pass' )
298
+ c8y_user = c8y .get_user_instance (headers )
299
+ # -> application key inherited
300
+ assert c8y_user .application_key == c8y .application_key
301
+
302
+ # create tenant instance
303
+ with patch .object (MultiTenantCumulocityApp , '_read_subscription_auths' ) as read_subscriptions :
304
+ # we mock _read_subscriptions so that we don't need an actual
305
+ # connection and have a proper return value
306
+ read_subscriptions .return_value = {'t12345' : HTTPBasicAuth ('username' , 'password' )}
307
+ c8y_tenant = c8y .get_tenant_instance ('t12345' )
308
+ assert c8y_tenant .application_key == c8y .application_key
309
+
310
+
269
311
@mock .patch .dict (os .environ , env_multi_tenant , clear = True )
270
312
def test_read_subscriptions ():
271
313
"""Verify that the subscriptions are read and parsed properly."""
0 commit comments