import json import struct import mock import pytest # type: ignore from google.auth import exceptions from google.oauth2 import webauthn_handler from google.oauth2 import webauthn_types @pytest.fixture def os_get_stub(): with mock.patch.object( webauthn_handler.os.environ, "get", return_value="gcloud_webauthn_plugin", name="fake os.environ.get", ) as mock_os_environ_get: yield mock_os_environ_get @pytest.fixture def subprocess_run_stub(): with mock.patch.object( webauthn_handler.subprocess, "run", name="fake subprocess.run" ) as mock_subprocess_run: yield mock_subprocess_run def test_PluginHandler_is_available(os_get_stub): test_handler = webauthn_handler.PluginHandler() assert test_handler.is_available() is True os_get_stub.return_value = None assert test_handler.is_available() is False GET_ASSERTION_REQUEST = webauthn_types.GetRequest( origin="fake_origin", rpid="fake_rpid", challenge="fake_challenge", allow_credentials=[webauthn_types.PublicKeyCredentialDescriptor(id="fake_id_1")], ) def test_malformated_get_assertion_response(os_get_stub, subprocess_run_stub): response_len = struct.pack("