Changeset View
Changeset View
Standalone View
Standalone View
swh/auth/pytest_plugin.py
# Copyright (C) 2020-2021 The Software Heritage developers | # Copyright (C) 2020-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from copy import copy | from copy import copy | ||||
from datetime import datetime, timezone | from datetime import datetime, timezone | ||||
import json | |||||
from typing import Dict, List, Optional | from typing import Dict, List, Optional | ||||
from unittest.mock import Mock | from unittest.mock import Mock | ||||
from keycloak.exceptions import KeycloakError | from keycloak.exceptions import KeycloakError | ||||
import pytest | import pytest | ||||
from swh.auth.keycloak import KeycloakOpenIDConnect | from swh.auth.keycloak import KeycloakOpenIDConnect | ||||
from swh.auth.tests.sample_data import OIDC_PROFILE, RAW_REALM_PUBLIC_KEY, USER_INFO | from swh.auth.tests.sample_data import OIDC_PROFILE, RAW_REALM_PUBLIC_KEY, USER_INFO | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | ) -> None: | ||||
self.auth_success = auth_success | self.auth_success = auth_success | ||||
if auth_success: | if auth_success: | ||||
self.authorization_code.return_value = copy(oidc_profile) | self.authorization_code.return_value = copy(oidc_profile) | ||||
self.refresh_token.return_value = copy(oidc_profile) | self.refresh_token.return_value = copy(oidc_profile) | ||||
self.login.return_value = copy(oidc_profile) | self.login.return_value = copy(oidc_profile) | ||||
self.userinfo.return_value = copy(user_info) | self.userinfo.return_value = copy(user_info) | ||||
else: | else: | ||||
self.authorization_url = Mock() # type: ignore | self.authorization_url = Mock() # type: ignore | ||||
exception = KeycloakError( | error = { | ||||
error_message="Authentication failed", response_code=401 | "error": "invalid_grant", | ||||
) | "error_description": "Invalid user credentials", | ||||
} | |||||
error_message = json.dumps(error).encode() | |||||
exception = KeycloakError(error_message=error_message, response_code=401) | |||||
self.authorization_code.side_effect = exception | self.authorization_code.side_effect = exception | ||||
self.authorization_url.side_effect = exception | self.authorization_url.side_effect = exception | ||||
self.refresh_token.side_effect = exception | self.refresh_token.side_effect = exception | ||||
self.userinfo.side_effect = exception | self.userinfo.side_effect = exception | ||||
self.logout.side_effect = exception | self.logout.side_effect = exception | ||||
self.login.side_effect = exception | self.login.side_effect = exception | ||||
Show All 33 Lines |