11# Ignoring pytest-related warnings:
22# pylint: disable=redefined-outer-name,unused-argument
3+ from urllib .parse import urljoin
34from xmlrpc .client import Fault
45
56import pytest
@@ -62,6 +63,18 @@ def test_get_products(mocked_responses, backends):
6263 assert {v ["name" ] for v in rhel ["versions" ]} == {"9.0" , "9.1" , "unspecified" }
6364
6465
66+ def test_get_product (mocked_responses , backends ):
67+ bz = open_bz (url = TEST_URL , ** backends )
68+
69+ product_ids = {product ["id" ] for product in bz .product_get (ptype = "enterable" ,
70+ include_fields = ["id" ])}
71+ product_names = {product ["name" ] for product in bz .product_get (ptype = "selectable" ,
72+ include_fields = ["name" ])}
73+ assert product_ids == {1 , 2 , 3 }
74+ assert product_names == {'Red Hat Enterprise Linux 9' , 'SUSE Linux Enterprise Server 15 SP6' ,
75+ 'TestProduct' }
76+
77+
6578def test_get_components (mocked_responses , backends ):
6679 bz = open_bz (url = TEST_URL , ** backends )
6780 components = bz .getcomponents (product = "SUSE Linux Enterprise Server 15 SP6" )
@@ -106,6 +119,16 @@ def test_get_bug_alias(mocked_responses, backends):
106119 assert bug .summary == "ZeroDivisionError in function foo_bar()"
107120
108121
122+ def test_bug_url (mocked_responses , backends ):
123+ bz = open_bz (url = TEST_URL , ** backends )
124+ bug_id = 2
125+
126+ # Ensure weburl is generated consistently whether
127+ # we are using XMLRPC or REST
128+ bug = bz .getbug (bug_id )
129+ assert bug .weburl == urljoin (TEST_URL , f"/show_bug.cgi?id={ bug_id } " )
130+
131+
109132def test_get_bug_alias_included_field (mocked_responses , backends ):
110133 bug_id , alias = 1 , "FOO-1"
111134 bz = open_bz (url = TEST_URL , ** backends )
@@ -117,6 +140,18 @@ def test_get_bug_alias_included_field(mocked_responses, backends):
117140 assert not hasattr (bug , "summary" )
118141
119142
143+ def test_get_bug_exclude_fields (mocked_responses , backends ):
144+ bz = open_bz (url = TEST_URL , ** backends )
145+
146+ # Check default extra_fields will pull in comments
147+ bug = bz .getbug (2 , exclude_fields = ["product" ])
148+ assert not hasattr (bug , "product" )
149+
150+ # Ensure that include_fields overrides default extra_fields
151+ bug = bz .getbug (2 )
152+ assert hasattr (bug , "product" )
153+
154+
120155def test_get_bug_404 (mocked_responses , backends ):
121156 bz = open_bz (url = TEST_URL , ** backends )
122157 try :
@@ -147,3 +182,39 @@ def test_get_bug_fields(mocked_responses, backends):
147182 assert fields == ["product" ]
148183 bz .getbugfields (names = ["product" , "bug_status" ], force_refresh = True )
149184 assert set (bz .bugfields ) == {"product" , "bug_status" }
185+
186+
187+ def test_query_autorefresh (mocked_responses , backends ):
188+ bz = open_bz (url = TEST_URL , ** backends )
189+
190+ bz .bug_autorefresh = True
191+ bug = bz .query (bz .build_query (bug_id = 1 , include_fields = ["summary" ]))[0 ]
192+ assert hasattr (bug , "component" )
193+ assert bool (bug .component )
194+
195+ bz .bug_autorefresh = False
196+ bug = bz .query (bz .build_query (bug_id = 1 , include_fields = ["summary" ]))[0 ]
197+ assert not hasattr (bug , "component" )
198+ try :
199+ assert bool (bug .component )
200+ except Exception as e :
201+ assert "adjust your include_fields" in str (e )
202+
203+
204+ def test_login_stubs (mocked_responses , backends ):
205+ # Explicitly set configpaths to avoid interference with an API key set by another test
206+ bz = open_bz (url = TEST_URL , configpaths = "/dev/null" , ** backends )
207+ bz_apikey = open_bz (url = TEST_URL , api_key = "random-and-secure-api-key" , ** backends )
208+
209+ # Failed login, verifies our backends are calling the correct API
210+ with pytest .raises (BugzillaError ) as e :
211+ bz .login ("foo" , "bar" )
212+ assert "Login failed" in str (e )
213+
214+ # Login is prohibited, when an API key is defined
215+ with pytest .raises (ValueError ) as e :
216+ bz_apikey .login ("foo" , "bar" )
217+ assert "cannot login when using an API key" in str (e )
218+
219+ # Works fine when not logged in
220+ bz .logout ()
0 commit comments