@@ -43,9 +43,21 @@ def teardown_function(function):
4343@pytest .fixture ()
4444def mock_hub_instance (requests_mock ):
4545 requests_mock .post (
46- "https://my-hub-host/ j_spring_security_check" ,
46+ "{}/ j_spring_security_check" . format ( fake_hub_host ) ,
4747 headers = {"Set-Cookie" : 'AUTHORIZATION_BEARER={}; Path=/; secure; Secure; HttpOnly' .format (invalid_bearer_token )}
4848 )
49+ requests_mock .get (
50+ "{}/api/current-version" .format (fake_hub_host ),
51+ json = {
52+ "version" : "2018.11.0" ,
53+ "_meta" : {
54+ "allow" : [
55+ "GET"
56+ ],
57+ "href" : "{}/api/current-version" .format (fake_hub_host )
58+ }
59+ }
60+ )
4961 yield HubInstance (fake_hub_host , "a_username" , "a_password" )
5062
5163@pytest .fixture ()
@@ -58,6 +70,18 @@ def mock_hub_instance_using_api_token(requests_mock):
5870 'Content-Type' : 'application/json'
5971 }
6072 )
73+ requests_mock .get (
74+ "{}/api/current-version" .format (fake_hub_host ),
75+ json = {
76+ "version" : "2018.11.0" ,
77+ "_meta" : {
78+ "allow" : [
79+ "GET"
80+ ],
81+ "href" : "{}/api/current-version" .format (fake_hub_host )
82+ }
83+ }
84+ )
6185
6286 yield HubInstance (fake_hub_host , api_token = made_up_api_token )
6387
@@ -82,6 +106,54 @@ def test_vulnerability_info(requests_mock):
82106 with open ("sample-vulnerability.json" ) as sample_vulnerability_file :
83107 yield json .loads (sample_vulnerability_file .read ())
84108
109+ def test_get_major_version (requests_mock ):
110+ requests_mock .post (
111+ "{}/j_spring_security_check" .format (fake_hub_host ),
112+ headers = {"Set-Cookie" : 'AUTHORIZATION_BEARER={}; Path=/; secure; Secure; HttpOnly' .format (invalid_bearer_token )}
113+ )
114+ for version in ["2018.11.0" , "5.0.2" , "4.8.3" , "3.7.2" ]:
115+ expected = version .split ("." )[0 ]
116+ requests_mock .get (
117+ "{}/api/current-version" .format (fake_hub_host ),
118+ json = {
119+ "version" : "{}" .format (version ),
120+ "_meta" : {
121+ "allow" : [
122+ "GET"
123+ ],
124+ "href" : "{}/api/current-version" .format (fake_hub_host )
125+ }
126+ }
127+ )
128+ hub = HubInstance (fake_hub_host , "a_username" , "a_password" )
129+ assert hub .bd_major_version == expected
130+
131+ def test_get_headers (mock_hub_instance ):
132+ # somewhat contrived, but it does execute all the paths
133+ # TODO: better way to test this one?
134+ #
135+ the_api_token = "fake-api-token"
136+ the_csrf_token = "fake-csrf-token"
137+ the_token = "fake-bearer-token"
138+ mock_hub_instance .config ['api_token' ] = the_api_token
139+ mock_hub_instance .csrf_token = the_csrf_token
140+ mock_hub_instance .token = the_token
141+
142+ assert mock_hub_instance .get_headers () == {
143+ 'X-CSRF-TOKEN' : the_csrf_token ,
144+ 'Authorization' : "Bearer {}" .format (the_token ),
145+ 'Content-Type' : 'application/json' }
146+
147+ del mock_hub_instance .config ['api_token' ]
148+ for bd_major_version in ["2018" , "5" , "4" , "3" ]:
149+ if bd_major_version == "3" :
150+ expected = {"Cookie" : mock_hub_instance .cookie }
151+ else :
152+ expected = {"Authorization" :"Bearer " + mock_hub_instance .token }
153+
154+ mock_hub_instance .bd_major_version = bd_major_version
155+ assert mock_hub_instance .get_headers () == expected
156+
85157def test_get_policy_url (mock_hub_instance ):
86158 assert mock_hub_instance ._get_policy_url () == fake_hub_host + "/api/policy-rules"
87159
@@ -114,6 +186,19 @@ def test_hub_instance_with_write_config(requests_mock):
114186 "https://my-hub-host/j_spring_security_check" ,
115187 headers = {"Set-Cookie" : 'AUTHORIZATION_BEARER={}; Path=/; secure; Secure; HttpOnly' .format (invalid_bearer_token )}
116188 )
189+ requests_mock .get (
190+ "{}/api/current-version" .format (fake_hub_host ),
191+ json = {
192+ "version" : "2018.11.0" ,
193+ "_meta" : {
194+ "allow" : [
195+ "GET"
196+ ],
197+ "href" : "{}/api/current-version" .format (fake_hub_host )
198+ }
199+ }
200+ )
201+
117202 with patch ("builtins.open" , new_callable = mock_open ()) as m :
118203 with patch ('json.dump' ) as m_json :
119204 hub = HubInstance (fake_hub_host , "a_username" , "a_password" )
@@ -126,6 +211,19 @@ def test_hub_instance_with_write_config_false(requests_mock):
126211 "https://my-hub-host/j_spring_security_check" ,
127212 headers = {"Set-Cookie" : 'AUTHORIZATION_BEARER={}; Path=/; secure; Secure; HttpOnly' .format (invalid_bearer_token )}
128213 )
214+ requests_mock .get (
215+ "{}/api/current-version" .format (fake_hub_host ),
216+ json = {
217+ "version" : "2018.11.0" ,
218+ "_meta" : {
219+ "allow" : [
220+ "GET"
221+ ],
222+ "href" : "{}/api/current-version" .format (fake_hub_host )
223+ }
224+ }
225+ )
226+
129227 with patch .object (HubInstance , "write_config" ) as mock_write_config :
130228 hub = HubInstance (fake_hub_host , "a_username" , "a_password" , write_config_flag = False )
131229
0 commit comments