Skip to content

Commit 2657eed

Browse files
committed
Fix review: Use AuthType enum values
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent 69eb1b4 commit 2657eed

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

py/selenium/webdriver/remote/client_config.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import base64
1818
import os
1919
import socket
20+
from enum import Enum
2021
from typing import Optional
2122
from urllib import parse
2223

@@ -26,6 +27,12 @@
2627
from selenium.webdriver.common.proxy import ProxyType
2728

2829

30+
class AuthType(Enum):
31+
BASIC = "Basic"
32+
BEARER = "Bearer"
33+
X_API_KEY = "X-API-Key"
34+
35+
2936
class ClientConfig:
3037
def __init__(
3138
self,
@@ -38,7 +45,7 @@ def __init__(
3845
ca_certs: Optional[str] = None,
3946
username: Optional[str] = None,
4047
password: Optional[str] = None,
41-
auth_type: Optional[str] = "Basic",
48+
auth_type: Optional[AuthType] = AuthType.BASIC,
4249
token: Optional[str] = None,
4350
user_agent: Optional[str] = None,
4451
extra_headers: Optional[dict] = None,
@@ -202,16 +209,16 @@ def password(self, value: str) -> None:
202209
self._password = value
203210

204211
@property
205-
def auth_type(self) -> str:
212+
def auth_type(self) -> AuthType:
206213
"""Returns the type of authentication to the remote server."""
207214
return self._auth_type
208215

209216
@auth_type.setter
210-
def auth_type(self, value: str) -> None:
217+
def auth_type(self, value: AuthType) -> None:
211218
"""Sets the type of authentication to the remote server if it is not
212219
using basic with username and password.
213220
214-
Support values: Bearer, X-API-Key. For others, please use `extra_headers` instead
221+
:Args: value - AuthType enum value. For others, please use `extra_headers` instead
215222
"""
216223
self._auth_type = value
217224

@@ -273,13 +280,12 @@ def get_proxy_url(self) -> Optional[str]:
273280

274281
def get_auth_header(self) -> Optional[dict]:
275282
"""Returns the authorization to add to the request headers."""
276-
auth_type = self.auth_type.lower()
277-
if auth_type == "basic" and self.username and self.password:
283+
if self.auth_type is AuthType.BASIC and self.username and self.password:
278284
credentials = f"{self.username}:{self.password}"
279285
encoded_credentials = base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
280-
return {"Authorization": f"Basic {encoded_credentials}"}
281-
if auth_type == "bearer" and self.token:
282-
return {"Authorization": f"Bearer {self.token}"}
283-
if auth_type == "x-api-key" and self.token:
284-
return {"X-API-Key": f"{self.token}"}
286+
return {"Authorization": f"{AuthType.BASIC.value} {encoded_credentials}"}
287+
if self.auth_type is AuthType.BEARER and self.token:
288+
return {"Authorization": f"{AuthType.BEARER.value} {self.token}"}
289+
if self.auth_type is AuthType.X_API_KEY and self.token:
290+
return {f"{AuthType.X_API_KEY.value}": f"{self.token}"}
285291
return None

py/test/unit/selenium/webdriver/remote/remote_connection_tests.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from selenium import __version__
2828
from selenium.webdriver import Proxy
2929
from selenium.webdriver.common.proxy import ProxyType
30+
from selenium.webdriver.remote.client_config import AuthType
3031
from selenium.webdriver.remote.remote_connection import ClientConfig
3132
from selenium.webdriver.remote.remote_connection import RemoteConnection
3233

@@ -93,7 +94,7 @@ def test_get_proxy_url_http(mock_proxy_settings):
9394

9495
def test_get_auth_header_if_client_config_pass_basic_auth():
9596
custom_config = ClientConfig(
96-
remote_server_addr="http://remote", keep_alive=True, username="user", password="pass", auth_type="Basic"
97+
remote_server_addr="http://remote", keep_alive=True, username="user", password="pass", auth_type=AuthType.BASIC
9798
)
9899
remote_connection = RemoteConnection(custom_config.remote_server_addr, client_config=custom_config)
99100
headers = remote_connection._client_config.get_auth_header()
@@ -102,7 +103,7 @@ def test_get_auth_header_if_client_config_pass_basic_auth():
102103

103104
def test_get_auth_header_if_client_config_pass_bearer_token():
104105
custom_config = ClientConfig(
105-
remote_server_addr="http://remote", keep_alive=True, auth_type="Bearer", token="dXNlcjpwYXNz"
106+
remote_server_addr="http://remote", keep_alive=True, auth_type=AuthType.BEARER, token="dXNlcjpwYXNz"
106107
)
107108
remote_connection = RemoteConnection(custom_config.remote_server_addr, client_config=custom_config)
108109
headers = remote_connection._client_config.get_auth_header()
@@ -111,7 +112,7 @@ def test_get_auth_header_if_client_config_pass_bearer_token():
111112

112113
def test_get_auth_header_if_client_config_pass_x_api_key():
113114
custom_config = ClientConfig(
114-
remote_server_addr="http://remote", keep_alive=True, auth_type="X-API-Key", token="abcdefgh123456789"
115+
remote_server_addr="http://remote", keep_alive=True, auth_type=AuthType.X_API_KEY, token="abcdefgh123456789"
115116
)
116117
remote_connection = RemoteConnection(custom_config.remote_server_addr, client_config=custom_config)
117118
headers = remote_connection._client_config.get_auth_header()

0 commit comments

Comments
 (0)