Skip to content

Commit 8e31a8f

Browse files
committed
Refactor calling .login() outside of __init__() method of Xmlrpc classes
and call .login() as part of TCMS.__init__() instead! `__init__` method calls overridden method : Call to self.login in __init__ method, which is overridden by method TCMSKerbXmlrpc.login. When an instance of a class is initialized, the super-class state should be fully initialized before it becomes visible to the subclass. Calling methods of the subclass in the superclass' __init__ method violates this important invariant.
1 parent 539a5af commit 8e31a8f

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

tcms_api/__init__.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,26 @@ def __init__(self):
112112
config = ConfigParser()
113113
config.read(self._path)
114114

115+
rpc_implementor = None
116+
server_url = self.server_url(config)
115117
if strtobool(config["tcms"].get("use_kerberos", "False")):
116118
# use Kerberos
117-
TCMS._connection = TCMSKerbXmlrpc(
118-
None, None, self.server_url(config)
119-
).server
120-
return
121-
122-
try:
123-
# use password authentication
124-
TCMS._connection = TCMSXmlrpc(
125-
config["tcms"]["username"],
126-
config["tcms"]["password"],
127-
self.server_url(config),
128-
).server
129-
except KeyError as err:
130-
raise RuntimeError(f"username/password required in {self._path}") from err
119+
rpc_implementor = TCMSKerbXmlrpc(None, None, server_url)
120+
else:
121+
try:
122+
# use password authentication
123+
rpc_implementor = TCMSXmlrpc(
124+
config["tcms"]["username"],
125+
config["tcms"]["password"],
126+
server_url,
127+
)
128+
except KeyError as err:
129+
raise RuntimeError(
130+
f"username/password required in {self._path}"
131+
) from err
132+
133+
rpc_implementor.login()
134+
TCMS._connection = rpc_implementor.server
131135

132136
return
133137

tcms_api/xmlrpc.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ def __init__(self, username, password, url):
116116
url, transport=self.transport, verbose=VERBOSE, allow_none=1
117117
)
118118

119-
self.login(username, password, url)
119+
self.username = username
120+
self.password = password
121+
self.url = url
120122

121-
def login(self, username, password, url): # pylint: disable=unused-argument
123+
def login(self):
122124
"""
123125
Login in the web app to save a session cookie in the cookie jar!
124126
"""
125-
self.server.Auth.login(username, password)
127+
self.server.Auth.login(self.username, self.password)
126128

127129

128130
class TCMSKerbXmlrpc(TCMSXmlrpc):
@@ -146,8 +148,8 @@ def __init__(self, username, password, url):
146148

147149
super().__init__(username, password, url)
148150

149-
def login(self, username, password, url):
150-
url = url.replace("xml-rpc", "login/kerberos")
151+
def login(self):
152+
url = self.url.replace("xml-rpc", "login/kerberos")
151153
hostname = get_hostname(url)
152154

153155
_, headers, _ = self.transport.get_host_info(hostname)

0 commit comments

Comments
 (0)