From 9dc6d56a8d66af5dc5ba0715db2b06a453a32f09 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Wed, 3 May 2023 14:53:35 +0300 Subject: [PATCH] fix flapping tests for oauth Original implementation sends requests to httpbin which sometimes might not be available. With proposed changes we are blocking redirects and just check request itself --- src/ahriman/core/status/web_client.py | 35 ++++++++++--------- .../web/views/user/test_views_user_login.py | 8 ++--- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/ahriman/core/status/web_client.py b/src/ahriman/core/status/web_client.py index 176a323e..a835fae1 100644 --- a/src/ahriman/core/status/web_client.py +++ b/src/ahriman/core/status/web_client.py @@ -102,12 +102,15 @@ class WebClient(Client, LazyLogging): return address, False @contextlib.contextmanager - def __execute_request(self) -> Generator[None, None, None]: + def __get_session(self) -> Generator[requests.Session, None, None]: """ execute request and handle exceptions + + Yields: + requests.Session: session for requests """ try: - yield + yield self.__session except requests.RequestException as e: if self.suppress_errors: return @@ -149,8 +152,8 @@ class WebClient(Client, LazyLogging): "password": self.user.password } - with self.__execute_request(): - response = self.__session.post(self._login_url, json=payload) + with self.__get_session() as session: + response = session.post(self._login_url, json=payload) response.raise_for_status() def _logs_url(self, package_base: str) -> str: @@ -192,8 +195,8 @@ class WebClient(Client, LazyLogging): "package": package.view() } - with self.__execute_request(): - response = self.__session.post(self._package_url(package.base), json=payload) + with self.__get_session() as session: + response = session.post(self._package_url(package.base), json=payload) response.raise_for_status() def get(self, package_base: str | None) -> list[tuple[Package, BuildStatus]]: @@ -206,8 +209,8 @@ class WebClient(Client, LazyLogging): Returns: list[tuple[Package, BuildStatus]]: list of current package description and status if it has been found """ - with self.__execute_request(): - response = self.__session.get(self._package_url(package_base or "")) + with self.__get_session() as session: + response = session.get(self._package_url(package_base or "")) response.raise_for_status() status_json = response.json() @@ -226,8 +229,8 @@ class WebClient(Client, LazyLogging): Returns: InternalStatus: current internal (web) service status """ - with self.__execute_request(): - response = self.__session.get(self._status_url) + with self.__get_session() as session: + response = session.get(self._status_url) response.raise_for_status() status_json = response.json() @@ -261,8 +264,8 @@ class WebClient(Client, LazyLogging): Args: package_base(str): basename to remove """ - with self.__execute_request(): - response = self.__session.delete(self._package_url(package_base)) + with self.__get_session() as session: + response = session.delete(self._package_url(package_base)) response.raise_for_status() def update(self, package_base: str, status: BuildStatusEnum) -> None: @@ -275,8 +278,8 @@ class WebClient(Client, LazyLogging): """ payload = {"status": status.value} - with self.__execute_request(): - response = self.__session.post(self._package_url(package_base), json=payload) + with self.__get_session() as session: + response = session.post(self._package_url(package_base), json=payload) response.raise_for_status() def update_self(self, status: BuildStatusEnum) -> None: @@ -288,6 +291,6 @@ class WebClient(Client, LazyLogging): """ payload = {"status": status.value} - with self.__execute_request(): - response = self.__session.post(self._status_url, json=payload) + with self.__get_session() as session: + response = session.post(self._status_url, json=payload) response.raise_for_status() diff --git a/tests/ahriman/web/views/user/test_views_user_login.py b/tests/ahriman/web/views/user/test_views_user_login.py index eca71d95..0afc7e0a 100644 --- a/tests/ahriman/web/views/user/test_views_user_login.py +++ b/tests/ahriman/web/views/user/test_views_user_login.py @@ -30,12 +30,12 @@ async def test_get_redirect_to_oauth(client_with_oauth_auth: TestClient) -> None must redirect to OAuth service provider in case if no code is supplied """ oauth = client_with_oauth_auth.app["validator"] - oauth.get_oauth_url.return_value = "https://httpbin.org" + oauth.get_oauth_url.return_value = "http://localhost" request_schema = pytest.helpers.schema_request(LoginView.get, location="querystring") payload = {} assert not request_schema.validate(payload) - response = await client_with_oauth_auth.get("/api/v1/login", params=payload) + response = await client_with_oauth_auth.get("/api/v1/login", params=payload, allow_redirects=False) assert response.ok oauth.get_oauth_url.assert_called_once_with() @@ -45,12 +45,12 @@ async def test_get_redirect_to_oauth_empty_code(client_with_oauth_auth: TestClie must redirect to OAuth service provider in case if empty code is supplied """ oauth = client_with_oauth_auth.app["validator"] - oauth.get_oauth_url.return_value = "https://httpbin.org" + oauth.get_oauth_url.return_value = "http://localhost" request_schema = pytest.helpers.schema_request(LoginView.get, location="querystring") payload = {"code": ""} assert not request_schema.validate(payload) - response = await client_with_oauth_auth.get("/api/v1/login", params=payload) + response = await client_with_oauth_auth.get("/api/v1/login", params=payload, allow_redirects=False) assert response.ok oauth.get_oauth_url.assert_called_once_with()