1
1
import json
2
2
from base64 import b64encode
3
3
from datetime import datetime
4
+ from unittest .mock import patch
4
5
5
6
from shared .encryption .standard import EncryptorWithAlreadyGeneratedKey
6
- from shared .license import LicenseInformation , get_current_license , parse_license
7
+ from shared .license import (
8
+ LICENSE_ERRORS_MESSAGES ,
9
+ LicenseInformation ,
10
+ get_current_license ,
11
+ parse_license ,
12
+ startup_license_logging ,
13
+ )
14
+ from tests .base import BaseTestCase
15
+
16
+ valid_trial_license_encrypted = "8rz8TfoZ1HDR5P2kpXSOaSvihqbHnJ4DANvDTB/J94tMjovTUUmuIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqwluwC03t22/UA+4SHwxHbi6IhBbYXCEggYcrwtyjcdA4y3yARixGEsNEwDqAzxXLOe95nMetpb1u1Jr8E6CWp/2QSqvIUww8qTkegESk+3CiH3bPrA71pW8w9KYDX65g=="
17
+ invalid_license_encrypted = (
18
+ "8rz8TfodsdsSOaSvih09nvnasu4DANvdsdsauIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqadsada"
19
+ )
7
20
8
21
9
22
def test_sample_license_checking ():
10
- encrypted_license = "8rz8TfoZ1HDR5P2kpXSOaSvihqbHnJ4DANvDTB/J94tMjovTUUmuIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqwluwC03t22/UA+4SHwxHbi6IhBbYXCEggYcrwtyjcdA4y3yARixGEsNEwDqAzxXLOe95nMetpb1u1Jr8E6CWp/2QSqvIUww8qTkegESk+3CiH3bPrA71pW8w9KYDX65g=="
23
+ encrypted_license = valid_trial_license_encrypted
11
24
expected_result = LicenseInformation (
12
25
is_valid = True ,
13
26
is_trial = True ,
@@ -56,9 +69,7 @@ def test_sample_license_checking_with_users_and_repos():
56
69
57
70
58
71
def test_invalid_license_checking_nonvalid_64encoded ():
59
- encrypted_license = (
60
- "8rz8TfodsdsSOaSvih09nvnasu4DANvdsdsauIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqadsada"
61
- )
72
+ encrypted_license = invalid_license_encrypted
62
73
expected_result = LicenseInformation (
63
74
is_valid = False ,
64
75
is_trial = False ,
@@ -111,7 +122,7 @@ def test_invalid_license_checking_wrong_key():
111
122
112
123
113
124
def test_get_current_license (mock_configuration ):
114
- encrypted_license = "8rz8TfoZ1HDR5P2kpXSOaSvihqbHnJ4DANvDTB/J94tMjovTUUmuIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqwluwC03t22/UA+4SHwxHbi6IhBbYXCEggYcrwtyjcdA4y3yARixGEsNEwDqAzxXLOe95nMetpb1u1Jr8E6CWp/2QSqvIUww8qTkegESk+3CiH3bPrA71pW8w9KYDX65g=="
125
+ encrypted_license = valid_trial_license_encrypted
115
126
mock_configuration .set_params ({"setup" : {"enterprise_license" : encrypted_license }})
116
127
expected_result = LicenseInformation (
117
128
is_valid = True ,
@@ -137,3 +148,48 @@ def test_get_current_license_no_license(mock_configuration):
137
148
expires = None ,
138
149
)
139
150
assert get_current_license () == expected_result
151
+
152
+
153
+ @patch ("builtins.print" )
154
+ class TestUserGivenSecret (BaseTestCase ):
155
+ def test_startup_license_logging_valid (self , mock_print , mock_configuration ):
156
+ encrypted_license = valid_trial_license_encrypted
157
+ mock_configuration .set_params (
158
+ {"setup" : {"enterprise_license" : encrypted_license }}
159
+ )
160
+
161
+ expected_log = [
162
+ "" ,
163
+ "==> Checking License" ,
164
+ " License is valid" ,
165
+ " License expires 2020-05-09 00:00:00 <==" ,
166
+ "" ,
167
+ ]
168
+
169
+ startup_license_logging ()
170
+ mock_print .assert_called_once_with (* expected_log , sep = "\n " )
171
+
172
+ @patch ("shared.license.parse_license" )
173
+ def test_startup_license_logging_invalid (
174
+ self , mock_license_parsing , mock_print , mock_configuration
175
+ ):
176
+ mock_license_parsing .return_value = LicenseInformation (
177
+ is_valid = False ,
178
+ message = LICENSE_ERRORS_MESSAGES ["no-license" ],
179
+ )
180
+
181
+ mock_configuration .set_params (
182
+ {"setup" : {"enterprise_license" : True }}
183
+ ) # value doesn't matter since parse_license is mocked
184
+
185
+ expected_log = [
186
+ "" ,
187
+ "==> Checking License" ,
188
+ " License is INVALID" ,
189
+ f" Warning: { LICENSE_ERRORS_MESSAGES ["no-license" ]} " ,
190
+ " License expires NOT FOUND <==" ,
191
+ "" ,
192
+ ]
193
+
194
+ startup_license_logging ()
195
+ mock_print .assert_called_once_with (* expected_log , sep = "\n " )
0 commit comments