|
| 1 | +"""Tests for the webserver module.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, PropertyMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ..webserver import AuthenticationWebRequestHandler, AuthenticationWebServer |
| 8 | + |
| 9 | + |
| 10 | +class TestAuthenticationWebServer: |
| 11 | + """Tests for AuthenticationWebServer.""" |
| 12 | + |
| 13 | + def test_no_keyring_attribute_set_from_kwargs(self): |
| 14 | + """Verify no_keyring is set from kwargs.""" |
| 15 | + with patch("socket.socket"): |
| 16 | + with patch.object(AuthenticationWebServer, "server_bind"): |
| 17 | + with patch.object(AuthenticationWebServer, "server_activate"): |
| 18 | + server = AuthenticationWebServer( |
| 19 | + ("127.0.0.1", 12400), |
| 20 | + AuthenticationWebRequestHandler, |
| 21 | + bind_and_activate=False, |
| 22 | + owner="testorg", |
| 23 | + no_keyring=True, |
| 24 | + ) |
| 25 | + assert server.no_keyring is True |
| 26 | + |
| 27 | + def test_no_keyring_defaults_to_false(self): |
| 28 | + """Verify no_keyring defaults to False when not provided.""" |
| 29 | + with patch("socket.socket"): |
| 30 | + with patch.object(AuthenticationWebServer, "server_bind"): |
| 31 | + with patch.object(AuthenticationWebServer, "server_activate"): |
| 32 | + server = AuthenticationWebServer( |
| 33 | + ("127.0.0.1", 12400), |
| 34 | + AuthenticationWebRequestHandler, |
| 35 | + bind_and_activate=False, |
| 36 | + owner="testorg", |
| 37 | + ) |
| 38 | + assert server.no_keyring is False |
| 39 | + |
| 40 | + |
| 41 | +class TestAuthenticationWebRequestHandlerNoKeyring: |
| 42 | + """Tests for AuthenticationWebRequestHandler with no_keyring flag.""" |
| 43 | + |
| 44 | + @pytest.fixture |
| 45 | + def mock_handler(self): |
| 46 | + """Create a mock handler with controlled attributes.""" |
| 47 | + with patch.object( |
| 48 | + AuthenticationWebRequestHandler, "__init__", lambda *args, **kwargs: None |
| 49 | + ): |
| 50 | + handler = AuthenticationWebRequestHandler.__new__( |
| 51 | + AuthenticationWebRequestHandler |
| 52 | + ) |
| 53 | + handler.server_instance = MagicMock() |
| 54 | + handler.server_instance.api_host = "https://api.cloudsmith.io" |
| 55 | + handler.refresh_api_on_success = False |
| 56 | + handler.session = MagicMock() |
| 57 | + handler.debug = False |
| 58 | + return handler |
| 59 | + |
| 60 | + def test_store_sso_tokens_called_when_no_keyring_false(self, mock_handler): |
| 61 | + """Verify store_sso_tokens is called when no_keyring is False.""" |
| 62 | + mock_handler.no_keyring = False |
| 63 | + |
| 64 | + with patch("cloudsmith_cli.cli.webserver.store_sso_tokens") as mock_store: |
| 65 | + with patch.object(mock_handler, "_return_success_response"): |
| 66 | + with patch.object( |
| 67 | + AuthenticationWebRequestHandler, |
| 68 | + "query_data", |
| 69 | + new_callable=PropertyMock, |
| 70 | + ) as mock_query: |
| 71 | + with patch.object( |
| 72 | + AuthenticationWebRequestHandler, |
| 73 | + "api_host", |
| 74 | + new_callable=PropertyMock, |
| 75 | + ) as mock_host: |
| 76 | + mock_query.return_value = { |
| 77 | + "access_token": "test_access_token", |
| 78 | + "refresh_token": "test_refresh_token", |
| 79 | + } |
| 80 | + mock_host.return_value = "https://api.cloudsmith.io" |
| 81 | + |
| 82 | + mock_handler.do_GET() |
| 83 | + |
| 84 | + mock_store.assert_called_once_with( |
| 85 | + "https://api.cloudsmith.io", |
| 86 | + "test_access_token", |
| 87 | + "test_refresh_token", |
| 88 | + ) |
| 89 | + |
| 90 | + def test_store_sso_tokens_not_called_when_no_keyring_true(self, mock_handler): |
| 91 | + """Verify store_sso_tokens is NOT called when no_keyring is True.""" |
| 92 | + mock_handler.no_keyring = True |
| 93 | + |
| 94 | + with patch("cloudsmith_cli.cli.webserver.store_sso_tokens") as mock_store: |
| 95 | + with patch("click.echo") as mock_echo: |
| 96 | + with patch.object(mock_handler, "_return_success_response"): |
| 97 | + with patch.object( |
| 98 | + AuthenticationWebRequestHandler, |
| 99 | + "query_data", |
| 100 | + new_callable=PropertyMock, |
| 101 | + ) as mock_query: |
| 102 | + with patch.object( |
| 103 | + AuthenticationWebRequestHandler, |
| 104 | + "api_host", |
| 105 | + new_callable=PropertyMock, |
| 106 | + ) as mock_host: |
| 107 | + mock_query.return_value = { |
| 108 | + "access_token": "test_access_token", |
| 109 | + "refresh_token": "test_refresh_token", |
| 110 | + } |
| 111 | + mock_host.return_value = "https://api.cloudsmith.io" |
| 112 | + |
| 113 | + mock_handler.do_GET() |
| 114 | + |
| 115 | + # store_sso_tokens should NOT be called |
| 116 | + mock_store.assert_not_called() |
| 117 | + |
| 118 | + # Message should be displayed to stderr |
| 119 | + mock_echo.assert_called_once_with( |
| 120 | + "SSO tokens not stored (--no-keyring enabled)", |
| 121 | + err=True, |
| 122 | + ) |
0 commit comments